hirb 0.1.2 → 0.2.2

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.
data/test/menu_test.rb ADDED
@@ -0,0 +1,94 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class Hirb::MenuTest < Test::Unit::TestCase
4
+ def menu(*args, &block)
5
+ # testing via menu's main use case (through console) instead of Hirb::Menu.render
6
+ @console ||= Object.new.extend(Hirb::Console)
7
+ @console.menu(*args, &block)
8
+ end
9
+
10
+ def basic_menu(*args, &block)
11
+ menu_input('1')
12
+ capture_stdout { menu(*args, &block).should == [1] }
13
+ end
14
+
15
+ def menu_input(input='')
16
+ $stdin.expects(:gets).returns(input)
17
+ end
18
+
19
+ context "menu" do
20
+ test "by default renders table menu" do
21
+ expected_menu = <<-MENU.unindent
22
+ +--------+-------+
23
+ | number | value |
24
+ +--------+-------+
25
+ | 1 | 1 |
26
+ | 2 | 2 |
27
+ | 3 | 3 |
28
+ +--------+-------+
29
+ 3 rows in set
30
+ MENU
31
+ basic_menu([1,2,3]).include?(expected_menu).should == true
32
+ end
33
+
34
+ test "with block renders" do
35
+ menu_input "1,2"
36
+ expected_result = [1,2]
37
+ capture_stdout {
38
+ menu([1,2,3]) {|e| e.should == expected_result }.should == expected_result
39
+ }
40
+ end
41
+
42
+ test "with block and no chosen doesn't call block" do
43
+ menu_input ""
44
+ block = lambda {|e| }
45
+ block.expects(:call).never
46
+ capture_stdout {
47
+ menu([1,2,3], &block).should == []
48
+ }
49
+ end
50
+
51
+ test "with valid helper_class option renders" do
52
+ Hirb::Helpers::Table.expects(:render)
53
+ basic_menu [1,2,3], :helper_class=>"Hirb::Helpers::Table"
54
+ end
55
+
56
+ test "with invalid helper_class option renders default menu" do
57
+ expected_menu = <<-MENU.unindent
58
+ 1: 1
59
+ 2: 2
60
+ 3: 3
61
+ MENU
62
+ basic_menu([1,2,3], :helper_class=>"SomeHelper").include?(expected_menu).should == true
63
+ end
64
+
65
+ test "with false helper_class option renders default menu" do
66
+ expected_menu = <<-MENU.unindent
67
+ 1: 1
68
+ 2: 2
69
+ 3: 3
70
+ MENU
71
+ basic_menu([1,2,3], :helper_class=>false).include?(expected_menu).should == true
72
+ end
73
+
74
+ test "prints prompt option" do
75
+ prompt = "Input or else ..."
76
+ basic_menu([1,2,3], :prompt=>prompt).include?(prompt).should == true
77
+ end
78
+
79
+ test "converts non-array inputs to array" do
80
+ Hirb::Helpers::AutoTable.expects(:render).with([1], anything)
81
+ basic_menu 1
82
+ end
83
+
84
+ test "with false ask option returns one choice without asking" do
85
+ $stdin.expects(:gets).never
86
+ menu([1], :ask=>false).should == [1]
87
+ end
88
+
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 }
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,49 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class Hirb::Helpers::ObjectTableTest < Test::Unit::TestCase
4
+ context "object table" do
5
+ before(:all) {
6
+ @pets = [stub(:name=>'rufus', :age=>7, :to_s=>'rufus'), stub(:name=>'alf', :age=>101, :to_s=>'alf')]
7
+ }
8
+ test "renders" do
9
+ expected_table = <<-TABLE.unindent
10
+ +-------+-----+
11
+ | name | age |
12
+ +-------+-----+
13
+ | rufus | 7 |
14
+ | alf | 101 |
15
+ +-------+-----+
16
+ 2 rows in set
17
+ TABLE
18
+ Hirb::Helpers::ObjectTable.render(@pets, :fields=>[:name, :age]).should == expected_table
19
+ end
20
+
21
+ test "with no options defaults to to_s field" do
22
+ expected_table = <<-TABLE.unindent
23
+ +-------+
24
+ | value |
25
+ +-------+
26
+ | rufus |
27
+ | alf |
28
+ +-------+
29
+ 2 rows in set
30
+ TABLE
31
+ Hirb::Helpers::ObjectTable.render(@pets).should == expected_table
32
+ end
33
+
34
+ test "renders simple arrays" do
35
+ expected_table = <<-TABLE.unindent
36
+ +-------+
37
+ | value |
38
+ +-------+
39
+ | 1 |
40
+ | 2 |
41
+ | 3 |
42
+ | 4 |
43
+ +-------+
44
+ 4 rows in set
45
+ TABLE
46
+ Hirb::Helpers::ObjectTable.render([1,2,3,4]).should == expected_table
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,164 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ module Hirb
4
+ class PagerTest < Test::Unit::TestCase
5
+ def pager; View.pager; end
6
+
7
+ def create_pageable_string(inspect_mode=false, size={})
8
+ size = {:width=>pager.width, :height=>pager.height}.merge(size)
9
+ seed = inspect_mode ? "a" : "a\n"
10
+ if inspect_mode
11
+ seed * (size[:width] * size[:height] + 1)
12
+ else
13
+ seed * (size[:height] + 1)
14
+ end
15
+ end
16
+
17
+ test "command_pager sets pager_command when command exists" do
18
+ Util.expects(:command_exists?).returns(true)
19
+ Pager.expects(:basic_pager)
20
+ Pager.command_pager 'blah', :pager_command=>'less'
21
+ end
22
+
23
+ test "command_pager doesn't set pager_command when command doesn't exist" do
24
+ Util.expects(:command_exists?).returns(false)
25
+ Pager.expects(:basic_pager).never
26
+ Pager.command_pager 'blah', :pager_command=>'moreless'
27
+ end
28
+
29
+ context "default_pager" do
30
+ before(:all) { reset_config; Hirb.enable {|c| c.pager = true}}
31
+ before(:each) { View.pager = nil; Pager.stubs(:pager_command).returns(nil) }
32
+ after(:all) { Hirb.disable }
33
+
34
+ test "pages once in normal mode" do
35
+ $stdin.expects(:gets).returns("\n")
36
+ output = capture_stdout { pager.page(create_pageable_string, false) }
37
+ output.include?('quit').should be(true)
38
+ output.include?('finished').should be(true)
39
+ end
40
+
41
+ test "doesn't page in normal mode" do
42
+ $stdin.expects(:gets).never
43
+ output = capture_stdout { pager.page("a\n", false) }
44
+ output.include?("a\n=== Pager finished. ===\n").should be(true)
45
+ end
46
+
47
+ test "pages once in inspect mode" do
48
+ $stdin.expects(:gets).returns("\n")
49
+ output = capture_stdout { pager.page(create_pageable_string(true), true) }
50
+ output.include?('quit').should be(true)
51
+ output.include?('finished').should be(true)
52
+ end
53
+
54
+ test "doesn't page in inspect mode" do
55
+ $stdin.expects(:gets).never
56
+ output = capture_stdout { pager.page("a", true) }
57
+ output.include?("a\n=== Pager finished. ===\n").should be(true)
58
+ end
59
+ end
60
+
61
+ context "pager" do
62
+ before(:all) { reset_config; Hirb.enable }
63
+ before(:each) { View.pager = nil; View.formatter = nil }
64
+ after(:all) { Hirb.disable }
65
+
66
+ def irb_eval(string)
67
+ context_stub = stub(:last_value=>string)
68
+ ::IRB::Irb.new(context_stub).output_value
69
+ end
70
+
71
+ # this mode is called within @irb.output_value
72
+ context "in inspect_mode" do
73
+ test "activates when output is wide enough" do
74
+ output = create_pageable_string(true)
75
+ pager.expects(:page).with(output.inspect, true)
76
+ View.expects(:render_output).returns(false)
77
+ irb_eval output
78
+ end
79
+
80
+ test "doesn't activate when output isn't wide enough" do
81
+ pager.expects(:page).never
82
+ View.expects(:render_output).returns(false)
83
+ irb_eval("a")
84
+ end
85
+
86
+ test "activates with an explicit width" do
87
+ View.config[:width] = 10
88
+ output = create_pageable_string true, :width=>10
89
+ pager.expects(:page).with(output.inspect, true)
90
+ View.expects(:render_output).returns(false)
91
+ irb_eval output
92
+ end
93
+
94
+ test "activates default_pager when pager command is invalid" do
95
+ Pager.expects(:pager_command).returns(nil)
96
+ output = create_pageable_string(true)
97
+ Pager.expects(:default_pager).with(output.inspect, anything)
98
+ View.expects(:render_output).returns(false)
99
+ capture_stdout { irb_eval output }
100
+ end
101
+ end
102
+
103
+ # this mode is called within Hirb::View.render_output
104
+ context "in normal mode" do
105
+ test "activates when output is long enough" do
106
+ output = create_pageable_string
107
+ View.formatter.expects(:format_output).returns(output)
108
+ pager.expects(:page).with(output, false)
109
+ irb_eval(output)
110
+ end
111
+
112
+ test "doesn't activate when output isn't long enough" do
113
+ output = "a\n"
114
+ View.formatter.expects(:format_output).returns(output)
115
+ pager.expects(:page).never
116
+ capture_stdout { irb_eval(output) }
117
+ end
118
+
119
+ test "activates with an explicit height" do
120
+ View.config[:height] = 100
121
+ output = create_pageable_string false, :height=>100
122
+ View.formatter.expects(:format_output).returns(output)
123
+ pager.expects(:page).with(output, false)
124
+ irb_eval(output)
125
+ end
126
+
127
+ test "activates default_pager when pager_command is invalid" do
128
+ Pager.expects(:pager_command).returns(nil)
129
+ output = create_pageable_string
130
+ Pager.expects(:default_pager).with(output, anything)
131
+ View.formatter.expects(:format_output).returns(output)
132
+ capture_stdout { irb_eval output }
133
+ end
134
+ end
135
+
136
+ test "activates pager_command with valid pager_command option" do
137
+ View.config[:pager_command] = "less"
138
+ View.expects(:render_output).returns(false)
139
+ Util.expects(:command_exists?).returns(true)
140
+ Pager.expects(:command_pager)
141
+ irb_eval create_pageable_string(true)
142
+ View.config[:pager_command] = nil
143
+ end
144
+
145
+ test "activates pager_command with pager_command option that has command options" do
146
+ View.config[:pager_command] = "less -r"
147
+ View.expects(:render_output).returns(false)
148
+ Util.expects(:command_exists?).with('less').returns(true)
149
+ Pager.expects(:command_pager)
150
+ irb_eval create_pageable_string(true)
151
+ View.config[:pager_command] = nil
152
+ end
153
+
154
+ test "doesn't activate pager_command with invalid pager_command option" do
155
+ View.config[:pager_command] = "moreless"
156
+ View.expects(:render_output).returns(false)
157
+ Util.expects(:command_exists?).returns(false)
158
+ Pager.expects(:default_pager)
159
+ irb_eval create_pageable_string(true)
160
+ View.config[:pager_command] = nil
161
+ end
162
+ end
163
+ end
164
+ end
data/test/table_test.rb CHANGED
@@ -4,6 +4,7 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
4
4
  def table(*args)
