command_line_reporter 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,20 +24,40 @@ include CommandLineReporter
24
24
 
25
25
  There are several methods the mixin provides that do not depend on the formatter used:
26
26
 
27
- 1. _report(hash) {block}_
27
+ * _header(hash)_ and _footer(hash)_
28
+ * _:title_ - The title text for the section. _Default: 'Report'_
29
+ * _:width_ - The width in characters for the section. _Default: 100_
30
+ * _:align_ - 'left'|'right'|'center' align the title text. _Default: 'left'_
31
+ * _:spacing_ - Number of vertical lines to leave as spacing after|before the header|footer.
32
+ _Default: 1_
33
+ * _:timestamp_ - Include a line indicating the timestamp below|above the header|footer text.
34
+ Either true|false. _Default: false_
35
+ * _:rule_ - true|false indicates whether to include a horizontal rule below|above the
36
+ header|footer. _Default: false_
37
+ * _report(hash) {block}_
28
38
  * The first argument is a hash that defines the options for the method. See the details in the
29
39
  formatter section for allowed values.
30
40
  * The second argument is a block of ruby code that you want executed within the context of the
31
41
  reporter. Any ruby code is allowed. See the examples that follow in the formatter sections for
32
42
  details.
33
- 1. _formatter=(string)_
34
- * Simple string indicating the formatter you want your application to use. At present the 2
35
- formatters are:
36
-
37
- 2. Progress - indicated by the string '_progress_'
38
- 2. Nested - indicated by the string '_nested_'
39
-
40
- The default is _nested_.
43
+ * _formatter=(string)_
44
+ * Factory method indicating the formatter you want your application to use. At present the 2
45
+ formatters are (_Default: 'nested'_):
46
+ * 'progress' - Use the progress formatter
47
+ * 'nested' - Use the nested (or documentation) formatter
48
+ * _horizontal_rule(hash)_
49
+ * _:char_ - The character used to build the rule. _Default: '-'_
50
+ * _:width_ - The width in characters of the rule. _Default: 100_
51
+ * _vertical_spacing(int)_
52
+ * Number of blank lines to output. _Default: 1_
53
+ * _datetime(hash)_
54
+ * _:align_ - 'left'|'center'|'right' alignment of the timestamp. _Default: 'left'_
55
+ * _:width_ - The width of the string in characters. _Default: 100_
56
+ * _:format_ - Any allowed format from #strftime#. _Default: %Y-%m-%d %H:%I:%S%p_
57
+ * _aligned(string, hash)_
58
+ * _text_ - String to display
59
+ * _:align_ - 'left'|'right'|'center' align the string text. _Default: 'left'_
60
+ * _:width_ - The width in characters of the string text. _Default: 100_
41
61
 
42
62
  ### Progress Formatter
43
63
 
@@ -50,7 +70,7 @@ class Example
50
70
  include CommandLineReporter
51
71
 
52
72
  def initialize
53
- formatter = 'progress'
73
+ self.formatter = 'progress'
54
74
  end
55
75
 
56
76
  def run
@@ -173,24 +193,42 @@ complete
173
193
  #### Instance Methods
174
194
 
175
195
  * _message_string(string)_ - This defines the string that is displayed as the first part of the
176
- message to the user. The default value is "_working_".
196
+ message to the user. The default value is "_working_".
197
+
198
+ ```ruby
199
+ formatter.message_string('working')
200
+ ```
201
+
177
202
  * _complete_string(string)_ - This defines the string that completes the message to the user for the
178
203
  report. The default value is "_complete_".
204
+
205
+ ```ruby
206
+ formatter.complete_string('done')
207
+ ```
208
+
179
209
  * _indent_size(int)_ - The number of spaces to indent for a single indentation level. The default
180
210
  value is 2 spaces.
181
211
 
212
+ ```ruby
213
+ formatter.indent_size(4)
214
+ ```
215
+
182
216
  #### Report Options
183
217
 
