ghazel-erubis_rails_helper 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/README.rdoc +66 -0
  2. data/lib/erubis_rails_helper/erubis/generator.rb +100 -0
  3. data/lib/erubis_rails_helper/template_handlers/erubis.rb +260 -0
  4. data/lib/erubis_rails_helper.rb +20 -0
  5. data/test/abstract_unit.rb +51 -0
  6. data/test/controller/action_pack_assertions_test.rb +516 -0
  7. data/test/controller/addresses_render_test.rb +37 -0
  8. data/test/controller/assert_select_test.rb +734 -0
  9. data/test/controller/base_test.rb +217 -0
  10. data/test/controller/benchmark_test.rb +32 -0
  11. data/test/controller/caching_test.rb +690 -0
  12. data/test/controller/capture_test.rb +66 -0
  13. data/test/controller/content_type_test.rb +168 -0
  14. data/test/controller/controller_fixtures/app/controllers/admin/user_controller.rb +0 -0
  15. data/test/controller/controller_fixtures/app/controllers/user_controller.rb +0 -0
  16. data/test/controller/controller_fixtures/vendor/plugins/bad_plugin/lib/plugin_controller.rb +0 -0
  17. data/test/controller/cookie_test.rb +111 -0
  18. data/test/controller/deprecation/deprecated_base_methods_test.rb +32 -0
  19. data/test/controller/dispatcher_test.rb +103 -0
  20. data/test/controller/fake_controllers.rb +33 -0
  21. data/test/controller/fake_models.rb +19 -0
  22. data/test/controller/filter_params_test.rb +49 -0
  23. data/test/controller/filters_test.rb +885 -0
  24. data/test/controller/flash_test.rb +142 -0
  25. data/test/controller/header_test.rb +14 -0
  26. data/test/controller/helper_test.rb +224 -0
  27. data/test/controller/html-scanner/cdata_node_test.rb +15 -0
  28. data/test/controller/html-scanner/document_test.rb +148 -0
  29. data/test/controller/html-scanner/node_test.rb +89 -0
  30. data/test/controller/html-scanner/sanitizer_test.rb +273 -0
  31. data/test/controller/html-scanner/tag_node_test.rb +238 -0
  32. data/test/controller/html-scanner/text_node_test.rb +50 -0
  33. data/test/controller/html-scanner/tokenizer_test.rb +131 -0
  34. data/test/controller/http_basic_authentication_test.rb +88 -0
  35. data/test/controller/http_digest_authentication_test.rb +178 -0
  36. data/test/controller/integration_test.rb +417 -0
  37. data/test/controller/layout_test.rb +204 -0
  38. data/test/controller/logging_test.rb +46 -0
  39. data/test/controller/middleware_stack_test.rb +90 -0
  40. data/test/controller/mime_responds_test.rb +536 -0
  41. data/test/controller/mime_type_test.rb +93 -0
  42. data/test/controller/polymorphic_routes_test.rb +293 -0
  43. data/test/controller/rack_test.rb +294 -0
  44. data/test/controller/record_identifier_test.rb +139 -0
  45. data/test/controller/redirect_test.rb +282 -0
  46. data/test/controller/render_test.rb +1752 -0
  47. data/test/controller/request/json_params_parsing_test.rb +45 -0
  48. data/test/controller/request/multipart_params_parsing_test.rb +223 -0
  49. data/test/controller/request/query_string_parsing_test.rb +120 -0
  50. data/test/controller/request/url_encoded_params_parsing_test.rb +184 -0
  51. data/test/controller/request/xml_params_parsing_test.rb +88 -0
  52. data/test/controller/request_forgery_protection_test.rb +246 -0
  53. data/test/controller/request_test.rb +407 -0
  54. data/test/controller/rescue_test.rb +536 -0
  55. data/test/controller/resources_test.rb +1341 -0
  56. data/test/controller/routing_test.rb +2557 -0
  57. data/test/controller/selector_test.rb +628 -0
  58. data/test/controller/send_file_test.rb +161 -0
  59. data/test/controller/session/cookie_store_test.rb +239 -0
  60. data/test/controller/session/mem_cache_store_test.rb +127 -0
  61. data/test/controller/session/test_session_test.rb +58 -0
  62. data/test/controller/test_test.rb +692 -0
  63. data/test/controller/translation_test.rb +26 -0
  64. data/test/controller/url_rewriter_test.rb +359 -0
  65. data/test/controller/verification_test.rb +270 -0
  66. data/test/controller/view_paths_test.rb +141 -0
  67. data/test/controller/webservice_test.rb +260 -0
  68. data/test/fixtures/alternate_helpers/foo_helper.rb +3 -0
  69. data/test/fixtures/company.rb +10 -0
  70. data/test/fixtures/developer.rb +9 -0
  71. data/test/fixtures/helpers/abc_helper.rb +5 -0
  72. data/test/fixtures/helpers/fun/games_helper.rb +3 -0
  73. data/test/fixtures/helpers/fun/pdf_helper.rb +3 -0
  74. data/test/fixtures/mascot.rb +3 -0
  75. data/test/fixtures/project.rb +3 -0
  76. data/test/fixtures/reply.rb +7 -0
  77. data/test/fixtures/topic.rb +3 -0
  78. data/test/template/asset_tag_helper_test.rb +713 -0
  79. data/test/template/atom_feed_helper_test.rb +286 -0
  80. data/test/template/benchmark_helper_test.rb +86 -0
  81. data/test/template/compiled_templates_test.rb +203 -0
  82. data/test/template/date_helper_i18n_test.rb +120 -0
  83. data/test/template/date_helper_test.rb +2469 -0
  84. data/test/template/erb_util_test.rb +24 -0
  85. data/test/template/form_helper_test.rb +1176 -0
  86. data/test/template/form_options_helper_test.rb +751 -0
  87. data/test/template/form_tag_helper_test.rb +327 -0
  88. data/test/template/javascript_helper_test.rb +106 -0
  89. data/test/template/number_helper_i18n_test.rb +69 -0
  90. data/test/template/number_helper_test.rb +128 -0
  91. data/test/template/prototype_helper_test.rb +639 -0
  92. data/test/template/record_tag_helper_test.rb +58 -0
  93. data/test/template/render_test.rb +291 -0
  94. data/test/template/sanitize_helper_test.rb +48 -0
  95. data/test/template/scriptaculous_helper_test.rb +90 -0
  96. data/test/template/tag_helper_test.rb +97 -0
  97. data/test/template/test_test.rb +54 -0
  98. data/test/template/text_helper_test.rb +520 -0
  99. data/test/template/translation_helper_test.rb +32 -0
  100. data/test/template/url_helper_test.rb +614 -0
  101. data/test/test_erubis_rails_helper.rb +4 -0
  102. data/test/testing_sandbox.rb +15 -0
  103. data/test/view/test_case_test.rb +8 -0
  104. metadata +174 -0
