table_helper 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,105 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class BodyRowByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ header = PluginAWeek::TableHelper::Header.new([])
6
+ @row = PluginAWeek::TableHelper::BodyRow.new(Object.new, header)
7
+ end
8
+
9
+ def test_should_not_alternate
10
+ assert !@row.alternate
11
+ end
12
+
13
+ def test_should_have_a_class_name
14
+ assert_equal 'row', @row[:class]
15
+ end
16
+ end
17
+
18
+ class BodyRowTest < Test::Unit::TestCase
19
+ class Post
20
+ def title
21
+ 'Default Value'
22
+ end
23
+ end
24
+
25
+ def setup
26
+ header = PluginAWeek::TableHelper::Header.new([])
27
+ header.column :title
28
+ @row = PluginAWeek::TableHelper::BodyRow.new(Post.new, header)
29
+ end
30
+
31
+ def test_should_generate_cell_accessors
32
+ assert @row.respond_to?(:title)
33
+ end
34
+
35
+ def test_should_override_default_cell_content_if_cell_specified
36
+ @row.title 'Hello World'
37
+ assert_equal '<tr class="row"><td class="title">Hello World</td></tr>', @row.html
38
+ end
39
+ end
40
+
41
+ class BodyRowWithNoColumnsTest < Test::Unit::TestCase
42
+ def setup
43
+ header = PluginAWeek::TableHelper::Header.new([])
44
+ @row = PluginAWeek::TableHelper::BodyRow.new(Object.new, header)
45
+ end
46
+
47
+ def test_should_not_build_cells
48
+ assert_equal '<tr class="row"></tr>', @row.html
49
+ end
50
+ end
51
+
52
+ class BodyRowWithCustomAttributeTest < Test::Unit::TestCase
53
+ class Post
54
+ def title
55
+ 'Default Value'
56
+ end
57
+ end
58
+
59
+ def setup
60
+ header = PluginAWeek::TableHelper::Header.new([])
61
+ header.column :title
62
+ header.column :author_name
63
+ @row = PluginAWeek::TableHelper::BodyRow.new(Post.new, header)
64
+ end
65
+
66
+ def test_should_use_attribute_values_as_cell_content
67
+ @row.author_name 'John Doe'
68
+ assert_equal '<tr class="row"><td class="title">Default Value</td><td class="author_name">John Doe</td></tr>', @row.html
69
+ end
70
+ end
71
+
72
+ class BodyRowWithMissingCellsTest < Test::Unit::TestCase
73
+ def setup
74
+ header = PluginAWeek::TableHelper::Header.new([])
75
+ header.column :title
76
+ header.column :author_name
77
+ @row = PluginAWeek::TableHelper::BodyRow.new(Object.new, header)
78
+ end
79
+
80
+ def test_should_build_missing_cells_if_cells_not_specified
81
+ assert_equal '<tr class="row"><td class="title empty"></td><td class="author_name empty"></td></tr>', @row.html
82
+ end
83
+
84
+ def test_should_skip_missing_cells_if_colspan_replaces_missing_cells
85
+ @row.title 'Hello World', :colspan => 2
86
+ assert_equal '<tr class="row"><td class="title" colspan="2">Hello World</td></tr>', @row.html
87
+ end
88
+
89
+ def test_should_not_skip_missing_cells_if_colspan_doesnt_replace_missing_cells
90
+ @row.title 'Hello World'
91
+ assert_equal '<tr class="row"><td class="title">Hello World</td><td class="author_name empty"></td></tr>', @row.html
92
+ end
93
+ end
94
+
95
+ class BodyRowAlternatingTest < Test::Unit::TestCase
96
+ def setup
97
+ header = PluginAWeek::TableHelper::Header.new([])
98
+ @row = PluginAWeek::TableHelper::BodyRow.new(Object.new, header)
99
+ @row.alternate = true
100
+ end
101
+
102
+ def test_should_add_alternate_class
103
+ assert_equal '<tr class="row alternate"></tr>', @row.html
104
+ end
105
+ end
data/test/body_test.rb ADDED
@@ -0,0 +1,271 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class BodyByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ header = PluginAWeek::TableHelper::Header.new([])
6
+ @body = PluginAWeek::TableHelper::Body.new([], header)
7
+ end
8
+
9
+ def test_should_not_alternate_rows
10
+ assert_nil @body.alternate_rows
11
+ end
12
+
13
+ def test_should_have_an_empty_caption
14
+ assert_equal 'No matches found.', @body.empty_caption
15
+ end
16
+ end
17
+
18
+ class BodyTest < Test::Unit::TestCase
19
+ class Post
20
+ attr_accessor :title
21
+
22
+ def initialize(title)
23
+ @title = title
24
+ end
25
+ end
26
+
27
+ def setup
28
+ @collection = [
29
+ Post.new('first'),
30
+ Post.new('second'),
31
+ Post.new('last')
32
+ ]
33
+ @header = PluginAWeek::TableHelper::Header.new(@collection)
34
+ @body = PluginAWeek::TableHelper::Body.new(@collection, @header)
35
+ end
36
+
37
+ def test_should_raise_exception_if_invalid_alternate_rows_specified
38
+ assert_raise(ArgumentError) {@body.alternate_rows = :invalid}
39
+ end
40
+
41
+ def test_should_not_raise_exception_for_odd_alternate_rows
42
+ assert_nothing_raised {@body.alternate_rows = :odd}
43
+ assert_equal :odd, @body.alternate_rows
44
+ end
45
+
46
+ def test_should_not_raise_exception_for_even_alternate_rows
47
+ assert_nothing_raised {@body.alternate_rows = :even}
48
+ assert_equal :even, @body.alternate_rows
49
+ end
50
+
51
+ def test_should_build_row_using_object_location_for_default_index
52
+ @header.column :title
53
+
54
+ @collection.each do |post|
55
+ html = @body.build_row(post) do |row, built_post, index|
56
+ assert_equal post, built_post
57
+ assert_equal @collection.index(post), index
58
+ end
59
+
60
+ expected = <<-end_eval
61
+ <tr class="row">
62
+ <td class="title">#{post.title}</td>
63
+ </tr>
64
+ end_eval
65
+ assert_html_equal expected, html
66
+ end
67
+ end
68
+
69
+ def test_should_build_row_using_custom_value_for_index
70
+ @header.column :title
71
+
72
+ html = @body.build_row(@collection.first, 1) do |row, post, index|
73
+ assert_equal @collection.first, post
74
+ assert_equal 1, index
75
+ end
76
+
77
+ expected = <<-end_eval
78
+ <tr class="row">
79
+ <td class="title">first</td>
80
+ </tr>
81
+ end_eval
82
+ assert_html_equal expected, html
83
+ end
84
+
85
+ def test_should_build_row_with_missing_cells
86
+ @header.column :title
87
+ @header.column :author_name
88
+
89
+ expected = <<-end_eval
90
+ <tr class="row">
91
+ <td class="title">first</td>
92
+ <td class="author_name empty"></td>
93
+ </tr>
94
+ end_eval
95
+ assert_html_equal expected, @body.build_row(@collection.first)
96
+ end
97
+
98
+ def test_should_build_all_rows
99
+ @header.column :title
100
+
101
+ expected = <<-end_eval
102
+ <tr class="row">
103
+ <td class="title">first</td>
104
+ </tr>
105
+ <tr class="row">
106
+ <td class="title">second</td>
107
+ </tr>
108
+ <tr class="row">
109
+ <td class="title">last</td>
110
+ </tr>
111
+ end_eval
112
+ assert_html_equal expected, @body.build
113
+ end
114
+
115
+ def test_html_should_use_body_tag
116
+ @header.column :title
117
+ @body.build
118
+
119
+ expected = <<-end_eval
120
+ <tbody>
121
+ <tr class="row">
122
+ <td class="title">first</td>
123
+ </tr>
124
+ <tr class="row">
125
+ <td class="title">second</td>
126
+ </tr>
127
+ <tr class="row">
128
+ <td class="title">last</td>
129
+ </tr>
130
+ </tbody>
131
+ end_eval
132
+ assert_html_equal expected, @body.html
133
+ end
134
+
135
+ def test_should_include_custom_attributes_in_body_tag
136
+ @header.column :title
137
+ @body[:class] = 'pretty'
138
+ @body.build
139
+
140
+ expected = <<-end_eval
141
+ <tbody class="pretty">
142
+ <tr class="row">
143
+ <td class="title">first</td>
144
+ </tr>
145
+ <tr class="row">
146
+ <td class="title">second</td>
147
+ </tr>
148
+ <tr class="row">
149
+ <td class="title">last</td>
150
+ </tr>
151
+ </tbody>
152
+ end_eval
153
+ assert_html_equal expected, @body.html
154
+ end
155
+ end
156
+
157
+ class BodyWithEmptyCollectionTest < Test::Unit::TestCase
158
+ def setup
159
+ @collection = []
160
+ @header = PluginAWeek::TableHelper::Header.new(@collection)
161
+ @body = PluginAWeek::TableHelper::Body.new(@collection, @header)
162
+ end
163
+
164
+ def test_should_show_no_content
165
+ expected = <<-end_eval
166
+ <tr class="no_content">
167
+ <td>No matches found.</td>
168
+ </tr>
169
+ end_eval
170
+ assert_html_equal expected, @body.build
171
+ end
172
+
173
+ def test_should_be_empty_if_no_empty_caption
174
+ @body.empty_caption = nil
175
+ assert_html_equal '', @body.build
176
+ end
177
+
178
+ def test_should_set_colspan_if_header_has_multiple_columns
179
+ @header.column :title
180
+ @header.column :author_name
181
+
182
+ expected = <<-end_eval
183
+ <tr class="no_content">
184
+ <td colspan="2">No matches found.</td>
185
+ </tr>
186
+ end_eval
187
+ assert_html_equal expected, @body.build
188
+ end
189
+ end
190
+
191
+ class BodyWithAlternatingEvenRowsTest < Test::Unit::TestCase
192
+ class Post
193
+ attr_accessor :title
194
+
195
+ def initialize(title)
196
+ @title = title
197
+ end
198
+ end
199
+
200
+ def setup
201
+ @collection = [
202
+ Post.new('first'),
203
+ Post.new('second'),
204
+ Post.new('last')
205
+ ]
206
+ @header = PluginAWeek::TableHelper::Header.new(@collection)
207
+ @header.column :title
208
+
209
+ @body = PluginAWeek::TableHelper::Body.new(@collection, @header)
210
+ @body.alternate_rows = :even
211
+ end
212
+
213
+ def test_should_alternate_even_row
214
+ expected = <<-end_eval
215
+ <tr class="row alternate">
216
+ <td class="title">first</td>
217
+ </tr>
218
+ end_eval
219
+ assert_html_equal expected, @body.build_row(@collection.first)
220
+ end
221
+
222
+ def test_should_not_alternate_odd_row
223
+ expected = <<-end_eval
224
+ <tr class="row">
225
+ <td class="title">second</td>
226
+ </tr>
227
+ end_eval
228
+ assert_html_equal expected, @body.build_row(@collection[1])
229
+ end
230
+ end
231
+
232
+ class BodyWithAlternatingOddRowsTest < Test::Unit::TestCase
233
+ class Post
234
+ attr_accessor :title
235
+
236
+ def initialize(title)
237
+ @title = title
238
+ end
239
+ end
240
+
241
+ def setup
242
+ @collection = [
243
+ Post.new('first'),
244
+ Post.new('second'),
245
+ Post.new('last')
246
+ ]
247
+ @header = PluginAWeek::TableHelper::Header.new(@collection)
248
+ @header.column :title
249
+
250
+ @body = PluginAWeek::TableHelper::Body.new(@collection, @header)
251
+ @body.alternate_rows = :odd
252
+ end
253
+
254
+ def test_should_alternate_odd_row
255
+ expected = <<-end_eval
256
+ <tr class="row alternate">
257
+ <td class="title">second</td>
258
+ </tr>
259
+ end_eval
260
+ assert_html_equal expected, @body.build_row(@collection[1])
261
+ end
262
+
263
+ def test_should_not_alternate_even_row
264
+ expected = <<-end_eval
265
+ <tr class="row">
266
+ <td class="title">first</td>
267
+ </tr>
268
+ end_eval
269
+ assert_html_equal expected, @body.build_row(@collection.first)
270
+ end
271
+ end
data/test/cell_test.rb ADDED
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CellByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @cell = PluginAWeek::TableHelper::Cell.new(:name)
6
+ end
7
+
8
+ def test_should_have_a_class_name
9
+ assert_equal 'name', @cell[:class]
10
+ end
11
+
12
+ def test_should_use_humanized_class_name_for_content
13
+ assert_equal '<td class="name">Name</td>', @cell.html
14
+ end
15
+
16
+ def test_should_create_a_data_cell
17
+ assert_equal '<td class="name">Name</td>', @cell.html
18
+ end
19
+ end
20
+
21
+ class CellTest < Test::Unit::TestCase
22
+ def test_should_use_custom_content_if_specified
23
+ cell = PluginAWeek::TableHelper::Cell.new(:name, 'John Doe')
24
+ assert_equal '<td class="name">John Doe</td>', cell.html
25
+ end
26
+
27
+ def test_should_include_custom_html_options
28
+ cell = PluginAWeek::TableHelper::Cell.new(:name, 'John Doe', {:float => 'left'})
29
+ assert_equal '<td class="name" float="left">John Doe</td>', cell.html
30
+ end
31
+
32
+ def test_should_append_automated_class_name_if_class_already_specified
33
+ cell = PluginAWeek::TableHelper::Cell.new(:name, 'John Doe', {:class => 'selected'})
34
+ assert_equal 'name selected', cell[:class]
35
+ assert_equal '<td class="name selected">John Doe</td>', cell.html
36
+ end
37
+
38
+ def test_should_raise_exception_if_content_type_is_invalid
39
+ assert_raise(ArgumentError) {PluginAWeek::TableHelper::Cell.new(:name).content_type = :invalid}
40
+ end
41
+ end
42
+
43
+ class CellWithHeaderContentType < Test::Unit::TestCase
44
+ def setup
45
+ @cell = PluginAWeek::TableHelper::Cell.new(:name)
46
+ @cell.content_type = :header
47
+ end
48
+
49
+ def test_should_a_header_cell
50
+ assert_equal '<th class="name">Name</th>', @cell.html
51
+ end
52
+ end
@@ -0,0 +1,254 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CollectionTableByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @table = PluginAWeek::TableHelper::CollectionTable.new([])
6
+ end
7
+
8
+ def test_cellspacing_should_be_zero
9
+ assert_equal '0', @table[:cellspacing]
10
+ end
11
+
12
+ def test_cellpadding_should_be_zero
13
+ assert_equal '0', @table[:cellpadding]
14
+ end
15
+
16
+ def test_should_include_header_and_footer
17
+ html = @table.build do |header, body|
18
+ assert_instance_of PluginAWeek::TableHelper::Header, header
19
+ assert_instance_of PluginAWeek::TableHelper::Body, body
20
+ end
21
+
22
+ expected = <<-end_eval
23
+ <thead style="display: none;">
24
+ <tr></tr>
25
+ </thead>
26
+ <tbody>
27
+ <tr class="no_content">
28
+ <td>No matches found.</td>
29
+ </tr>
30
+ </tbody>
31
+ end_eval
32
+ assert_html_equal expected, html
33
+ end
34
+ end
35
+
36
+ class CollectionTableTest < Test::Unit::TestCase
37
+ class Note
38
+ def self.column_names
39
+ ['title', 'author_name']
40
+ end
41
+
42
+ attr_accessor :title
43
+
44
+ def initialize(title)
45
+ @title = title
46
+ end
47
+ end
48
+
49
+ class Post < Note
50
+ def self.column_names
51
+ ['title']
52
+ end
53
+ end
54
+
55
+ class Reflection
56
+ def klass
57
+ Note
58
+ end
59
+ end
60
+
61
+ class NoteCollection < Array
62
+ def proxy_reflection
63
+ Reflection.new
64
+ end
65
+ end
66
+
67
+ def setup
68
+ @collection = [
69
+ Post.new('first'),
70
+ Post.new('second'),
71
+ Post.new('last')
72
+ ]
73
+ end
74
+
75
+ def test_should_raise_exception_if_invalid_option_specified
76
+ assert_raise(ArgumentError) {PluginAWeek::TableHelper::CollectionTable.new([], :invalid => true)}
77
+ end
78
+
79
+ def test_should_only_include_body_if_no_header
80
+ table = PluginAWeek::TableHelper::CollectionTable.new(@collection, :header => false)
81
+ html = table.build do |body|
82
+ assert_instance_of PluginAWeek::TableHelper::Body, body
83
+ end
84
+
85
+ expected = <<-end_eval
86
+ <tbody>
87
+ <tr class="row">
88
+ <td class="title">first</td>
89
+ </tr>
90
+ <tr class="row">
91
+ <td class="title">second</td>
92
+ </tr>
93
+ <tr class="row">
94
+ <td class="title">last</td>
95
+ </tr>
96
+ </tbody>
97
+ end_eval
98
+ assert_html_equal expected, html
99
+ end
100
+
101
+ def test_should_include_body_and_footer_if_no_header_and_footer
102
+ table = PluginAWeek::TableHelper::CollectionTable.new(@collection, :header => false, :footer => true)
103
+ html = table.build do |body, footer|
104
+ assert_instance_of PluginAWeek::TableHelper::Body, body
105
+ assert_instance_of PluginAWeek::TableHelper::Footer, footer
106
+ end
107
+
108
+ expected = <<-end_eval
109
+ <tbody>
110
+ <tr class="row">
111
+ <td class="title">first</td>
112
+ </tr>
113
+ <tr class="row">
114
+ <td class="title">second</td>
115
+ </tr>
116
+ <tr class="row">
117
+ <td class="title">last</td>
118
+ </tr>
119
+ </tbody>
120
+ <tfoot>
121
+ <tr>
122
+ </tr>
123
+ </tfoot>
124
+ end_eval
125
+ assert_html_equal expected, html
126
+ end
127
+
128
+ def test_should_include_header_body_and_footer_if_footer
129
+ table = PluginAWeek::TableHelper::CollectionTable.new(@collection, :footer => true)
130
+ html = table.build do |header, body, footer|
131
+ assert_instance_of PluginAWeek::TableHelper::Header, header
132
+ assert_instance_of PluginAWeek::TableHelper::Body, body
133
+ assert_instance_of PluginAWeek::TableHelper::Footer, footer
134
+ end
135
+
136
+ expected = <<-end_eval
137
+ <thead>
138
+ <tr>
139
+ <th class="title" scope="col">Title</th>
140
+ </tr>
141
+ </thead>
142
+ <tbody>
143
+ <tr class="row">
144
+ <td class="title">first</td>
145
+ </tr>
146
+ <tr class="row">
147
+ <td class="title">second</td>
148
+ </tr>
149
+ <tr class="row">
150
+ <td class="title">last</td>
151
+ </tr>
152
+ </tbody>
153
+ <tfoot>
154
+ <tr>
155
+ </tr>
156
+ </tfoot>
157
+ end_eval
158
+ assert_html_equal expected, html
159
+ end
160
+
161
+ def test_should_use_custom_class_to_generate_columns
162
+ table = PluginAWeek::TableHelper::CollectionTable.new(@collection, :class => Note)
163
+ html = table.build
164
+
165
+ expected = <<-end_eval
166
+ <thead>
167
+ <tr>
168
+ <th class="title" scope="col">Title</th>
169
+ <th class="author_name" scope="col">Author Name</th>
170
+ </tr>
171
+ </thead>
172
+ <tbody>
173
+ <tr class="row">
174
+ <td class="title">first</td>
175
+ <td class="author_name empty"></td>
176
+ </tr>
177
+ <tr class="row">
178
+ <td class="title">second</td>
179
+ <td class="author_name empty"></td>
180
+ </tr>
181
+ <tr class="row">
182
+ <td class="title">last</td>
183
+ <td class="author_name empty"></td>
184
+ </tr>
185
+ </tbody>
186
+ end_eval
187
+ assert_html_equal expected, html
188
+ end
189
+
190
+ def test_should_use_class_from_first_element_to_generate_columns
191
+ @collection.insert(0, Note.new('zeroth'))
192
+ table = PluginAWeek::TableHelper::CollectionTable.new(@collection)
193
+ html = table.build
194
+
195
+ expected = <<-end_eval
196
+ <thead>
197
+ <tr>
198
+ <th class="title" scope="col">Title</th>
199
+ <th class="author_name" scope="col">Author Name</th>
200
+ </tr>
201
+ </thead>
202
+ <tbody>
203
+ <tr class="row">
204
+ <td class="title">zeroth</td>
205
+ <td class="author_name empty"></td>
206
+ </tr>
207
+ <tr class="row">
208
+ <td class="title">first</td>
209
+ <td class="author_name empty"></td>
210
+ </tr>
211
+ <tr class="row">
212
+ <td class="title">second</td>
213
+ <td class="author_name empty"></td>
214
+ </tr>
215
+ <tr class="row">
216
+ <td class="title">last</td>
217
+ <td class="author_name empty"></td>
218
+ </tr>
219
+ </tbody>
220
+ end_eval
221
+ assert_html_equal expected, html
222
+ end
223
+
224
+ def test_should_use_class_from_proxy_reflection_to_generate_columns
225
+ collection = NoteCollection.new
226
+ collection.concat(@collection)
227
+ table = PluginAWeek::TableHelper::CollectionTable.new(collection)
228
+ html = table.build
229
+
230
+ expected = <<-end_eval
231
+ <thead>
232
+ <tr>
233
+ <th class="title" scope="col">Title</th>
234
+ <th class="author_name" scope="col">Author Name</th>
235
+ </tr>
236
+ </thead>
237
+ <tbody>
238
+ <tr class="row">
239
+ <td class="title">first</td>
240
+ <td class="author_name empty"></td>
241
+ </tr>
242
+ <tr class="row">
243
+ <td class="title">second</td>
244
+ <td class="author_name empty"></td>
245
+ </tr>
246
+ <tr class="row">
247
+ <td class="title">last</td>
248
+ <td class="author_name empty"></td>
249
+ </tr>
250
+ </tbody>
251
+ end_eval
252
+ assert_html_equal expected, html
253
+ end
254
+ end