5
5
  Hirb::Helpers::Table.render(*args)
6
6
  end
7
+ before(:all) { reset_config }
7
8
 
8
9
  context "basic table" do
9
10
  test "renders" do
@@ -41,7 +42,7 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
41
42
  TABLE
42
43
  table([{'a'=>1, 'b'=>2}, {'a'=>3, 'b'=>4}]).should == expected_table
43
44
  end
44
-
45
+
45
46
  test "with array only rows renders" do
46
47
  expected_table = <<-TABLE.unindent
47
48
  +---+---+
@@ -54,11 +55,13 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
54
55
  TABLE
55
56
  table([[1,2], [3,4]]).should == expected_table
56
57
  end
57
-
58
- test "with too many fields raises error" do
59
- assert_raises(Hirb::Helpers::Table::TooManyFieldsForWidthError) { table([Array.new(70, 'AAA')]) }
58
+
59
+ test "with too many fields defaults to vertical table" do
60
+ rows = [Array.new(25, "A"* 10)]
61
+ Hirb::Helpers::VerticalTable.expects(:render).with(rows, anything)
62
+ capture_stderr { table(rows)}.should =~ /Error/
60
63
  end
61
-
64
+
62
65
  test "with no rows renders" do
63
66
  table([]).should == "0 rows in set"