@@ -0,0 +1,751 @@
1
+ require 'abstract_unit'
2
+ require 'tzinfo'
3
+
4
+ TZInfo::Timezone.cattr_reader :loaded_zones
5
+
6
+ class FormOptionsHelperTest < ActionView::TestCase
7
+ tests ActionView::Helpers::FormOptionsHelper
8
+
9
+ silence_warnings do
10
+ Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin)
11
+ Continent = Struct.new('Continent', :continent_name, :countries)
12
+ Country = Struct.new('Country', :country_id, :country_name)
13
+ Firm = Struct.new('Firm', :time_zone)
14
+ Album = Struct.new('Album', :id, :title, :genre)
15
+ end
16
+
17
+ def setup
18
+ @fake_timezones = %w(A B C D E).inject([]) do |zones, id|
19
+ tz = TZInfo::Timezone.loaded_zones[id] = stub(:name => id, :to_s => id)
20
+ ActiveSupport::TimeZone.stubs(:[]).with(id).returns(tz)
21
+ zones << tz
22
+ end
23
+ ActiveSupport::TimeZone.stubs(:all).returns(@fake_timezones)
24
+ end
25
+
26
+ def test_collection_options
27
+ assert_dom_equal(
28
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
29
+ options_from_collection_for_select(dummy_posts, "author_name", "title")
30
+ )
31
+ end
32
+
33
+
34
+ def test_collection_options_with_preselected_value
35
+ assert_dom_equal(
36
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
37
+ options_from_collection_for_select(dummy_posts, "author_name", "title", "Babe")
38
+ )
39
+ end
40
+
41
+ def test_collection_options_with_preselected_value_array
42
+ assert_dom_equal(
43
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\" selected=\"selected\">Cabe went home</option>",
44
+ options_from_collection_for_select(dummy_posts, "author_name", "title", [ "Babe", "Cabe" ])
45
+ )
46
+ end
47
+
48
+ def test_collection_options_with_proc_for_selected
49
+ assert_dom_equal(
50
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
51
+ options_from_collection_for_select(dummy_posts, "author_name", "title", lambda{|p| p.author_name == 'Babe' })
52
+ )
53
+ end
54
+
55
+ def test_collection_options_with_disabled_value
56
+ assert_dom_equal(
57
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" disabled=\"disabled\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>",
58
+ options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => "Babe")
59
+ )
60
+ end
61
+
62
+ def test_collection_options_with_disabled_array
63
+ assert_dom_equal(
64
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" disabled=\"disabled\">Babe went home</option>\n<option value=\"Cabe\" disabled=\"disabled\">Cabe went home</option>",
65
+ options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => [ "Babe", "Cabe" ])
66
+ )
67
+ end
68
+
69
+ def test_collection_options_with_preselected_and_disabled_value
70
+ assert_dom_equal(
71
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" disabled=\"disabled\">Babe went home</option>\n<option value=\"Cabe\" selected=\"selected\">Cabe went home</option>",
72
+ options_from_collection_for_select(dummy_posts, "author_name", "title", :selected => "Cabe", :disabled => "Babe")
73
+ )
74
+ end
75
+
76
+ def test_collection_options_with_proc_for_disabled
77
+ assert_dom_equal(
78
+ "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\" disabled=\"disabled\">Babe went home</option>\n<option value=\"Cabe\" disabled=\"disabled\">Cabe went home</option>",
79
+ options_from_collection_for_select(dummy_posts, "author_name", "title", :disabled => lambda{|p| %w(Babe Cabe).include? p.author_name })
80
+ )
81
+ end
82
+
83
+ def test_array_options_for_select
84
+ assert_dom_equal(
85
+ "<option value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>\n<option value=\"USA\">USA</option>\n<option value=\"Sweden\">Sweden</option>",
86
+ options_for_select([ "<Denmark>", "USA", "Sweden" ])
87
+ )
88
+ end
89
+
90
+ def test_array_options_for_select_with_selection
91
+ assert_dom_equal(
92
+ "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" selected=\"selected\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>",
93
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], "<USA>")
94
+ )
95
+ end
96
+
97
+ def test_array_options_for_select_with_selection_array
98
+ assert_dom_equal(
99
+ "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" selected=\"selected\">&lt;USA&gt;</option>\n<option value=\"Sweden\" selected=\"selected\">Sweden</option>",
100
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], [ "<USA>", "Sweden" ])
101
+ )
102
+ end
103
+
104
+ def test_array_options_for_select_with_disabled_value
105
+ assert_dom_equal(
106
+ "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" disabled=\"disabled\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>",
107
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], :disabled => "<USA>")
108
+ )
109
+ end
110
+
111
+ def test_array_options_for_select_with_disabled_array
112
+ assert_dom_equal(
113
+ "<option value=\"Denmark\">Denmark</option>\n<option value=\"&lt;USA&gt;\" disabled=\"disabled\">&lt;USA&gt;</option>\n<option value=\"Sweden\" disabled=\"disabled\">Sweden</option>",
114
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], :disabled => ["<USA>", "Sweden"])
115
+ )
116
+ end
117
+
118
+ def test_array_options_for_select_with_selection_and_disabled_value
119
+ assert_dom_equal(
120
+ "<option value=\"Denmark\" selected=\"selected\">Denmark</option>\n<option value=\"&lt;USA&gt;\" disabled=\"disabled\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>",
121
+ options_for_select([ "Denmark", "<USA>", "Sweden" ], :selected => "Denmark", :disabled => "<USA>")
122
+ )
123
+ end
124
+
125
+ def test_array_options_for_string_include_in_other_string_bug_fix
126
+ assert_dom_equal(
127
+ "<option value=\"ruby\">ruby</option>\n<option value=\"rubyonrails\" selected=\"selected\">rubyonrails</option>",
128
+ options_for_select([ "ruby", "rubyonrails" ], "rubyonrails")
129
+ )
130
+ assert_dom_equal(
131
+ "<option value=\"ruby\" selected=\"selected\">ruby</option>\n<option value=\"rubyonrails\">rubyonrails</option>",
132
+ options_for_select([ "ruby", "rubyonrails" ], "ruby")
133
+ )
134
+ assert_dom_equal(
135
+ %(<option value="ruby" selected="selected">ruby</option>\n<option value="rubyonrails">rubyonrails</option>\n<option value=""></option>),
136
+ options_for_select([ "ruby", "rubyonrails", nil ], "ruby")
137
+ )
138
+ end
139
+
140
+ def test_hash_options_for_select
141
+ assert_dom_equal(
142
+ "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\">$</option>",
143
+ options_for_select("$" => "Dollar", "<DKR>" => "<Kroner>").split("\n").sort.join("\n")
144
+ )
145
+ assert_dom_equal(
146
+ "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
147
+ options_for_select({ "$" => "Dollar", "<DKR>" => "<Kroner>" }, "Dollar").split("\n").sort.join("\n")
148
+ )
149
+ assert_dom_equal(
150
+ "<option value=\"&lt;Kroner&gt;\" selected=\"selected\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
151
+ options_for_select({ "$" => "Dollar", "<DKR>" => "<Kroner>" }, [ "Dollar", "<Kroner>" ]).split("\n").sort.join("\n")
152
+ )
153
+ end
154
+
155
+ def test_ducktyped_options_for_select
156
+ quack = Struct.new(:first, :last)
157
+ assert_dom_equal(
158
+ "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\">$</option>",
159
+ options_for_select([quack.new("<DKR>", "<Kroner>"), quack.new("$", "Dollar")])
160
+ )
161
+ assert_dom_equal(
162
+ "<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
163
+ options_for_select([quack.new("<DKR>", "<Kroner>"), quack.new("$", "Dollar")], "Dollar")
164
+ )
165
+ assert_dom_equal(
166
+ "<option value=\"&lt;Kroner&gt;\" selected=\"selected\">&lt;DKR&gt;</option>\n<option value=\"Dollar\" selected=\"selected\">$</option>",
167
+ options_for_select([quack.new("<DKR>", "<Kroner>"), quack.new("$", "Dollar")], ["Dollar", "<Kroner>"])
168
+ )
169
+ end
170
+
171
+ def test_option_groups_from_collection_for_select
172
+ @continents = [
173
+ Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")] ),
174
+ Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] )
175
+ ]
176
+
177
+ assert_dom_equal(
178
+ "<optgroup label=\"&lt;Africa&gt;\"><option value=\"&lt;sa&gt;\">&lt;South Africa&gt;</option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option value=\"dk\" selected=\"selected\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>",
179
+ option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk")
180
+ )
181
+ end
182
+
183
+ def test_grouped_options_for_select_with_array
184
+ assert_dom_equal(
185
+ "<optgroup label=\"North America\"><option value=\"US\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup><optgroup label=\"Europe\"><option value=\"GB\">Great Britain</option>\n<option value=\"Germany\">Germany</option></optgroup>",
186
+ grouped_options_for_select([
187
+ ["North America",
188
+ [['United States','US'],"Canada"]],
189
+ ["Europe",
190
+ [["Great Britain","GB"], "Germany"]]
191
+ ])
192
+ )
193
+ end
194
+
195
+ def test_grouped_options_for_select_with_selected_and_prompt
196
+ assert_dom_equal(
197
+ "<option value=\"\">Choose a product...</option><optgroup label=\"Hats\"><option value=\"Baseball Cap\">Baseball Cap</option>\n<option selected=\"selected\" value=\"Cowboy Hat\">Cowboy Hat</option></optgroup>",
198
+ grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", "Choose a product...")
199
+ )
200
+ end
201
+
202
+ def test_optgroups_with_with_options_with_hash
203
+ assert_dom_equal(
204
+ "<optgroup label=\"Europe\"><option value=\"Denmark\">Denmark</option>\n<option value=\"Germany\">Germany</option></optgroup><optgroup label=\"North America\"><option value=\"United States\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup>",
205
+ grouped_options_for_select({'North America' => ['United States','Canada'], 'Europe' => ['Denmark','Germany']})
206
+ )
207
+ end
208
+
209
+ def test_time_zone_options_no_parms
210
+ opts = time_zone_options_for_select
211
+ assert_dom_equal "<option value=\"A\">A</option>\n" +
212
+ "<option value=\"B\">B</option>\n" +
213
+ "<option value=\"C\">C</option>\n" +
214
+ "<option value=\"D\">D</option>\n" +
215
+ "<option value=\"E\">E</option>",
216
+ opts
217
+ end
218
+
219
+ def test_time_zone_options_with_selected
220
+ opts = time_zone_options_for_select( "D" )
221
+ assert_dom_equal "<option value=\"A\">A</option>\n" +
222
+ "<option value=\"B\">B</option>\n" +
223
+ "<option value=\"C\">C</option>\n" +
224
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
225
+ "<option value=\"E\">E</option>",
226
+ opts
227
+ end
228
+
229
+ def test_time_zone_options_with_unknown_selected
230
+ opts = time_zone_options_for_select( "K" )
231
+ assert_dom_equal "<option value=\"A\">A</option>\n" +
232
+ "<option value=\"B\">B</option>\n" +
233
+ "<option value=\"C\">C</option>\n" +
234
+ "<option value=\"D\">D</option>\n" +
235
+ "<option value=\"E\">E</option>",
236
+ opts
237
+ end
238
+
239
+ def test_time_zone_options_with_priority_zones
240
+ zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ]
241
+ opts = time_zone_options_for_select( nil, zones )
242
+ assert_dom_equal "<option value=\"B\">B</option>\n" +
243
+ "<option value=\"E\">E</option>" +
244
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
245
+ "<option value=\"A\">A</option>\n" +
246
+ "<option value=\"C\">C</option>\n" +
247
+ "<option value=\"D\">D</option>",
248
+ opts
249
+ end
250
+
251
+ def test_time_zone_options_with_selected_priority_zones
252
+ zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ]
253
+ opts = time_zone_options_for_select( "E", zones )
254
+ assert_dom_equal "<option value=\"B\">B</option>\n" +
255
+ "<option value=\"E\" selected=\"selected\">E</option>" +
256
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
257
+ "<option value=\"A\">A</option>\n" +
258
+ "<option value=\"C\">C</option>\n" +
259
+ "<option value=\"D\">D</option>",
260
+ opts
261
+ end
262
+
263
+ def test_time_zone_options_with_unselected_priority_zones
264
+ zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ]
265
+ opts = time_zone_options_for_select( "C", zones )
266
+ assert_dom_equal "<option value=\"B\">B</option>\n" +
267
+ "<option value=\"E\">E</option>" +
268
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
269
+ "<option value=\"A\">A</option>\n" +
270
+ "<option value=\"C\" selected=\"selected\">C</option>\n" +
271
+ "<option value=\"D\">D</option>",
272
+ opts
273
+ end
274
+
275
+ def test_select
276
+ @post = Post.new
277
+ @post.category = "<mus>"
278
+ assert_dom_equal(
279
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
280
+ select("post", "category", %w( abe <mus> hest))
281
+ )
282
+ end
283
+
284
+ def test_select_under_fields_for
285
+ @post = Post.new
286
+ @post.category = "<mus>"
287
+
288
+ fields_for :post, @post do |f|
289
+ concat f.select(:category, %w( abe <mus> hest))
290
+ end
291
+
292
+ assert_dom_equal(
293
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
294
+ output_buffer
295
+ )
296
+ end
297
+
298
+ def test_select_under_fields_for_with_index
299
+ @post = Post.new
300
+ @post.category = "<mus>"
301
+
302
+ fields_for :post, @post, :index => 108 do |f|
303
+ concat f.select(:category, %w( abe <mus> hest))
304
+ end
305
+
306
+ assert_dom_equal(
307
+ "<select id=\"post_108_category\" name=\"post[108][category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
308
+ output_buffer
309
+ )
310
+ end
311
+
312
+ def test_select_under_fields_for_with_auto_index
313
+ @post = Post.new
314
+ @post.category = "<mus>"
315
+ def @post.to_param; 108; end
316
+
317
+ fields_for "post[]", @post do |f|
318
+ concat f.select(:category, %w( abe <mus> hest))
319
+ end
320
+
321
+ assert_dom_equal(
322
+ "<select id=\"post_108_category\" name=\"post[108][category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
323
+ output_buffer
324
+ )
325
+ end
326
+
327
+ def test_select_with_blank
328
+ @post = Post.new
329
+ @post.category = "<mus>"
330
+ assert_dom_equal(
331
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
332
+ select("post", "category", %w( abe <mus> hest), :include_blank => true)
333
+ )
334
+ end
335
+
336
+ def test_select_with_blank_as_string
337
+ @post = Post.new
338
+ @post.category = "<mus>"
339
+ assert_dom_equal(
340
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">None</option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
341
+ select("post", "category", %w( abe <mus> hest), :include_blank => 'None')
342
+ )
343
+ end
344
+
345
+ def test_select_with_default_prompt
346
+ @post = Post.new
347
+ @post.category = ""
348
+ assert_dom_equal(
349
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
350
+ select("post", "category", %w( abe <mus> hest), :prompt => true)
351
+ )
352
+ end
353
+
354
+ def test_select_no_prompt_when_select_has_value
355
+ @post = Post.new
356
+ @post.category = "<mus>"
357
+ assert_dom_equal(
358
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
359
+ select("post", "category", %w( abe <mus> hest), :prompt => true)
360
+ )
361
+ end
362
+
363
+ def test_select_with_given_prompt
364
+ @post = Post.new
365
+ @post.category = ""
366
+ assert_dom_equal(
367
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">The prompt</option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
368
+ select("post", "category", %w( abe <mus> hest), :prompt => 'The prompt')
369
+ )
370
+ end
371
+
372
+ def test_select_with_prompt_and_blank
373
+ @post = Post.new
374
+ @post.category = ""
375
+ assert_dom_equal(
376
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
377
+ select("post", "category", %w( abe <mus> hest), :prompt => true, :include_blank => true)
378
+ )
379
+ end
380
+
381
+ def test_select_with_selected_value
382
+ @post = Post.new
383
+ @post.category = "<mus>"
384
+ assert_dom_equal(
385
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\" selected=\"selected\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
386
+ select("post", "category", %w( abe <mus> hest ), :selected => 'abe')
387
+ )
388
+ end
389
+
390
+ def test_select_with_index_option
391
+ @album = Album.new
392
+ @album.id = 1
393
+
394
+ expected = "<select id=\"album__genre\" name=\"album[][genre]\"><option value=\"rap\">rap</option>\n<option value=\"rock\">rock</option>\n<option value=\"country\">country</option></select>"
395
+
396
+ assert_dom_equal(
397
+ expected,
398
+ select("album[]", "genre", %w[rap rock country], {}, { :index => nil })
399
+ )
400
+ end
401
+
402
+ def test_select_with_selected_nil
403
+ @post = Post.new
404
+ @post.category = "<mus>"
405
+ assert_dom_equal(
406
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
407
+ select("post", "category", %w( abe <mus> hest ), :selected => nil)
408
+ )
409
+ end
410
+
411
+ def test_select_with_disabled_value
412
+ @post = Post.new
413
+ @post.category = "<mus>"
414
+ assert_dom_equal(
415
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\" disabled=\"disabled\">hest</option></select>",
416
+ select("post", "category", %w( abe <mus> hest ), :disabled => 'hest')
417
+ )
418
+ end
419
+
420
+ def test_select_with_disabled_array
421
+ @post = Post.new
422
+ @post.category = "<mus>"
423
+ assert_dom_equal(
424
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"abe\" disabled=\"disabled\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\" disabled=\"disabled\">hest</option></select>",
425
+ select("post", "category", %w( abe <mus> hest ), :disabled => ['hest', 'abe'])
426
+ )
427
+ end
428
+
429
+ def test_collection_select
430
+ @post = Post.new
431
+ @post.author_name = "Babe"
432
+
433
+ assert_dom_equal(
434
+ "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
435
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name")
436
+ )
437
+ end
438
+
439
+ def test_collection_select_under_fields_for
440
+ @post = Post.new
441
+ @post.author_name = "Babe"
442
+
443
+ fields_for :post, @post do |f|
444
+ concat f.collection_select(:author_name, dummy_posts, :author_name, :author_name)
445
+ end
446
+
447
+ assert_dom_equal(
448
+ "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
449
+ output_buffer
450
+ )
451
+ end
452
+
453
+ def test_collection_select_under_fields_for_with_index
454
+ @post = Post.new
455
+ @post.author_name = "Babe"
456
+
457
+ fields_for :post, @post, :index => 815 do |f|
458
+ concat f.collection_select(:author_name, dummy_posts, :author_name, :author_name)
459
+ end
460
+
461
+ assert_dom_equal(
462
+ "<select id=\"post_815_author_name\" name=\"post[815][author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
463
+ output_buffer
464
+ )
465
+ end
466
+
467
+ def test_collection_select_under_fields_for_with_auto_index
468
+ @post = Post.new
469
+ @post.author_name = "Babe"
470
+ def @post.to_param; 815; end
471
+
472
+ fields_for "post[]", @post do |f|
473
+ concat f.collection_select(:author_name, dummy_posts, :author_name, :author_name)
474
+ end
475
+
476
+ assert_dom_equal(
477
+ "<select id=\"post_815_author_name\" name=\"post[815][author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
478
+ output_buffer
479
+ )
480
+ end
481
+
482
+ def test_collection_select_with_blank_and_style
483
+ @post = Post.new
484
+ @post.author_name = "Babe"
485
+
486
+ assert_dom_equal(
487
+ "<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option value=\"\"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
488
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { :include_blank => true }, "style" => "width: 200px")
489
+ )
490
+ end
491
+
492
+ def test_collection_select_with_blank_as_string_and_style
493
+ @post = Post.new
494
+ @post.author_name = "Babe"
495
+
496
+ assert_dom_equal(
497
+ "<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option value=\"\">No Selection</option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
498
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { :include_blank => 'No Selection' }, "style" => "width: 200px")
499
+ )
500
+ end
501
+
502
+ def test_collection_select_with_multiple_option_appends_array_brackets
503
+ @post = Post.new
504
+ @post.author_name = "Babe"
505
+
506
+ expected = "<select id=\"post_author_name\" name=\"post[author_name][]\" multiple=\"multiple\"><option value=\"\"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>"
507
+
508
+ # Should suffix default name with [].
509
+ assert_dom_equal expected, collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { :include_blank => true }, :multiple => true)
510
+
511
+ # Shouldn't suffix custom name with [].
512
+ assert_dom_equal expected, collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { :include_blank => true, :name => 'post[author_name][]' }, :multiple => true)
513
+ end
514
+
515
+ def test_collection_select_with_blank_and_selected
516
+ @post = Post.new
517
+ @post.author_name = "Babe"
518
+
519
+ assert_dom_equal(
520
+ %{<select id="post_author_name" name="post[author_name]"><option value=""></option>\n<option value="&lt;Abe&gt;" selected="selected">&lt;Abe&gt;</option>\n<option value="Babe">Babe</option>\n<option value="Cabe">Cabe</option></select>},
521
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name", {:include_blank => true, :selected => "<Abe>"})
522
+ )
523
+ end
524
+
525
+ def test_collection_select_with_disabled
526
+ @post = Post.new
527
+ @post.author_name = "Babe"
528
+
529
+ assert_dom_equal(
530
+ "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\" disabled=\"disabled\">Cabe</option></select>",
531
+ collection_select("post", "author_name", dummy_posts, "author_name", "author_name", :disabled => 'Cabe')
532
+ )
533
+ end
534
+
535
+ def test_time_zone_select
536
+ @firm = Firm.new("D")
537
+ html = time_zone_select( "firm", "time_zone" )
538
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
539
+ "<option value=\"A\">A</option>\n" +
540
+ "<option value=\"B\">B</option>\n" +
541
+ "<option value=\"C\">C</option>\n" +
542
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
543
+ "<option value=\"E\">E</option>" +
544
+ "</select>",
545
+ html
546
+ end
547
+
548
+ def test_time_zone_select_under_fields_for
549
+ @firm = Firm.new("D")
550
+
551
+ fields_for :firm, @firm do |f|
552
+ concat f.time_zone_select(:time_zone)
553
+ end
554
+
555
+ assert_dom_equal(
556
+ "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
557
+ "<option value=\"A\">A</option>\n" +
558
+ "<option value=\"B\">B</option>\n" +
559
+ "<option value=\"C\">C</option>\n" +
560
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
561
+ "<option value=\"E\">E</option>" +
562
+ "</select>",
563
+ output_buffer
564
+ )
565
+ end
566
+
567
+ def test_time_zone_select_under_fields_for_with_index
568
+ @firm = Firm.new("D")
569
+
570
+ fields_for :firm, @firm, :index => 305 do |f|
571
+ concat f.time_zone_select(:time_zone)
572
+ end
573
+
574
+ assert_dom_equal(
575
+ "<select id=\"firm_305_time_zone\" name=\"firm[305][time_zone]\">" +
576
+ "<option value=\"A\">A</option>\n" +
577
+ "<option value=\"B\">B</option>\n" +
578
+ "<option value=\"C\">C</option>\n" +
579
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
580
+ "<option value=\"E\">E</option>" +
581
+ "</select>",
582
+ output_buffer
583
+ )
584
+ end
585
+
586
+ def test_time_zone_select_under_fields_for_with_auto_index
587
+ @firm = Firm.new("D")
588
+ def @firm.to_param; 305; end
589
+
590
+ fields_for "firm[]", @firm do |f|
591
+ concat f.time_zone_select(:time_zone)
592
+ end
593
+
594
+ assert_dom_equal(
595
+ "<select id=\"firm_305_time_zone\" name=\"firm[305][time_zone]\">" +
596
+ "<option value=\"A\">A</option>\n" +
597
+ "<option value=\"B\">B</option>\n" +
598
+ "<option value=\"C\">C</option>\n" +
599
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
600
+ "<option value=\"E\">E</option>" +
601
+ "</select>",
602
+ output_buffer
603
+ )
604
+ end
605
+
606
+ def test_time_zone_select_with_blank
607
+ @firm = Firm.new("D")
608
+ html = time_zone_select("firm", "time_zone", nil, :include_blank => true)
609
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
610
+ "<option value=\"\"></option>\n" +
611
+ "<option value=\"A\">A</option>\n" +
612
+ "<option value=\"B\">B</option>\n" +
613
+ "<option value=\"C\">C</option>\n" +
614
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
615
+ "<option value=\"E\">E</option>" +
616
+ "</select>",
617
+ html
618
+ end
619
+
620
+ def test_time_zone_select_with_blank_as_string
621
+ @firm = Firm.new("D")
622
+ html = time_zone_select("firm", "time_zone", nil, :include_blank => 'No Zone')
623
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
624
+ "<option value=\"\">No Zone</option>\n" +
625
+ "<option value=\"A\">A</option>\n" +
626
+ "<option value=\"B\">B</option>\n" +
627
+ "<option value=\"C\">C</option>\n" +
628
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
629
+ "<option value=\"E\">E</option>" +
630
+ "</select>",
631
+ html
632
+ end
633
+
634
+ def test_time_zone_select_with_style
635
+ @firm = Firm.new("D")
636
+ html = time_zone_select("firm", "time_zone", nil, {},
637
+ "style" => "color: red")
638
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
639
+ "<option value=\"A\">A</option>\n" +
640
+ "<option value=\"B\">B</option>\n" +
641
+ "<option value=\"C\">C</option>\n" +
642
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
643
+ "<option value=\"E\">E</option>" +
644
+ "</select>",
645
+ html
646
+ assert_dom_equal html, time_zone_select("firm", "time_zone", nil, {},
647
+ :style => "color: red")
648
+ end
649
+
650
+ def test_time_zone_select_with_blank_and_style
651
+ @firm = Firm.new("D")
652
+ html = time_zone_select("firm", "time_zone", nil,
653
+ { :include_blank => true }, "style" => "color: red")
654
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
655
+ "<option value=\"\"></option>\n" +
656
+ "<option value=\"A\">A</option>\n" +
657
+ "<option value=\"B\">B</option>\n" +
658
+ "<option value=\"C\">C</option>\n" +
659
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
660
+ "<option value=\"E\">E</option>" +
661
+ "</select>",
662
+ html
663
+ assert_dom_equal html, time_zone_select("firm", "time_zone", nil,
664
+ { :include_blank => true }, :style => "color: red")
665
+ end
666
+
667
+ def test_time_zone_select_with_blank_as_string_and_style
668
+ @firm = Firm.new("D")
669
+ html = time_zone_select("firm", "time_zone", nil,
670
+ { :include_blank => 'No Zone' }, "style" => "color: red")
671
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" +
672
+ "<option value=\"\">No Zone</option>\n" +
673
+ "<option value=\"A\">A</option>\n" +
674
+ "<option value=\"B\">B</option>\n" +
675
+ "<option value=\"C\">C</option>\n" +
676
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
677
+ "<option value=\"E\">E</option>" +
678
+ "</select>",
679
+ html
680
+ assert_dom_equal html, time_zone_select("firm", "time_zone", nil,
681
+ { :include_blank => 'No Zone' }, :style => "color: red")
682
+ end
683
+
684
+ def test_time_zone_select_with_priority_zones
685
+ @firm = Firm.new("D")
686
+ zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ]
687
+ html = time_zone_select("firm", "time_zone", zones )
688
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
689
+ "<option value=\"A\">A</option>\n" +
690
+ "<option value=\"D\" selected=\"selected\">D</option>" +
691
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
692
+ "<option value=\"B\">B</option>\n" +
693
+ "<option value=\"C\">C</option>\n" +
694
+ "<option value=\"E\">E</option>" +
695
+ "</select>",
696
+ html
697
+ end
698
+
699
+ def test_time_zone_select_with_priority_zones_as_regexp
700
+ @firm = Firm.new("D")
701
+ @fake_timezones.each_with_index do |tz, i|
702
+ tz.stubs(:=~).returns(i.zero? || i == 3)
703
+ end
704
+
705
+ html = time_zone_select("firm", "time_zone", /A|D/)
706
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
707
+ "<option value=\"A\">A</option>\n" +
708
+ "<option value=\"D\" selected=\"selected\">D</option>" +
709
+ "<option value=\"\" disabled=\"disabled\">-------------</option>\n" +
710
+ "<option value=\"B\">B</option>\n" +
711
+ "<option value=\"C\">C</option>\n" +
712
+ "<option value=\"E\">E</option>" +
713
+ "</select>",
714
+ html
715
+ end
716
+
717
+ def test_time_zone_select_with_default_time_zone_and_nil_value
718
+ @firm = Firm.new()
719
+ @firm.time_zone = nil
720
+ html = time_zone_select( "firm", "time_zone", nil, :default => 'B' )
721
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
722
+ "<option value=\"A\">A</option>\n" +
723
+ "<option value=\"B\" selected=\"selected\">B</option>\n" +
724
+ "<option value=\"C\">C</option>\n" +
725
+ "<option value=\"D\">D</option>\n" +
726
+ "<option value=\"E\">E</option>" +
727
+ "</select>",
728
+ html
729
+ end
730
+
731
+ def test_time_zone_select_with_default_time_zone_and_value
732
+ @firm = Firm.new('D')
733
+ html = time_zone_select( "firm", "time_zone", nil, :default => 'B' )
734
+ assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" +
735
+ "<option value=\"A\">A</option>\n" +
736
+ "<option value=\"B\">B</option>\n" +
737
+ "<option value=\"C\">C</option>\n" +
738
+ "<option value=\"D\" selected=\"selected\">D</option>\n" +
739
+ "<option value=\"E\">E</option>" +
740
+ "</select>",
741
+ html
742
+ end
743
+
744
+ private
745
+
746
+ def dummy_posts
747
+ [ Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"),
748
+ Post.new("Babe went home", "Babe", "To a little house", "shh!"),
749
+ Post.new("Cabe went home", "Cabe", "To a little house", "shh!") ]
750
+ end
751
+ end