table_helper 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,12 +2,16 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class BodyByDefaultTest < Test::Unit::TestCase
4
4
  def setup
5
- header = TableHelper::Header.new([])
6
- @body = TableHelper::Body.new([], header)
5
+ @table = TableHelper::CollectionTable.new([])
6
+ @body = TableHelper::Body.new(@table)
7
7
  end
8
8
 
9
- def test_should_not_alternate_rows
10
- assert_nil @body.alternate_rows
9
+ def test_should_have_a_table
10
+ assert_equal @table, @body.table
11
+ end
12
+
13
+ def test_should_not_alternate
14
+ assert_nil @body.alternate
11
15
  end
12
16
 
13
17
  def test_should_have_an_empty_caption
@@ -16,50 +20,108 @@ class BodyByDefaultTest < Test::Unit::TestCase
16
20
  end
17
21
 
18
22
  class BodyTest < Test::Unit::TestCase
19
- class Post
20
- attr_accessor :title
21
-
22
- def initialize(title)
23
- @title = title
24
- end
23
+ def setup
24
+ @table = TableHelper::CollectionTable.new([])
25
+ @body = TableHelper::Body.new(@table)
26
+ end
27
+
28
+ def test_should_raise_exception_if_invalid_alternate_specified
29
+ assert_raise(ArgumentError) {@body.alternate = :invalid}
25
30
  end
26
31
 
32
+ def test_should_not_raise_exception_for_odd_alternate
33
+ assert_nothing_raised {@body.alternate = :odd}
34
+ assert_equal :odd, @body.alternate
35
+ end
36
+
37
+ def test_should_not_raise_exception_for_even_alternate
38
+ assert_nothing_raised {@body.alternate = :even}
39
+ assert_equal :even, @body.alternate
40
+ end
41
+ end
42
+
43
+ class BodyWithEmptyCollectionTest < Test::Unit::TestCase
27
44
  def setup
28
- @collection = [
29
- Post.new('first'),
30
- Post.new('second'),
31
- Post.new('last')
32
- ]
33
- @header = TableHelper::Header.new(@collection)
34
- @body = TableHelper::Body.new(@collection, @header)
45
+ @table = TableHelper::CollectionTable.new([])
46
+ @body = TableHelper::Body.new(@table)
47
+ end
48
+
49
+ def test_should_have_no_content_if_no_empty_caption
50
+ @body.empty_caption = nil
51
+ assert_html_equal '<tbody></tbody>', @body.html
52
+ end
53
+
54
+ def test_should_show_content_if_empty_caption
55
+ expected = <<-end_str
56
+ <tbody>
57
+ <tr class="ui-collection-empty">
58
+ <td>No matches found.</td>
59
+ </tr>
60
+ </tbody>
61
+ end_str
62
+ assert_html_equal expected, @body.html
35
63
  end
36
64
 
37
- def test_should_raise_exception_if_invalid_alternate_rows_specified
38
- assert_raise(ArgumentError) {@body.alternate_rows = :invalid}
65
+ def test_should_use_custom_empty_caption_class_if_specified
66
+ original_empty_caption_class = TableHelper::Body.empty_caption_class
67
+ TableHelper::Body.empty_caption_class = 'ui-collection-empty_caption'
68
+
69
+ expected = <<-end_str
70
+ <tbody>
71
+ <tr class="ui-collection-empty_caption">
72
+ <td>No matches found.</td>
73
+ </tr>
74
+ </tbody>
75
+ end_str
76
+ assert_html_equal expected, @body.html
77
+ ensure
78
+ TableHelper::Body.empty_caption_class = original_empty_caption_class
39
79
  end
40
80
 
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
81
+ def test_should_set_colspan_if_header_has_multiple_columns
82
+ @table.header :title, :author_name
83
+
84
+ expected = <<-end_str
85
+ <tbody>
86
+ <tr class="ui-collection-empty">
87
+ <td colspan="2">No matches found.</td>
88
+ </tr>
89
+ </tbody>
90
+ end_str
91
+ assert_html_equal expected, @body.html
92
+ end
93
+ end
94
+
95
+ class BodyWithCollectionTest < Test::Unit::TestCase
96
+ class Post
97
+ attr_accessor :title
98
+
99
+ def initialize(title)
100
+ @title = title
101
+ end
44
102
  end
45
103
 
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
104
+ def setup
105
+ @collection = [Post.new('first'), Post.new('second'), Post.new('last')]
106
+ @table = TableHelper::CollectionTable.new(@collection)
107
+ @table.header :title
108
+
109
+ @body = TableHelper::Body.new(@table)
49
110
  end
50
111
 
51
112
  def test_should_build_row_using_object_location_for_default_index
52
- @header.column :title
113
+ build_post = nil
114
+ index = nil
115
+ @body.each {|row, build_post, index|}
53
116
 
