hirb 0.2.9 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
data/test/hirb_test.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  class HirbTest < Test::Unit::TestCase
4
- before(:each) {Hirb.instance_eval "@config = nil"}
4
+ before(:all) { Hirb.config_files = nil }
5
+ before(:each) { Hirb.config = nil }
5
6
 
6
7
  test "config converts yaml when config file exists" do
7
8
  yaml_data = {:blah=>'blah'}
8
9
  File.stubs('exists?').returns(true)
10
+ Hirb.config_files = ['ok']
9
11
  YAML::expects(:load_file).returns(yaml_data)
10
12
  Hirb.config.should == yaml_data
11
13
  end
@@ -17,14 +19,21 @@ class HirbTest < Test::Unit::TestCase
17
19
 
18
20
  test "config reloads if given explicit reload" do
19
21
  Hirb.config
20
- Hirb.expects(:read_config_file)
22
+ Hirb.expects(:read_config_file).returns({})
21
23
  Hirb.config(true)
22
24
  end
23
25
 
26
+ test "config reads multiple config files and merges them" do
27
+ Hirb.config_files = %w{one two}
28
+ Hirb.expects(:read_config_file).times(2).returns({:output=>{"String"=>:auto_table}}, {:output=>{"Array"=>:auto_table}})
29
+ Hirb.config.should == {:output=>{"Array"=>:auto_table, "String"=>:auto_table}}
30
+ Hirb.config_files = nil
31
+ end
32
+
24
33
  test "config_file sets correctly when no ENV['HOME']" do
25
- Hirb.config_file = nil
34
+ Hirb.config_files = nil
26
35
  home = ENV.delete('HOME')
27
- Hirb.config_file.class.should == String
36
+ Hirb.config_files[0].class.should == String
28
37
  ENV["HOME"] = home
29
38
  end
30
39
  end
data/test/menu_test.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  class Hirb::MenuTest < Test::Unit::TestCase
4
+ before(:all) { Hirb::View.instance_variable_set("@config", :width=>Hirb::View::DEFAULT_WIDTH) }
5
+
4
6
  def menu(*args, &block)
5
7
  # testing via menu's main use case (through console) instead of Hirb::Menu.render
6
8
  @console ||= Object.new.extend(Hirb::Console)
@@ -41,11 +43,11 @@ class Hirb::MenuTest < Test::Unit::TestCase
41
43
 
42
44
  test "with block and no chosen doesn't call block" do
43
45
  menu_input ""
44
- block = lambda {|e| }
45
- block.expects(:call).never
46
+ block = lambda {|e| @called = true }
46
47
  capture_stdout {
47
48
  menu([1,2,3], &block).should == []
48
49
  }
50
+ assert !@called
49
51
  end
50
52
 
51
53
  test "with valid helper_class option renders" do
@@ -86,9 +88,136 @@ class Hirb::MenuTest < Test::Unit::TestCase
86
88
  menu([1], :ask=>false).should == [1]
87
89
  end
88
90
 
