table_print 1.5.7 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,19 +6,19 @@ describe TablePrint::TimeFormatter do
6
6
  describe "#format" do
7
7
  it "only operates on Time objects" do
8
8
  f = TablePrint::TimeFormatter.new
9
- f.format(12).should == 12
9
+ expect(f.format(12)).to eq 12
10
10
  end
11
11
 
12
12
  it "uses the config'd time_format" do
13
13
  f = TablePrint::TimeFormatter.new
14
14
  time = Time.local(2012, 01, 11, 1, 23, 45)
15
- f.format(time).should == "2012-01-11 01:23:45" # default time format is set in config.rb
15
+ expect(f.format(time)).to eq "2012-01-11 01:23:45" # default time format is set in config.rb
16
16
  end
17
17
 
18
18
  it "overrides the config'd time format with one it was passed" do
19
19
  f = TablePrint::TimeFormatter.new("%Y")
20
20
  time = Time.local(2012, 01, 11, 1, 23, 45)
21
- f.format(time).should == "2012" # default time format is set in config.rb
21
+ expect(f.format(time)).to eq "2012" # default time format is set in config.rb
22
22
  end
23
23
  end
24
24
  end
@@ -30,11 +30,11 @@ describe TablePrint::NoNewlineFormatter do
30
30
 
31
31
  describe "#format" do
32
32
  it "replaces carriage returns with spaces" do
33
- @f.format("foo\r\nbar").should == "foo bar"
33
+ expect(@f.format("foo\r\nbar")).to eq "foo bar"
34
34
  end
35
35
 
36
36
  it "replaces newlines with spaces" do
37
- @f.format("foo\nbar").should == "foo bar"
37
+ expect(@f.format("foo\nbar")).to eq "foo bar"
38
38
  end
39
39
  end
40
40
  end
@@ -46,19 +46,25 @@ describe TablePrint::FixedWidthFormatter do
46
46
 
47
47
  describe "#format" do
48
48
  it "pads a short field to the specified width" do
49
- @f.format("asdf").should == "asdf "
49
+ expect(@f.format("asdf")).to eq "asdf "
50
+ end
51
+
52
+ it "truncates without the periods when the width can't fit them" do
53
+ expect(TablePrint::FixedWidthFormatter.new(3).format("1234567890")).to eq "123"
54
+ expect(TablePrint::FixedWidthFormatter.new(1).format("1234567890")).to eq "1"
55
+ expect(TablePrint::FixedWidthFormatter.new(0).format("1234567890")).to eq ""
50
56
  end
51
57
 
52
58
  it "truncates long fields with periods" do
53
- @f.format("1234567890123456").should == "1234567..."
59
+ expect(@f.format("1234567890123456")).to eq "1234567..."
54
60
  end
55
61
 
56
62
  it "uses an empty string in place of nils" do
57
- @f.format(nil).should == " "
63
+ expect(@f.format(nil)).to eq " "
58
64
  end
59
65
 
60
66
  it "turns objects into strings before trying to format them" do
61
- @f.format(123).should == "123 "
67
+ expect(@f.format(123)).to eq "123 "
62
68
  end
63
69
  end
64
70
  end
@@ -5,7 +5,7 @@ describe "#constructive_merge" do
5
5
  x = {'reviews' => {'user' => {}}}
6
6
  y = {'reviews' => {'ratings' => {}}}
7
7
  x.extend TablePrint::HashExtensions::ConstructiveMerge
8
- x.constructive_merge(y).should == {'reviews' => {'user' => {}, 'ratings' => {}}}
8
+ expect(x.constructive_merge(y)).to eq({'reviews' => {'user' => {}, 'ratings' => {}}})
9
9
  end
10
10
  end
11
11
 
@@ -15,6 +15,6 @@ describe "#constructive_merge!" do
15
15
  y = {'reviews' => {'ratings' => {}}}
16
16
  x.extend TablePrint::HashExtensions::ConstructiveMerge
17
17
  x.constructive_merge!(y)
18
- x.should == {'reviews' => {'user' => {}, 'ratings' => {}}}
18
+ expect(x).to eq({'reviews' => {'user' => {}, 'ratings' => {}}})
19
19
  end