54
117
  @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
118
+ html = @body.build_row(post)
119
+ assert_equal post, build_post
120
+ assert_equal @collection.index(post), index
59
121
 
60
122
  expected = <<-end_str
61
- <tr class="row">
62
- <td class="title">#{post.title}</td>
123
+ <tr class="post ui-collection-result">
124
+ <td class="post-title">#{post.title}</td>
63
125
  </tr>
64
126
  end_str
65
127
  assert_html_equal expected, html
@@ -67,65 +129,45 @@ class BodyTest < Test::Unit::TestCase
67
129
  end
68
130
 
69
131
  def test_should_build_row_using_custom_value_for_index
70
- @header.column :title
132
+ post = nil
133
+ index = nil
134
+ @body.each {|row, post, index|}
71
135
 
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
136
+ html = @body.build_row(@collection.first, 1)
137
+ assert_equal @collection.first, post
138
+ assert_equal 1, index
76
139
 
77
140
  expected = <<-end_str
78
- <tr class="row">
79
- <td class="title">first</td>
141
+ <tr class="post ui-collection-result">
142
+ <td class="post-title">first</td>
80
143
  </tr>
81
144
  end_str
82
145
  assert_html_equal expected, html
83
146
  end
84
147
 
85
148
  def test_should_build_row_with_missing_cells
86
- @header.column :title
87
- @header.column :author_name
149
+ header = @table.header :author_name
88
150
 
89
151
  expected = <<-end_str
90
- <tr class="row">
91
- <td class="title">first</td>
92
- <td class="author_name empty"></td>
152
+ <tr class="post ui-collection-result">
153
+ <td class="post-title">first</td>
154
+ <td class="post-author_name ui-state-empty"></td>
93
155
  </tr>
94
156
  end_str
95
157
  assert_html_equal expected, @body.build_row(@collection.first)
96
158
  end
97
159
 
98
- def test_should_build_all_rows
99
- @header.column :title
100
-
101
- expected = <<-end_str
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_str
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
-
160
+ def test_should_build_html
119
161
  expected = <<-end_str
120
162
  <tbody>
121
- <tr class="row">
122
- <td class="title">first</td>
163
+ <tr class="post ui-collection-result">
164
+ <td class="post-title">first</td>
123
165
  </tr>
124
- <tr class="row">
125
- <td class="title">second</td>
166
+ <tr class="post ui-collection-result">
167
+ <td class="post-title">second</td>
126
168
  </tr>
127
- <tr class="row">
128
- <td class="title">last</td>
169
+ <tr class="post ui-collection-result">
170
+ <td class="post-title">last</td>
129
171
  </tr>
130
172
  </tbody>
131
173
  end_str
@@ -133,20 +175,18 @@ class BodyTest < Test::Unit::TestCase
133
175
  end
134
176
 
135
177
  def test_should_include_custom_attributes_in_body_tag
136
- @header.column :title
137
178
  @body[:class] = 'pretty'
138
- @body.build
139
179
 
140
180
  expected = <<-end_str
141
181
  <tbody class="pretty">
142
- <tr class="row">
143
- <td class="title">first</td>
182
+ <tr class="post ui-collection-result">
183
+ <td class="post-title">first</td>
144
184
  </tr>
145
- <tr class="row">
146
- <td class="title">second</td>
185
+ <tr class="post ui-collection-result">
186
+ <td class="post-title">second</td>
147
187
  </tr>
148
- <tr class="row">
149
- <td class="title">last</td>
188
+ <tr class="post ui-collection-result">
189
+ <td class="post-title">last</td>
150
190
  </tr>
151
191
  </tbody>
152
192
  end_str
@@ -154,37 +194,33 @@ class BodyTest < Test::Unit::TestCase
154
194
  end
155
195
  end
156
196
 
157
- class BodyWithEmptyCollectionTest < Test::Unit::TestCase
197
+ class BodyWithCustomBuilderTest < Test::Unit::TestCase
158
198
  def setup
159
- @collection = []
160
- @header = TableHelper::Header.new(@collection)
161
- @body = TableHelper::Body.new(@collection, @header)
162
- end
163
-
164
- def test_should_show_no_content
165
- expected = <<-end_str
166
- <tr class="no_content">
167
- <td>No matches found.</td>
168
- </tr>
169
- end_str
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
199
+ @collection = [Object.new, Object.new, Object.new]
200
+ @table = TableHelper::CollectionTable.new(@collection)
201
+ @table.header :index
202
+
203
+ @body = TableHelper::Body.new(@table)
204
+ @body.each do |row, object, index|
205
+ row.index index.to_s
206
+ end
176
207
  end
177
208
 