89
- test "with validate_one option returns chosen one" do
90
- menu_input '2'
91
- capture_stdout { menu([1,2,3], :validate_one=> true).should == 2 }
91
+ test "with no items to choose from always return without asking" do
92
+ $stdin.expects(:gets).never
93
+ menu([], :ask=>false).should == []
94
+ menu([], :ask=>true).should == []
95
+ end
96
+
97
+ test "with directions option turns off directions" do
98
+ menu_input('blah')
99
+ capture_stdout { menu([1], :directions=>false) }.should_not =~ /range.*all/
100
+ end
101
+ end
102
+
103
+ def two_d_menu(options={})
104
+ if options[:invokes] || options[:invoke]
105
+ cmd = options[:command] || 'p'
106
+ (options[:invokes] || [options[:invoke]]).each {|e|
107
+ Hirb::Menu.any_instance.expects(:invoke).with(cmd, e)
108
+ }
109
+ end
110
+
111
+ capture_stdout {
112
+ return menu(options[:output] || [{:a=>1, :bro=>2}, {:a=>3, :bro=>4}],
113
+ {:two_d=>true}.merge(options))
114
+ }
115
+ end
116
+
117
+ context "2d menu" do
118
+ test "with default field from last_table renders" do
119
+ menu_input "1"
120
+ two_d_menu.should == [1]
121
+ end
122
+
123
+ test "with default field from fields option renders" do
124
+ menu_input "1"
125
+ two_d_menu(:fields=>[:bro, :a]).should == [2]
126
+ end
127
+
128
+ test "with default field option renders" do
129
+ menu_input "1"
130
+ two_d_menu(:default_field=>:bro).should == [2]
131
+ end
132
+
133
+ test "with non-table helper class renders" do
134
+ menu_input "1"
135
+ two_d_menu(:helper_class=>false, :fields=>[:a,:bro]).should == [1]
136
+ end
137
+
138
+ test "with no default field prints error" do
139
+ menu_input "1"
140
+ capture_stderr { two_d_menu(:fields=>[]) }.should =~ /No default.*found/
141
+ end
142
+
143
+ test "with invalid field prints error" do
144
+ menu_input "1:z"
145
+ capture_stderr { two_d_menu }.should =~ /Invalid.*'z'/
146
+ end
147
+
148
+ test "with choice from abbreviated field" do
149
+ menu_input "2:b"
150
+ two_d_menu.should == [4]
151
+ end
152
+
153
+ test "with choices from multiple fields renders" do
154
+ menu_input "1 2:bro"
155
+ two_d_menu.should == [1,4]
156
+ end
157
+ end
158
+
159
+ context "action menu" do
160
+ test "invokes" do
161
+ menu_input "p 1 2:bro"
162
+ two_d_menu(:action=>true, :invoke=>[[1,4]])
163
+ end
164
+
165
+ test "with 1d invokes" do
166
+ menu_input "p 1"
167
+ two_d_menu(:action=>true, :two_d=>nil, :invoke=>[[{:a=>1, :bro=>2}]])
168
+ end
169
+
170
+ test "with non-choice arguments invokes" do
171
+ menu_input "p arg1 1"
172
+ two_d_menu :action=>true, :invoke=>['arg1', [1]]
173
+ end
174
+
175
+ test "with multiple choice arguments flattens them into arg" do
176
+ menu_input "p arg1 1 2:bro arg2"
177
+ two_d_menu :action=>true, :invoke=>['arg1', [1,4], 'arg2']
178
+ end
179
+
180
+ test "with nothing chosen prints error" do
181
+ menu_input "cmd"
182
+ capture_stderr { two_d_menu(:action=>true) }.should =~ /No rows chosen/
183
+ end
184
+
185
+ test "with no command given prints error" do
186
+ menu_input "1"
187
+ capture_stderr { two_d_menu(:action=>true) }.should =~ /No command given/
188
+ end
189
+
190
+ test "with multi_action option invokes" do
191
+ menu_input "p 1 2:bro"
192
+ two_d_menu(:action=>true, :multi_action=>true, :invokes=>[[1], [4]])
193
+ end
194
+
195
+ test "with command option invokes" do
196
+ menu_input "1"
197
+ two_d_menu(:action=>true, :command=>'p', :invoke=>[[1]])
198
+ end
199
+
200
+ test "with command option and empty input doesn't invoke action and exists silently" do
201
+ Hirb::Menu.any_instance.expects(:invoke).never
202
+ menu_input ""
203
+ two_d_menu(:action=>true, :command=>'p').should == nil
204
+ end
205
+
206
+ test "with action_object option invokes" do
207
+ obj = mock(:blah=>true)
208
+ menu_input "blah 1"
209
+ two_d_menu(:action=>true, :action_object=>obj)
210
+ end
211
+
212
+ test "with ask false and defaults invokes" do
213
+ two_d_menu(:output=>[{:a=>1, :bro=>2}], :action=>true, :ask=>false, :default_field=>:a,
214
+ :command=>'p', :invoke=>[[1]])
215
+ end
216
+
217
+ test "with ask false and no defaults prints error" do
218
+ capture_stderr {
219
+ two_d_menu(:output=>[{:a=>1, :bro=>2}], :action=>true, :ask=>false, :command=>'p')
220
+ }.should =~ /Default.*required/
92
221
  end
93
222
  end
94
223
  end
@@ -2,6 +2,10 @@ require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  class Hirb::Helpers::ObjectTableTest < Test::Unit::TestCase
4
4
  context "object table" do
