coral_core 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,489 @@
1
+
2
+ require 'spec_helper'
3
+
4
+ module Coral
5
+
6
+ describe Interface do
7
+
8
+ #---------------------------------------------------------------------------
9
+ # UI functionality
10
+
11
+ describe "#say" do
12
+
13
+ #-------------------------------------------------------------------------
14
+ # Delegation
15
+
16
+ it "can delegate to another class that contains this method" do
17
+ output = double('output')
18
+ output.should_receive(:puts).with('message')
19
+
20
+ ui = Interface.new({
21
+ :output => output,
22
+ :printer => :puts,
23
+ })
24
+ Interface.new({ :ui_delegate => ui }).say(:info, 'message')
25
+ end
26
+
27
+ #-------------------------------------------------------------------------
28
+ # Output formats
29
+
30
+ it "prints a message with default options" do
31
+ output1 = double('output1')
32
+ output1.should_receive(:puts).with('message')
33
+
34
+ Interface.new({ :output => output1 }).say(:info, 'message')
35
+
36
+ output2 = double('output2')
37
+ output2.should_receive(:puts).with('[component] message')
38
+
39
+ Interface.new({
40
+ :resource => 'component',
41
+ :output => output2,
42
+ }).say(:info, 'message')
43
+ end
44
+
45
+ #---
46
+
47
+ it "prints a message with and without newlines included" do
48
+ output1 = double('output1')
49
+ output1.should_receive(:puts).with('message')
50
+
51
+ test = Interface.new({ :output => output1 })
52
+ test.say(:info, 'message', { :new_line => true })
53
+
54
+ output2 = double('output2')
55
+ output2.should_receive(:print).with('message')
56
+
57
+ test = Interface.new({ :output => output2 })
58
+ test.say(:info, 'message', { :new_line => false })
59
+ end
60
+
61
+ #---
62
+
63
+ it "routes message to output and error channels based on type given" do
64
+ [:info, :warn, :success].each do |type|
65
+ output = double('output')
66
+ output.should_receive(:puts).with('message')
67
+
68
+ Interface.new({
69
+ :output => output,
70
+ :printer => :puts,
71
+ :color => false,
72
+ }).say(type, 'message')
73
+ end
74
+
75
+ error = double('error')
76
+ error.should_receive(:puts).with('message')
77
+
78
+ Interface.new({
79
+ :error => error,
80
+ :printer => :puts,
81
+ :color => false,
82
+ }).say(:error, 'message')
83
+ end
84
+
85
+ #---
86
+
87
+ it "routes message to output and error channels based on channel given" do
88
+ [:info, :warn, :success].each do |type|
89
+ output = double('output')
90
+ output.should_receive(:puts).with('message')
91
+
92
+ Interface.new({
93
+ :output => output,
94
+ :printer => :puts,
95
+ }).say(:info, 'message', { :channel => type })
96
+ end
97
+
98
+ error = double('error')
99
+ error.should_receive(:puts).with('message')
100
+
101
+ Interface.new({
102
+ :error => error,
103
+ :printer => :puts,
104
+ :color => false,
105
+ }).say(:info, 'message', { :channel => :error })
106
+ end
107
+ end
108
+
109
+ #---
110
+
111
+ describe "#ask" do
112
+
113
+ #-------------------------------------------------------------------------
114
+ # Delegation
115
+
116
+ it "can delegate to another class that contains this method"
117
+
118
+ #-------------------------------------------------------------------------
119
+ # Input
120
+
121
+ it "displays a prompt and returns user feedback"
122
+ end
123
+
124
+ #---
125
+
126
+ describe "#info" do
127
+
128
+ #-------------------------------------------------------------------------
129
+ # Delegation
130
+
131
+ it "can delegate to another class that contains this method" do
132
+ output = double('output')
133
+ output.should_receive(:puts).with('message')
134
+
135
+ ui = Interface.new({
136
+ :output => output,
137
+ :printer => :puts,
138
+ })
139
+ Interface.new({ :ui_delegate => ui }).info('message')
140
+ end
141
+
142
+ #-------------------------------------------------------------------------
143
+ # Printing
144
+
145
+ it "prints an uncolored information message" do
146
+ output = double('output')
147
+ output.should_receive(:puts).with('message')
148
+
149
+ Interface.new({
150
+ :output => output,
151
+ :printer => :puts,
152
+ }).info('message')
153
+ end
154
+ end
155
+
156
+ #---
157
+
158
+ describe "#warn" do
159
+
160
+ #-------------------------------------------------------------------------
161
+ # Delegation
162
+
163
+ it "can delegate to another class that contains this method" do
164
+ output = double('output')
165
+ output.should_receive(:puts).with('message')
166
+
167
+ ui = Interface.new({
168
+ :output => output,
169
+ :printer => :puts,
170
+ :color => false,
171
+ })
172
+ Interface.new({ :ui_delegate => ui }).warn('message')
173
+ end
174
+
175
+ #-------------------------------------------------------------------------
176
+ # Printing
177
+
178
+ it "prints an uncolored warning message" do
179
+ output = double('output')
180
+ output.should_receive(:puts).with('message')
181
+
182
+ Interface.new({
183
+ :output => output,
184
+ :printer => :puts,
185
+ :color => false,
186
+ }).warn('message')
187
+ end
188
+
189
+ #---
190
+
191
+ it "prints a colored warning message" do
192
+ output = double('output')
193
+ output.should_receive(:print).with(/^\e\[33mmessage\e\[0m$/)
194
+
195
+ Interface.new({
196
+ :output => output,
197
+ :color => true,
198
+ }).warn('message', { :new_line => false })
199
+ end
200
+ end
201
+
202
+ #---
203
+
204
+ describe "#error" do
205
+
206
+ #-------------------------------------------------------------------------
207
+ # Delegation
208
+
209
+ it "can delegate to another class that contains this method" do
210
+ error = double('error')
211
+ error.should_receive(:puts).with('message')
212
+
213
+ ui = Interface.new({
214
+ :error => error,
215
+ :printer => :puts,
216
+ :color => false,
217
+ })
218
+ Interface.new({ :ui_delegate => ui }).error('message')
219
+ end
220
+
221
+ #-------------------------------------------------------------------------
222
+ # Printing
223
+
224
+ it "prints an uncolored error message" do
225
+ error = double('error')
226
+ error.should_receive(:puts).with('message')
227
+
228
+ Interface.new({
229
+ :error => error,
230
+ :printer => :puts,
231
+ :color => false,
232
+ }).error('message')
233
+ end
234
+
235
+ #---
236
+
237
+ it "prints a colored error message" do
238
+ error = double('error')
239
+ error.should_receive(:print).with(/^\e\[31mmessage\e\[0m$/)
240
+
241
+ Interface.new({
242
+ :error => error,
243
+ :color => true,
244
+ }).error('message', { :new_line => false })
245
+ end
246
+ end
247
+
248
+ #---
249
+
250
+ describe "#success" do
251
+
252
+ #-------------------------------------------------------------------------
253
+ # Delegation
254
+
255
+ it "can delegate to another class that contains this method" do
256
+ output = double('output')
257
+ output.should_receive(:puts).with('message')
258
+
259
+ ui = Interface.new({
260
+ :output => output,
261
+ :printer => :puts,
262
+ :color => false,
263
+ })
264
+ Interface.new({ :ui_delegate => ui }).success('message')
265
+ end
266
+
267
+ #-------------------------------------------------------------------------
268
+ # Printing
269
+
270
+ it "prints an uncolored success message" do
271
+ output = double('output')
272
+ output.should_receive(:puts).with('message')
273
+
274
+ Interface.new({
275
+ :output => output,
276
+ :printer => :puts,
277
+ :color => false,
278
+ }).success('message')
279
+ end
280
+
281
+ #---
282
+
283
+ it "prints a colored success message" do
284
+ output = double('output')
285
+ output.should_receive(:print).with(/^\e\[32mmessage\e\[0m$/)
286
+
287
+ Interface.new({
288
+ :output => output,
289
+ :color => true,
290
+ }).success('message', { :new_line => false })
291
+ end
292
+ end
293
+
294
+ #---------------------------------------------------------------------------
295
+ # Utilities
296
+
297
+ describe "#format_message" do
298
+
299
+ #-------------------------------------------------------------------------
300
+ # Delegation
301
+
302
+ it "can delegate to another class that contains this method" do
303
+ message = Interface.new({
304
+ :ui_delegate => Interface.new('delegate')
305
+ }).format_message(:info, 'message', { :prefix => true })
306
+
307
+ message.should == '[delegate] message'
308
+ end
309
+
310
+ #-------------------------------------------------------------------------
311
+ # Prefix specifications
312
+
313
+ it "returns without a prefix because no resource" do
314
+ message = Interface.new.format_message(:info, 'message', { :prefix => true })
315
+ message.should == 'message'
316
+ end
317
+
318
+ #---
319
+
320
+ it "returns without a prefix because prefix is false" do
321
+ message = Interface.new('component').format_message(:info, 'message', { :prefix => false })
322
+ message.should == 'message'
323
+ end
324
+
325
+ #---
326
+
327
+ it "returns without a prefix because no prefix option given" do
328
+ message = Interface.new('component').format_message(:info, 'message')
329
+ message.should == 'message'
330
+ end
331
+
332
+ #---
333
+
334
+ it "returns with a prefix if resource and prefix option given" do
335
+ message = Interface.new('component').format_message(:info, 'message', { :prefix => true })
336
+ message.should == '[component] message'
337
+ end
338
+
339
+ #-------------------------------------------------------------------------
340
+ # Color specifications
341
+
342
+ it "formats a error message in red if color enabled" do
343
+ message = Interface.new({
344
+ :resource => 'component',
345
+ :color => true,
346
+ }).format_message(:error, 'message')
347
+ message.should match(/^\e\[31mmessage\e\[0m$/)
348
+ end
349
+
350
+ #---
351
+
352
+ it "formats a warning message in yellow if color enabled" do
353
+ message = Interface.new({
354
+ :resource => 'component',
355
+ :color => true,
356
+ }).format_message(:warn, 'message')
357
+ message.should match(/^\e\[33mmessage\e\[0m$/)
358
+ end
359
+
360
+ #---
361
+
362
+ it "formats a success message in green if color enabled" do
363
+ message = Interface.new({
364
+ :resource => 'component',
365
+ :color => true,
366
+ }).format_message(:success, 'message')
367
+ message.should match(/^\e\[32mmessage\e\[0m$/)
368
+ end
369
+ end
370
+
371
+ #---------------------------------------------------------------------------
372
+
373
+ describe "#safe_puts" do
374
+
375
+ #-------------------------------------------------------------------------
376
+ # Delegation
377
+
378
+ it "can delegate to another class that contains this method" do
379
+ output = double('output')
380
+ output.should_receive(:puts).with('message')
381
+
382
+ ui = Interface.new({
383
+ :output => output,
384
+ :printer => :puts,
385
+ })
386
+ Interface.new({ :ui_delegate => ui }).safe_puts('message')
387
+ end
388
+
389
+ #-------------------------------------------------------------------------
390
+ # Instance configuration
391
+
392
+ it "prints an empty string unless message given" do
393
+ output = double('output')
394
+ output.should_receive(:puts).with('')
395
+
396
+ Interface.new({
397
+ :output => output,
398
+ :printer => :puts,
399
+ }).safe_puts()
400
+ end
401
+
402
+ #---
403
+
404
+ it "prints to different output channels if they are given" do
405
+ output1 = double('output1')
406
+ output1.should_receive(:puts).with('message')
407
+
408
+ test = Interface.new({
409
+ :output => output1,
410
+ :printer => :puts,
411
+ })
412
+ test.safe_puts('message')
413
+
414
+ output2 = double('output2')
415
+ output2.should_receive(:puts).with('message')
416
+
417
+ test.output = output2
418
+ test.safe_puts('message')
419
+ end
420
+
421
+ #---
422
+
423
+ it "prints with puts if puts printer option given" do
424
+ output = double('output')
425
+ output.should_receive(:puts).with('message')
426
+
427
+ Interface.new({
428
+ :output => output,
429
+ :printer => :puts,
430
+ }).safe_puts('message')
431
+ end
432
+
433
+ #---
434
+
435
+ it "prints with print if print printer option given" do
436
+ output = double('output')
437
+ output.should_receive(:print).with('message')
438
+
439
+ Interface.new({
440
+ :output => output,
441
+ :printer => :print,
442
+ }).safe_puts('message')
443
+ end
444
+
445
+ #-------------------------------------------------------------------------
446
+ # Method configuration
447
+
448
+ it "can override the instance output channel" do
449
+ output1 = double('output1')
450
+ output1.should_not_receive(:puts).with('message')
451
+
452
+ output2 = double('output2')
453
+ output2.should_receive(:puts).with('message')
454
+
455
+ Interface.new({
456
+ :output => output1,
457
+ :printer => :puts,
458
+ }).safe_puts('message', { :channel => output2 })
459
+ end
460
+
461
+ #---
462
+
463
+ it "can override the instance printer handler" do
464
+ output = double('output')
465
+ output.should_not_receive(:puts).with('message')
466
+ output.should_receive(:print).with('message')
467
+
468
+ Interface.new({
469
+ :output => output,
470
+ :printer => :puts,
471
+ }).safe_puts('message', { :printer => :print })
472
+ end
473
+ end
474
+
475
+ #---------------------------------------------------------------------------
476
+
477
+ describe "#check_delegate" do
478
+
479
+ it "returns false if no delegate exists" do
480
+ Interface.new.check_delegate('safe_puts').should be_false
481
+ end
482
+ it "returns true if a delegate exists and it implements given method" do
483
+ test = Interface.new({ :ui_delegate => Interface.new })
484
+ test.check_delegate('safe_puts').should be_true
485
+ test.check_delegate('nonexistent').should be_false
486
+ end
487
+ end
488
+ end
489
+ end
@@ -0,0 +1,29 @@
1
+
2
+ class MockInput
3
+
4
+ #-----------------------------------------------------------------------------
5
+ # Constructor / Destructor
6
+
7
+ def initialize(strings)
8
+ if strings.is_a?(String)
9
+ strings = [ strings ]
10
+ end
11
+ @strings = strings
12
+ end
13
+
14
+ #---
15
+
16
+ def self.with(strings)
17
+ $stdin = self.new(strings)
18
+ yield
19
+ ensure
20
+ $stdin = STDIN
21
+ end
22
+
23
+ #-----------------------------------------------------------------------------
24
+ # Accessors / Modifiers
25
+
26
+ def gets
27
+ return @strings.shift
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+
2
+ module Kernel
3
+
4
+ #-----------------------------------------------------------------------------
5
+ # Utilities
6
+
7
+ def capture
8
+ out = StringIO.new
9
+ $stdout = out
10
+
11
+ error = StringIO.new
12
+ $stderr = error
13
+
14
+ # Go do stuff!
15
+ yield
16
+ return out, error
17
+
18
+ ensure
19
+ $stdout = STDOUT
20
+ $stderr = STDERR
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+
2
+ require 'rspec'
3
+ require 'stringio'
4
+ require 'coral_core'
5
+
6
+ require 'coral_test_kernel'
7
+ require 'coral_mock_input'
8
+
9
+ #-------------------------------------------------------------------------------
10
+
11
+ RSpec.configure do |config|
12
+ config.mock_framework = :rspec
13
+ config.color_enabled = true
14
+ config.formatter = 'documentation'
15
+ end