178
- def test_should_set_colspan_if_header_has_multiple_columns
179
- @header.column :title
180
- @header.column :author_name
181
-
209
+ def test_should_use_custom_builder
182
210
  expected = <<-end_str
183
- <tr class="no_content">
184
- <td colspan="2">No matches found.</td>
185
- </tr>
211
+ <tbody>
212
+ <tr class="object ui-collection-result">
213
+ <td class="object-index">0</td>
214
+ </tr>
215
+ <tr class="object ui-collection-result">
216
+ <td class="object-index">1</td>
217
+ </tr>
218
+ <tr class="object ui-collection-result">
219
+ <td class="object-index">2</td>
220
+ </tr>
221
+ </tbody>
186
222
  end_str
187
- assert_html_equal expected, @body.build
223
+ assert_html_equal expected, @body.html
188
224
  end
189
225
  end
190
226
 
@@ -198,22 +234,18 @@ class BodyWithAlternatingEvenRowsTest < Test::Unit::TestCase
198
234
  end
199
235
 
200
236
  def setup
201
- @collection = [
202
- Post.new('first'),
203
- Post.new('second'),
204
- Post.new('last')
205
- ]
206
- @header = TableHelper::Header.new(@collection)
207
- @header.column :title
237
+ @collection = [Post.new('first'), Post.new('second'), Post.new('last')]
238
+ table = TableHelper::CollectionTable.new(@collection)
239
+ table.header :title
208
240
 
209
- @body = TableHelper::Body.new(@collection, @header)
210
- @body.alternate_rows = :even
241
+ @body = TableHelper::Body.new(table)
242
+ @body.alternate = :even
211
243
  end
212
244
 
213
245
  def test_should_alternate_even_row
214
246
  expected = <<-end_str
215
- <tr class="row alternate">
216
- <td class="title">first</td>
247
+ <tr class="post ui-collection-result ui-state-alternate">
248
+ <td class="post-title">first</td>
217
249
  </tr>
218
250
  end_str
219
251
  assert_html_equal expected, @body.build_row(@collection.first)
@@ -221,8 +253,8 @@ class BodyWithAlternatingEvenRowsTest < Test::Unit::TestCase
221
253
 
222
254
  def test_should_not_alternate_odd_row
223
255
  expected = <<-end_str
224
- <tr class="row">
225
- <td class="title">second</td>
256
+ <tr class="post ui-collection-result">
257
+ <td class="post-title">second</td>
226
258
  </tr>
227
259
  end_str
228
260
  assert_html_equal expected, @body.build_row(@collection[1])
@@ -239,22 +271,18 @@ class BodyWithAlternatingOddRowsTest < Test::Unit::TestCase
239
271
  end
240
272
 
241
273
  def setup
242
- @collection = [
243
- Post.new('first'),
244
- Post.new('second'),
245
- Post.new('last')
246
- ]
247
- @header = TableHelper::Header.new(@collection)
248
- @header.column :title
274
+ @collection = [Post.new('first'), Post.new('second'), Post.new('last')]
275
+ table = TableHelper::CollectionTable.new(@collection)
276
+ table.header :title
249
277
 
250
- @body = TableHelper::Body.new(@collection, @header)
251
- @body.alternate_rows = :odd
278
+ @body = TableHelper::Body.new(table)
279
+ @body.alternate = :odd
252
280
  end
253
281
 
254
282
  def test_should_alternate_odd_row
255
283
  expected = <<-end_str
256
- <tr class="row alternate">
257
- <td class="title">second</td>
284
+ <tr class="post ui-collection-result ui-state-alternate">
285
+ <td class="post-title">second</td>
258
286
  </tr>
259
287
  end_str
260
288
  assert_html_equal expected, @body.build_row(@collection[1])
@@ -262,8 +290,8 @@ class BodyWithAlternatingOddRowsTest < Test::Unit::TestCase
262
290
 
263
291
  def test_should_not_alternate_even_row
264
292
  expected = <<-end_str
265
- <tr class="row">
266
- <td class="title">first</td>
293
+ <tr class="post ui-collection-result">
294
+ <td class="post-title">first</td>
267
295
  </tr>
268
296
  end_str
269
297
  assert_html_equal expected, @body.build_row(@collection.first)
@@ -5,38 +5,108 @@ class CellByDefaultTest < Test::Unit::TestCase
5
5
  @cell = TableHelper::Cell.new(:name)
6
6
  end
7
7
 
8
+ def test_should_have_content
9
+ assert_equal 'Name', @cell.content
10
+ end
11
+
12
+ def test_should_have_a_content_type
13
+ assert_equal :data, @cell.content_type
14
+ end
15
+
8
16
  def test_should_have_a_class_name
9
17
  assert_equal 'name', @cell[:class]
10
18
  end
11
19
 