5
+ def table(*args)
6
+ Hirb::Helpers::ObjectTable.render(*args)
7
+ end
8
+
5
9
  before(:all) {
6
10
  @pets = [stub(:name=>'rufus', :age=>7, :to_s=>'rufus'), stub(:name=>'alf', :age=>101, :to_s=>'alf')]
7
11
  }
@@ -15,7 +19,7 @@ class Hirb::Helpers::ObjectTableTest < Test::Unit::TestCase
15
19
  +-------+-----+
16
20
  2 rows in set
17
21
  TABLE
18
- Hirb::Helpers::ObjectTable.render(@pets, :fields=>[:name, :age]).should == expected_table
22
+ table(@pets, :fields=>[:name, :age]).should == expected_table
19
23
  end
20
24
 
21
25
  test "with no options defaults to to_s field" do
@@ -28,7 +32,7 @@ class Hirb::Helpers::ObjectTableTest < Test::Unit::TestCase
28
32
  +-------+
29
33
  2 rows in set
30
34
  TABLE
31
- Hirb::Helpers::ObjectTable.render(@pets).should == expected_table
35
+ table(@pets).should == expected_table
32
36
  end
33
37
 
34
38
  test "renders simple arrays" do
@@ -43,14 +47,29 @@ class Hirb::Helpers::ObjectTableTest < Test::Unit::TestCase
43
47
  +-------+
44
48
  4 rows in set
45
49
  TABLE
46
- Hirb::Helpers::ObjectTable.render([1,2,3,4]).should == expected_table
50
+ table([1,2,3,4]).should == expected_table
51
+ end
52
+
53
+ test "renders simple arrays with custom header" do
54
+ expected_table = <<-TABLE.unindent
55
+ +-----+
56
+ | num |
57
+ +-----+
58
+ | 1 |
59
+ | 2 |
60
+ | 3 |
61
+ | 4 |
62
+ +-----+
63
+ 4 rows in set
64
+ TABLE
65
+ table([1,2,3,4], :headers=>{:to_s=>'num'}).should == expected_table
47
66
  end
48
67
 
49
68
  test "with empty fields" do
50
69
  expected_table = <<-TABLE.unindent
51
70
  0 rows in set
52
71
  TABLE
53
- Hirb::Helpers::ObjectTable.render(@pets, :fields => []).should == expected_table
72
+ table(@pets, :fields => []).should == expected_table
54
73
  end
55
74
  end
56
75
  end
data/test/pager_test.rb CHANGED
@@ -27,7 +27,7 @@ module Hirb
27
27
  end
28
28
 
29
29
  context "default_pager" do
30
- before(:all) { reset_config; Hirb.enable {|c| c.pager = true}}
30
+ before(:all) { reset_config; Hirb.enable :pager=>true }
31
31
  before(:each) { View.pager = nil; Pager.stubs(:pager_command).returns(nil) }
32
32
  after(:all) { Hirb.disable }
33
33
 
@@ -0,0 +1,61 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class Hirb::Helpers::Table
4
+ class ResizerTest < Test::Unit::TestCase
5
+ def table(options)
6
+ @table = Hirb::Helpers::Table.new [options[:field_lengths].keys.inject({}) {|t,e| t[e] = '1'; t}]
7
+ @table.field_lengths = options[:field_lengths]
8
+ @table.width = options[:width]
9
+ @table.max_fields = options[:max_fields] if options[:max_fields]
10
+ @width, @field_lengths = @table.width, @table.field_lengths
11
+ end
12
+
13
+ test "resize ensures columns total doesn't exceed max width" do
14
+ table :field_lengths=>{:f1=>135, :f2=>45, :f3=>4, :f4=>55}, :width=>195
15
+ Resizer.resize!(@table)
16
+ @field_lengths.values.inject {|a,e| a+=e}.should <= @width
17
+ end
18
+
19
+ test "resize sets columns by relative lengths" do
20
+ table :field_lengths=>{:a=>30, :b=>30, :c=>40}, :width=>60
21
+ Resizer.resize!(@table)
22
+ @field_lengths.values.inject {|a,e| a+=e}.should <= @width
23
+ @field_lengths.values.uniq.size.should_not == 1
24
+ end
25
+
26
+ test "resize sets all columns roughly equal when adusting long fields don't work" do
27
+ table :field_lengths=>{:field1=>10, :field2=>15, :field3=>100}, :width=>20
28
+ Resizer.resize!(@table)
29
+ @field_lengths.values.inject {|a,e| a+=e}.should <= @width
30
+ @field_lengths.values.each {|e| e.should <= 4 }
31
+ end
32
+
33
+ context "add_extra_width and max_fields" do
34
+ def table_and_resize(options={})
35
+ defaults = {:field_lengths=>{:f1=>135, :f2=>30, :f3=>4, :f4=>100}, :width=>195, :max_fields=>{:f1=>80, :f4=>30} }
36
+ table defaults.merge(options)
37
+ Resizer.resize! @table
38
+ end
39
+
40
+ test "doesn't add to already maxed out field" do
41
+ table_and_resize
42
+ @field_lengths[:f3].should == 4
43
+ end
44
+
45
+ test "restricted before adding width" do
46
+ table_and_resize
47
+ @field_lengths[:f4].should <= 30
48
+ end
49
+
50
+ test "adds to restricted field" do
51
+ table_and_resize
52
+ @field_lengths[:f1].should <= 80
53
+ end
54
+
55
+ test "adds to unrestricted field" do
56
+ table_and_resize :field_lengths=>{:f1=>135, :f2=>70, :f3=>4, :f4=>100}
57
+ @field_lengths[:f2].should == 70
58
+ end
59
+ end
60
+ end
61
+ end
data/test/table_test.rb CHANGED
@@ -81,17 +81,45 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
81
81
  table([{:name=>"アイウエオカキ"}, {:name=>"クケコサシスセソタチツテ"}, {:name=>"Tata l'asticote"}, {:name=>"toto létoile PAOLI"}]).should == expected_table
