padrino-helpers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +7 -0
  5. data/Rakefile +58 -0
  6. data/VERSION +1 -0
  7. data/lib/padrino-helpers.rb +16 -0
  8. data/lib/padrino-helpers/asset_tag_helpers.rb +97 -0
  9. data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +139 -0
  10. data/lib/padrino-helpers/form_builder/standard_form_builder.rb +37 -0
  11. data/lib/padrino-helpers/form_helpers.rb +194 -0
  12. data/lib/padrino-helpers/format_helpers.rb +74 -0
  13. data/lib/padrino-helpers/output_helpers.rb +98 -0
  14. data/lib/padrino-helpers/render_helpers.rb +63 -0
  15. data/lib/padrino-helpers/tag_helpers.rb +42 -0
  16. data/test/active_support_helpers.rb +7 -0
  17. data/test/fixtures/markup_app/app.rb +61 -0
  18. data/test/fixtures/markup_app/views/capture_concat.erb +14 -0
  19. data/test/fixtures/markup_app/views/capture_concat.haml +13 -0
  20. data/test/fixtures/markup_app/views/content_for.erb +11 -0
  21. data/test/fixtures/markup_app/views/content_for.haml +9 -0
  22. data/test/fixtures/markup_app/views/content_tag.erb +11 -0
  23. data/test/fixtures/markup_app/views/content_tag.haml +9 -0
  24. data/test/fixtures/markup_app/views/fields_for.erb +8 -0
  25. data/test/fixtures/markup_app/views/fields_for.haml +6 -0
  26. data/test/fixtures/markup_app/views/form_for.erb +56 -0
  27. data/test/fixtures/markup_app/views/form_for.haml +47 -0
  28. data/test/fixtures/markup_app/views/form_tag.erb +57 -0
  29. data/test/fixtures/markup_app/views/form_tag.haml +45 -0
  30. data/test/fixtures/markup_app/views/link_to.erb +5 -0
  31. data/test/fixtures/markup_app/views/link_to.haml +4 -0
  32. data/test/fixtures/markup_app/views/mail_to.erb +3 -0
  33. data/test/fixtures/markup_app/views/mail_to.haml +3 -0
  34. data/test/fixtures/render_app/app.rb +53 -0
  35. data/test/fixtures/render_app/views/erb/test.erb +1 -0
  36. data/test/fixtures/render_app/views/haml/test.haml +1 -0
  37. data/test/fixtures/render_app/views/template/_user.haml +7 -0
  38. data/test/fixtures/render_app/views/template/haml_template.haml +1 -0
  39. data/test/fixtures/render_app/views/template/some_template.haml +2 -0
  40. data/test/helper.rb +73 -0
  41. data/test/test_asset_tag_helpers.rb +127 -0
  42. data/test/test_form_builder.rb +611 -0
  43. data/test/test_form_helpers.rb +406 -0
  44. data/test/test_format_helpers.rb +96 -0
  45. data/test/test_output_helpers.rb +63 -0
  46. data/test/test_render_helpers.rb +78 -0
  47. data/test/test_tag_helpers.rb +73 -0
  48. metadata +174 -0