64
67
  end
@@ -89,6 +92,12 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
89
92
  TABLE
90
93
  table([[1,2,3]], :fields=>[0,2]).should == expected_table
91
94
  end
95
+
96
+ test "fields and number options copies fields option and does not modify it" do
97
+ options = {:fields=>[:f1], :number=>true}
98
+ table({:f1=>1, :f2=>2}, options)
99
+ options[:fields].should == [:f1]
100
+ end
92
101
 
93
102
  test "invalid fields option renders empty columns" do
94
103
  expected_table = <<-TABLE.unindent
@@ -141,11 +150,11 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
141
150
 
142
151
  test "max_width option renders" do
143
152
  expected_table = <<-TABLE.unindent
144
- +--------------------------+---+------------+
145
- | a | b | c |
146
- +--------------------------+---+------------+
147
- | AAAAAAAAAAAAAAAAAAAAA... | 2 | CCCCCCCCCC |
148
- +--------------------------+---+------------+
153
+ +-----------+---+-----------+
154
+ | a | b | c |
155
+ +-----------+---+-----------+
156
+ | AAAAAA... | 2 | CCCCCC... |
157
+ +-----------+---+-----------+
149
158
  1 row in set
150
159
  TABLE
151
160
  table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>30).should == expected_table
@@ -163,18 +172,19 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
163
172
  table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>nil).should == expected_table