82
82
  end
83
83
 
84
- test "with newlines renders with newlines stringified" do
84
+ test "stringifies newlines and tabs and renders" do
85
85
  expected_table = <<-TABLE.unindent
86
86
  +-----+---+
87
87
  | a | b |
88
88
  +-----+---+
89
89
  | 1#{'\n'} | 2 |
90
- | 3 | 4 |
90
+ | 3#{'\t'} | 4 |
91
91
  +-----+---+
92
92
  2 rows in set
93
93
  TABLE
94
- table([{'a'=>"1\n", 'b'=>2}, {'a'=>3, 'b'=>4}]).should == expected_table
94
+ value = [{'a'=>"1\n", 'b'=>2}, {'a'=>"3\t", 'b'=>4}]
95
+ table(value).should == expected_table
96
+ value.should == [{'a'=>"1\n", 'b'=>2}, {'a'=>"3\t", 'b'=>4}]
97
+ end
98
+
99
+ test "with a field of only array values renders values comma joined" do
100
+ expected_table = <<-TABLE.unindent
101
+ +----+------+
102
+ | a | b |
103
+ +----+------+
104
+ | 12 | 1, 2 |
105
+ | ok | 3, 4 |
106
+ +----+------+
107
+ 2 rows in set
108
+ TABLE
109
+ # depends on 1.8 Array#to_s
110
+ table([{:a=>[1,2], :b=>[1,2]}, {:a=>'ok', :b=>[3,4]}]).should == expected_table
111
+ end
112
+
113
+ test "with filter class default doesn't override explicit filters" do
114
+ expected_table = <<-TABLE.unindent
115
+ +------+-------+
116
+ | name | value |
117
+ +------+-------+
118
+ | a | b1 |
119
+ +------+-------+
120
+ 1 row in set
121
+ TABLE
122
+ table([{:name=>'a', :value=>{:b=>1}}], :filters=>{:value=>:to_s}).should == expected_table
95
123
  end
96
124
  end
97
125
 
@@ -136,11 +164,11 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
136
164
  | 4 | |
137
165
  +---+---+
138
166
  2 rows in set
139
- TABLE
167
+ TABLE
140
168
  table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :fields=>[:b, :c]).should == expected_table
141
169
  end
142
170
 
143
- test "invalid fields in field_lengths option renders" do
171
+ test "invalid field in max_fields option renders" do
144
172
  expected_table = <<-TABLE.unindent
145
173
  +------------+---+
146
174
  | a | b |
@@ -148,11 +176,11 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
148
176
  | AAAAAAA... | 2 |
149
177
  +------------+---+
150
178
  1 row in set
151
- TABLE
152
- table([{:a=> "A" * 50, :b=>2}], :field_lengths=>{:a=>10,:c=>10}).should == expected_table
179
+ TABLE
180
+ table([{:a=> "A" * 50, :b=>2}], :max_fields=>{:a=>10,:c=>10}).should == expected_table
153
181
  end
