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/CHANGELOG.rdoc +13 -0
- data/LICENSE.txt +1 -1
- data/README.rdoc +1 -1
- data/VERSION.yml +3 -2
- data/lib/hirb.rb +22 -9
- data/lib/hirb/formatter.rb +1 -5
- data/lib/hirb/helpers/active_record_table.rb +1 -1
- data/lib/hirb/helpers/object_table.rb +2 -3
- data/lib/hirb/helpers/table.rb +104 -90
- data/lib/hirb/helpers/table/filters.rb +10 -0
- data/lib/hirb/helpers/table/resizer.rb +82 -0
- data/lib/hirb/helpers/vertical_table.rb +12 -6
- data/lib/hirb/menu.rb +204 -36
- data/lib/hirb/util.rb +11 -2
- data/lib/hirb/view.rb +29 -10
- data/test/auto_table_test.rb +8 -23
- data/test/formatter_test.rb +2 -2
- data/test/hirb_test.rb +13 -4
- data/test/menu_test.rb +134 -5
- data/test/object_table_test.rb +23 -4
- data/test/pager_test.rb +1 -1
- data/test/resizer_test.rb +61 -0
- data/test/table_test.rb +182 -75
- data/test/util_test.rb +5 -0
- data/test/view_test.rb +36 -4
- metadata +6 -2
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(:
|
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.
|
34
|
+
Hirb.config_files = nil
|
26
35
|
home = ENV.delete('HOME')
|
27
|
-
Hirb.
|
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
|
90
|
-
|
91
|
-
|
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
|
data/test/object_table_test.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
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 "
|
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
|
90
|
+
| 3#{'\t'} | 4 |
|
91
91
|
+-----+---+
|
92
92
|
2 rows in set
|
93
93
|
TABLE
|
94
|
-
|
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
|
-
|
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
|
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
|
-
|
152
|
-
table([{:a=> "A" * 50, :b=>2}], :
|
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 "
|
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
|
-
|
164
|
-
table([{:a=> "A" * 50, :b=>2}], :
|
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 "
|
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
|
-
|
176
|
-
table([{:a=> "A" * 50, :b=>2}], :
|
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 |
|
185
|
-
|
221
|
+
+-----------+---+------------+
|
222
|
+
| a | b | c |
|
223
|
+
+-----------+---+------------+
|
224
|
+
| AAAAAA... | 2 | CCCCCCCCCC |
|
225
|
+
+-----------+---+------------+
|
186
226
|
1 row in set
|
187
|
-
|
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 "
|
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
|
-
|
200
|
-
table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :
|
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 |
|
209
|
-
|
245
|
+
+-----------+---+------------+
|
246
|
+
| a | b | c |
|
247
|
+
+-----------+---+------------+
|
248
|
+
| AAAAAA... | 2 | CCCCCCCCCC |
|
249
|
+
+-----------+---+------------+
|
210
250
|
1 row in set
|
211
|
-
|
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
|
-
|
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
|
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
|
-
|
239
|
-
table([{:a=> "A", :b=>2}], :headers=>{:a=>"field A"}, :
|
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
|
-
|
383
|
-
|
384
|
-
|
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
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
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
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
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
|