hirb 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ class Hirb::Views::ActiveRecord_Base #:nodoc:
2
+ def self.default_options
3
+ {:ancestor=>true}
4
+ end
5
+
6
+ def self.render(*args)
7
+ Hirb::Helpers::ActiveRecordTable.render(*args)
8
+ end
9
+ end
@@ -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
@@ -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
@@ -0,0 +1,263 @@
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
+
8
+ context "basic table" do
9
+ test "renders" do
10
+ expected_table = <<-TABLE.unindent
11
+ +---+---+
12
+ | a | b |
13
+ +---+---+
14
+ | 1 | 2 |
15
+ | 3 | 4 |
16
+ +---+---+
17
+ 2 rows in set
18
+ TABLE
19
+ table([{:a=>1, :b=>2}, {:a=>3, :b=>4}]).should == expected_table
20
+ end
21
+
22
+ test "with no headers renders" do
23
+ expected_table = <<-TABLE.unindent
24
+ +---+---+
25
+ | 1 | 2 |
26
+ +---+---+
27
+ 1 row in set
28
+ TABLE
29
+ table([{:a=>1, :b=>2}], :headers=>nil).should == expected_table
30
+ end
31
+
32
+ test "with string keys renders" do
33
+ expected_table = <<-TABLE.unindent
34
+ +---+---+
35
+ | a | b |
36
+ +---+---+
37
+ | 1 | 2 |
38
+ | 3 | 4 |
39
+ +---+---+
40
+ 2 rows in set
41
+ TABLE
42
+ table([{'a'=>1, 'b'=>2}, {'a'=>3, 'b'=>4}]).should == expected_table
43
+ end
44
+
45
+ test "with array only rows renders" do
46
+ expected_table = <<-TABLE.unindent
47
+ +---+---+
48
+ | 0 | 1 |
49
+ +---+---+
50
+ | 1 | 2 |
51
+ | 3 | 4 |
52
+ +---+---+
53
+ 2 rows in set
54
+ TABLE
55
+ table([[1,2], [3,4]]).should == expected_table
56
+ end
57
+
58
+ test "with too many fields raises error" do
59
+ assert_raises(Hirb::Helpers::Table::TooManyFieldsForWidthError) { table([Array.new(70, 'AAA')]) }
60
+ end
61
+
62
+ test "with no rows renders" do
63
+ table([]).should == "0 rows in set"
64
+ end
65
+ end
66
+
67
+ context "table with" do
68
+ test "fields option renders" do
69
+ expected_table = <<-TABLE.unindent
70
+ +---+---+
71
+ | b | a |
72
+ +---+---+
73
+ | 2 | 1 |
74
+ | 4 | 3 |
75
+ +---+---+
76
+ 2 rows in set
77
+ TABLE
78
+ table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :fields=>[:b, :a]).should == expected_table
79
+ end
80
+
81
+ test "fields option and array only rows" do
82
+ expected_table = <<-TABLE.unindent
83
+ +---+---+
84
+ | 0 | 2 |
85
+ +---+---+
86
+ | 1 | 3 |
87
+ +---+---+
88
+ 1 row in set
89
+ TABLE
90
+ table([[1,2,3]], :fields=>[0,2]).should == expected_table
91
+ end
92
+
93
+ test "invalid fields option renders empty columns" do
94
+ expected_table = <<-TABLE.unindent
95
+ +---+---+
96
+ | b | c |
97
+ +---+---+
98
+ | 2 | |
99
+ | 4 | |
100
+ +---+---+
101
+ 2 rows in set
102
+ TABLE
103
+ table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :fields=>[:b, :c]).should == expected_table
104
+ end
105
+
106
+ test "invalid fields in field_lengths option renders" do
107
+ expected_table = <<-TABLE.unindent
108
+ +------------+---+
109
+ | a | b |
110
+ +------------+---+
111
+ | AAAAAAA... | 2 |
112
+ +------------+---+
113
+ 1 row in set
114
+ TABLE
115
+ table([{:a=> "A" * 50, :b=>2}], :field_lengths=>{:a=>10,:c=>10}).should == expected_table
116
+ end
117
+
118
+ test "field_lengths option and field_lengths less than 3 characters renders" do
119
+ expected_table = <<-TABLE.unindent
120
+ +----+---+
121
+ | a | b |
122
+ +----+---+
123
+ | AA | 2 |
124
+ +----+---+
125
+ 1 row in set
126
+ TABLE
127
+ table([{:a=> "A" * 50, :b=>2}], :field_lengths=>{:a=>2}).should == expected_table
128
+ end
129
+
130
+ test "field_lengths option renders" do
131
+ expected_table = <<-TABLE.unindent
132
+ +------------+---+
133
+ | a | b |
134
+ +------------+---+
135
+ | AAAAAAA... | 2 |
136
+ +------------+---+
137
+ 1 row in set
138
+ TABLE
139
+ table([{:a=> "A" * 50, :b=>2}], :field_lengths=>{:a=>10}).should == expected_table
140
+ end
141
+
142
+ test "max_width option renders" do
143
+ expected_table = <<-TABLE.unindent
144
+ +--------------------------+---+------------+
145
+ | a | b | c |
146
+ +--------------------------+---+------------+
147
+ | AAAAAAAAAAAAAAAAAAAAA... | 2 | CCCCCCCCCC |
148
+ +--------------------------+---+------------+
149
+ 1 row in set
150
+ TABLE
151
+ table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>30).should == expected_table
152
+ end
153
+
154
+ test "max_width option nil renders full table" do
155
+ expected_table = <<-TABLE.unindent
156
+ +----------------------------------------------------+---+------------+
157
+ | a | b | c |
158
+ +----------------------------------------------------+---+------------+
159
+ | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | 2 | CCCCCCCCCC |
160
+ +----------------------------------------------------+---+------------+
161
+ 1 row in set
162
+ TABLE
163
+ table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>nil).should == expected_table
164
+ end
165
+
166
+ test "global max_width renders" do
167
+ expected_table = <<-TABLE.unindent
168
+ +--------------------------+---+------------+
169
+ | a | b | c |
170
+ +--------------------------+---+------------+
171
+ | AAAAAAAAAAAAAAAAAAAAA... | 2 | CCCCCCCCCC |
172
+ +--------------------------+---+------------+
173
+ 1 row in set
174
+ TABLE
175
+ Hirb::Helpers::Table.max_width = 30
176
+ table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}]).should == expected_table
177
+ Hirb::Helpers::Table.max_width = Hirb::Helpers::Table::DEFAULT_MAX_WIDTH
178
+ end
179
+
180
+ test "headers option and headers longer than fields renders" do
181
+ expected_table = <<-TABLE.unindent
182
+ +---+---------+---------+
183
+ | a | field B | field C |
184
+ +---+---------+---------+
185
+ | A | 2 | C |
186
+ +---+---------+---------+
187
+ 1 row in set
188
+ TABLE
189
+ table([{:a=> "A", :b=>2, :c=>"C"}], :headers=>{:b=>"field B", :c=>"field C"}).should == expected_table
190
+ end
191
+
192
+ test "headers option and headers shortened by field_lengths renders" do
193
+ expected_table = <<-TABLE.unindent
194
+ +-------+---+
195
+ | fi... | b |
196
+ +-------+---+
197
+ | A | 2 |
198
+ +-------+---+
199
+ 1 row in set
200
+ TABLE
201
+ table([{:a=> "A", :b=>2}], :headers=>{:a=>"field A"}, :field_lengths=>{:a=>5}).should == expected_table
202
+ end
203
+
204
+ test "with headers option as an array renders" do
205
+ expected_table = <<-TABLE.unindent
206
+ +---+---+
207
+ | A | B |
208
+ +---+---+
209
+ | 1 | 2 |
210
+ | 3 | 4 |
211
+ +---+---+
212
+ 2 rows in set
213
+ TABLE
214
+ table([[1,2], [3,4]], :headers=>['A', 'B']).should == expected_table
215
+ end
216
+
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
224
+ expected_table = <<-TABLE.unindent
225
+ +-------+-----+
226
+ | name | age |
227
+ +-------+-----+
228
+ | rufus | 7 |
229
+ | alf | 101 |
230
+ +-------+-----+
231
+ 2 rows in set
232
+ TABLE
233
+ Hirb::Helpers::ObjectTable.render(@pets, :fields=>[:name, :age]).should == expected_table
234
+ end
235
+
236
+ test "with no options fields raises ArgumentError" do
237
+ assert_raises(ArgumentError) { Hirb::Helpers::ObjectTable.render(@pets) }
238
+ 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
246
+ expected_table = <<-TABLE.unindent
247
+ +-----+-------+
248
+ | age | name |
249
+ +-----+-------+
250
+ | 7 | rufus |
251
+ | 101 | alf |
252
+ +-----+-------+
253
+ 2 rows in set
254
+ TABLE
255
+ Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
256
+ end
257
+ end
258
+
259
+ test "restrict_field_lengths handles many fields" do
260
+ @table = Hirb::Helpers::Table.new([{:field1=>'f1', :field2=>'f2', :field3=>'f3'}])
261
+ @table.restrict_field_lengths({:field1=>10, :field2=>15, :field3=>100}, 10)
262
+ end
263
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'context' #gem install jeremymcanally-context -s http://gems.github.com
4
+ require 'matchy' #gem install jeremymcanally-matchy -s http://gems.github.com
5
+ require 'mocha'
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'hirb'
8
+
9
+ class Test::Unit::TestCase
10
+ end
11
+
12
+ class String
13
+ def unindent(num=nil)
14
+ regex = num ? /^\s{#{num}}/ : /^\s*/
15
+ gsub(regex, '').chomp
16
+ end
17
+ end
@@ -0,0 +1,167 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
4
+ def tree(*args)
5
+ Hirb::Helpers::Tree.render(*args)
6
+ end
7
+
8
+ context "basic tree" do
9
+ test "with hash nodes renders" do
10
+ expected_tree = <<-TREE.unindent(6)
11
+ 0.0
12
+ 1.1
13
+ 2.2
14
+ 3.2
15
+ 4.1
16
+ TREE
17
+ tree([{:level=>0, :value=>'0.0'}, {:level=>1, :value=>'1.1'}, {:level=>2, :value=>'2.2'},{:level=>2, :value=>'3.2'},
18
+ {:level=>1, :value=>'4.1'}]).should == expected_tree
19
+ end
20
+
21
+ test "with array nodes renders" do
22
+ expected_tree = <<-TREE.unindent(6)
23
+ 0.0
24
+ 1.1
25
+ 2.2
26
+ 3.2
27
+ 4.1
28
+ TREE
29
+ tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]]).should == expected_tree
30
+ end
31
+
32
+ test "with non-string values renders" do
33
+ expected_tree = <<-TREE.unindent(6)
34
+ 0.0
35
+ 1.1
36
+ 2.2
37
+ 3.2
38
+ 4.1
39
+ TREE
40
+ tree([[0,0.0],[1,1.1],[2,2.2],[2,3.2],[1,4.1]]).should == expected_tree
41
+ end
42
+
43
+ test "with indent option renders" do
44
+ expected_tree = <<-TREE.unindent(6)
45
+ 0.0
46
+ 1.1
47
+ 2.2
48
+ 3.2
49
+ 4.1
50
+ TREE
51
+ tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :indent=>2).should == expected_tree
52
+ end
53
+
54
+ test "with limit option renders" do
55
+ expected_tree = <<-TREE.unindent(6)
56
+ 0.0
57
+ 1.1
58
+ 4.1
59
+ TREE
60
+ tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :limit=>1).should == expected_tree
61
+ end
62
+
63
+ test "with description option renders" do
64
+ expected_tree = <<-TREE.unindent(6)
65
+ 0.0
66
+ 1.1
67
+ 2.2
68
+ 3.2
69
+ 4.1
70
+
71
+ 5 nodes in tree
72
+ TREE
73
+ tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :description=>true).should == expected_tree
74
+ end
75
+
76
+ test "with type directory renders" do
77
+ expected_tree = <<-TREE.unindent
78
+ 0.0
79
+ |-- 1.1
80
+ | |-- 2.2
81
+ | `-- 3.2
82
+ `-- 4.1
83
+ TREE
84
+ tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :type=>:directory).should == expected_tree
85
+ end
86
+
87
+ test "with type directory and multiple children per level renders" do
88
+ expected_tree = <<-TREE.unindent
89
+ 0.0
90
+ |-- 1.1
91
+ | |-- 2.2
92
+ | | `-- 3.3
93
+ | `-- 4.2
94
+ | `-- 5.3
95
+ `-- 6.1
96
+ TREE
97
+ tree([[0,'0.0'], [1,'1.1'], [2,'2.2'],[3,'3.3'],[2,'4.2'],[3,'5.3'],[1,'6.1']], :type=>:directory).should == expected_tree
98
+ end
99
+
100
+ test "with type number renders" do
101
+ expected_tree = <<-TREE.unindent(6)
102
+ 1. 0
103
+ 1. 1
104
+ 1. 2
105
+ 2. 3
106
+ 2. 4
107
+ TREE
108
+ tree([[0,'0'],[1,'1'],[2,'2'],[2,'3'],[1,'4']], :type=>:number)
109
+ end
110
+ end
111
+
112
+ def mock_node(value, value_method)
113
+ children = []
114
+ value,children = *value if value.is_a?(Array)
115
+ mock(value_method=>value, :children=>children.map {|e| mock_node(e, value_method)})
116
+ end
117
+
118
+ context "parent_child_tree" do
119
+ test "with name value renders" do
120
+ expected_tree = <<-TREE.unindent
121
+ 0.0
122
+ |-- 1.1
123
+ |-- 2.1
124
+ | `-- 3.2
125
+ `-- 4.1
126
+ TREE
127
+ root = mock_node(['0.0', ['1.1', ['2.1', '3.2'], '4.1']], :name)
128
+ Hirb::Helpers::ParentChildTree.render(root, :type=>:directory).should == expected_tree
129
+ end
130
+
131
+ test "with object_id value renders" do
132
+ expected_tree = <<-TREE.unindent
133
+ 0.0
134
+ |-- 1.1
135
+ |-- 2.1
136
+ | `-- 3.2
137
+ `-- 4.1
138
+ TREE
139
+ root = mock_node(['0.0', ['1.1', ['2.1', '3.2'], '4.1']], :object_id)
140
+ Hirb::Helpers::ParentChildTree.render(root, :type=>:directory).should == expected_tree
141
+ end
142
+
143
+ test "with value_method option renders" do
144
+ expected_tree = <<-TREE.unindent
145
+ 0.0
146
+ |-- 1.1
147
+ |-- 2.1
148
+ | `-- 3.2
149
+ `-- 4.1
150
+ TREE
151
+ root = mock_node(['0.0', ['1.1', ['2.1', '3.2'], '4.1']], :blah)
152
+ Hirb::Helpers::ParentChildTree.render(root, :type=>:directory, :value_method=>:blah).should == expected_tree
153
+ end
154
+ end
155
+
156
+ test "tree with parentless nodes renders ParentlessNodeError" do
157
+ assert_raises(Hirb::Helpers::Tree::ParentlessNodeError) { tree([[0, "0.0"], [2, '1.2']], :validate=>true) }
158
+ end
159
+
160
+ test "tree with hash nodes missing level raises MissingLevelError" do
161
+ assert_raises(Hirb::Helpers::Tree::Node::MissingLevelError) { tree([{:value=>'ok'}]) }
162
+ end
163
+
164
+ test "tree with hash nodes missing level raises MissingValueError" do
165
+ assert_raises(Hirb::Helpers::Tree::Node::MissingValueError) { tree([{:level=>0}]) }
166
+ end
167
+ end