Chrononaut-hirb 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +19 -0
- data/LICENSE.txt +22 -0
- data/README.rdoc +164 -0
- data/Rakefile +50 -0
- data/VERSION.yml +4 -0
- data/lib/hirb.rb +56 -0
- data/lib/hirb/console.rb +43 -0
- data/lib/hirb/formatter.rb +199 -0
- data/lib/hirb/hash_struct.rb +17 -0
- data/lib/hirb/helpers.rb +7 -0
- data/lib/hirb/helpers/active_record_table.rb +16 -0
- data/lib/hirb/helpers/auto_table.rb +15 -0
- data/lib/hirb/helpers/object_table.rb +15 -0
- data/lib/hirb/helpers/parent_child_tree.rb +22 -0
- data/lib/hirb/helpers/table.rb +243 -0
- data/lib/hirb/helpers/tree.rb +177 -0
- data/lib/hirb/import_object.rb +10 -0
- data/lib/hirb/menu.rb +47 -0
- data/lib/hirb/pager.rb +94 -0
- data/lib/hirb/util.rb +80 -0
- data/lib/hirb/view.rb +177 -0
- data/lib/hirb/views/activerecord_base.rb +9 -0
- data/test/console_test.rb +12 -0
- data/test/formatter_test.rb +172 -0
- data/test/hirb_test.rb +23 -0
- data/test/import_test.rb +9 -0
- data/test/menu_test.rb +94 -0
- data/test/pager_test.rb +164 -0
- data/test/table_test.rb +374 -0
- data/test/test_helper.rb +47 -0
- data/test/tree_test.rb +167 -0
- data/test/util_test.rb +56 -0
- data/test/view_test.rb +116 -0
- metadata +96 -0
data/test/hirb_test.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class HirbTest < Test::Unit::TestCase
|
4
|
+
before(:each) {Hirb.instance_eval "@config = nil"}
|
5
|
+
|
6
|
+
test "config converts yaml when config file exists" do
|
7
|
+
yaml_data = {:blah=>'blah'}
|
8
|
+
File.stubs('exists?').returns(true)
|
9
|
+
YAML::expects(:load_file).returns(yaml_data)
|
10
|
+
Hirb.config.should == yaml_data
|
11
|
+
end
|
12
|
+
|
13
|
+
test "config defaults to hash when no config file" do
|
14
|
+
File.stubs('exists?').returns(false)
|
15
|
+
Hirb.config.should == {}
|
16
|
+
end
|
17
|
+
|
18
|
+
test "config reloads if given explicit reload" do
|
19
|
+
Hirb.config
|
20
|
+
Hirb.expects(:read_config_file)
|
21
|
+
Hirb.config(true)
|
22
|
+
end
|
23
|
+
end
|
data/test/import_test.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class Hirb::ImportTest < Test::Unit::TestCase
|
4
|
+
test "require import_object extends Object" do
|
5
|
+
Object.ancestors.map {|e| e.to_s}.include?("Hirb::ObjectMethods").should be(false)
|
6
|
+
require 'hirb/import_object'
|
7
|
+
Object.ancestors.map {|e| e.to_s}.include?("Hirb::ObjectMethods").should be(true)
|
8
|
+
end
|
9
|
+
end
|
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
|
data/test/pager_test.rb
ADDED
@@ -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
ADDED
@@ -0,0 +1,374 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class Hirb::Helpers::TableTest < Test::Unit::TestCase
|
4
|
+
def table(*args)
|
5
|
+
Hirb::Helpers::Table.render(*args)
|
6
|
+
end
|
7
|
+
before(:all) { reset_config }
|
8
|
+
|
9
|
+
context "basic table" do
|
10
|
+
test "renders" do
|
11
|
+
expected_table = <<-TABLE.unindent
|
12
|
+
+---+---+
|
13
|
+
| a | b |
|
14
|
+
+---+---+
|
15
|
+
| 1 | 2 |
|
16
|
+
| 3 | 4 |
|
17
|
+
+---+---+
|
18
|
+
2 rows in set
|
19
|
+
TABLE
|
20
|
+
table([{:a=>1, :b=>2}, {:a=>3, :b=>4}]).should == expected_table
|
21
|
+
end
|
22
|
+
|
23
|
+
test "with no headers renders" do
|
24
|
+
expected_table = <<-TABLE.unindent
|
25
|
+
+---+---+
|
26
|
+
| 1 | 2 |
|
27
|
+
+---+---+
|
28
|
+
1 row in set
|
29
|
+
TABLE
|
30
|
+
table([{:a=>1, :b=>2}], :headers=>nil).should == expected_table
|
31
|
+
end
|
32
|
+
|
33
|
+
test "with string keys renders" do
|
34
|
+
expected_table = <<-TABLE.unindent
|
35
|
+
+---+---+
|
36
|
+
| a | b |
|
37
|
+
+---+---+
|
38
|
+
| 1 | 2 |
|
39
|
+
| 3 | 4 |
|
40
|
+
+---+---+
|
41
|
+
2 rows in set
|
42
|
+
TABLE
|
43
|
+
table([{'a'=>1, 'b'=>2}, {'a'=>3, 'b'=>4}]).should == expected_table
|
44
|
+
end
|
45
|
+
|
46
|
+
test "with array only rows renders" do
|
47
|
+
expected_table = <<-TABLE.unindent
|
48
|
+
+---+---+
|
49
|
+
| 0 | 1 |
|
50
|
+
+---+---+
|
51
|
+
| 1 | 2 |
|
52
|
+
| 3 | 4 |
|
53
|
+
+---+---+
|
54
|
+
2 rows in set
|
55
|
+
TABLE
|
56
|
+
table([[1,2], [3,4]]).should == expected_table
|
57
|
+
end
|
58
|
+
|
59
|
+
test "with too many fields raises error" do
|
60
|
+
assert_raises(Hirb::Helpers::Table::TooManyFieldsForWidthError) { table([Array.new(70, 'AAA')]) }
|
61
|
+
end
|
62
|
+
|
63
|
+
test "with no rows renders" do
|
64
|
+
table([]).should == "0 rows in set"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "table with" do
|
69
|
+
test "fields option renders" do
|
70
|
+
expected_table = <<-TABLE.unindent
|
71
|
+
+---+---+
|
72
|
+
| b | a |
|
73
|
+
+---+---+
|
74
|
+
| 2 | 1 |
|
75
|
+
| 4 | 3 |
|
76
|
+
+---+---+
|
77
|
+
2 rows in set
|
78
|
+
TABLE
|
79
|
+
table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :fields=>[:b, :a]).should == expected_table
|
80
|
+
end
|
81
|
+
|
82
|
+
test "fields option and array only rows" do
|
83
|
+
expected_table = <<-TABLE.unindent
|
84
|
+
+---+---+
|
85
|
+
| 0 | 2 |
|
86
|
+
+---+---+
|
87
|
+
| 1 | 3 |
|
88
|
+
+---+---+
|
89
|
+
1 row in set
|
90
|
+
TABLE
|
91
|
+
table([[1,2,3]], :fields=>[0,2]).should == expected_table
|
92
|
+
end
|
93
|
+
|
94
|
+
test "fields and number options copies fields option and does not modify it" do
|
95
|
+
options = {:fields=>[:f1], :number=>true}
|
96
|
+
table({:f1=>1, :f2=>2}, options)
|
97
|
+
options[:fields].should == [:f1]
|
98
|
+
end
|
99
|
+
|
100
|
+
test "invalid fields option renders empty columns" do
|
101
|
+
expected_table = <<-TABLE.unindent
|
102
|
+
+---+---+
|
103
|
+
| b | c |
|
104
|
+
+---+---+
|
105
|
+
| 2 | |
|
106
|
+
| 4 | |
|
107
|
+
+---+---+
|
108
|
+
2 rows in set
|
109
|
+
TABLE
|
110
|
+
table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :fields=>[:b, :c]).should == expected_table
|
111
|
+
end
|
112
|
+
|
113
|
+
test "invalid fields in field_lengths option renders" do
|
114
|
+
expected_table = <<-TABLE.unindent
|
115
|
+
+------------+---+
|
116
|
+
| a | b |
|
117
|
+
+------------+---+
|
118
|
+
| AAAAAAA... | 2 |
|
119
|
+
+------------+---+
|
120
|
+
1 row in set
|
121
|
+
TABLE
|
122
|
+
table([{:a=> "A" * 50, :b=>2}], :field_lengths=>{:a=>10,:c=>10}).should == expected_table
|
123
|
+
end
|
124
|
+
|
125
|
+
test "field_lengths option and field_lengths less than 3 characters renders" do
|
126
|
+
expected_table = <<-TABLE.unindent
|
127
|
+
+----+---+
|
128
|
+
| a | b |
|
129
|
+
+----+---+
|
130
|
+
| AA | 2 |
|
131
|
+
+----+---+
|
132
|
+
1 row in set
|
133
|
+
TABLE
|
134
|
+
table([{:a=> "A" * 50, :b=>2}], :field_lengths=>{:a=>2}).should == expected_table
|
135
|
+
end
|
136
|
+
|
137
|
+
test "field_lengths option renders" do
|
138
|
+
expected_table = <<-TABLE.unindent
|
139
|
+
+------------+---+
|
140
|
+
| a | b |
|
141
|
+
+------------+---+
|
142
|
+
| AAAAAAA... | 2 |
|
143
|
+
+------------+---+
|
144
|
+
1 row in set
|
145
|
+
TABLE
|
146
|
+
table([{:a=> "A" * 50, :b=>2}], :field_lengths=>{:a=>10}).should == expected_table
|
147
|
+
end
|
148
|
+
|
149
|
+
test "max_width option renders" do
|
150
|
+
expected_table = <<-TABLE.unindent
|
151
|
+
+-----------+---+-----------+
|
152
|
+
| a | b | c |
|
153
|
+
+-----------+---+-----------+
|
154
|
+
| AAAAAA... | 2 | CCCCCC... |
|
155
|
+
+-----------+---+-----------+
|
156
|
+
1 row in set
|
157
|
+
TABLE
|
158
|
+
table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>30).should == expected_table
|
159
|
+
end
|
160
|
+
|
161
|
+
test "max_width option nil renders full table" do
|
162
|
+
expected_table = <<-TABLE.unindent
|
163
|
+
+----------------------------------------------------+---+------------+
|
164
|
+
| a | b | c |
|
165
|
+
+----------------------------------------------------+---+------------+
|
166
|
+
| AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | 2 | CCCCCCCCCC |
|
167
|
+
+----------------------------------------------------+---+------------+
|
168
|
+
1 row in set
|
169
|
+
TABLE
|
170
|
+
table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>nil).should == expected_table
|
171
|
+
end
|
172
|
+
|
173
|
+
test "global width renders" do
|
174
|
+
expected_table = <<-TABLE.unindent
|
175
|
+
+-----------+---+-----------+
|
176
|
+
| a | b | c |
|
177
|
+
+-----------+---+-----------+
|
178
|
+
| AAAAAA... | 2 | CCCCCC... |
|
179
|
+
+-----------+---+-----------+
|
180
|
+
1 row in set
|
181
|
+
TABLE
|
182
|
+
Hirb::View.load_config
|
183
|
+
Hirb::View.resize(30)
|
184
|
+
table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}]).should == expected_table
|
185
|
+
reset_config
|
186
|
+
end
|
187
|
+
|
188
|
+
test "headers option and headers longer than fields renders" do
|
189
|
+
expected_table = <<-TABLE.unindent
|
190
|
+
+---+---------+---------+
|
191
|
+
| a | field B | field C |
|
192
|
+
+---+---------+---------+
|
193
|
+
| A | 2 | C |
|
194
|
+
+---+---------+---------+
|
195
|
+
1 row in set
|
196
|
+
TABLE
|
197
|
+
table([{:a=> "A", :b=>2, :c=>"C"}], :headers=>{:b=>"field B", :c=>"field C"}).should == expected_table
|
198
|
+
end
|
199
|
+
|
200
|
+
test "headers option and headers shortened by field_lengths renders" do
|
201
|
+
expected_table = <<-TABLE.unindent
|
202
|
+
+-------+---+
|
203
|
+
| fi... | b |
|
204
|
+
+-------+---+
|
205
|
+
| A | 2 |
|
206
|
+
+-------+---+
|
207
|
+
1 row in set
|
208
|
+
TABLE
|
209
|
+
table([{:a=> "A", :b=>2}], :headers=>{:a=>"field A"}, :field_lengths=>{:a=>5}).should == expected_table
|
210
|
+
end
|
211
|
+
|
212
|
+
test "headers option as an array renders" do
|
213
|
+
expected_table = <<-TABLE.unindent
|
214
|
+
+---+---+
|
215
|
+
| A | B |
|
216
|
+
+---+---+
|
217
|
+
| 1 | 2 |
|
218
|
+
| 3 | 4 |
|
219
|
+
+---+---+
|
220
|
+
2 rows in set
|
221
|
+
TABLE
|
222
|
+
table([[1,2], [3,4]], :headers=>['A', 'B']).should == expected_table
|
223
|
+
end
|
224
|
+
|
225
|
+
test "filters option renders" do
|
226
|
+
expected_table = <<-TABLE.unindent
|
227
|
+
+-----------+---+
|
228
|
+
| 0 | 1 |
|
229
|
+
+-----------+---+
|
230
|
+
| s,o,m,e | 2 |
|
231
|
+
| t,h,i,n,g | 1 |
|
232
|
+
+-----------+---+
|
233
|
+
2 rows in set
|
234
|
+
TABLE
|
235
|
+
table([['some', {:num=>2}], ['thing', {:num=>1}]], :filters=>{0=>lambda {|e| e.split("").join(",")},
|
236
|
+
1=>[:[], :num]}).should == expected_table
|
237
|
+
end
|
238
|
+
|
239
|
+
test "number option renders" do
|
240
|
+
expected_table = <<-TABLE.unindent
|
241
|
+
+--------+---+---+
|
242
|
+
| number | 0 | 1 |
|
243
|
+
+--------+---+---+
|
244
|
+
| 1 | a | b |
|
245
|
+
| 2 | c | d |
|
246
|
+
+--------+---+---+
|
247
|
+
2 rows in set
|
248
|
+
TABLE
|
249
|
+
table([['a','b'], ['c', 'd']], :number=>true).should == expected_table
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
context "object table" do
|
254
|
+
before(:all) {
|
255
|
+
@pets = [stub(:name=>'rufus', :age=>7, :to_s=>'rufus'), stub(:name=>'alf', :age=>101, :to_s=>'alf')]
|
256
|
+
}
|
257
|
+
test "renders" do
|
258
|
+
expected_table = <<-TABLE.unindent
|
259
|
+
+-------+-----+
|
260
|
+
| name | age |
|
261
|
+
+-------+-----+
|
262
|
+
| rufus | 7 |
|
263
|
+
| alf | 101 |
|
264
|
+
+-------+-----+
|
265
|
+
2 rows in set
|
266
|
+
TABLE
|
267
|
+
Hirb::Helpers::ObjectTable.render(@pets, :fields=>[:name, :age]).should == expected_table
|
268
|
+
end
|
269
|
+
|
270
|
+
test "with no options defaults to to_s field" do
|
271
|
+
expected_table = <<-TABLE.unindent
|
272
|
+
+-------+
|
273
|
+
| value |
|
274
|
+
+-------+
|
275
|
+
| rufus |
|
276
|
+
| alf |
|
277
|
+
+-------+
|
278
|
+
2 rows in set
|
279
|
+
TABLE
|
280
|
+
Hirb::Helpers::ObjectTable.render(@pets).should == expected_table
|
281
|
+
end
|
282
|
+
|
283
|
+
test "renders simple arrays" do
|
284
|
+
expected_table = <<-TABLE.unindent
|
285
|
+
+-------+
|
286
|
+
| value |
|
287
|
+
+-------+
|
288
|
+
| 1 |
|
289
|
+
| 2 |
|
290
|
+
| 3 |
|
291
|
+
| 4 |
|
292
|
+
+-------+
|
293
|
+
4 rows in set
|
294
|
+
TABLE
|
295
|
+
Hirb::Helpers::ObjectTable.render([1,2,3,4]).should == expected_table
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
context "activerecord table" do
|
300
|
+
before(:all) {
|
301
|
+
@pets = [stub(:name=>'rufus', :age=>7, :class=>mock(:column_names=>[:age, :name])), stub(:name=>'alf', :age=>101)]
|
302
|
+
}
|
303
|
+
test "renders" do
|
304
|
+
expected_table = <<-TABLE.unindent
|
305
|
+
+-----+-------+
|
306
|
+
| age | name |
|
307
|
+
+-----+-------+
|
308
|
+
| 7 | rufus |
|
309
|
+
| 101 | alf |
|
310
|
+
+-----+-------+
|
311
|
+
2 rows in set
|
312
|
+
TABLE
|
313
|
+
Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
context "auto table" do
|
318
|
+
test "converts nonarrays to arrays and renders" do
|
319
|
+
require 'set'
|
320
|
+
expected_table = <<-TABLE.unindent
|
321
|
+
+-------+
|
322
|
+
| value |
|
323
|
+
+-------+
|
324
|
+
| 1 |
|
325
|
+
| 2 |
|
326
|
+
| 3 |
|
327
|
+
+-------+
|
328
|
+
3 rows in set
|
329
|
+
TABLE
|
330
|
+
Hirb::Helpers::AutoTable.render(::Set.new([1,2,3])).should == expected_table
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
context "vertical table" do
|
335
|
+
test "should display vertically" do
|
336
|
+
require "set"
|
337
|
+
expected_table = <<-TABLE.unindent
|
338
|
+
*** 1. row ***
|
339
|
+
a: A
|
340
|
+
b: B
|
341
|
+
c: C
|
342
|
+
1 row in set
|
343
|
+
TABLE
|
344
|
+
|
345
|
+
table([{:a => "A", :b => "B", :c => "C"}], :vertical => true).should == expected_table
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
test "restrict_field_lengths ensures columns total doesn't exceed max width" do
|
350
|
+
@table = Hirb::Helpers::Table.new([{:f1=>'f1', :f2=>'2', :f3=>'3', :f4=>'4'}])
|
351
|
+
field_lengths = {:f1=>135, :f2=>45, :f3=>4, :f4=>55}
|
352
|
+
width = 195
|
353
|
+
@table.restrict_field_lengths(field_lengths, width)
|
354
|
+
field_lengths.values.inject {|a,e| a+=e}.should <= width
|
355
|
+
end
|
356
|
+
|
357
|
+
test "restrict_field_lengths sets columns by relative lengths" do
|
358
|
+
@table = Hirb::Helpers::Table.new([{:a=>'a', :b=>'b', :c=>'c'}])
|
359
|
+
field_lengths = {:a=>30, :b=>30, :c=>40}
|
360
|
+
width = 60
|
361
|
+
@table.restrict_field_lengths(field_lengths, width)
|
362
|
+
field_lengths.values.inject {|a,e| a+=e}.should <= width
|
363
|
+
field_lengths.values.uniq.size.should_not == 1
|
364
|
+
end
|
365
|
+
|
366
|
+
test "restrict_field_lengths sets all columns equal when no long_field and relative methods don't work" do
|
367
|
+
@table = Hirb::Helpers::Table.new([{:field1=>'f1', :field2=>'f2', :field3=>'f3'}])
|
368
|
+
field_lengths = {:field1=>10, :field2=>15, :field3=>100}
|
369
|
+
width = 20
|
370
|
+
@table.restrict_field_lengths(field_lengths, width)
|
371
|
+
field_lengths.values.inject {|a,e| a+=e}.should <= width
|
372
|
+
field_lengths.values.uniq.size.should == 1
|
373
|
+
end
|
374
|
+
end
|