12
- def test_should_use_humanized_class_name_for_content
20
+ def test_should_build_html
13
21
  assert_equal '<td class="name">Name</td>', @cell.html
14
22
  end
23
+ end
24
+
25
+ class CellTest < Test::Unit::TestCase
26
+ def test_should_raise_exception_if_content_type_is_invalid
27
+ assert_raise(ArgumentError) {TableHelper::Cell.new(:name).content_type = :invalid}
28
+ end
29
+ end
30
+
31
+ class CellWithCustomContentTest < Test::Unit::TestCase
32
+ def setup
33
+ @cell = TableHelper::Cell.new(:name, 'John Smith')
34
+ end
15
35
 
16
- def test_should_create_a_data_cell
17
- assert_equal '<td class="name">Name</td>', @cell.html
36
+ def test_should_use_custom_content
37
+ assert_equal 'John Smith', @cell.content
18
38
  end
19
39
  end
20
40
 
21
- class CellTest < Test::Unit::TestCase
22
- def test_should_use_custom_content_if_specified
23
- cell = TableHelper::Cell.new(:name, 'John Doe')
24
- assert_equal '<td class="name">John Doe</td>', cell.html
41
+ class CellWithEmptyContentTest < Test::Unit::TestCase
42
+ def setup
43
+ @cell = TableHelper::Cell.new(:name, nil)
44
+ end
45
+
46
+ def test_should_include_empty_class
47
+ assert_equal 'name ui-state-empty', @cell[:class]
25
48
  end
26
49
 
27
- def test_should_include_custom_html_options
28
- cell = TableHelper::Cell.new(:name, 'John Doe', {:float => 'left'})
29
- assert_equal '<td class="name" float="left">John Doe</td>', cell.html
50
+ def test_should_use_custom_empty_class_if_specified
51
+ original_empty_class = TableHelper::Cell.empty_class
52
+ TableHelper::Cell.empty_class = 'ui-state-blank'
53
+
54
+ cell = TableHelper::Cell.new(:name, nil)
55
+ assert_equal 'name ui-state-blank', cell[:class]
56
+ ensure
57
+ TableHelper::Cell.empty_class = original_empty_class
58
+ end
59
+
60
+ def test_should_build_html
61
+ assert_equal '<td class="name ui-state-empty"></td>', @cell.html
62
+ end
63
+ end
64
+
65
+ class CellWithCustomHtmlOptionsTest < Test::Unit::TestCase
66
+ def setup
67
+ @cell = TableHelper::Cell.new(:name, :style => 'float: left;')
68
+ end
69
+
70
+ def test_should_include_custom_options
71
+ assert_equal 'float: left;', @cell[:style]
72
+ end
73
+
74
+ def test_should_build_html
75
+ assert_equal '<td class="name" style="float: left;">Name</td>', @cell.html
30
76
  end
31
77
 
32
78
  def test_should_append_automated_class_name_if_class_already_specified
33
- cell = 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
79
+ cell = TableHelper::Cell.new(:name, :class => 'selected')
80
+ assert_equal 'selected name', cell[:class]
81
+ assert_equal '<td class="selected name">Name</td>', cell.html
82
+ end
83
+ end
84
+
85
+ class CellWithCustomContentAndHtmlOptionsTest < Test::Unit::TestCase
86
+ def setup
87
+ @cell = TableHelper::Cell.new(:name, 'John Smith', :style => 'float: left;')
36
88
  end
37
89
 
38
- def test_should_raise_exception_if_content_type_is_invalid
39
- assert_raise(ArgumentError) {TableHelper::Cell.new(:name).content_type = :invalid}
90
+ def test_should_use_custom_content
91
+ assert_equal 'John Smith', @cell.content
92
+ end
93
+
94
+ def test_should_include_custom_options
95
+ assert_equal 'float: left;', @cell[:style]
96
+ end
97
+
98
+ def test_should_build_html
99
+ assert_equal '<td class="name" style="float: left;">John Smith</td>', @cell.html
100
+ end
101
+ end
102
+
103
+ class CellWithNamespaceTest < Test::Unit::TestCase
104
+ def setup
105
+ @cell = TableHelper::Cell.new(:name, :namespace => 'post')
106
+ end
107
+
108
+ def test_should_namespace_class
109
+ assert_equal 'post-name', @cell[:class]
40
110
  end
41
111
  end
42
112
 
@@ -46,7 +116,11 @@ class CellWithHeaderContentType < Test::Unit::TestCase
46
116
  @cell.content_type = :header
47
117
  end
48
118
 
49
- def test_should_a_header_cell
119
+ def test_should_have_a_content_type
120
+ assert_equal :header, @cell.content_type
121
+ end
122
+
123
+ def test_should_build_a_header_cell
50
124
  assert_equal '<th class="name">Name</th>', @cell.html
51
125
  end
52
126
  end