164
173
  end
165
174
 
166
- test "global max_width renders" do
175
+ test "global width renders" do
167
176
  expected_table = <<-TABLE.unindent
168
- +--------------------------+---+------------+
169
- | a | b | c |
170
- +--------------------------+---+------------+
171
- | AAAAAAAAAAAAAAAAAAAAA... | 2 | CCCCCCCCCC |
172
- +--------------------------+---+------------+
177
+ +-----------+---+-----------+
178
+ | a | b | c |
179
+ +-----------+---+-----------+
180
+ | AAAAAA... | 2 | CCCCCC... |
181
+ +-----------+---+-----------+
173
182
  1 row in set
174
183
  TABLE
175
- Hirb::Helpers::Table.max_width = 30
184
+ Hirb::View.load_config
185
+ Hirb::View.resize(30)
176
186
  table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}]).should == expected_table
177
- Hirb::Helpers::Table.max_width = Hirb::Helpers::Table::DEFAULT_MAX_WIDTH
187
+ reset_config
178
188
  end
179
189
 
180
190
  test "headers option and headers longer than fields renders" do
@@ -201,7 +211,7 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
201
211
  table([{:a=> "A", :b=>2}], :headers=>{:a=>"field A"}, :field_lengths=>{:a=>5}).should == expected_table
202
212
  end
203
213
 
204
- test "with headers option as an array renders" do
214
+ test "headers option as an array renders" do
205
215
  expected_table = <<-TABLE.unindent
206
216
  +---+---+
207
217
  | A | B |
@@ -214,50 +224,70 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
214
224
  table([[1,2], [3,4]], :headers=>['A', 'B']).should == expected_table
215
225
  end
216
226
 
217
- end
218
-
219
- context "object table" do
220
- before(:all) {
221
- @pets = [stub(:name=>'rufus', :age=>7), stub(:name=>'alf', :age=>101)]
222
- }
223
- test "renders" do
227
+ test "filters option renders" do
224
228
  expected_table = <<-TABLE.unindent