20
20
  end
@@ -11,7 +11,17 @@ describe TablePrint::Printable do
11
11
  Sandbox.add_attributes("Hat", "brand")
12
12
 
13
13
  p = Sandbox::Hat.new
14
- TablePrint::Printable.default_display_methods(p).should == %W(brand)
14
+ expect(TablePrint::Printable.default_display_methods(p)).to eq %W(brand)
15
+ end
16
+
17
+ it "uses an OpenStruct's attributes rather than its methods" do
18
+ expect(TablePrint::Printable.default_display_methods(OpenStruct.new(:name => "alice", :age => 30))).to eq [:name, :age]
19
+ end
20
+
21
+ it "shows booleans in a single value column" do
22
+ methods = TablePrint::Printable.default_display_methods(true)
23
+ expect(methods.length).to eq 1
24
+ expect(methods.first.keys).to eq ["value"]
15
25
  end
16
26
 
17
27
  it "ignores dangerous methods" do
@@ -19,7 +29,7 @@ describe TablePrint::Printable do
19
29
  Sandbox.add_method("Hat", "brand!") {}
20
30
 
21
31
  p = Sandbox::Hat.new
22
- TablePrint::Printable.default_display_methods(p).should == []
32
+ expect(TablePrint::Printable.default_display_methods(p)).to eq []
23
33
  end
24
34
 
25
35
  it "ignores methods defined in a superclass" do
@@ -28,7 +38,7 @@ describe TablePrint::Printable do
28
38
  Sandbox.add_attributes("Hat::Bowler", "brim_width")
29
39
 
30
40
  p = Sandbox::Hat::Bowler.new
31
- TablePrint::Printable.default_display_methods(p).should == %W(brim_width)
41
+ expect(TablePrint::Printable.default_display_methods(p)).to eq %W(brim_width)
32
42
  end
33
43
 
34
44
  it "ignores methods that require arguments" do
@@ -37,11 +47,11 @@ describe TablePrint::Printable do
37
47
  Sandbox.add_method("Hat", "tip?") { |person| person.rapscallion? }
38
48
 
39
49
  p = Sandbox::Hat.new
40
- TablePrint::Printable.default_display_methods(p).should == %W(brand)
50
+ expect(TablePrint::Printable.default_display_methods(p)).to eq %W(brand)
41
51
  end
42
52
 
43
53
  it "ignores methods from an included module" do
44
- pending "waiting for Cat to support module manipulation"
54
+ skip "waiting for Cat to support module manipulation"
45
55
  end
46
56
 
47
57
  it "uses column information when available (eg, from ActiveRecord objects)"
@@ -51,7 +61,7 @@ describe TablePrint::Printable do
51
61
  obj = test_struct.new
52
62
  obj.foo = 1
53
63
  obj.bar = 2
54
- TablePrint::Printable.default_display_methods(obj).should == [:foo, :bar]
64
+ expect(TablePrint::Printable.default_display_methods(obj)).to eq [:foo, :bar]
55
65
  end
56
66
 
57
67
 
@@ -3,29 +3,29 @@ require 'spec_helper'
3
3
  describe TablePrint::Returnable do
4
4
  it "returns its initialized value from its to_s method" do
5
5
  r = TablePrint::Returnable.new("foobar")
6
- r.to_s.should == "foobar"
6
+ expect(r.to_s).to eq "foobar"
7
7
  end
8
8
 
9
9
  it "returns its initialized value from its inspect method" do
10
10
  # 1.8.7 calls inspect on return values
11
11
  r = TablePrint::Returnable.new("foobar")
12
- r.inspect.should == "foobar"
12
+ expect(r.inspect).to eq "foobar"
13
13
  end
14
14
 
15
15
  it "passes #set through to TablePrint::Config" do
16
- TablePrint::Config.should_receive(:set).with(Object, [:foo])
16
+ expect(TablePrint::Config).to receive(:set).with(Object, [:foo])
17
17
  r = TablePrint::Returnable.new
18
18
  r.set(Object, :foo)
19
19
  end
20
20
 
21
21
  it "passes #clear through to TablePrint::Config" do