154
182
 
155
- test "field_lengths option and field_lengths less than 3 characters renders" do
183
+ test "max_fields option with fields less than 3 characters renders" do
156
184
  expected_table = <<-TABLE.unindent
157
185
  +----+---+
158
186
  | a | b |
@@ -160,11 +188,11 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
160
188
  | AA | 2 |
161
189
  +----+---+
162
190
  1 row in set
163
- TABLE
164
- table([{:a=> "A" * 50, :b=>2}], :field_lengths=>{:a=>2}).should == expected_table
191
+ TABLE
192
+ table([{:a=> "A" * 50, :b=>2}], :max_fields=>{:a=>2}, :resize=>false).should == expected_table
165
193
  end
166
194
 
167
- test "field_lengths option renders" do
195
+ test "max_fields option without resize renders" do
168
196
  expected_table = <<-TABLE.unindent
169
197
  +------------+---+
170
198
  | a | b |
@@ -172,23 +200,35 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
172
200
  | AAAAAAA... | 2 |
173
201
  +------------+---+
174
202
  1 row in set
175
- TABLE
176
- table([{:a=> "A" * 50, :b=>2}], :field_lengths=>{:a=>10}).should == expected_table
203
+ TABLE
204
+ table([{:a=> "A" * 50, :b=>2}], :max_fields=>{:a=>10}, :resize=>false).should == expected_table
205
+ end
206
+
207
+ test "max_fields option with percentage renders" do
208
+ expected_table = <<-TABLE.unindent
209
+ +------------------+---+
210
+ | a | b |
211
+ +------------------+---+
212
+ | AAAAAAAAAAAAA... | 2 |
213
+ +------------------+---+
214
+ 1 row in set
215
+ TABLE
216
+ table([{:a=> "A" * 50, :b=>2}], :max_fields=>{:a=>'0.15'}).should == expected_table
177
217
  end
178
218
 
179
219
  test "max_width option renders" do
180
220
  expected_table = <<-TABLE.unindent
181
- +-----------+---+-----------+
182
- | a | b | c |
183
- +-----------+---+-----------+
184
- | AAAAAA... | 2 | CCCCCC... |
185
- +-----------+---+-----------+
221
+ +-----------+---+------------+
222
+ | a | b | c |
223
+ +-----------+---+------------+
224
+ | AAAAAA... | 2 | CCCCCCCCCC |
225
+ +-----------+---+------------+
186
226
  1 row in set
187
- TABLE
227
+ TABLE
188
228
  table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>30).should == expected_table
189
229
  end
190
230
 
191
- test "max_width option nil renders full table" do
231
+ test "resize option false renders full table" do
192
232
  expected_table = <<-TABLE.unindent
193
233
  +----------------------------------------------------+---+------------+
194
234
  | a | b | c |
@@ -196,19 +236,19 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
196
236
  | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | 2 | CCCCCCCCCC |
197
237
  +----------------------------------------------------+---+------------+
198
238
  1 row in set
199
- TABLE
200
- table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>nil).should == expected_table
239
+ TABLE
240
+ table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :resize=>false).should == expected_table
201
241
  end
202
242
 
203
243
  test "global width renders" do
204
244
  expected_table = <<-TABLE.unindent
205
- +-----------+---+-----------+
206
- | a | b | c |
207
- +-----------+---+-----------+
208
- | AAAAAA... | 2 | CCCCCC... |
209
- +-----------+---+-----------+
245
+ +-----------+---+------------+
246
+ | a | b | c |
247
+ +-----------+---+------------+
248
+ | AAAAAA... | 2 | CCCCCCCCCC |
249
+ +-----------+---+------------+
210
250
  1 row in set
211
- TABLE
251
+ TABLE
212
252
  Hirb::View.load_config
213
253
  Hirb::View.resize(30)
214
254
  table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}]).should == expected_table
@@ -223,11 +263,11 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
223
263
  | A | 2 | C |
224
264
  +---+---------+---------+
225
265
  1 row in set
226
- TABLE
266
+ TABLE
227
267
  table([{:a=> "A", :b=>2, :c=>"C"}], :headers=>{:b=>"field B", :c=>"field C"}).should == expected_table
228
268
  end