@@ -0,0 +1,611 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require File.dirname(__FILE__) + '/fixtures/markup_app/app'
3
+
4
+ class TestFormBuilder < Test::Unit::TestCase
5
+ include Padrino::Helpers::FormHelpers
6
+
7
+ def app
8
+ MarkupDemo.tap { |app| app.set :environment, :test }
9
+ end
10
+
11
+ def setup
12
+ error_stub = stub(:full_messages => ["1", "2"], :none? => false)
13
+ role_types = [stub(:name => 'Admin', :id => 1), stub(:name => 'Moderate', :id => 2), stub(:name => 'Limited', :id => 3)]
14
+ @user = stub(:errors => error_stub, :class => 'User', :first_name => "Joe", :session_id => 54)
15
+ @user.stubs(:role_types => role_types, :role => "1")
16
+ @user_none = stub(:errors => stub(:none? => true), :class => 'User')
17
+ end
18
+
19
+ def standard_builder(object=@user)
20
+ Padrino::Helpers::FormBuilder::StandardFormBuilder.new(self, object)
21
+ end
22
+
23
+ context 'for #form_for method' do
24
+ should "display correct form html" do
25
+ actual_html = form_for(@user, '/register', :id => 'register', :method => 'post') { "Demo" }
26
+ assert_has_tag('form', :action => '/register', :id => 'register', :method => 'post', :content => "Demo") { actual_html }
27
+ assert_has_tag('form input[type=hidden]', :name => '_method', :count => 0) { actual_html } # no method action field
28
+ end
29
+
30
+ should "display correct form html with fake object" do
31
+ actual_html = form_for(:markup_user, '/register', :id => 'register', :method => 'post') { |f| f.text_field :username }
32
+ assert_has_tag('form', :action => '/register', :id => 'register', :method => 'post') { actual_html }
33
+ assert_has_tag('form input', :type => 'text', :name => 'markup_user[username]') { actual_html }
34
+ assert_has_tag('form input[type=hidden]', :name => '_method', :count => 0) { actual_html } # no method action field
35
+ end
36
+
37
+ should "display correct form html with method :put" do
38
+ actual_html = form_for(@user, '/update', :method => 'put') { "Demo" }
39
+ assert_has_tag('form', :action => '/update', :method => 'post') { actual_html }
40
+ assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'put') { actual_html }
41
+ end
42
+
43
+ should "display correct form html with method :delete" do
44
+ actual_html = form_for(@user, '/destroy', :method => 'delete') { "Demo" }
45
+ assert_has_tag('form', :action => '/destroy', :method => 'post') { actual_html }
46
+ assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'delete') { actual_html }
47
+ end
48
+
49
+ should "display correct form html with multipart" do
50
+ actual_html = form_for(@user, '/register', :multipart => true) { "Demo" }
51
+ assert_has_tag('form', :action => '/register', :enctype => "multipart/form-data") { actual_html }
52
+ end
53
+
54
+ should "support changing form builder type" do
55
+ form_html = lambda { form_for(@user, '/register', :builder => "AbstractFormBuilder") { |f| f.text_field_block(:name) } }
56
+ assert_raise(NoMethodError) { form_html.call }
57
+ end
58
+
59
+ should "support using default standard builder" do
60
+ actual_html = form_for(@user, '/register') { |f| f.text_field_block(:name) }
61
+ assert_has_tag('form p input[type=text]') { actual_html }
62
+ end
63
+
64
+ should "display fail for form with nil object" do
65
+ assert_raises(RuntimeError) { form_for(@not_real, '/register', :id => 'register', :method => 'post') { "Demo" } }
66
+ end
67
+
68
+ should "display correct form in haml" do
69
+ visit '/haml/form_for'
70
+ assert_have_selector :form, :action => '/demo', :id => 'demo'
71
+ assert_have_selector :form, :action => '/another_demo', :id => 'demo2', :method => 'get'
72
+ assert_have_selector :form, :action => '/third_demo', :id => 'demo3', :method => 'get'
73
+ end
74
+
75
+ should "display correct form in erb" do
76
+ visit '/erb/form_for'
77
+ assert_have_selector :form, :action => '/demo', :id => 'demo'
78
+ assert_have_selector :form, :action => '/another_demo', :id => 'demo2', :method => 'get'
79
+ assert_have_selector :form, :action => '/third_demo', :id => 'demo3', :method => 'get'
80
+ end
81
+ end
82
+
83
+ context 'for #fields_for method' do
84
+ should 'display correct fields html' do
85
+ actual_html = fields_for(@user) { |f| f.text_field(:first_name) }
86
+ assert_has_tag(:input, :type => 'text', :name => 'user[first_name]', :id => 'user_first_name') { actual_html }
87
+ end
88
+
89
+ should 'display correct fields html with symbol object' do
90
+ actual_html = fields_for(:markup_user) { |f| f.text_field(:first_name) }
91
+ assert_has_tag(:input, :type => 'text', :name => 'markup_user[first_name]', :id => 'markup_user_first_name') { actual_html }
92
+ end
93
+
94
+ should "display fail for nil object" do
95
+ assert_raises(RuntimeError) { fields_for(@not_real) { |f| "Demo" } }
96
+ end
97
+
98
+ should 'display correct simple fields in haml' do
99
+ visit '/haml/fields_for'
100
+ assert_have_selector :form, :action => '/demo1', :id => 'demo-fields-for'
101
+ assert_have_selector '#demo-fields-for input', :type => 'text', :name => 'markup_user[gender]', :value => 'male'
102
+ assert_have_selector '#demo-fields-for input', :type => 'checkbox', :name => 'permission[can_edit]', :value => '1', :checked => 'checked'
103
+ assert_have_selector '#demo-fields-for input', :type => 'checkbox', :name => 'permission[can_delete]'
104
+ end
105
+
106
+ should "display correct simple fields in erb" do
107
+ visit '/erb/fields_for'
108
+ assert_have_selector :form, :action => '/demo1', :id => 'demo-fields-for'
109
+ assert_have_selector '#demo-fields-for input', :type => 'text', :name => 'markup_user[gender]', :value => 'male'
110
+ assert_have_selector '#demo-fields-for input', :type => 'checkbox', :name => 'permission[can_edit]', :value => '1', :checked => 'checked'
111
+ assert_have_selector '#demo-fields-for input', :type => 'checkbox', :name => 'permission[can_delete]'
112
+ end
113
+ end
114
+
115
+ # ===========================
116
+ # AbstractFormBuilder
117
+ # ===========================
118
+
119
+ context 'for #error_messages method' do
120
+ should "display correct form html with no record" do
121
+ actual_html = standard_builder(@user_none).error_messages(:header_message => "Demo form cannot be saved")
122
+ assert actual_html.blank?
123
+ end
124
+
125
+ should "display correct form html with valid record" do
126
+ actual_html = standard_builder.error_messages(:header_message => "Demo form cannot be saved")
127
+ assert_has_tag('div.field-errors p', :content => "Demo form cannot be saved") { actual_html }
128
+ assert_has_tag('div.field-errors ul.errors-list li', :content => "1") { actual_html }
129
+ assert_has_tag('div.field-errors ul.errors-list li', :content => "2") { actual_html }
130
+ end
131
+
132
+ should "display correct form in haml" do
133
+ visit '/haml/form_for'
134
+ assert_have_selector '#demo div.field-errors p', :content => "custom MarkupUser cannot be saved!"
135
+ assert_have_selector '#demo div.field-errors ul.errors-list li', :content => "This is a fake error"
136
+ assert_have_selector '#demo2 div.field-errors p', :content => "custom MarkupUser cannot be saved!"
137
+ assert_have_selector '#demo2 div.field-errors ul.errors-list li', :content => "This is a fake error"
138
+ end
139
+
140
+ should "display correct form in erb" do
141
+ visit '/erb/form_for'
142
+ assert_have_selector '#demo div.field-errors p', :content => "custom MarkupUser cannot be saved!"
143
+ assert_have_selector '#demo div.field-errors ul.errors-list li', :content => "This is a fake error"
144
+ assert_have_selector '#demo2 div.field-errors p', :content => "custom MarkupUser cannot be saved!"
145
+ assert_have_selector '#demo2 div.field-errors ul.errors-list li', :content => "This is a fake error"
146
+ end
147
+ end
148
+
149
+ context 'for #label method' do
150
+ should "display correct label html" do
151
+ actual_html = standard_builder.label(:first_name, :class => 'large', :caption => "F. Name")
152
+ assert_has_tag('label', :class => 'large', :for => 'user_first_name', :content => "F. Name: ") { actual_html }
153
+ end
154
+
155
+ should "display correct label in haml" do
156
+ visit '/haml/form_for'
157
+ assert_have_selector '#demo label', :content => "Login: ", :class => 'user-label'
158
+ assert_have_selector '#demo label', :content => "About Me: "
159
+ assert_have_selector '#demo2 label', :content => "Nickname: ", :class => 'label'
160
+ end
161
+
162
+ should "display correct label in erb" do
163
+ visit '/erb/form_for'
164
+ assert_have_selector '#demo label', :content => "Login: ", :class => 'user-label'
165
+ assert_have_selector '#demo label', :content => "About Me: "
166
+ assert_have_selector '#demo2 label', :content => "Nickname: ", :class => 'label'
167
+ end
168
+ end
169
+
170
+ context 'for #hidden_field method' do
171
+ should "display correct hidden field html" do
172
+ actual_html = standard_builder.hidden_field(:session_id, :class => 'hidden')
173
+ assert_has_tag('input.hidden[type=hidden]', :value => "54", :id => 'user_session_id', :name => 'user[session_id]') { actual_html }
174
+ end
175
+
176
+ should "display correct hidden field in haml" do
177
+ visit '/haml/form_for'
178
+ assert_have_selector '#demo input[type=hidden]', :id => 'markup_user_session_id', :value => "45"
179
+ assert_have_selector '#demo2 input', :type => 'hidden', :name => 'markup_user[session_id]'
180
+ end
181
+
182
+ should "display correct hidden field in erb" do
183
+ visit '/erb/form_for'
184
+ assert_have_selector '#demo input[type=hidden]', :id => 'markup_user_session_id', :value => "45"
185
+ assert_have_selector '#demo2 input', :type => 'hidden', :name => 'markup_user[session_id]'
186
+ end
187
+ end
188
+
189
+ context 'for #text_field method' do
190
+ should "display correct text field html" do
191
+ actual_html = standard_builder.text_field(:first_name, :class => 'large')
192
+ assert_has_tag('input.large[type=text]', :value => "Joe", :id => 'user_first_name', :name => 'user[first_name]') { actual_html }
193
+ end
194
+
195
+ should "display correct text field in haml" do
196
+ visit '/haml/form_for'
197
+ assert_have_selector '#demo input.user-text[type=text]', :id => 'markup_user_username', :value => "John"
198
+ assert_have_selector '#demo2 input', :type => 'text', :class => 'input', :name => 'markup_user[username]'
199
+ end
200
+
201
+ should "display correct text field in erb" do
202
+ visit '/erb/form_for'
203
+ assert_have_selector '#demo input.user-text[type=text]', :id => 'markup_user_username', :value => "John"
204
+ assert_have_selector '#demo2 input', :type => 'text', :class => 'input', :name => 'markup_user[username]'
205
+ end
206
+ end
207
+
208
+ context 'for #check_box method' do
209
+ should "display correct checkbox html" do
210
+ actual_html = standard_builder.check_box(:confirm_destroy, :class => 'large')
211
+ assert_has_tag('input.large[type=checkbox]', :id => 'user_confirm_destroy', :name => 'user[confirm_destroy]') { actual_html }
212
+ assert_has_tag('input[type=hidden]', :name => 'user[confirm_destroy]', :value => '0') { actual_html }
213
+ end
214
+
215
+ should "display correct checkbox html when checked" do
216
+ actual_html = standard_builder.check_box(:confirm_destroy, :checked => true)
217
+ assert_has_tag('input[type=checkbox]', :checked => 'checked', :name => 'user[confirm_destroy]') { actual_html }
218
+ end
219
+
220
+ should "display correct checkbox html as checked when object value matches" do
221
+ @user.stubs(:show_favorites => 'human')
222
+ actual_html = standard_builder.check_box(:show_favorites, :value => 'human')
223
+ assert_has_tag('input[type=checkbox]', :checked => 'checked', :name => 'user[show_favorites]') { actual_html }
224
+ end
225
+
226
+ should "display correct checkbox html as checked when object value is true" do
227
+ @user.stubs(:show_favorites => true)
228
+ actual_html = standard_builder.check_box(:show_favorites, :value => '1')
229
+ assert_has_tag('input[type=checkbox]', :checked => 'checked', :name => 'user[show_favorites]') { actual_html }
230
+ end
231
+
232
+ should "display correct checkbox html as unchecked when object value doesn't match" do
233
+ @user.stubs(:show_favorites => 'alien')
234
+ actual_html = standard_builder.check_box(:show_favorites, :value => 'human')
235
+ assert_has_no_tag('input[type=checkbox]', :checked => 'checked') { actual_html }
236
+ end
237
+
238
+ should "display correct checkbox html as unchecked when object value is false" do
239
+ @user.stubs(:show_favorites => false)
240
+ actual_html = standard_builder.check_box(:show_favorites, :value => '1')
241
+ assert_has_no_tag('input[type=checkbox]', :checked => 'checked') { actual_html }
242
+ end
243
+
244
+ should "display correct unchecked hidden field when specified" do
245
+ actual_html = standard_builder.check_box(:show_favorites, :value => 'female', :uncheck_value => 'false')
246
+ assert_has_tag('input[type=hidden]', :name => 'user[show_favorites]', :value => 'false') { actual_html }
247
+ end
248
+
249
+ should "display correct checkbox in haml" do
250
+ visit '/haml/form_for'
251
+ assert_have_selector '#demo input[type=checkbox]', :checked => 'checked', :id => 'markup_user_remember_me', :name => 'markup_user[remember_me]'
252
+ end
253
+
254
+ should "display correct checkbox in erb" do
255
+ visit '/erb/form_for'
256
+ assert_have_selector '#demo input[type=checkbox]', :checked => 'checked', :id => 'markup_user_remember_me', :name => 'markup_user[remember_me]'
257
+ end
258
+ end
259
+
260
+ context 'for #radio_button method' do
261
+ should "display correct radio button html" do
262
+ actual_html = standard_builder.radio_button(:gender, :value => 'male', :class => 'large')
263
+ assert_has_tag('input.large[type=radio]', :id => 'user_gender_male', :name => 'user[gender]', :value => 'male') { actual_html }
264
+ end
265
+
266
+ should "display correct radio button html when checked" do
267
+ actual_html = standard_builder.radio_button(:gender, :checked => true)
268
+ assert_has_tag('input[type=radio]', :checked => 'checked', :name => 'user[gender]') { actual_html }
269
+ end
270
+
271
+ should "display correct radio button html as checked when object value matches" do
272
+ @user.stubs(:gender => 'male')
273
+ actual_html = standard_builder.radio_button(:gender, :value => 'male')
274
+ assert_has_tag('input[type=radio]', :checked => 'checked', :name => 'user[gender]') { actual_html }
275
+ end
276
+
277
+ should "display correct radio button html as unchecked when object value doesn't match" do
278
+ @user.stubs(:gender => 'male')
279
+ actual_html = standard_builder.radio_button(:gender, :value => 'female')
280
+ assert_has_no_tag('input[type=radio]', :checked => 'checked') { actual_html }
281
+ end
282
+
283
+ should "display correct radio button in haml" do
284
+ visit '/haml/form_for'
285
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_male', :name => 'markup_user[gender]', :value => 'male'
286
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_female', :name => 'markup_user[gender]', :value => 'female'
287
+ assert_have_selector '#demo input[type=radio][checked=checked]', :id => 'markup_user_gender_male'
288
+ end
289
+
290
+ should "display correct radio button in erb" do
291
+ visit '/erb/form_for'
292
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_male', :name => 'markup_user[gender]', :value => 'male'
293
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_female', :name => 'markup_user[gender]', :value => 'female'
294
+ assert_have_selector '#demo input[type=radio][checked=checked]', :id => 'markup_user_gender_male'
295
+ end
296
+ end
297
+
298
+ context 'for #text_area method' do
299
+ should "display correct text_area html" do
300
+ actual_html = standard_builder.text_area(:about, :class => 'large')
301
+ assert_has_tag('textarea.large', :id => 'user_about', :name => 'user[about]') { actual_html }
302
+ end
303
+
304
+ should "display correct text_area in haml" do
305
+ visit '/haml/form_for'
306
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
307
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
308
+ end
309
+
310
+ should "display correct text_area in erb" do
311
+ visit '/erb/form_for'
312
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
313
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
314
+ end
315
+ end
316
+
317
+ context 'for #password_field method' do
318
+ should "display correct password_field html" do
319
+ actual_html = standard_builder.password_field(:code, :class => 'large')
320
+ assert_has_tag('input.large[type=password]', :id => 'user_code', :name => 'user[code]') { actual_html }
321
+ end
322
+
323
+ should "display correct password_field in haml" do
324
+ visit '/haml/form_for'
325
+ assert_have_selector '#demo input', :type => 'password', :class => 'user-password', :value => 'secret'
326
+ assert_have_selector '#demo2 input', :type => 'password', :class => 'input', :name => 'markup_user[code]'
327
+ end
328
+
329
+ should "display correct password_field in erb" do
330
+ visit '/erb/form_for'
331
+ assert_have_selector '#demo input', :type => 'password', :class => 'user-password', :value => 'secret'
332
+ assert_have_selector '#demo2 input', :type => 'password', :class => 'input', :name => 'markup_user[code]'
333
+ end
334
+ end
335
+
336
+ context 'for #file_field method' do
337
+ should "display correct file_field html" do
338
+ actual_html = standard_builder.file_field(:photo, :class => 'large')
339
+ assert_has_tag('input.large[type=file]', :id => 'user_photo', :name => 'user[photo]') { actual_html }
340
+ end
341
+
342
+ should "display correct file_field in haml" do
343
+ visit '/haml/form_for'
344
+ assert_have_selector '#demo input.user-photo', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
345
+ assert_have_selector '#demo2 input.upload', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
346
+ end
347
+
348
+ should "display correct file_field in erb" do
349
+ visit '/erb/form_for'
350
+ assert_have_selector '#demo input.user-photo', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
351
+ assert_have_selector '#demo2 input.upload', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
352
+ end
353
+ end
354
+
355
+ context 'for #select method' do
356
+ should "display correct select html" do
357
+ actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'], :class => 'selecty')
358
+ assert_has_tag('select.selecty', :id => 'user_state', :name => 'user[state]') { actual_html }
359
+ assert_has_tag('select.selecty option', :count => 3) { actual_html }
360
+ assert_has_tag('select.selecty option', :value => 'California', :content => 'California') { actual_html }
361
+ assert_has_tag('select.selecty option', :value => 'Texas', :content => 'Texas') { actual_html }
362
+ assert_has_tag('select.selecty option', :value => 'Wyoming', :content => 'Wyoming') { actual_html }
363
+ end
364
+
365
+ should "display correct select html with selected item if it matches value" do
366
+ @user.stubs(:state => 'California')
367
+ actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'])
368
+ assert_has_tag('select', :id => 'user_state', :name => 'user[state]') { actual_html }
369
+ assert_has_tag('select option', :selected => 'selected', :count => 1) { actual_html }
370
+ assert_has_tag('select option', :value => 'California', :selected => 'selected') { actual_html }
371
+ end
372
+
373
+ should "display correct select html with include_blank" do
374
+ actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'], :include_blank => true)
375
+ assert_has_tag('select', :id => 'user_state', :name => 'user[state]') { actual_html }
376
+ assert_has_tag('select option', :count => 4) { actual_html }
377
+ assert_has_tag('select option:first-child', :content => '') { actual_html }
378
+ end
379
+
380
+ should "display correct select html with collection passed in" do
381
+ actual_html = standard_builder.select(:role, :collection => @user.role_types, :fields => [:name, :id])
382
+ assert_has_tag('select', :id => 'user_role', :name => 'user[role]') { actual_html }
383
+ assert_has_tag('select option', :count => 3) { actual_html }
384
+ assert_has_tag('select option', :value => '1', :content => 'Admin', :selected => 'selected') { actual_html }
385
+ assert_has_tag('select option', :value => '2', :content => 'Moderate') { actual_html }
386
+ assert_has_tag('select option', :value => '3', :content => 'Limited') { actual_html }
387
+ end
388
+
389
+ should "display correct select in haml" do
390
+ visit '/haml/form_for'
391
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
392
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
393
+ end
394
+
395
+ should "display correct select in erb" do
396
+ visit '/erb/form_for'
397
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
398
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
399
+ end
400
+ end
401
+
402
+ context 'for #submit method' do
403
+ should "display correct submit button html with no options" do
404
+ actual_html = standard_builder.submit
405
+ assert_has_tag('input[type=submit]', :value => "Submit") { actual_html }
406
+ end
407
+
408
+ should "display correct submit button html" do
409
+ actual_html = standard_builder.submit("Commit", :class => 'large')
410
+ assert_has_tag('input.large[type=submit]', :value => "Commit") { actual_html }
411
+ end
412
+
413
+ should "display correct submit button in haml" do
414
+ visit '/haml/form_for'
415
+ assert_have_selector '#demo input', :type => 'submit', :id => 'demo-button', :class => 'success'
416
+ assert_have_selector '#demo2 input', :type => 'submit', :class => 'button', :value => "Create"
417
+ end
418
+
419
+ should "display correct submit button in erb" do
420
+ visit '/erb/form_for'
421
+ assert_have_selector '#demo input', :type => 'submit', :id => 'demo-button', :class => 'success'
422
+ assert_have_selector '#demo2 input', :type => 'submit', :class => 'button', :value => "Create"
423
+ end
424
+ end
425
+
426
+ context 'for #image_submit method' do
427
+ should "display correct image submit button html with no options" do
428
+ actual_html = standard_builder.image_submit('buttons/ok.png')
429
+ assert_has_tag('input[type=image]', :src => "/images/buttons/ok.png") { actual_html }
430
+ end
431
+
432
+ should "display correct image submit button html" do
433
+ actual_html = standard_builder.image_submit('/system/ok.png', :class => 'large')
434
+ assert_has_tag('input.large[type=image]', :src => "/system/ok.png") { actual_html }
435
+ end
436
+
437
+ should "display correct image submit button in haml" do
438
+ visit '/haml/form_for'
439
+ assert_have_selector '#demo input', :type => 'image', :id => 'image-button', :src => '/images/buttons/post.png'
440
+ assert_have_selector '#demo2 input', :type => 'image', :class => 'image', :src => "/images/buttons/ok.png"
441
+ end
442
+
443
+ should "display correct image submit button in erb" do
444
+ visit '/erb/form_for'
445
+ assert_have_selector '#demo input', :type => 'image', :id => 'image-button', :src => '/images/buttons/post.png'
446
+ assert_have_selector '#demo2 input', :type => 'image', :class => 'image', :src => "/images/buttons/ok.png"
447
+ end
448
+ end
449
+
450
+ # ===========================
451
+ # StandardFormBuilder
452
+ # ===========================
453
+
454
+ context 'for #text_field_block method' do
455
+ should "display correct text field block html" do
456
+ actual_html = standard_builder.text_field_block(:first_name, :class => 'large', :caption => "FName")
457
+ assert_has_tag('p label', :for => 'user_first_name', :content => "FName: ") { actual_html }
458
+ assert_has_tag('p input.large[type=text]', :value => "Joe", :id => 'user_first_name', :name => 'user[first_name]') { actual_html }
459
+ end
460
+
461
+ should "display correct text field block in haml" do
462
+ visit '/haml/form_for'
463
+ assert_have_selector '#demo2 p label', :for => 'markup_user_username', :content => "Nickname: ", :class => 'label'
464
+ assert_have_selector '#demo2 p input', :type => 'text', :name => 'markup_user[username]', :id => 'markup_user_username'
465
+ end
466
+
467
+ should "display correct text field block in erb" do
468
+ visit '/erb/form_for'
469
+ assert_have_selector '#demo2 p label', :for => 'markup_user_username', :content => "Nickname: ", :class => 'label'
470
+ assert_have_selector '#demo2 p input', :type => 'text', :name => 'markup_user[username]', :id => 'markup_user_username'
471
+ end
472
+ end
473
+
474
+ context 'for #text_area_block method' do
475
+ should "display correct text area block html" do
476
+ actual_html = standard_builder.text_area_block(:about, :class => 'large', :caption => "About Me")
477
+ assert_has_tag('p label', :for => 'user_about', :content => "About Me: ") { actual_html }
478
+ assert_has_tag('p textarea', :id => 'user_about', :name => 'user[about]') { actual_html }
479
+ end
480
+
481
+ should "display correct text area block in haml" do
482
+ visit '/haml/form_for'
483
+ assert_have_selector '#demo2 p label', :for => 'markup_user_about', :content => "About: "
484
+ assert_have_selector '#demo2 p textarea', :name => 'markup_user[about]', :id => 'markup_user_about'
485
+ end
486
+
487
+ should "display correct text area block in erb" do
488
+ visit '/erb/form_for'
489
+ assert_have_selector '#demo2 p label', :for => 'markup_user_about', :content => "About: "
490
+ assert_have_selector '#demo2 p textarea', :name => 'markup_user[about]', :id => 'markup_user_about'
491
+ end
492
+ end
493
+
494
+ context 'for #password_field_block method' do
495
+ should "display correct password field block html" do
496
+ actual_html = standard_builder.password_field_block(:keycode, :class => 'large', :caption => "Code")
497
+ assert_has_tag('p label', :for => 'user_keycode', :content => "Code: ") { actual_html }
498
+ assert_has_tag('p input.large[type=password]', :id => 'user_keycode', :name => 'user[keycode]') { actual_html }
499
+ end
500
+
501
+ should "display correct password field block in haml" do
502
+ visit '/haml/form_for'
503
+ assert_have_selector '#demo2 p label', :for => 'markup_user_code', :content => "Code: "
504
+ assert_have_selector '#demo2 p input', :type => 'password', :name => 'markup_user[code]', :id => 'markup_user_code'
505
+ end
506
+
507
+ should "display correct password field block in erb" do
508
+ visit '/erb/form_for'
509
+ assert_have_selector '#demo2 p label', :for => 'markup_user_code', :content => "Code: "
510
+ assert_have_selector '#demo2 p input', :type => 'password', :name => 'markup_user[code]', :id => 'markup_user_code'
511
+ end
512
+ end
513
+
514
+ context 'for #file_field_block method' do
515
+ should "display correct file field block html" do
516
+ actual_html = standard_builder.file_field_block(:photo, :class => 'large', :caption => "Photo")
517
+ assert_has_tag('p label', :for => 'user_photo', :content => "Photo: ") { actual_html }
518
+ assert_has_tag('p input.large[type=file]', :id => 'user_photo', :name => 'user[photo]') { actual_html }
519
+ end
520
+
521
+ should "display correct file field block in haml" do
522
+ visit '/haml/form_for'
523
+ assert_have_selector '#demo2 p label', :for => 'markup_user_photo', :content => "Photo: "
524
+ assert_have_selector '#demo2 p input.upload', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
525
+ end
526
+
527
+ should "display correct file field block in erb" do
528
+ visit '/erb/form_for'
529
+ assert_have_selector '#demo2 p label', :for => 'markup_user_photo', :content => "Photo: "
530
+ assert_have_selector '#demo2 p input.upload', :type => 'file', :name => 'markup_user[photo]', :id => 'markup_user_photo'
531
+ end
532
+ end
533
+
534
+ context 'for #check_box_block method' do
535
+ should "display correct check box block html" do
536
+ actual_html = standard_builder.check_box_block(:remember_me, :class => 'large', :caption => "Remember session")
537
+ assert_has_tag('p label', :for => 'user_remember_me', :content => "Remember session: ") { actual_html }
538
+ assert_has_tag('p input.large[type=checkbox]', :id => 'user_remember_me', :name => 'user[remember_me]') { actual_html }
539
+ end
540
+
541
+ should "display correct check box block in haml" do
542
+ visit '/haml/form_for'
543
+ assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember Me: "
544
+ assert_have_selector '#demo2 p input.checker', :type => 'checkbox', :name => 'markup_user[remember_me]'
545
+ end
546
+
547
+ should "display correct check box block in erb" do
548
+ visit '/erb/form_for'
549
+ assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember Me: "
550
+ assert_have_selector '#demo2 p input.checker', :type => 'checkbox', :name => 'markup_user[remember_me]'
551
+ end
552
+ end
553
+
554
+ context 'for #select_block method' do
555
+ should "display correct select_block block html" do
556
+ actual_html = standard_builder.select_block(:country, :options => ['USA', 'Canada'], :class => 'large', :caption => "Your country")
557
+ assert_has_tag('p label', :for => 'user_country', :content => "Your country: ") { actual_html }
558
+ assert_has_tag('p select', :id => 'user_country', :name => 'user[country]') { actual_html }
559
+ assert_has_tag('p select option', :content => 'USA') { actual_html }
560
+ assert_has_tag('p select option', :content => 'Canada') { actual_html }
561
+ end
562
+
563
+ should "display correct select_block block in haml" do
564
+ visit '/haml/form_for'
565
+ assert_have_selector '#demo2 p label', :for => 'markup_user_state', :content => "State: "
566
+ assert_have_selector '#demo2 p select', :name => 'markup_user[state]', :id => 'markup_user_state'
567
+ assert_have_selector '#demo2 p select option', :content => 'California'
568
+ assert_have_selector '#demo2 p select option', :content => 'Texas'
569
+ end
570
+
571
+ should "display correct select_block block in erb" do
572
+ visit '/erb/form_for'
573
+ assert_have_selector '#demo2 p label', :for => 'markup_user_state', :content => "State: "
574
+ assert_have_selector '#demo2 p select', :name => 'markup_user[state]', :id => 'markup_user_state'
575
+ end
576
+ end
577
+
578
+ context 'for #submit_block method' do
579
+ should "display correct submit block html" do
580
+ actual_html = standard_builder.submit_block("Update", :class => 'large')
581
+ assert_has_tag('p input.large[type=submit]', :value => 'Update') { actual_html }
582
+ end
583
+
584
+ should "display correct submit block in haml" do
585
+ visit '/haml/form_for'
586
+ assert_have_selector '#demo2 p input', :type => 'submit', :class => 'button'
587
+ end
588
+
589
+ should "display correct submit block in erb" do
590
+ visit '/erb/form_for'
591
+ assert_have_selector '#demo2 p input', :type => 'submit', :class => 'button'
592
+ end
593
+ end
594
+
595
+ context 'for #image_submit_block method' do
596
+ should "display correct image submit block html" do
597
+ actual_html = standard_builder.image_submit_block("buttons/ok.png", :class => 'large')
598
+ assert_has_tag('p input.large[type=image]', :src => '/images/buttons/ok.png') { actual_html }
599
+ end
600
+
601
+ should "display correct image submit block in haml" do
602
+ visit '/haml/form_for'
603
+ assert_have_selector '#demo2 p input', :type => 'image', :class => 'image', :src => '/images/buttons/ok.png'
604
+ end
605
+
606
+ should "display correct image submit block in erb" do
607
+ visit '/erb/form_for'
608
+ assert_have_selector '#demo2 p input', :type => 'image', :class => 'image', :src => '/images/buttons/ok.png'
609
+ end
610
+ end
611
+ end