22
- TablePrint::Config.should_receive(:clear).with(Object)
22
+ expect(TablePrint::Config).to receive(:clear).with(Object)
23
23
  r = TablePrint::Returnable.new
24
24
  r.clear(Object)
25
25
  end
26
26
 
27
27
  it "passes #config_for through to TablePrint::Config.for" do
28
- TablePrint::Config.should_receive(:for).with(Object)
28
+ expect(TablePrint::Config).to receive(:for).with(Object)
29
29
  r = TablePrint::Returnable.new
30
30
  r.config_for(Object)
31
31
  end
@@ -10,19 +10,19 @@ describe RowRecursion do
10
10
  it "assigns the column object to the column name" do
11
11
  column = Column.new(:name => "foobar")
12
12
  parent.set_column(column)
13
- parent.column_for(:foobar).should == column
13
+ expect(parent.column_for(:foobar)).to eq column
14
14
  end
15
15
  end
16
16
 
17
17
  describe "#add_child" do
18
18
  it "adds the child to my children" do
19
19
  parent.add_child(child)
20
- parent.child_count.should == 1
20
+ expect(parent.child_count).to eq 1
21
21
  end
22
22
 
23
23
  it "sets me as my child's parent" do
24
24
  parent.add_child(child)
25
- child.parent.should == parent
25
+ expect(child.parent).to eq parent
26
26
  end
27
27
  end
28
28
 
@@ -31,13 +31,13 @@ describe RowRecursion do
31
31
 
32
32
  it "adds all the children to myself" do
33
33
  parent.add_children([child, child2])
34
- parent.child_count.should == 2
34
+ expect(parent.child_count).to eq 2
35
35
  end
36
36
 
37
37
  it "sets me as their parent" do
38
38
  parent.add_children([child, child2])
39
- child.parent.should == parent
40
- child2.parent.should == parent
39
+ expect(child.parent).to eq parent
40
+ expect(child2.parent).to eq parent
41
41
  end
42
42
  end
43
43
 
@@ -46,17 +46,17 @@ describe RowRecursion do
46
46
  child.set_cell_values(:title => 'foobar')
47
47
  parent.add_child(child)
48
48
 
49
- parent.columns.length.should == 1
50
- parent.columns.first.name.should == 'title'
51
- parent.columns.first.data.should == ['foobar']
49
+ expect(parent.columns.length).to eq 1
50
+ expect(parent.columns.first.name).to eq 'title'
51
+ expect(parent.columns.first.data).to eq ['foobar']
52
52
  end
53
53
 
54
54
  it "gets the columns from the root node" do
55
55
  parent.add_child(child)
56
56
  child.set_cell_values(:title => 'foobar')
57
57
 
58
- parent.columns.length.should == 1
59
- child.columns.should == parent.columns
58
+ expect(parent.columns.length).to eq 1
59
+ expect(child.columns).to eq parent.columns
60
60
  end
61
61
  end
62
62
 
@@ -65,7 +65,7 @@ describe RowRecursion do
65
65
  parent.add_child(child)
66
66
  child.set_cell_values(:title => 'foobar')
67
67
  column = parent.columns.first
68
- parent.column_for(:title).should == column
68
+ expect(parent.column_for(:title)).to eq column
69
69
  end
70
70
  end
71
71
 
@@ -76,7 +76,7 @@ describe RowRecursion do
76
76
  column = parent.columns.first
77
77
  parent.add_formatter(:title, {})
78
78
 
79
- column.formatters.should == [{}]
79
+ expect(column.formatters).to eq [{}]
80
80
  end
81
81
  end
82
82
 
@@ -88,7 +88,7 @@ describe RowRecursion do
88
88
  r1.set_cell_values(:title => 'foobar')
89
89
  r2.set_cell_values(:subtitle => 'elemental')
90
90
 
91
- parent.width.should == 18
91
+ expect(parent.width).to eq 18
92
92
  end
93
93
  end
94
94
 
@@ -99,13 +99,31 @@ describe RowRecursion do
99
99
 
100
100
  r1.set_cell_values(:title => 'a' * 5, :description => 'b' * 3, :category => 'c' * 10)
101
101
  r2.set_cell_values(:title => 'a' * 6, :description => 'b' * 4, :category => 'c' * 9)