184
218
  The following are the allowed values of the options hash argument to the _report_ method:
185
219
 
186
- * _message_ - The string that is displayed as the first part of the message to the user
187
- * _complete_ - The string that completes the message to the user
188
- * _type_ - Define as 'inline' if you want the message and complete strings on the same line
189
- * _indent_size_ - The number of spaces to indent the current level
220
+ * _:message_ - The string that is displayed as the first part of the message to the user
221
+ * _:complete_ - The string that completes the message to the user
222
+ * _:type_ - Define as 'inline' if you want the message and complete strings on the same line
223
+ * _:indent_size_ - The number of spaces to indent the current level
224
+
225
+ ```ruby
226
+ report(:message => 'running', :complete => 'finished', :type => 'inline', :indent_size => 8) do
227
+ # code here
228
+ end
229
+ ```
190
230
 
191
231
  ### To Do
192
232
 
193
233
  1. Add the progress method to the top level mixin so that there is no need to invoke through the
194
234
  formatter.
195
- 2. Add a _preface_ and _epilogue_ methods so that beginning and ending aspects of the report may be
196
- added without using _puts_ and _print_.
@@ -3,6 +3,8 @@ require 'command_line_reporter'
3
3
  class Example
4
4
  include CommandLineReporter
5
5
 
6
+ NYAN_CHARS = "****[;::;<](^-^)"
7
+
6
8
  def initialize
7
9
  self.formatter = 'progress'
8
10
  end
@@ -13,10 +15,12 @@ class Example
13
15
  report do
14
16
  10.times do
15
17
  x += 1
18
+ sleep 0.1
16
19
  formatter.progress
17
20
 
18
21
  10.times do
19
22
  x += 1
23
+ sleep 0.1
20
24
  formatter.progress
21
25
  end
22
26
  end
@@ -27,24 +31,35 @@ class Example
27
31
  report do
28
32
  10.times do
29
33
  y += 1
30
- sleep 1
31
- formatter.progress("#{y*10+10}%")
34
+ sleep 0.1
35
+ formatter.progress("#{y*10}%")
32
36
  end
33
37
  end
34
38
 
35
39
  report do
36
40
  3.times do
37
41
  formatter.progress("\\")
38
- sleep 1
42
+ sleep 0.1
39
43
  formatter.progress("/")
40
- sleep 1
44
+ sleep 0.1
41
45
  formatter.progress("-")
42
- sleep 1
46
+ sleep 0.1
43
47
  end
44
48
  end
45
49
 
46
- puts "x: #{x}"
47
- puts "y: #{y}"
50
+ report do
51
+ 100.times do
52
+ self.formatter.progress(erase_chars + NYAN_CHARS)
53
+ sleep 0.1
54
+ end
55
+ end
56
+
57
+ aligned "x: #{x}"
58
+ aligned "y: #{y}"
59
+ end
60
+
61
+ def erase_chars
62
+ "\10" * NYAN_CHARS.size + " "
48
63
  end
49
64
  end
50
65
 
@@ -0,0 +1,22 @@
1
+ require 'command_line_reporter'
2
+
3
+ class Example
4
+ include CommandLineReporter
5
+
6
+ def run
7
+ x,y,z = 0,0,0
8
+
9
+ header(:title => 'Simple Report Example', :align => 'center', :timestamp => true, :rule => true)
10
+
11
+ report(:message => 'calculating sums') do
12
+ x = 2 + 2
13
+ y = 10 - x
14
+ z = x + y
15
+ end
16
+
17
+ footer(:title => 'Values', :width => 15)
18
+ %w(x y z).each {|v| aligned("#{v}: #{eval v}")}
19
+ end
20
+ end
21
+
22
+ Example.new.run
@@ -3,10 +3,10 @@ Dir[File.join(File.dirname(__FILE__), '*_formatter.rb')].each {|r| require r}
3
3
  module CommandLineReporter
4
4
  attr_reader :formatter
5
5
 