229
269
 
230
- test "headers option and headers shortened by field_lengths renders" do
270
+ test "headers option and headers shortened by max_fields renders" do
231
271
  expected_table = <<-TABLE.unindent
232
272
  +-------+---+
233
273
  | fi... | b |
@@ -235,8 +275,8 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
235
275
  | A | 2 |
236
276
  +-------+---+
237
277
  1 row in set
238
- TABLE
239
- table([{:a=> "A", :b=>2}], :headers=>{:a=>"field A"}, :field_lengths=>{:a=>5}).should == expected_table
278
+ TABLE
279
+ table([{:a=> "A", :b=>2}], :headers=>{:a=>"field A"}, :max_fields=>{:a=>5}, :resize=>false).should == expected_table
240
280
  end
241
281
 
242
282
  test "headers option as an array renders" do
@@ -251,7 +291,19 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
251
291
  TABLE
252
292
  table([[1,2], [3,4]], :headers=>['A', 'B']).should == expected_table
253
293
  end
254
-
294
+
295
+ test "header_filter option renders" do
296
+ expected_table = <<-TABLE.unindent
297
+ +---+---+
298
+ | A | B |
299
+ +---+---+
300
+ | 2 | 3 |
301
+ +---+---+
302
+ 1 row in set
303
+ TABLE
304
+ table([{:a=> 2, :b=>3}], :header_filter=>:capitalize).should == expected_table
305
+ end
306
+
255
307
  test "filters option renders" do
256
308
  expected_table = <<-TABLE.unindent
257
309
  +-----------+---+
@@ -266,6 +318,22 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
266
318
  1=>[:[], :num]}).should == expected_table
267
319
  end
268
320
 
321
+ test "filters option calls Filters method and renders" do
322
+ module ::Hirb::Helpers::Table::Filters
323
+ def semicolon_join(arr); arr.join('; '); end
324
+ end
325
+
326
+ expected_table = <<-TABLE.unindent
327
+ +------+------------------------------+
328
+ | 0 | 1 |
329
+ +------+------------------------------+
330
+ | some | unsightly; unreadable; array |
331
+ +------+------------------------------+
332
+ 1 row in set
333
+ TABLE
334
+ table([[['some'], %w{unsightly unreadable array}]], :filters=>{1=>:semicolon_join}).should == expected_table
335
+ end
336
+
269
337
  test "number option renders" do
270
338
  expected_table = <<-TABLE.unindent
271
339
  +--------+---+---+
@@ -333,6 +401,17 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
333
401
  table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], options).should == expected_table
334
402
  end
335
403
 
404
+ test "hide_empty and vertical options renders" do
405
+ expected_table = <<-TABLE.unindent
406
+ *** 1. row ***
407
+ b: 2
408
+ *** 2. row ***
409
+ a: 3
410
+ 2 rows in set
411
+ TABLE
412
+ table([{:a=>'', :b=>2}, {:a=>3, :b=>nil}], :hide_empty=>true, :vertical=>true).should == expected_table
413
+ end
414
+
336
415
  test "all_fields option renders all fields" do
337
416
  expected_table = <<-TABLE.unindent
338
417
  +---+---+---+
@@ -357,51 +436,79 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
357
436
  2 rows in set
358
437
  TABLE
359
438
  table([[1,2],[2,3]], :change_fields=>{0=>'name', 1=>'value'}).should == expected_table
439
+ table([[1,2],[2,3]], :change_fields=>['name', 'value']).should == expected_table
360
440
  end
361
- end
362
-
363
- test "table can detect and run callbacks" do
364
- Hirb::Helpers::Table.send(:define_method, :and_one_callback) do |obj, opt|
365
- obj.each {|row| row.each {|k,v| row[k] += opt[:add] } }
366
- obj
367
- end
368
-
369
- expected_table = <<-TABLE.unindent
370
- +---+---+
371
- | a | b |
372
- +---+---+
373
- | 2 | 3 |
374
- | 4 | 5 |
375
- +---+---+
376
- 2 rows in set
377
- TABLE
378
- table([{'a'=>1, 'b'=>2}, {'a'=>3, 'b'=>4}], :add=>1).should == expected_table
379
- Hirb::Helpers::Table.send(:remove_method, :and_one_callback)
380
- end
381
441
 