102
- parent.header.size.should == parent.horizontal_separator.size
102
+ expect(parent.header.size).to eq parent.horizontal_separator.size
103
103
  compare_rows(parent.horizontal_separator, '-' * 6 + '-|-' + '-' * 'description'.size + '-|-' + '-' * 10)
104
104
  end
105
105
 
106
+ it "uses the character from the config" do
107
+ child.set_cell_values(:title => 'foobar', :author => 'chris')
108
+ TablePrint::Config.set(:horizontal_separator, ["="])
109
+
110
+ expect(child.horizontal_separator).to eq '=' * 6 + '=|=' + '=' * 6
111
+ ensure
112
+ TablePrint::Config.clear(:horizontal_separator)
113
+ end
114
+
115
+ it "is absent when the config is nil" do
116
+ child.set_cell_values(:title => 'foobar')
117
+ TablePrint::Config.set(:horizontal_separator, [nil])
118
+
119
+ expect(child.horizontal_separator).to be_nil
120
+ ensure
121
+ TablePrint::Config.clear(:horizontal_separator)
122
+ end
123
+
106
124
  it "matches the header width" do
107
125
  child.set_cell_values(:title => 'foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar')
108
- child.horizontal_separator.should == '------------------------------' # 30 hyphens
126
+ expect(child.horizontal_separator).to eq '------------------------------' # 30 hyphens
109
127
  end
110
128
  end
111
129
 
@@ -123,15 +141,15 @@ describe TablePrint::RowGroup do
123
141
  group = RowGroup.new
124
142
  group.add_child(Row.new.set_cell_values(:title => 'foo'))
125
143
  group.add_child(Row.new.set_cell_values(:title => 'bar'))
126
- group.raw_column_data(:title).should == ['foo', 'bar']
144
+ expect(group.raw_column_data(:title)).to eq ['foo', 'bar']
127
145
  end
128
146
  end
129
147
  end
130
148
 
131
149
  def compare_rows(actual_rows, expected_rows)
132
- actual_rows.split("\n").length.should == expected_rows.split("\n").length
150
+ expect(actual_rows.split("\n").length).to eq expected_rows.split("\n").length
133
151
  actual_rows.split("\n").zip(expected_rows.split("\n")).each do |actual, expected|