6
- def report(options = {}, &block)
7
- self.formatter = 'nested' if self.formatter.nil?
8
- self.formatter.format(options, block)
9
- end
6
+ DEFAULTS = {
7
+ :width => 100,
8
+ :align => 'left',
9
+ }
10
10
 
11
11
  def formatter=(type = 'nested')
12
12
  name = type.capitalize + 'Formatter'
@@ -17,4 +17,97 @@ module CommandLineReporter
17
17
  rescue
18
18
  raise ArgumentError, 'Invalid formatter specified'
19
19
  end
20
+
21
+ def header(options = {})
22
+ section(:header, options)
23
+ end
24
+
25
+ def report(options = {}, &block)
26
+ self.formatter.format(options, block)
27
+ rescue NoMethodError
28
+ self.formatter = 'nested'
29
+ retry
30
+ end
31
+
32
+ def footer(options = {})
33
+ section(:footer, options)
34
+ end
35
+
36
+ def horizontal_rule(options = {})
37
+ validate_options(options, :char, :width)
38
+
39
+ char = options[:char].is_a?(String) ? options[:char] : '-'
40
+ width = options[:width] || DEFAULTS[:width]
41
+
42
+ puts char * width
43
+ end
44
+
45
+ def vertical_spacing(lines = 1)
46
+ puts "\n" * lines
47
+ rescue
48
+ raise ArgumentError
49
+ end
50
+
51
+ def datetime(options = {})
52
+ validate_options(options, :align, :width, :format)
53
+
54
+ format = options[:format] || '%Y-%m-%d - %l:%M:%S%p'
55
+ align = options[:align] || DEFAULTS[:align]
56
+ width = options[:width] || DEFAULTS[:width]
57
+
58
+ text = Time.now.strftime(format)
59
+
60
+ raise Exception if text.size > width
61
+
62
+ aligned(text, {:align => align, :width => width})
63
+ end
64
+
65
+ def aligned(text, options = {})
66
+ validate_options(options, :align, :width)
67
+
68
+ align = options[:align] || DEFAULTS[:align]
69
+ width = options[:width] || DEFAULTS[:width]
70
+
71
+ case align
72
+ when 'left'
73
+ puts text
74
+ when 'right'
75
+ puts text.rjust(width)
76
+ when 'center'
77
+ puts text.rjust((width - text.size)/2 + text.size)
78
+ else
79
+ raise ArgumentError
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def validate_options(options, *allowed_keys)
86
+ raise ArgumentError unless (options.keys - allowed_keys).empty?
87
+ end
88
+
89
+ def section(type, options)
90
+ validate_options(options, :title, :width, :align, :spacing, :timestamp, :rule)
91
+
92
+ title = options[:title] || 'Report'
93
+ width = options[:width] || DEFAULTS[:width]
94
+ align = options[:align] || DEFAULTS[:align]
95
+ lines = options[:spacing] || 1
96
+
97
+ # This also ensures that width is a Fixnum
98
+ raise ArgumentError if title.size > width
99
+
100
+ if type == :footer
101
+ vertical_spacing(lines)
102
+ horizontal_rule(:char => options[:rule], :width => width) if options[:rule]
103
+ end
104
+
105
+ aligned(title, {:align => align, :width => width})
106
+ datetime(:align => align, :width => width) if options[:timestamp]
107
+
108
+ if type == :header
109
+ horizontal_rule(:char => options[:rule], :width => width) if options[:rule]
110
+ vertical_spacing(lines)
111
+ end
112
+ end
20
113
  end
@@ -1,3 +1,3 @@
1
1
  module CommandLineReporter
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -10,11 +10,15 @@ describe CommandLineReporter do
10
10
 
11
11
  subject { use_class.new }
12
12
 
13
+ before :each do
14
+ @timestamp_regex = /\d{4}-\d{2}-\d{2} - (\d| )\d:\d{2}:\d{2}[AP]M/
15
+ end
16
+
13
17
  describe '#formatter=' do
