table_helper 0.0.3

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.
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class FooterByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @footer = PluginAWeek::TableHelper::Footer.new([])
6
+ end
7
+
8
+ def test_should_hdie_when_empty
9
+ assert @footer.hide_when_empty
10
+ end
11
+ end
12
+
13
+ class FooterTest < Test::Unit::TestCase
14
+ def setup
15
+ @footer = PluginAWeek::TableHelper::Footer.new([1])
16
+ end
17
+
18
+ def test_should_include_custom_attributes
19
+ @footer[:class] = 'pretty'
20
+
21
+ expected = <<-end_eval
22
+ <tfoot class="pretty">
23
+ <tr>
24
+ </tr>
25
+ </tfoot>
26
+ end_eval
27
+ assert_html_equal expected, @footer.html
28
+ end
29
+
30
+ def test_should_include_created_cells_when_built
31
+ @footer.cell :total, 20
32
+
33
+ expected = <<-end_eval
34
+ <tfoot>
35
+ <tr>
36
+ <td class="total">20</td>
37
+ </tr>
38
+ </tfoot>
39
+ end_eval
40
+ assert_html_equal expected, @footer.html
41
+ end
42
+ end
43
+
44
+ class FooterWithEmptyCollectionTest < Test::Unit::TestCase
45
+ def setup
46
+ @footer = PluginAWeek::TableHelper::Footer.new([])
47
+ end
48
+
49
+ def test_should_not_display_if_hide_when_empty
50
+ @footer.hide_when_empty = true
51
+
52
+ expected = <<-end_eval
53
+ <tfoot style="display: none;">
54
+ <tr>
55
+ </tr>
56
+ </tfoot>
57
+ end_eval
58
+ assert_html_equal expected, @footer.html
59
+ end
60
+
61
+ def test_should_display_if_not_hide_when_empty
62
+ @footer.hide_when_empty = false
63
+
64
+ expected = <<-end_eval
65
+ <tfoot>
66
+ <tr>
67
+ </tr>
68
+ </tfoot>
69
+ end_eval
70
+ assert_html_equal expected, @footer.html
71
+ end
72
+ end
@@ -0,0 +1,210 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class HeaderByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @header = PluginAWeek::TableHelper::Header.new([])
6
+ end
7
+
8
+ def test_should_hide_when_empty
9
+ assert @header.hide_when_empty
10
+ end
11
+
12
+ def test_should_have_no_columns
13
+ assert_equal [], @header.column_names
14
+ end
15
+ end
16
+
17
+ class HeaderTest < Test::Unit::TestCase
18
+ class Post
19
+ def self.column_names
20
+ ['title', 'author_name']
21
+ end
22
+ end
23
+
24
+ class Reflection
25
+ def klass
26
+ Post
27
+ end
28
+ end
29
+
30
+ class PostCollection < Array
31
+ def proxy_reflection
32
+ Reflection.new
33
+ end
34
+ end
35
+
36
+ def test_should_use_class_columns_if_class_has_column_names
37
+ header = PluginAWeek::TableHelper::Header.new([], Post)
38
+ assert_equal ['title', 'author_name'], header.column_names
39
+ end
40
+
41
+ def test_should_have_no_column_names_if_class_has_no_column_names
42
+ header = PluginAWeek::TableHelper::Header.new([], Array)
43
+ assert_equal [], header.column_names
44
+ end
45
+
46
+ def test_should_use_class_columns_if_collection_has_objects
47
+ header = PluginAWeek::TableHelper::Header.new([Post.new])
48
+ assert_equal ['title', 'author_name'], header.column_names
49
+ end
50
+
51
+ def test_should_use_class_columns_if_collection_is_proxy
52
+ header = PluginAWeek::TableHelper::Header.new(PostCollection.new)
53
+ assert_equal ['title', 'author_name'], header.column_names
54
+ end
55
+
56
+ def test_should_create_column_readers_if_column_names_found
57
+ header = PluginAWeek::TableHelper::Header.new([], Post)
58
+
59
+ assert header.respond_to?(:title)
60
+ assert_instance_of PluginAWeek::TableHelper::Cell, header.title
61
+
62
+ assert header.respond_to?(:author_name)
63
+ assert_instance_of PluginAWeek::TableHelper::Cell, header.author_name
64
+ end
65
+
66
+ def test_should_create_column_reader_when_column_is_created
67
+ header = PluginAWeek::TableHelper::Header.new([])
68
+ header.column :title
69
+
70
+ assert_equal ['title'], header.column_names
71
+ assert_instance_of PluginAWeek::TableHelper::Cell, header.title
72
+ end
73
+
74
+ def test_should_set_column_scope
75
+ header = PluginAWeek::TableHelper::Header.new([])
76
+ header.column :title
77
+ assert_equal 'col', header.title[:scope]
78
+ end
79
+
80
+ def test_should_allow_html_options_to_be_specified_for_new_columns
81
+ header = PluginAWeek::TableHelper::Header.new([])
82
+ header.column :title, 'Title', :class => 'pretty'
83
+ assert_equal 'title pretty', header.title[:class]
84
+ end
85
+
86
+ def test_should_use_column_name_for_default_content
87
+ header = PluginAWeek::TableHelper::Header.new([])
88
+ header.column :title
89
+ assert_equal '<th class="title" scope="col">Title</th>', header.title.html
90
+ end
91
+
92
+ def test_should_sanitize_column_names
93
+ header = PluginAWeek::TableHelper::Header.new([])
94
+ header.column 'the-title'
95
+
96
+ assert header.respond_to?(:the_title)
97
+ assert_instance_of PluginAWeek::TableHelper::Cell, header.the_title
98
+ end
99
+
100
+ def test_should_clear_existing_columns_when_first_column_is_created
101
+ header = PluginAWeek::TableHelper::Header.new([], Post)
102
+ assert_equal ['title', 'author_name'], header.column_names
103
+
104
+ header.column :created_on
105
+
106
+ assert !header.respond_to?(:title)
107
+ assert !header.respond_to?(:author_name)
108
+ assert_equal ['created_on'], header.column_names
109
+ end
110
+
111
+ def test_should_include_html_options
112
+ header = PluginAWeek::TableHelper::Header.new([Post.new])
113
+ header[:class] = 'pretty'
114
+
115
+ expected = <<-end_eval
116
+ <thead class="pretty">
117
+ <tr>
118
+ <th class="title" scope="col">Title</th>
119
+ <th class="author_name" scope="col">Author Name</th>
120
+ </tr>
121
+ </thead>
122
+ end_eval
123
+ assert_html_equal expected, header.html
124
+ end
125
+
126
+ def test_should_include_html_options_for_header_row
127
+ header = PluginAWeek::TableHelper::Header.new([Post.new])
128
+ header.row[:class] = 'pretty'
129
+
130
+ expected = <<-end_eval
131
+ <thead>
132
+ <tr class="pretty">
133
+ <th class="title" scope="col">Title</th>
134
+ <th class="author_name" scope="col">Author Name</th>
135
+ </tr>
136
+ </thead>
137
+ end_eval
138
+ assert_html_equal expected, header.html
139
+ end
140
+ end
141
+
142
+ class HeaderWithEmptyCollectionTest < Test::Unit::TestCase
143
+ def setup
144
+ @header = PluginAWeek::TableHelper::Header.new([])
145
+ end
146
+
147
+ def test_should_not_display_if_hide_when_empty
148
+ @header.hide_when_empty = true
149
+
150
+ expected = <<-end_eval
151
+ <thead style="display: none;">
152
+ <tr>
153
+ </tr>
154
+ </thead>
155
+ end_eval
156
+ assert_html_equal expected, @header.html
157
+ end
158
+
159
+ def test_should_display_if_not_hide_when_empty
160
+ @header.hide_when_empty = false
161
+
162
+ expected = <<-end_eval
163
+ <thead>
164
+ <tr>
165
+ </tr>
166
+ </thead>
167
+ end_eval
168
+ assert_html_equal expected, @header.html
169
+ end
170
+ end
171
+
172
+ class HeaderWithCollectionTest < Test::Unit::TestCase
173
+ class Post
174
+ def self.column_names
175
+ ['title', 'author_name']
176
+ end
177
+ end
178
+
179
+ def setup
180
+ @header = PluginAWeek::TableHelper::Header.new([Post.new])
181
+ end
182
+
183
+ def test_should_display_if_hide_when_empty
184
+ @header.hide_when_empty = true
185
+
186
+ expected = <<-end_eval
187
+ <thead>
188
+ <tr>
189
+ <th class="title" scope="col">Title</th>
190
+ <th class="author_name" scope="col">Author Name</th>
191
+ </tr>
192
+ </thead>
193
+ end_eval
194
+ assert_html_equal expected, @header.html
195
+ end
196
+
197
+ def test_should_display_if_not_hide_when_empty
198
+ @header.hide_when_empty = false
199
+
200
+ expected = <<-end_eval
201
+ <thead>
202
+ <tr>
203
+ <th class="title" scope="col">Title</th>
204
+ <th class="author_name" scope="col">Author Name</th>
205
+ </tr>
206
+ </thead>
207
+ end_eval
208
+ assert_html_equal expected, @header.html
209
+ end
210
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class HtmlElementByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @element = PluginAWeek::TableHelper::HtmlElement.new
6
+ end
7
+
8
+ def test_should_generate_an_empty_tag
9
+ assert_equal '<></>', @element.html
10
+ end
11
+
12
+ def test_not_have_any_html_options
13
+ element = PluginAWeek::TableHelper::HtmlElement.new
14
+ assert_nil element[:class]
15
+ end
16
+ end
17
+
18
+ class HtmlElementTest < Test::Unit::TestCase
19
+ class DivElement < PluginAWeek::TableHelper::HtmlElement
20
+ def tag_name
21
+ 'div'
22
+ end
23
+ end
24
+
25
+ def test_should_set_html_options_on_initialization
26
+ element = PluginAWeek::TableHelper::HtmlElement.new(:class => 'fancy')
27
+ assert_equal 'fancy', element[:class]
28
+ end
29
+
30
+ def test_should_symbolize_html_options
31
+ element = PluginAWeek::TableHelper::HtmlElement.new('class' => 'fancy')
32
+ assert_equal 'fancy', element[:class]
33
+ end
34
+
35
+ def test_should_generate_entire_element_if_content_and_tag_name_specified
36
+ element = DivElement.new
37
+ element.instance_eval do
38
+ def content
39
+ 'hello world'
40
+ end
41
+ end
42
+
43
+ assert_equal '<div>hello world</div>', element.html
44
+ end
45
+
46
+ def test_should_include_html_options_in_element_tag
47
+ element = DivElement.new
48
+ element[:class] = 'fancy'
49
+
50
+ assert_equal '<div class="fancy"></div>', element.html
51
+ end
52
+
53
+ def test_should_save_changes_in_html_options
54
+ element = PluginAWeek::TableHelper::HtmlElement.new
55
+ element[:float] = 'left'
56
+ assert_equal 'left', element[:float]
57
+ end
58
+ end
59
+
60
+ class HtmlElementWithNoContentTest < Test::Unit::TestCase
61
+ class DivElement < PluginAWeek::TableHelper::HtmlElement
62
+ def tag_name
63
+ 'div'
64
+ end
65
+ end
66
+
67
+ def setup
68
+ @element = DivElement.new
69
+ end
70
+
71
+ def test_should_generate_empty_tags
72
+ assert_equal '<div></div>', @element.html
73
+ end
74
+ end
data/test/row_test.rb ADDED
@@ -0,0 +1,115 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class RowByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @row = PluginAWeek::TableHelper::Row.new
6
+ end
7
+
8
+ def test_should_not_set_any_html_options
9
+ assert_nil @row[:class]
10
+ end
11
+
12
+ def test_should_not_have_any_cells
13
+ assert_equal [], @row.cell_names
14
+ end
15
+ end
16
+
17
+
18
+ class RowTest < Test::Unit::TestCase
19
+ def setup
20
+ @row = PluginAWeek::TableHelper::Row.new
21
+ end
22
+
23
+ def test_should_create_reader_after_building_cell
24
+ @row.cell :name
25
+ assert @row.respond_to?(:name)
26
+ assert_instance_of PluginAWeek::TableHelper::Cell, @row.name
27
+ end
28
+
29
+ def test_should_use_cell_name_for_class_name
30
+ @row.cell :name
31
+ assert_equal 'name', @row.name[:class]
32
+ end
33
+
34
+ def test_should_sanitize_cell_name_for_reader_method
35
+ @row.cell 'the-name'
36
+
37
+ assert @row.respond_to?(:the_name)
38
+ assert_instance_of PluginAWeek::TableHelper::Cell, @row.the_name
39
+ end
40
+
41
+ def test_should_use_unsanitized_cell_name_for_cell_class
42
+ @row.cell 'the-name'
43
+ assert_equal ['the-name'], @row.cell_names
44
+ assert_equal 'the-name', @row.the_name[:class]
45
+ end
46
+
47
+ def test_should_allow_html_options_when_building_cell
48
+ @row.cell :name, 'Name', :class => 'pretty'
49
+
50
+ assert_equal 'name pretty', @row.name[:class]
51
+ end
52
+ end
53
+
54
+ class RowWithoutCellsTest < Test::Unit::TestCase
55
+ def setup
56
+ @row = PluginAWeek::TableHelper::Row.new
57
+ end
58
+
59
+ def test_should_be_able_to_clear_cells
60
+ @row.clear
61
+ assert_equal [], @row.cell_names
62
+ end
63
+
64
+ def test_should_build_html
65
+ assert_equal '<tr></tr>', @row.html
66
+ end
67
+ end
68
+
69
+ class RowWithCellsTest < Test::Unit::TestCase
70
+ def setup
71
+ @row = PluginAWeek::TableHelper::Row.new
72
+ @row.cell :name
73
+ end
74
+
75
+ def test_should_have_cell_names
76
+ assert_equal ['name'], @row.cell_names
77
+ end
78
+
79
+ def test_should_be_able_to_clear_existing_cells
80
+ @row.clear
81
+
82
+ assert_equal [], @row.cell_names
83
+ end
84
+
85
+ def test_should_build_html
86
+ assert_equal '<tr><td class="name">Name</td></tr>', @row.html
87
+ end
88
+
89
+ def test_should_create_new_cell_if_cell_already_exists
90
+ old_cell = @row.name
91
+
92
+ @row.cell :name
93
+ assert_not_same old_cell, @row.name
94
+ end
95
+
96
+ def test_should_be_able_to_read_cell_after_clearing
97
+ @row.clear
98
+ @row.cell :name
99
+
100
+ assert_equal ['name'], @row.cell_names
101
+ assert @row.respond_to?(:name)
102
+ end
103
+ end
104
+
105
+ class RowWithMultipleCellsTest < Test::Unit::TestCase
106
+ def setup
107
+ @row = PluginAWeek::TableHelper::Row.new
108
+ @row.cell :name
109
+ @row.cell :location
110
+ end
111
+
112
+ def test_should_build_html
113
+ assert_equal '<tr><td class="name">Name</td><td class="location">Location</td></tr>', @row.html
114
+ end
115
+ end