225
- +-------+-----+
226
- | name | age |
227
- +-------+-----+
228
- | rufus | 7 |
229
- | alf | 101 |
230
- +-------+-----+
229
+ +-----------+---+
230
+ | 0 | 1 |
231
+ +-----------+---+
232
+ | s,o,m,e | 2 |
233
+ | t,h,i,n,g | 1 |
234
+ +-----------+---+
231
235
  2 rows in set
232
236
  TABLE
233
- Hirb::Helpers::ObjectTable.render(@pets, :fields=>[:name, :age]).should == expected_table
237
+ table([['some', {:num=>2}], ['thing', {:num=>1}]], :filters=>{0=>lambda {|e| e.split("").join(",")},
238
+ 1=>[:[], :num]}).should == expected_table
234
239
  end
235
-
236
- test "with no options fields raises ArgumentError" do
237
- assert_raises(ArgumentError) { Hirb::Helpers::ObjectTable.render(@pets) }
240
+
241
+ test "number option renders" do
242
+ expected_table = <<-TABLE.unindent
243
+ +--------+---+---+
244
+ | number | 0 | 1 |
245
+ +--------+---+---+
246
+ | 1 | a | b |
247
+ | 2 | c | d |
248
+ +--------+---+---+
249
+ 2 rows in set
250
+ TABLE
251
+ table([['a','b'], ['c', 'd']], :number=>true).should == expected_table
238
252
  end
239
- end
240
-
241
- context "activerecord table" do
242
- before(:all) {
243
- @pets = [stub(:name=>'rufus', :age=>7, :attribute_names=>['age', 'name']), stub(:name=>'alf', :age=>101)]
244
- }
245
- test "renders" do
253
+
254
+ test "vertical option renders vertical table" do
246
255
  expected_table = <<-TABLE.unindent
247
- +-----+-------+
248
- | age | name |
249
- +-----+-------+
250
- | 7 | rufus |
251
- | 101 | alf |
252
- +-----+-------+
256
+ *** 1. row ***
257
+ a: 1
258
+ b: 2
259
+ *** 2. row ***
260
+ a: 3
261
+ b: 4
253
262
  2 rows in set
254
263
  TABLE
255
- Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
264
+ table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :vertical=>true).should == expected_table
256
265
  end
257
266
  end
258
267
 
259
- test "restrict_field_lengths handles many fields" do
268
+ test "restrict_field_lengths ensures columns total doesn't exceed max width" do
269
+ @table = Hirb::Helpers::Table.new([{:f1=>'f1', :f2=>'2', :f3=>'3', :f4=>'4'}])
270
+ field_lengths = {:f1=>135, :f2=>45, :f3=>4, :f4=>55}
271
+ width = 195
272
+ @table.restrict_field_lengths(field_lengths, width)
273
+ field_lengths.values.inject {|a,e| a+=e}.should <= width
274
+ end
275
+
276
+ test "restrict_field_lengths sets columns by relative lengths" do
277
+ @table = Hirb::Helpers::Table.new([{:a=>'a', :b=>'b', :c=>'c'}])
278
+ field_lengths = {:a=>30, :b=>30, :c=>40}
279
+ width = 60
280
+ @table.restrict_field_lengths(field_lengths, width)
281
+ field_lengths.values.inject {|a,e| a+=e}.should <= width
282
+ field_lengths.values.uniq.size.should_not == 1
283
+ end
284
+
285
+ test "restrict_field_lengths sets all columns equal when no long_field and relative methods don't work" do
260
286
  @table = Hirb::Helpers::Table.new([{:field1=>'f1', :field2=>'f2', :field3=>'f3'}])
261
- @table.restrict_field_lengths({:field1=>10, :field2=>15, :field3=>100}, 10)
287
+ field_lengths = {:field1=>10, :field2=>15, :field3=>100}
288
+ width = 20
289
+ @table.restrict_field_lengths(field_lengths, width)
290
+ field_lengths.values.inject {|a,e| a+=e}.should <= width
291
+ field_lengths.values.uniq.size.should == 1
262
292
  end
263
293
  end