14
18
  it 'only allows allowed formatters' do
15
- lambda {
19
+ expect {
16
20
  subject.formatter = 'asfd'
17
- }.should raise_exception ArgumentError
21
+ }.to raise_error ArgumentError
18
22
  end
19
23
 
20
24
  it 'specifies the progress formatter' do
@@ -30,8 +34,479 @@ describe CommandLineReporter do
30
34
 
31
35
  describe '#report' do
32
36
  it 'uses the nested formatter as default' do
33
- subject.report { }
37
+ capture_stdout {
38
+ subject.report { }
39
+ }
40
+
34
41
  subject.formatter.class.should == CommandLineReporter::NestedFormatter
35
42
  end
43
+
44
+ it 'uses the progress formatter' do
45
+ capture_stdout {
46
+ subject.formatter = 'progress'
47
+ subject.report { }
48
+ }
49
+
50
+ subject.formatter.class.should == CommandLineReporter::ProgressFormatter
51
+ end
52
+ end
53
+
54
+ describe '#header' do
55
+ context 'argument validation' do
56
+
57
+ it 'does not accept an invalid option' do
58
+ expect {
59
+ subject.header(:asdf => 'tests')
60
+ }.to raise_error ArgumentError
61
+ end
62
+
63
+ it 'accepts a title' do
64
+ expect {
65
+ subject.should_receive(:puts).any_number_of_times
66
+ subject.header(:title => 'test')
67
+ }.to_not raise_error ArgumentError
68
+ end
69
+
70
+ it 'does not allow a title > width' do
71
+ expect {
72
+ subject.header(:title => 'xxxxxxxxxxx', :width => 5)
73
+ }.to raise_error ArgumentError
74
+ end
75
+
76
+ it 'accepts width' do
77
+ expect {
78
+ subject.should_receive(:puts).any_number_of_times
79
+ subject.header(:width => 100)
80
+ }.to_not raise_error ArgumentError
81
+ end
82
+
83
+ it 'ensure width is a number' do
84
+ expect {
85
+ subject.header(:width => '100')
86
+ }.to raise_error ArgumentError
87
+ end
88
+
89
+ it 'accepts align' do
90
+ expect {
91
+ subject.should_receive(:puts).any_number_of_times
92
+ subject.header(:align => 'center')
93
+ }.to_not raise_error ArgumentError
94
+ end
95
+
96
+ it 'ensure align is a valid value' do
97
+ expect {
98
+ subject.header(:align => :asdf)
99
+ }.to raise_error ArgumentError
100
+ end
101
+
102
+ it 'accepts spacing' do
103
+ expect {
104
+ subject.should_receive(:puts).any_number_of_times
105
+ subject.header(:spacing => 2)
106
+ }.to_not raise_error ArgumentError
107
+ end
108
+
109
+ it 'accepts timestamp' do
110
+ expect {
111
+ subject.should_receive(:puts).any_number_of_times
112
+ subject.header(:timestamp => true)
113
+ }.to_not raise_error ArgumentError
114
+ end
115
+ end
116
+
117
+ context 'alignment' do
118
+ before :each do
119
+ @title = 'test11test'
120
+ end
121
+
122
+ it 'left aligns title by default' do
123
+ subject.should_receive(:puts).with(@title)
124
+ subject.should_receive(:puts).with("\n")
125
+ subject.header(:title => @title) { }
126
+ end
127
+
128
+ it 'left aligns title' do
129
+ subject.should_receive(:puts).with(@title)
130
+ subject.should_receive(:puts).with("\n")
131
+ subject.header(:title => @title, :align => 'left') { }
132
+ end
133
+
134
+ it 'right aligns title using default width' do
135
+ subject.should_receive(:puts).with(' ' * 90 + @title)
136
+ subject.should_receive(:puts).with("\n")
137
+ subject.header(:title => @title, :align => 'right')
138
+ end
139
+
140
+ it 'right aligns title using specified width' do
141
+ subject.should_receive(:puts).with(' ' * 40 + @title)
142
+ subject.should_receive(:puts).with("\n")
143
+ subject.header(:title => @title, :align => 'right', :width => 50)
144
+ end
145
+
146
+ it 'center aligns title using default width' do
147
+ subject.should_receive(:puts).with(' ' * 45 + @title)
148
+ subject.should_receive(:puts).with("\n")
149
+ subject.header(:title => @title, :align => 'center')
150
+ end
151
+
152
+ it 'center aligns title using specified width' do
153
+ subject.should_receive(:puts).with(' ' * 35 + @title)
154
+ subject.should_receive(:puts).with("\n")
155
+ subject.header(:title => @title, :align => 'center', :width => 80)
156
+ end
157
+ end
158
+
159
+ context 'spacing' do
160
+ it 'defaults to a single line of spacing between report' do
161
+ subject.should_receive(:puts).with('title')
162
+ subject.should_receive(:puts).with("\n")
163
+ subject.header(:title => 'title')
164
+ end
165
+
166
+ it 'uses the defined spacing between report' do
167
+ subject.should_receive(:puts).with('title')
168
+ subject.should_receive(:puts).with("\n" * 3)
169
+ subject.header(:title => 'title', :spacing => 3)
170
+ end
171
+ end
172
+
173
+ context 'timestamp subheading' do
174
+ it 'is added with default alignment' do
175
+ subject.should_receive(:puts).with('title')
176
+ subject.should_receive(:puts).with(/^#{@timestamp_regex}/)
177
+ subject.should_receive(:puts).with("\n")
178
+ subject.header(:title => 'title', :timestamp => true)
179
+ end
180
+
181
+ it 'added with right alignment' do
182
+ subject.should_receive(:puts).with(/^ *title$/)
183
+ subject.should_receive(:puts).with(/^ *#{@timestamp_regex}$/)
184
+ subject.should_receive(:puts).with("\n")
185
+ subject.header(:title => 'title', :align => 'right', :timestamp => true, :width => 80)
186
+ end
187
+
188
+ it 'added with center alignment' do
189
+ subject.should_receive(:puts).with(/^ *title *$/)
190
+ subject.should_receive(:puts).with(/^ *#{@timestamp_regex} *$/)
191
+ subject.should_receive(:puts).with("\n")
192
+ subject.header(:title => 'title', :align => 'center', :timestamp => true, :width => 80)
193
+ end
194
+ end
195
+
196
+ context 'horizontal rule' do
197
+ it 'uses dashes by default' do
198
+ subject.should_receive(:puts)
199
+ subject.should_receive(:puts).with('-' * 100)
200
+ subject.should_receive(:puts)
201
+ subject.header(:rule => true)
202
+ end
203
+
204
+ it 'uses = as the rule character' do
205
+ subject.should_receive(:puts)
206
+ subject.should_receive(:puts).with('=' * 100)
207
+ subject.should_receive(:puts)
208
+ subject.header(:rule => '=')
209
+ end
210
+ end
211
+ end
212
+
213
+ describe '#horizontal_rule' do
214
+ context 'argument validation' do
215
+ it 'does not allow invalid options' do
216
+ expect {
217
+ subject.horizontal_rule(:asdf => true)
218
+ }.to raise_error ArgumentError
219
+ end
220
+
221
+ it 'accepts char' do
222
+ expect {
223
+ subject.should_receive(:puts)
224
+ subject.horizontal_rule(:char => '*')
225
+ }.to_not raise_error ArgumentError
226
+ end
227
+
228
+ it 'accepts width' do
229
+ expect {
230
+ subject.should_receive(:puts)
231
+ subject.horizontal_rule(:width => 10)
232
+ }.to_not raise_error ArgumentError
233
+ end
234
+ end
235
+
236
+ context 'drawing' do
237
+ it 'writes a 100 yard dash by default' do
238
+ subject.should_receive(:puts).with('-' * 100)
239
+ subject.horizontal_rule
240
+ end
241
+
242
+ it 'writes a 100 yard asterisk' do
243
+ subject.should_receive(:puts).with('*' * 100)
244
+ subject.horizontal_rule(:char => '*')
245
+ end
246
+
247
+ it 'writes a 50 yard equals' do
248
+ subject.should_receive(:puts).with('=' * 50)
249
+ subject.horizontal_rule(:char => '=', :width => 50)
250
+ end
251
+ end
252
+ end
253
+
254
+ describe '#vertical_spacing' do
255
+ it 'accepts a fixnum as a valid argument' do
256
+ expect {
257
+ subject.vertical_spacing('asdf')
258
+ }.to raise_error ArgumentError
259
+ end
260
+
261
+ it 'prints carriage returns for the number of lines' do
262
+ subject.should_receive(:puts).with("\n" * 3)
263
+ subject.vertical_spacing(3)
264
+ end
265
+ end
266
+
267
+ describe '#datetime' do
268
+ context 'argument validation' do
269
+ it 'does not allow invalid options' do
270
+ expect {
271
+ subject.datetime(:asdf => true)
272
+ }.to raise_error ArgumentError
273
+ end
274
+
275
+ it 'accepts align' do
276
+ expect {
277
+ subject.should_receive(:puts)
278
+ subject.datetime(:align => 'left')
279
+ }.to_not raise_error ArgumentError
280
+ end
281
+
282
+ it 'accepts width' do
283
+ expect {
284
+ subject.should_receive(:puts)
285
+ subject.datetime(:width => 70)
286
+ }.to_not raise_error ArgumentError
287
+ end
288
+
289
+ it 'accepts format' do
290
+ expect {
291
+ subject.should_receive(:puts)
292
+ subject.datetime(:format => '%m/%d/%Y')
293
+ }.to_not raise_error ArgumentError
294
+ end
295
+
296
+ it 'does not allow invalid width' do
297
+ expect {
298
+ subject.datetime(:align => 'right', :width => 'asdf')
299
+ }.to raise_error
300
+ end
301
+
302
+ it 'does not allow invalid align' do
303
+ expect {
304
+ subject.datetime(:align => 1234)
305
+ }.to raise_error
306
+ end
307
+
308
+ it 'does not allow a timestamp format larger than the width' do
309
+ expect {
310
+ subject.datetime(:width => 8)
311
+ }.to raise_error
312
+ end
313
+ end
314
+
315
+ context 'display' do
316
+ it 'a default format - left aligned' do
317
+ subject.should_receive(:puts).with(/^#{@timestamp_regex} *$/)
318
+ subject.datetime
319
+ end
320
+
321
+ it 'a default format - right aligned' do
322
+ subject.should_receive(:puts).with(/^ *#{@timestamp_regex}$/)
323
+ subject.datetime(:align => 'right')
324
+ end
325
+
326
+ it 'a default format - center aligned' do
327
+ subject.should_receive(:puts).with(/^ *#{@timestamp_regex} *$/)
328
+ subject.datetime(:align => 'center')
329
+ end
330
+
331
+ it 'a modified format' do
332
+ subject.should_receive(:puts).with(/^\d{2}\/\d{2}\/\d{2} *$/)
333
+ subject.datetime(:format => '%y/%m/%d')
334
+ end
335
+ end
336
+ end
337
+
338
+ describe '#aligned' do
339
+ context 'argument validation' do
340
+ it 'accepts align' do
341
+ expect {
342
+ subject.should_receive(:puts).any_number_of_times
343
+ subject.aligned('test', :align => 'left')
344
+ }.to_not raise_error
345
+ end
346
+
347
+ it 'does not allow invalid align values' do
348
+ expect {
349
+ subject.aligned('test', :align => 1234)
350
+ }.to raise_error ArgumentError
351
+ end
352
+
353
+ it 'accepts width' do
354
+ expect {
355
+ subject.should_receive(:puts).any_number_of_times
356
+ subject.aligned('test', :width => 40)
357
+ }.to_not raise_error
358
+ end
359
+
360
+ it 'does not allow invalid width values' do
361
+ expect {
362
+ subject.aligned('test', :align => 'right', :width => 'asdf')
363
+ }.to raise_error
364
+ end
365
+ end
366
+ end
367
+
368
+ describe '#footer' do
369
+ context 'argument validation' do
370
+ it 'accepts title' do
371
+ expect {
372
+ subject.should_receive(:puts).any_number_of_times
373
+ subject.footer(:title => 'test')
374
+ }.to_not raise_error
375
+ end
376
+
377
+ it 'accepts align' do
378
+ expect {
379
+ subject.should_receive(:puts).any_number_of_times
380
+ subject.footer(:align => 'right')
381
+ }.to_not raise_error ArgumentError
382
+ end
383
+
384
+ it 'does not accept invalid align' do
385
+ expect {
386
+ subject.header(:align => 1234)
387
+ }.to raise_error ArgumentError
388
+ end
389
+
390
+ it 'accepts width' do
391
+ expect {
392
+ subject.should_receive(:puts).any_number_of_times
393
+ subject.footer(:width => 50)
394
+ }.to_not raise_error
395
+ end
396
+
397
+ it 'does not accept invalid width' do
398
+ expect {
399
+ subject.footer(:width => 'asdf')
400
+ }.to raise_error ArgumentError
401
+ end
402
+
403
+ it 'does not allow title > width' do
404
+ expect {
405
+ subject.footer(:title => 'testtesttest', :width => 6)
406
+ }.to raise_error ArgumentError
407
+ end
408
+
409
+ it 'accepts spacing' do
410
+ expect {
411
+ subject.should_receive(:puts).any_number_of_times
412
+ subject.footer(:spacing => 3)
413
+ }.to_not raise_error
414
+ end
415
+ end
416
+
417
+ context 'alignment' do
418
+ before :each do
419
+ @title = 'test12test'
420
+ end
421
+
422
+ it 'left aligns the title by default' do
423
+ subject.should_receive(:puts).with("\n")
424
+ subject.should_receive(:puts).with(@title)
425
+ subject.footer(:title => @title)
426
+ end
427
+
428
+ it 'left aligns the title' do
429
+ subject.should_receive(:puts).with("\n")
430
+ subject.should_receive(:puts).with(@title)
431
+ subject.footer(:title => @title, :align => 'left')
432
+ end
433
+
434
+ it 'right aligns the title' do
435
+ subject.should_receive(:puts).with("\n")
436
+ subject.should_receive(:puts).with(' ' * 90 + @title)
437
+ subject.footer(:title => @title, :align => 'right')
438
+ end
439
+
440
+ it 'right aligns the title using width' do
441
+ subject.should_receive(:puts).with("\n")
442
+ subject.should_receive(:puts).with(' ' * 40 + @title)
443
+ subject.footer(:title => @title, :align => 'right', :width => 50)
444
+ end
445
+
446
+ it 'center aligns the title' do
447
+ subject.should_receive(:puts).with("\n")
448
+ subject.should_receive(:puts).with(' ' * 45 + @title)
449
+ subject.footer(:title => @title, :align => 'center')
450
+ end
451
+
452
+ it 'center aligns the title using width' do
453
+ subject.should_receive(:puts).with("\n")
454
+ subject.should_receive(:puts).with(' ' * 35 + @title)
455
+ subject.footer(:title => @title, :align => 'center', :width => 80)
456
+ end
457
+ end
458
+
459
+ context 'spacing' do
460
+ it 'defaults to a single line of spacing between report' do
461
+ subject.should_receive(:puts).with("\n")
462
+ subject.should_receive(:puts).with('title')
463
+ subject.footer(:title => 'title')
464
+ end
465
+
466
+ it 'uses the defined spacing between report' do
467
+ subject.should_receive(:puts).with("\n" * 3)
468
+ subject.should_receive(:puts).with('title')
469
+ subject.footer(:title => 'title', :spacing => 3)
470
+ end
471
+ end
472
+
473
+ context 'timestamp subheading' do
474
+ it 'is added with default alignment' do
475
+ subject.should_receive(:puts).with("\n")
476
+ subject.should_receive(:puts).with('title')
477
+ subject.should_receive(:puts).with(/^#{@timestamp_regex}/)
478
+ subject.footer(:title => 'title', :timestamp => true)
479
+ end
480
+
481
+ it 'added with right alignment' do
482
+ subject.should_receive(:puts).with("\n")
483
+ subject.should_receive(:puts).with(/^ *title$/)
484
+ subject.should_receive(:puts).with(/^ *#{@timestamp_regex}$/)
485
+ subject.header(:title => 'title', :align => 'right', :timestamp => true, :width => 80)
486
+ end
487
+
488
+ it 'added with center alignment' do
489
+ subject.should_receive(:puts).with("\n")
490
+ subject.should_receive(:puts).with(/^ *title *$/)
491
+ subject.should_receive(:puts).with(/^ *#{@timestamp_regex} *$/)
492
+ subject.header(:title => 'title', :align => 'center', :timestamp => true, :width => 80)
493
+ end
494
+ end
495
+
496
+ context 'horizontal rule' do
497
+ it 'uses dashes by default' do
498
+ subject.should_receive(:puts)
499
+ subject.should_receive(:puts).with('-' * 100)
500
+ subject.should_receive(:puts)
501
+ subject.footer(:rule => true)
502
+ end
503
+
504
+ it 'uses = as the rule character' do
505
+ subject.should_receive(:puts)
506
+ subject.should_receive(:puts).with('=' * 100)
507
+ subject.should_receive(:puts)
508
+ subject.footer(:rule => '=')
509
+ end
510
+ end
36
511
  end
37
512
  end
@@ -1 +1,3 @@
1
1
  $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,8 @@
1
+ require 'stringio'
2
+
3
+ def capture_stdout
4
+ $stdout = StringIO.new
5
+ yield
6
+ ensure
7
+ $stdout = STDOUT
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_line_reporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-11-28 00:00:00.000000000Z
13
+ date: 2011-12-01 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
17
- requirement: &2151943860 !ruby/object:Gem::Requirement
17
+ requirement: &2151943740 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: 1.0.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2151943860
25
+ version_requirements: *2151943740
26
26
  description: This gem makes it easy to provide a report while your ruby script is
27
27
  executing
28
28
  email: baywes@gmail.com
@@ -32,6 +32,7 @@ extra_rdoc_files: []
32
32
  files:
33
33
  - examples/nested.rb
34
34
  - examples/progress.rb
35
+ - examples/simple.rb
35
36
  - lib/command_line_reporter.rb
36
37
  - lib/nested_formatter.rb
37
38
  - lib/progress_formatter.rb
@@ -41,6 +42,7 @@ files:
41
42
  - spec/nested_formatter_spec.rb
42
43
  - spec/progress_formatter_spec.rb
43
44
  - spec/spec_helper.rb
45
+ - spec/support/helpers/stdout.rb
44
46
  homepage: http://github.com/wbailey/command_line_reporter
45
47
  licenses: []
46
48
  post_install_message:
@@ -55,7 +57,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
57
  version: '0'
56
58
  segments:
57
59
  - 0
58
- hash: 4291773844303302858
60
+ hash: -846505284794212082
59
61
  required_rubygems_version: !ruby/object:Gem::Requirement
60
62
  none: false
61
63
  requirements:
@@ -73,3 +75,4 @@ test_files:
73
75
  - spec/nested_formatter_spec.rb
74
76
  - spec/progress_formatter_spec.rb
75
77
  - spec/spec_helper.rb
78
+ - spec/support/helpers/stdout.rb