382
- test "restrict_field_lengths ensures columns total doesn't exceed max width" do
383
- @table = Hirb::Helpers::Table.new([{:f1=>'f1', :f2=>'2', :f3=>'3', :f4=>'4'}])
384
- field_lengths = {:f1=>135, :f2=>45, :f3=>4, :f4=>55}
385
- width = 195
386
- @table.restrict_field_lengths(field_lengths, width)
387
- field_lengths.values.inject {|a,e| a+=e}.should <= width
388
- end
442
+ test "return_rows option returns rows" do
443
+ table([[1,2],[2,3]], :return_rows=>true).should == [{0=>"1", 1=>"2"}, {0=>"2", 1=>"3"}]
444
+ end
389
445
 
390
- test "restrict_field_lengths sets columns by relative lengths" do
391
- @table = Hirb::Helpers::Table.new([{:a=>'a', :b=>'b', :c=>'c'}])
392
- field_lengths = {:a=>30, :b=>30, :c=>40}
393
- width = 60
394
- @table.restrict_field_lengths(field_lengths, width)
395
- field_lengths.values.inject {|a,e| a+=e}.should <= width
396
- field_lengths.values.uniq.size.should_not == 1
446
+ test "filter_any option filters any value" do
447
+ expected_table = <<-TABLE.unindent
448
+ +---------+
449
+ | a |
450
+ +---------+
451
+ | {:b=>1} |
452
+ | 2 |
453
+ +---------+
454
+ 2 rows in set
455
+ TABLE
456
+ table([{:a=>{:b=>1}}, {:a=>2}], :filter_any=>true).should == expected_table
457
+ end
458
+
459
+ test "filter_classes option overrides class-wide filter_classes" do
460
+ expected_table = <<-TABLE.unindent
461
+ +----+
462
+ | a |
463
+ +----+
464
+ | b1 |
465
+ +----+
466
+ 1 row in set
467
+ TABLE
468
+ table([{:a=>{:b=>1}}], :filter_classes=>{Hash=>:to_s}).should == expected_table
469
+ end
397
470
  end
398
471
 
399
- test "restrict_field_lengths sets all columns equal when no long_field and relative methods don't work" do
400
- @table = Hirb::Helpers::Table.new([{:field1=>'f1', :field2=>'f2', :field3=>'f3'}])
401
- field_lengths = {:field1=>10, :field2=>15, :field3=>100}
402
- width = 20
403
- @table.restrict_field_lengths(field_lengths, width)
404
- field_lengths.values.inject {|a,e| a+=e}.should <= width
405
- field_lengths.values.uniq.size.should == 1
472
+ context "table with callbacks" do
473
+ before(:all) {
474
+ Hirb::Helpers::Table.send(:define_method, :and_one_callback) do |obj, opt|
475
+ obj.each {|row| row.each {|k,v| row[k] += opt[:add] } }
476
+ obj
477
+ end
478
+ }
479
+ after(:all) { Hirb::Helpers::Table.send(:remove_method, :and_one_callback) }
480
+
481
+ test "detects and runs them" do
482
+ expected_table = <<-TABLE.unindent
483
+ +---+---+
484
+ | a | b |
485
+ +---+---+
486
+ | 2 | 3 |
487
+ | 4 | 5 |
488
+ +---+---+
489
+ 2 rows in set
490
+ TABLE
491
+ table([{'a'=>1, 'b'=>2}, {'a'=>3, 'b'=>4}], :add=>1).should == expected_table
492
+ end
493
+
494
+ test "doesn't run callbacks in delete_callbacks option" do
495
+ Hirb::Helpers::Table.send(:define_method, :and_two_callback) do |obj, opt|
496
+ obj.each {|row| row.each {|k,v| row[k] = row[k] * 2 } }
497
+ obj
498
+ end
499
+
500
+ expected_table = <<-TABLE.unindent
501
+ +---+---+
502
+ | a | b |
503
+ +---+---+
504
+ | 2 | 3 |
505
+ | 4 | 5 |
506
+ +---+---+
507
+ 2 rows in set
508
+ TABLE
509
+ table([{'a'=>1, 'b'=>2}, {'a'=>3, 'b'=>4}], :add=>1, :delete_callbacks=>[:and_two]).should == expected_table
510
+
511
+ Hirb::Helpers::Table.send(:remove_method, :and_two_callback)
512
+ end
406
513
  end
407
- end
514
+ end