any_view 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +47 -0
  4. data/README.rdoc +369 -0
  5. data/Rakefile +57 -0
  6. data/lib/any_view/asset_tag_helpers.rb +103 -0
  7. data/lib/any_view/core_ext/array.rb +7 -0
  8. data/lib/any_view/core_ext/hash.rb +41 -0
  9. data/lib/any_view/core_ext/string.rb +17 -0
  10. data/lib/any_view/form_builder/abstract_form_builder.rb +128 -0
  11. data/lib/any_view/form_builder/standard_form_builder.rb +37 -0
  12. data/lib/any_view/form_helpers.rb +217 -0
  13. data/lib/any_view/format_helpers.rb +49 -0
  14. data/lib/any_view/tag_helpers.rb +47 -0
  15. data/lib/any_view/tilt_base.rb +94 -0
  16. data/lib/any_view.rb +30 -0
  17. data/test/fixtures/basic_form_for.erb +3 -0
  18. data/test/fixtures/builder_type_form_for.erb +3 -0
  19. data/test/fixtures/capture_concat.erb +14 -0
  20. data/test/fixtures/capture_concat.haml +13 -0
  21. data/test/fixtures/content_for.erb +11 -0
  22. data/test/fixtures/content_for.haml +9 -0
  23. data/test/fixtures/content_tag.erb +11 -0
  24. data/test/fixtures/content_tag.haml +9 -0
  25. data/test/fixtures/delete_form_for.erb +3 -0
  26. data/test/fixtures/field_set_tag.erb +3 -0
  27. data/test/fixtures/fields_for.erb +8 -0
  28. data/test/fixtures/fields_for.haml +6 -0
  29. data/test/fixtures/fields_for_basic.erb +3 -0
  30. data/test/fixtures/fields_for_nil.erb +3 -0
  31. data/test/fixtures/form_for.erb +56 -0
  32. data/test/fixtures/form_for.haml +47 -0
  33. data/test/fixtures/form_for_nil.erb +3 -0
  34. data/test/fixtures/form_tag.erb +57 -0
  35. data/test/fixtures/form_tag.haml +45 -0
  36. data/test/fixtures/form_tag_methods.erb +19 -0
  37. data/test/fixtures/form_tag_methods.haml +15 -0
  38. data/test/fixtures/link_to.erb +5 -0
  39. data/test/fixtures/link_to.haml +4 -0
  40. data/test/fixtures/mail_to.erb +3 -0
  41. data/test/fixtures/mail_to.haml +3 -0
  42. data/test/fixtures/multipart.erb +12 -0
  43. data/test/fixtures/multipart_form_for.erb +3 -0
  44. data/test/fixtures/put_form_for.erb +3 -0
  45. data/test/fixtures/standard_form_builder.erb +3 -0
  46. data/test/helper.rb +121 -0
  47. data/test/test_asset_tag_helpers.rb +176 -0
  48. data/test/test_form_builder.rb +607 -0
  49. data/test/test_form_helpers.rb +453 -0
  50. data/test/test_format_helpers.rb +59 -0
  51. data/test/test_tag_helpers.rb +65 -0
  52. metadata +160 -0