134
- actual.split(//).sort.join.should == expected.split(//).sort.join
152
+ expect(actual.split(//).sort.join).to eq expected.split(//).sort.join
135
153
  end
136
154
  end
137
155
 
@@ -160,16 +178,16 @@ describe TablePrint::Row do
160
178
  f1 = Sandbox::DoubleFormatter.new
161
179
  f2 = Sandbox::ChopFormatter.new
162
180
 
163
- row.stub(:column_for) {OpenStruct.new(:width => 11, :formatters => [f1, f2])}
181
+ allow(row).to receive(:column_for).and_return(OpenStruct.new(:width => 11, :formatters => [f1, f2]))
164
182
 
165
- row.apply_formatters(:title, "foobar").should == "foobarfooba"
183
+ expect(row.apply_formatters(:title, "foobar")).to eq "foobarfooba"
166
184
  end
167
185
 
168
186
  it "uses the config'd time_format to format times" do
169
- row.stub(:column_for) {OpenStruct.new(:width => 20, :formatters => [], :time_format => "%Y %m %d")}
187
+ allow(row).to receive(:column_for).and_return(OpenStruct.new(:width => 20, :formatters => [], :time_format => "%Y %m %d"))
170
188
 
171
189
  time_formatter = TablePrint::TimeFormatter.new
172
- TablePrint::TimeFormatter.should_receive(:new).with("%Y %m %d") {time_formatter}
190
+ expect(TablePrint::TimeFormatter).to receive(:new).with("%Y %m %d") {time_formatter}
173
191
  row.apply_formatters(:title, Time.local(2012, 6, 1, 14, 20, 20))
174
192
  end
175
193
  end
@@ -184,7 +202,7 @@ describe TablePrint::Row do
184
202
  end
185
203
  row.add_child(group)
186
204
 
187
- row.raw_column_data('title').should == ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
205
+ expect(row.raw_column_data('title')).to eq ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
188
206
  end
189
207
  end
190
208
 
@@ -206,11 +224,11 @@ describe TablePrint::Row do
206
224
  end
207
225
 
208
226
  it "pulls the cells up into the parent" do
209
- @row.cells.should == {"foo" => "foo", "bar" => "bar"}
227
+ expect(@row.cells).to eq({"foo" => "foo", "bar" => "bar"})
210
228
  end
211
229
 
212
230
  it "dereferences the now-defunct group" do
213
- @row.children.length.should == 0
231
+ expect(@row.children.length).to eq 0
214
232
  end
215
233
  end
216
234
 
@@ -233,13 +251,13 @@ describe TablePrint::Row do
233
251
  end
234
252
 
235
253
  it "pulls the cells from the first row up into the parent" do
236
- @row.cells.should == {"foo" => "foo", "bar" => "bar"}
254
+ expect(@row.cells).to eq({"foo" => "foo", "bar" => "bar"})
237
255
  end
238
256
 
239
257
  it "deletes the absorbed row but leaves the second row in the group" do
240
- @row.children.length.should == 1
241
- @row.children.first.children.length.should == 1
242
- @row.children.first.children.first.cells.should == {"bar" => "baz"}
258
+ expect(@row.children.length).to eq 1
259
+ expect(@row.children.first.children.length).to eq 1
260
+ expect(@row.children.first.children.first.cells).to eq({"bar" => "baz"})
243
261
  end
244
262
  end
245
263
 
@@ -264,11 +282,11 @@ describe TablePrint::Row do
264
282
  end
265
283
 
266
284
  it "pulls the cells from both groups into the parent" do
267
- @row.cells.should == {"foo" => "foo", "bar" => "bar", "baz" => "baz"}
285
+ expect(@row.cells).to eq({"foo" => "foo", "bar" => "bar", "baz" => "baz"})
268
286
  end
269
287
 
270
288
  it "dereferences both now-defunct groups" do
271
- @row.children.length.should == 0
289
+ expect(@row.children.length).to eq 0
272
290
  end
273
291
  end
274
292
 
@@ -290,11 +308,11 @@ describe TablePrint::Row do
290
308
  end
291
309
 
292
310
  it "pulls the cells from both groups into the parent" do
293
- @row.cells.should == {"foo" => "foo", "bar" => "bar"}
311
+ expect(@row.cells).to eq({"foo" => "foo", "bar" => "bar"})
294
312
  end
295
313
 
296
314
  it "dereferences both now-defunct groups" do
297
- @row.children.length.should == 0
315
+ expect(@row.children.length).to eq 0
298
316
  end
299
317
  end
300
318
 
@@ -322,13 +340,13 @@ describe TablePrint::Row do
322
340
  end
323
341
 
324
342
  it "pulls the single row and the first row from the double into itself" do
325
- @row.cells.should == {"foo" => "foo", "bar" => "bar", "baz" => "baz"}
343
+ expect(@row.cells).to eq({"foo" => "foo", "bar" => "bar", "baz" => "baz"})
326
344
  end
327
345
 
328
346
  it "keeps the second row from the second group in its own group" do
329
- @row.children.length.should == 1
330
- @row.children.first.children.length.should == 1
331
- @row.children.first.children.first.cells.should == {"baz" => "bazaar"}
347
+ expect(@row.children.length).to eq 1
348
+ expect(@row.children.first.children.length).to eq 1
349
+ expect(@row.children.first.children.first.cells).to eq({"baz" => "bazaar"})
332
350
  end
333
351
  end
334
352
 
@@ -354,11 +372,11 @@ describe TablePrint::Row do
354
372
  end
355
373
 
356
374
  it "pulls the cells from both groups into the parent" do
357
- @row.cells.should == {"foo" => "foo", "bar" => "bar", "baz" => "baz"}
375
+ expect(@row.cells).to eq({"foo" => "foo", "bar" => "bar", "baz" => "baz"})
358
376
  end
359
377
 
360
378
  it "dereferences both now-defunct groups" do
361
- @row.children.length.should == 0
379
+ expect(@row.children.length).to eq 0
362
380
  end
363
381
  end
364
382
 
@@ -387,13 +405,13 @@ describe TablePrint::Row do
387
405
  end
388
406
 
389
407
  it "pulls the first row from each group up into itself" do
390
- @row.cells.should == {"foo" => "foo", "bar" => "bar", "baz" => "baz"}
408
+ expect(@row.cells).to eq({"foo" => "foo", "bar" => "bar", "baz" => "baz"})
391
409
  end
392
410
 
393
411
  it "deletes only the intermediary group" do
394
- @row.children.length.should == 1
395
- @row.children.first.children.length.should == 1
396
- @row.children.first.children.first.cells.should == {"baz" => "bazaar"}
412
+ expect(@row.children.length).to eq 1
413
+ expect(@row.children.first.children.length).to eq 1
414
+ expect(@row.children.first.children.first.cells).to eq({"baz" => "bazaar"})
397
415
  end
398
416
  end
399
417
 
@@ -425,19 +443,19 @@ describe TablePrint::Row do
425
443
  end
426
444
 
427
445
  it "pulls the first row from the first group into the parent" do
428
- @row.cells.should == {"foo" => "foo", "bar" => "bar"}
446
+ expect(@row.cells).to eq({"foo" => "foo", "bar" => "bar"})
429
447
  end
430
448
 
431
449
  it "leaves the second row in the first group" do
432
- @row.children.length.should == 2
433
- @row.children.first.children.length.should == 1
434
- @row.children.first.children.first.cells.should == {"bar" => "bar2"}
450
+ expect(@row.children.length).to eq 2
451
+ expect(@row.children.first.children.length).to eq 1
452
+ expect(@row.children.first.children.first.cells).to eq({"bar" => "bar2"})
435
453
  end
436
454
 
437
455
  it "leaves the second group alone" do
438
- @row.children.last.children.length.should == 2
439
- @row.children.last.children.first.cells.should == {"baz" => "bazaar"}
440
- @row.children.last.children.last.cells.should == {"baz" => "bazaar2"}
456
+ expect(@row.children.last.children.length).to eq 2
457
+ expect(@row.children.last.children.first.cells).to eq({"baz" => "bazaar"})
458
+ expect(@row.children.last.children.last.cells).to eq({"baz" => "bazaar2"})
441
459
  end
442
460
  end
443
461
 
@@ -478,16 +496,16 @@ describe TablePrint::Row do
478
496
  end
479
497
 
480
498
  it "pulls the first row from the first child into itself" do
481
- @row.cells.should == {"foo" => "foo", "bar" => "bar", "barry" => "bare"}
499
+ expect(@row.cells).to eq({"foo" => "foo", "bar" => "bar", "barry" => "bare"})
482
500
  end
483
501
 
484
502
  it "leaves the second row from the first child in the first group" do
485
- @row.children.first.children.first.cells.should == {"barry" => "bart"}
503
+ expect(@row.children.first.children.first.cells).to eq({"barry" => "bart"})
486
504
  end
487
505
 
488
506
  it "collapses the second group" do
489
- @row.children.last.children.first.cells.should == {"bar" => "baz", "barry" => "bazaar"}
490
- @row.children.last.children.first.children.first.children.first.cells.should == {"barry" => "bizarre"}
507
+ expect(@row.children.last.children.first.cells).to eq ({"bar" => "baz", "barry" => "bazaar"})
508
+ expect(@row.children.last.children.first.children.first.children.first.cells).to eq({"barry" => "bizarre"})
491
509
  end
492
510
  end
493
511
  end
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,16 @@
1
1
  require 'cat'
2
- gem 'rspec'
3
2
  require 'table_print'
4
3
  require 'ostruct'
5
4
 
6
- RSpec.configure do |c|
7
- c.filter_run :focus => true
8
- c.run_all_when_everything_filtered = true
9
- end
5
+ RSpec.configure do |config|
6
+ config.filter_run_when_matching :focus
7
+
8
+ config.expect_with :rspec do |expectations|
9
+ expectations.syntax = :expect
10
+ end
10
11
 
12
+ config.mock_with :rspec do |mocks|
13
+ mocks.syntax = :expect
14
+ mocks.verify_partial_doubles = true
15
+ end
16
+ end