@@ -0,0 +1,453 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestFormHelpers < Test::Unit::TestCase
4
+
5
+ context 'for #form_tag method' do
6
+
7
+ context "method faking" do
8
+ should "display correct form with post as default" do
9
+ actual_html = render("form_tag_methods.erb")
10
+ # default
11
+ assert_has_tag(:form, :class => "method-default", :method => 'post') { actual_html }
12
+ assert_has_no_selector('form.method-default input', :type => 'hidden', :name => "_method") { actual_html }
13
+ end
14
+ should "display no hidden field for get method in erb" do
15
+ actual_html = render("form_tag_methods.erb")
16
+ # get
17
+ assert_has_tag(:form, :class => "method-get", :method => 'get') { actual_html }
18
+ assert_has_no_selector('form.method-get input', :type => 'hidden', :name => "_method") { actual_html }
19
+ end
20
+ should "display no hidden field for the post method in erb" do
21
+ actual_html = render("form_tag_methods.erb")
22
+ # post
23
+ assert_has_tag(:form, :class => "method-post", :method => 'post') { actual_html }
24
+ assert_has_no_selector('form.method-post input', :type => 'hidden', :name => "_method") { actual_html }
25
+ end
26
+ should "display a hidden field for the put method in erb" do
27
+ actual_html = render("form_tag_methods.erb")
28
+ # put
29
+ assert_has_tag(:form, :class => "method-put", :method => 'post') { actual_html }
30
+ assert_has_selector('form input', :type => 'hidden', :name => "_method", :value => 'put') { actual_html }
31
+ end
32
+ should "display a hidden field for the delete method in erb" do
33
+ actual_html = render("form_tag_methods.erb")
34
+ # delete
35
+ assert_has_tag(:form, :class => "method-delete", :method => 'get') { actual_html }
36
+ assert_has_selector('form input', :type => 'hidden', :name => "_method", :value => 'delete') { actual_html }
37
+ end
38
+ should "display correct form with post as default in haml" do
39
+ actual_html = render("form_tag_methods.haml")
40
+ # default
41
+ assert_has_tag(:form, :class => "method-default", :method => 'post') { actual_html }
42
+ assert_has_no_selector('form.method-default input', :type => 'hidden', :name => "_method") { actual_html }
43
+ end
44
+ should "display no hidden field for get method in haml" do
45
+ actual_html = render("form_tag_methods.haml")
46
+ # get
47
+ assert_has_tag(:form, :class => "method-get", :method => 'get') { actual_html }
48
+ assert_has_no_selector('form.method-get input', :type => 'hidden', :name => "_method") { actual_html }
49
+ end
50
+ should "display no hidden field for the post method in haml" do
51
+ actual_html = render("form_tag_methods.haml")
52
+ # post
53
+ assert_has_tag(:form, :class => "method-post", :method => 'post') { actual_html }
54
+ assert_has_no_selector('form.method-post input', :type => 'hidden', :name => "_method") { actual_html }
55
+ end
56
+ should "display a hidden field for the put method in haml" do
57
+ actual_html = render("form_tag_methods.haml")
58
+ # put
59
+ assert_has_tag(:form, :class => "method-put", :method => 'post') { actual_html }
60
+ assert_has_selector('form input', :type => 'hidden', :name => "_method", :value => 'put') { actual_html }
61
+ end
62
+ should "display a hidden field for the delete method in haml" do
63
+ actual_html = render("form_tag_methods.haml")
64
+ # delete
65
+ assert_has_tag(:form, :class => "method-delete", :method => 'get') { actual_html }
66
+ assert_has_selector('form input', :type => 'hidden', :name => "_method", :value => 'delete') { actual_html }
67
+ end
68
+ end
69
+
70
+ should "display correct form with multipart encoding explicity" do
71
+ actual_html = render("multipart.erb")
72
+ assert_has_tag(:form, :class => "explicit", :enctype => "multipart/form-data") { actual_html }
73
+ end
74
+
75
+ should "display correct form with multipart encoding implicitly" do
76
+ actual_html = render("multipart.erb")
77
+ assert_has_tag(:form, :class => "implicit", :enctype => "multipart/form-data"){actual_html}
78
+ end
79
+
80
+ should "not display multipart encoding when there is no file tag" do
81
+ actual_html = render("multipart.erb")
82
+ assert_has_no_tag(:form, :class => "no_file", :enctype => "multipart/form-data"){actual_html}
83
+ end
84
+
85
+ should "display correct forms in erb" do
86
+ result = render("form_tag.erb")
87
+ assert_has_selector('form.simple-form', :action => '/simple'){result}
88
+ assert_has_selector('form.advanced-form', :action => '/advanced', :id => 'advanced', :method => 'post'){result}
89
+ end
90
+
91
+ should "display correct forms in haml" do
92
+ result = render("form_tag.haml")
93
+ assert_has_selector('form.simple-form', :action => '/simple'){result}
94
+ assert_has_selector('form.advanced-form', :action => '/advanced', :id => 'advanced', :method => 'get'){result}
95
+ end
96
+ end
97
+
98
+ context 'for #field_set_tag method' do
99
+ should "display correct field_sets in ruby" do
100
+ actual_html = render('field_set_tag.erb')
101
+ assert_has_tag(:fieldset, :class => 'basic') { actual_html }
102
+ assert_has_tag('fieldset legend', :content => "Basic") { actual_html }
103
+ end
104
+
105
+ should "display correct field_sets in erb" do
106
+ result = render("form_tag.erb")
107
+ assert_has_selector('form.simple-form fieldset', :count => 1){ result }
108
+ assert_has_no_selector('form.simple-form fieldset legend'){result}
109
+ assert_has_selector('form.advanced-form fieldset', :count => 1, :class => 'advanced-field-set'){result}
110
+ assert_has_selector('form.advanced-form fieldset legend', :content => "Advanced"){result}
111
+ end
112
+
113
+ should "display correct field_sets in haml" do
114
+ result = render("form_tag.haml")
115
+ assert_has_selector('form.simple-form fieldset', :count => 1){result}
116
+ assert_has_no_selector('form.simple-form fieldset legend'){result}
117
+ assert_has_selector('form.advanced-form fieldset', :count => 1, :class => 'advanced-field-set'){result}
118
+ assert_has_selector('form.advanced-form fieldset legend', :content => "Advanced"){result}
119
+ end
120
+ end
121
+
122
+ context 'for #error_messages_for method' do
123
+ should "display correct error messages list in ruby" do
124
+ user = stub(:class => "User", :errors => stub(:full_messages => ["1", "2"], :none? => false), :blank? => false)
125
+ actual_html = view_context.error_messages_for(user)
126
+ assert_has_tag('div.field-errors') { actual_html }
127
+ assert_has_tag('div.field-errors p', :content => "The user could not be saved") { actual_html }
128
+ assert_has_tag('div.field-errors ul.errors-list') { actual_html }
129
+ assert_has_tag('div.field-errors ul.errors-list li', :count => 2) { actual_html }
130
+ end
131
+
132
+ should "display correct error messages list in erb" do
133
+ result = render("form_tag.erb")
134
+ assert_has_no_selector('form.simple-form .field-errors'){result}
135
+ assert_has_selector('form.advanced-form .field-errors'){result}
136
+ assert_has_selector('form.advanced-form .field-errors p', :content => "There are problems with saving user!"){result}
137
+ assert_has_selector('form.advanced-form .field-errors ul.errors-list'){result}
138
+ assert_has_selector('form.advanced-form .field-errors ul.errors-list li', :count => 3){result}
139
+ assert_has_selector('form.advanced-form .field-errors ul.errors-list li', :content => "This is a second fake error"){result}
140
+ end
141
+
142
+ should "display correct error messages list in haml" do
143
+ result = render("form_tag.haml")
144
+ assert_has_no_selector('form.simple-form .field-errors'){result}
145
+ assert_has_selector('form.advanced-form .field-errors'){result}
146
+ assert_has_selector('form.advanced-form .field-errors p', :content => "There are problems with saving user!"){result}
147
+ assert_has_selector('form.advanced-form .field-errors ul.errors-list'){result}
148
+ assert_has_selector('form.advanced-form .field-errors ul.errors-list li', :count => 3){result}
149
+ assert_has_selector('form.advanced-form .field-errors ul.errors-list li', :content => "This is a second fake error"){result}
150
+ end
151
+ end
152
+
153
+ context 'for #label_tag method' do
154
+ should "display label tag in ruby" do
155
+ actual_html = view_context.label_tag(:username, :class => 'long-label', :caption => "Nickname")
156
+ assert_has_tag(:label, :for => 'username', :class => 'long-label', :content => "Nickname: ") { actual_html }
157
+ end
158
+
159
+ should "display label tag in erb for simple form" do
160
+ result = render("form_tag.erb")
161
+ assert_has_selector('form.simple-form label', :count => 4){result}
162
+ assert_has_selector('form.simple-form label', :content => "Username", :for => 'username'){result}
163
+ assert_has_selector('form.simple-form label', :content => "Password", :for => 'password'){result}
164
+ assert_has_selector('form.simple-form label', :content => "Gender", :for => 'gender'){result}
165
+ end
166
+
167
+ should "display label tag in erb for advanced form" do
168
+ result = render("form_tag.erb")
169
+ assert_has_selector('form.advanced-form label', :count => 6){result}
170
+ assert_has_selector('form.advanced-form label.first', :content => "Nickname", :for => 'username'){result}
171
+ assert_has_selector('form.advanced-form label.first', :content => "Password", :for => 'password'){result}
172
+ assert_has_selector('form.advanced-form label.about', :content => "About Me", :for => 'about'){result}
173
+ assert_has_selector('form.advanced-form label.photo', :content => "Photo" , :for => 'photo'){result}
174
+ assert_has_selector('form.advanced-form label.gender', :content => "Gender" , :for => 'gender'){result}
175
+ end
176
+
177
+ should "display label tag in haml for simple form" do
178
+ result = render("form_tag.haml")
179
+ assert_has_selector('form.simple-form label', :count => 4){result}
180
+ assert_has_selector('form.simple-form label', :content => "Username", :for => 'username'){result}
181
+ assert_has_selector('form.simple-form label', :content => "Password", :for => 'password'){result}
182
+ assert_has_selector('form.simple-form label', :content => "Gender", :for => 'gender'){result}
183
+ end
184
+
185
+ should "display label tag in haml for advanced form" do
186
+ result = render("form_tag.haml")
187
+ assert_has_selector('form.advanced-form label', :count => 6){result}
188
+ assert_has_selector('form.advanced-form label.first', :content => "Nickname", :for => 'username'){result}
189
+ assert_has_selector('form.advanced-form label.first', :content => "Password", :for => 'password'){result}
190
+ assert_has_selector('form.advanced-form label.about', :content => "About Me", :for => 'about'){result}
191
+ assert_has_selector('form.advanced-form label.photo', :content => "Photo" , :for => 'photo'){result}
192
+ assert_has_selector('form.advanced-form label.gender', :content => "Gender" , :for => 'gender'){result}
193
+ end
194
+ end
195
+
196
+ context 'for #hidden_field_tag method' do
197
+ should "display hidden field in ruby" do
198
+ actual_html = view_context.hidden_field_tag(:session_key, :id => 'session_id', :value => '56768')
199
+ assert_has_tag(:input, :type => 'hidden', :id => "session_id", :name => 'session_key', :value => '56768') { actual_html }
200
+ end
201
+
202
+ should "display hidden field in erb" do
203
+ result = render('form_tag.haml')
204
+ assert_has_selector('form.simple-form input[type=hidden]', :count => 1, :name => 'session_id', :value => "__secret__"){result}
205
+ assert_has_selector('form.advanced-form input[type=hidden]', :count => 1, :name => 'session_id', :value => "__secret__"){result}
206
+ end
207
+
208
+ should "display hidden field in haml" do
209
+ result = render("form_tag.haml")
210
+ assert_has_selector('form.simple-form input[type=hidden]', :count => 1, :name => 'session_id', :value => "__secret__"){result}
211
+ assert_has_selector('form.advanced-form input[type=hidden]', :count => 1, :name => 'session_id', :value => "__secret__"){result}
212
+ end
213
+ end
214
+
215
+ context 'for #text_field_tag method' do
216
+ should "display text field in ruby" do
217
+ actual_html = view_context.text_field_tag(:username, :class => 'long')
218
+ assert_has_tag(:input, :type => 'text', :class => "long", :name => 'username') { actual_html }
219
+ end
220
+
221
+ should "display text field in erb" do
222
+ result = render("form_tag.haml")
223
+ assert_has_selector('form.simple-form input[type=text]', :count => 1, :name => 'username'){result}
224
+ assert_has_selector('form.advanced-form fieldset input[type=text]', :count => 1, :name => 'username', :id => 'the_username'){result}
225
+ end
226
+
227
+ should "display text field in haml" do
228
+ result = render("form_tag.haml")
229
+ assert_has_selector('form.simple-form input[type=text]', :count => 1, :name => 'username'){result}
230
+ assert_has_selector('form.advanced-form fieldset input[type=text]', :count => 1, :name => 'username', :id => 'the_username'){result}
231
+ end
232
+ end
233
+
234
+ context 'for #text_area_tag method' do
235
+ should "display text area in ruby" do
236
+ actual_html = view_context.text_area_tag(:about, :class => 'long')
237
+ assert_has_tag(:textarea, :class => "long", :name => 'about') { actual_html }
238
+ end
239
+
240
+ should "display text area in erb" do
241
+ result = render("form_tag.erb")
242
+ assert_has_selector('form.advanced-form textarea', :count => 1, :name => 'about', :class => 'large'){result}
243
+ end
244
+
245
+ should "display text area in haml" do
246
+ result = render("form_tag.haml")
247
+ assert_has_selector('form.advanced-form textarea', :count => 1, :name => 'about', :class => 'large'){result}
248
+ end
249
+ end
250
+
251
+ context 'for #password_field_tag method' do
252
+ should "display password field in ruby" do
253
+ actual_html = view_context.password_field_tag(:password, :class => 'long')
254
+ assert_has_tag(:input, :type => 'password', :class => "long", :name => 'password') { actual_html }
255
+ end
256
+
257
+ should "display password field in erb" do
258
+ result = render("form_tag.erb")
259
+ assert_has_selector('form.simple-form input[type=password]', :count => 1, :name => 'password'){result}
260
+ assert_has_selector('form.advanced-form input[type=password]', :count => 1, :name => 'password'){result}
261
+ end
262
+
263
+ should "display password field in haml" do
264
+ result = render("form_tag.haml")
265
+ assert_has_selector('form.simple-form input[type=password]', :count => 1, :name => 'password'){result}
266
+ assert_has_selector('form.advanced-form input[type=password]', :count => 1, :name => 'password'){result}
267
+ end
268
+ end
269
+
270
+ context 'for #file_field_tag method' do
271
+ should "display file field in ruby" do
272
+ actual_html = view_context.file_field_tag(:photo, :class => 'photo')
273
+ assert_has_tag(:input, :type => 'file', :class => "photo", :name => 'photo') { actual_html }
274
+ end
275
+
276
+ should "display file field in erb" do
277
+ result = render("form_tag.erb")
278
+ assert_has_selector('form.advanced-form input[type=file]', :count => 1, :name => 'photo', :class => 'upload'){result}
279
+ end
280
+
281
+ should "display file field in haml" do
282
+ result = render("form_tag.haml")
283
+ assert_has_selector('form.advanced-form input[type=file]', :count => 1, :name => 'photo', :class => 'upload'){result}
284
+ end
285
+ end
286
+
287
+ context "for #check_box_tag method" do
288
+ should "display check_box tag in ruby" do
289
+ actual_html = view_context.check_box_tag("clear_session")
290
+ assert_has_tag(:input, :type => 'checkbox', :value => '1', :name => 'clear_session') { actual_html }
291
+ assert_has_no_tag(:input, :type => 'hidden') { actual_html }
292
+ end
293
+
294
+ should "display check_box tag in ruby with extended attributes" do
295
+ actual_html = view_context.check_box_tag("clear_session", :disabled => true, :checked => true)
296
+ assert_has_tag(:input, :type => 'checkbox', :disabled => 'disabled', :checked => 'checked') { actual_html }
297
+ end
298
+
299
+ should "display check_box tag in erb" do
300
+ result = render("form_tag.erb")
301
+ assert_has_selector('form.simple-form input[type=checkbox]', :count => 1){result}
302
+ assert_has_selector('form.advanced-form input[type=checkbox]', :value => "1", :checked => 'checked'){result}
303
+ end
304
+
305
+ should "display check_box tag in haml" do
306
+ result = render("form_tag.haml")
307
+ assert_has_selector('form.simple-form input[type=checkbox]', :count => 1){result}
308
+ assert_has_selector('form.advanced-form input[type=checkbox]', :value => "1", :checked => 'checked'){result}
309
+ end
310
+ end
311
+
312
+ context "for #radio_button_tag method" do
313
+ should "display radio_button tag in ruby" do
314
+ actual_html = view_context.radio_button_tag("gender", :value => 'male')
315
+ assert_has_tag(:input, :type => 'radio', :value => 'male', :name => 'gender') { actual_html }
316
+ end
317
+
318
+ should "display radio_button tag in ruby with extended attributes" do
319
+ actual_html = view_context.radio_button_tag("gender", :disabled => true, :checked => true)
320
+ assert_has_tag(:input, :type => 'radio', :disabled => 'disabled', :checked => 'checked') { actual_html }
321
+ end
322
+
323
+ should "display radio_button tag in erb" do
324
+ result = render("form_tag.erb")
325
+ assert_has_selector('form.simple-form input[type=radio]', :count => 1, :value => 'male'){result}
326
+ assert_has_selector('form.simple-form input[type=radio]', :count => 1, :value => 'female'){result}
327
+ assert_has_selector('form.advanced-form input[type=radio]', :value => "male", :checked => 'checked'){result}
328
+ assert_has_selector('form.advanced-form input[type=radio]', :value => "female"){result}
329
+ end
330
+
331
+ should "display radio_button tag in haml" do
332
+ result = render("form_tag.haml")
333
+ assert_has_selector('form.simple-form input[type=radio]', :count => 1, :value => 'male'){result}
334
+ assert_has_selector('form.simple-form input[type=radio]', :count => 1, :value => 'female'){result}
335
+ assert_has_selector('form.advanced-form input[type=radio]', :value => "male", :checked => 'checked'){result}
336
+ assert_has_selector('form.advanced-form input[type=radio]', :value => "female"){result}
337
+ end
338
+ end
339
+
340
+ context "for #select_tag method" do
341
+ should "display select tag in ruby" do
342
+ actual_html = view_context.select_tag(:favorite_color, :options => ['green', 'blue', 'black'], :include_blank => true)
343
+ assert_has_tag(:select, :name => 'favorite_color') { actual_html }
344
+ assert_has_tag('select option:first-child', :content => '') { actual_html }
345
+ assert_has_tag('select option', :content => 'green', :value => 'green') { actual_html }
346
+ assert_has_tag('select option', :content => 'blue', :value => 'blue') { actual_html }
347
+ assert_has_tag('select option', :content => 'black', :value => 'black') { actual_html }
348
+ end
349
+
350
+ should "display select tag in ruby with extended attributes" do
351
+ actual_html = view_context.select_tag(:favorite_color, :disabled => true, :options => ['only', 'option'])
352
+ assert_has_tag(:select, :disabled => 'disabled') { actual_html }
353
+ end
354
+
355
+ should "display select tag in ruby with multiple attribute" do
356
+ actual_html = view_context.select_tag(:favorite_color, :multiple => true, :options => ['only', 'option'])
357
+ assert_has_tag(:select, :multiple => 'multiple', :name => 'favorite_color[]') { actual_html }
358
+ end
359
+
360
+ should "display options with values and selected" do
361
+ options = [['Green', 'green1'], ['Blue', 'blue1'], ['Black', "black1"]]
362
+ actual_html = view_context.select_tag(:favorite_color, :options => options, :selected => 'green1')
363
+ assert_has_tag(:select, :name => 'favorite_color') { actual_html }
364
+ assert_has_tag('select option', :selected => 'selected', :count => 1) { actual_html }
365
+ assert_has_tag('select option', :content => 'Green', :value => 'green1', :selected => 'selected') { actual_html }
366
+ assert_has_tag('select option', :content => 'Blue', :value => 'blue1') { actual_html }
367
+ assert_has_tag('select option', :content => 'Black', :value => 'black1') { actual_html }
368
+ end
369
+
370
+ should "display select tag in erb" do
371
+ result = render("form_tag.erb")
372
+ assert_has_selector('form.simple-form select', :count => 1, :name => 'color'){result}
373
+ assert_has_selector('select option', :content => 'green', :value => 'green'){result}
374
+ assert_has_selector('select option', :content => 'orange', :value => 'orange'){result}
375
+ assert_has_selector('select option', :content => 'purple', :value => 'purple'){result}
376
+ assert_has_selector('form.advanced-form select', :name => 'fav_color'){result}
377
+ assert_has_selector('select option', :content => 'green', :value => '1'){result}
378
+ assert_has_selector('select option', :content => 'orange', :value => '2', :selected => 'selected'){result}
379
+ assert_has_selector('select option', :content => 'purple', :value => '3'){result}
380
+ end
381
+
382
+ should "display select tag in haml" do
383
+ result = render("form_tag.haml")
384
+ assert_has_selector('form.simple-form select', :count => 1, :name => 'color'){result}
385
+ assert_has_selector('select option', :content => 'green', :value => 'green'){result}
386
+ assert_has_selector('select option', :content => 'orange', :value => 'orange'){result}
387
+ assert_has_selector('select option', :content => 'purple', :value => 'purple'){result}
388
+ assert_has_selector('form.advanced-form select', :name => 'fav_color'){result}
389
+ assert_has_selector('select option', :content => 'green', :value => '1'){result}
390
+ assert_has_selector('select option', :content => 'orange', :value => '2', :selected => 'selected'){result}
391
+ assert_has_selector('select option', :content => 'purple', :value => '3'){result}
392
+ end
393
+ end
394
+
395
+ context 'for #submit_tag method' do
396
+ should "display submit tag in ruby" do
397
+ actual_html = view_context.submit_tag("Update", :class => 'success')
398
+ assert_has_tag(:input, :type => 'submit', :class => "success", :value => 'Update') { actual_html }
399
+ end
400
+
401
+ should "display submit tag in erb" do
402
+ result = render("form_tag.erb")
403
+ assert_has_selector('form.simple-form input[type=submit]', :count => 1, :value => "Submit"){result}
404
+ assert_has_selector('form.advanced-form input[type=submit]', :count => 1, :value => "Login"){result}
405
+ end
406
+
407
+ should "display submit tag in haml" do
408
+ result = render("form_tag.haml")
409
+ assert_has_selector('form.simple-form input[type=submit]', :count => 1, :value => "Submit"){result}
410
+ assert_has_selector('form.advanced-form input[type=submit]', :count => 1, :value => "Login"){result}
411
+ end
412
+ end
413
+
414
+ context 'for #button_tag method' do
415
+ should "display submit tag in ruby" do
416
+ actual_html = view_context.button_tag("Cancel", :class => 'clear')
417
+ assert_has_tag(:input, :type => 'button', :class => "clear", :value => 'Cancel') { actual_html }
418
+ end
419
+
420
+ should "display submit tag in erb" do
421
+ result = render("form_tag.erb")
422
+ assert_has_selector('form.advanced-form input[type=button]', :count => 1, :value => "Cancel"){result}
423
+ end
424
+
425
+ should "display submit tag in haml" do
426
+ result = render("form_tag.haml")
427
+ assert_has_selector('form.advanced-form input[type=button]', :count => 1, :value => "Cancel"){result}
428
+ end
429
+ end
430
+
431
+ context 'for #image_submit_tag method' do
432
+ should "display image submit tag in ruby with relative path" do
433
+ actual_html = view_context.image_submit_tag('buttons/ok.png', :class => 'success')
434
+ assert_has_tag(:input, :type => 'image', :class => "success", :src => '/images/buttons/ok.png') { actual_html }
435
+ end
436
+
437
+ should "display image submit tag in ruby with absolute path" do
438
+ actual_html = view_context.image_submit_tag('/system/ok.png', :class => 'success')
439
+ assert_has_tag(:input, :type => 'image', :class => "success", :src => '/system/ok.png') { actual_html }
440
+ end
441
+
442
+ should "display image submit tag in erb" do
443
+ result = render("form_tag.erb")
444
+ assert_has_selector('form.advanced-form input[type=image]', :count => 1, :src => "/images/buttons/submit.png"){result}
445
+ end
446
+
447
+ should "display image submit tag in haml" do
448
+ result = render("form_tag.haml")
449
+ assert_has_selector('form.advanced-form input[type=image]', :count => 1, :src => "/images/buttons/submit.png"){result}
450
+ end
451
+ end
452
+
453
+ end
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestFormatHelpers < Test::Unit::TestCase
4
+
5
+ context 'for #simple_format method' do
6
+
7
+ should "format simple text into html format" do
8
+ actual_text = view_context.simple_format("Here is some basic text...\n...with a line break.")
9
+ assert_equal "<p>Here is some basic text...\n<br />...with a line break.</p>", actual_text
10
+ end
11
+
12
+ should "format more text into html format" do
13
+ actual_text = view_context.simple_format("We want to put a paragraph...\n\n...right there.")
14
+ assert_equal "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>", actual_text
15
+ end
16
+
17
+ should "support defining a class for the paragraphs" do
18
+ actual_text = view_context.simple_format("Look ma! A class!", :class => 'description')
19
+ assert_equal "<p class=\"description\">Look ma! A class!</p>", actual_text
20
+ end
21
+ end
22
+
23
+ context 'for #truncate method' do
24
+ should "support default truncation" do
25
+ actual_text = view_context.truncate("Once upon a time in a world far far away")
26
+ assert_equal "Once upon a time in a world...", actual_text
27
+ end
28
+
29
+ should "support specifying length" do
30
+ actual_text = view_context.truncate("Once upon a time in a world far far away", :length => 14)
31
+ assert_equal "Once upon a...", actual_text
32
+ end
33
+
34
+ should "support specifying omission text" do
35
+ actual_text = view_context.truncate("And they found that many people were sleeping better.", :length => 25, :omission => "(clipped)")
36
+ assert_equal "And they found t(clipped)", actual_text
37
+ end
38
+ end
39
+
40
+ context 'for #h and #h! method' do
41
+ should "escape the simple html" do
42
+ assert_equal '&lt;h1&gt;hello&lt;/h1&gt;', view_context.h('<h1>hello</h1>')
43
+ assert_equal '&lt;h1&gt;hello&lt;/h1&gt;', view_context.escape_html('<h1>hello</h1>')
44
+ end
45
+
46
+ should "escape all brackets, quotes and ampersands" do
47
+ assert_equal '&lt;h1&gt;&lt;&gt;&quot;&amp;demo&amp;&quot;&lt;&gt;&lt;/h1&gt;', view_context.h('<h1><>"&demo&"<></h1>')
48
+ end
49
+
50
+ should "return default text if text is empty" do
51
+ assert_equal 'default', view_context.h!("", "default")
52
+ assert_equal '&nbsp;', view_context.h!("")
53
+ end
54
+
55
+ should "return text escaped if not empty" do
56
+ assert_equal '&lt;h1&gt;hello&lt;/h1&gt;', view_context.h!('<h1>hello</h1>')
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestTagHelpers < Test::Unit::TestCase
4
+ context 'for #tag method' do
5
+ should("support tags with no content no attributes") do
6
+ assert_has_tag(:br) { view_context.tag(:br) }
7
+ end
8
+ should("support tags with no content with attributes") do
9
+ actual_html = view_context.tag(:br, :style => 'clear:both', :class => 'yellow')
10
+ assert_has_tag(:br, :class => 'yellow', :style=>'clear:both') { actual_html }
11
+ end
12
+ should "support selected attribute by using 'selected' if true" do
13
+ actual_html = view_context.tag(:option, :selected => true)
14
+ assert_has_tag('option', :selected => 'selected') { actual_html }
15
+ end
16
+ should "support tags with content no attributes" do
17
+ assert_has_tag(:p, :content => "Demo String") { view_context.tag(:p, :content => "Demo String") }
18
+ end
19
+ should "support tags with content and attributes" do
20
+ actual_html = view_context.tag(:p, :content => "Demo", :class => 'large', :id => 'intro')
21
+ assert_has_tag('p#intro.large', :content => "Demo") { actual_html }
22
+ end
23
+ should "support open tags" do
24
+ actual_html = view_context.tag(:p, :class => 'demo', :open => true)
25
+ assert_equal "<p class=\"demo\">", actual_html
26
+ end
27
+ end
28
+
29
+ context 'for #content_tag method' do
30
+ should "support tags with erb" do
31
+ result = render("content_tag.erb")
32
+ assert_has_selector(:p, :content => "Test 1", :class => 'test', :id => 'test1'){result}
33
+ assert_has_selector(:p, :content => "Test 2"){result}
34
+ assert_has_selector(:p, :content => "Test 3"){result}
35
+ assert_has_selector(:p, :content => "Test 4"){result}
36
+ end
37
+
38
+ should "support tags with haml" do
39
+ result = render('content_tag.haml')
40
+ assert_has_selector(:p, :content => "Test 1", :class => 'test', :id => 'test1'){result}
41
+ assert_has_selector(:p, :content => "Test 2"){result}
42
+ assert_has_selector(:p, :content => "Test 3", :class => 'test', :id => 'test3'){result}
43
+ assert_has_selector(:p, :content => "Test 4"){result}
44
+ end
45
+ end
46
+
47
+ context 'for #input_tag method' do
48
+ should "support field with type" do
49
+ assert_has_tag('input[type=text]') { view_context.input_tag(:text) }
50
+ end
51
+ should "support field with type and options" do
52
+ actual_html = view_context.input_tag(:text, :class => "first", :id => 'texter')
53
+ assert_has_tag('input.first#texter[type=text]') { actual_html }
54
+ end
55
+ should "support checked attribute by using 'checked' if true" do
56
+ actual_html = view_context.input_tag(:checkbox, :checked => true)
57
+ assert_has_tag('input[type=checkbox]', :checked => 'checked') { actual_html }
58
+ end
59
+ should "support disabled attribute by using 'disabled' if true" do
60
+ actual_html = view_context.input_tag(:checkbox, :disabled => true)
61
+ assert_has_tag('input[type=checkbox]', :disabled => 'disabled') { actual_html }
62
+ end
63
+ end
64
+
65
+ end