forme 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/forme_spec.rb DELETED
@@ -1,1292 +0,0 @@
1
- require_relative 'spec_helper'
2
-
3
- describe "Forme plain forms" do
4
- def sel(opts, s)
5
- opts.map{|o| "<option #{'selected="selected" ' if o == s}value=\"#{o}\">#{sprintf("%02i", o)}</option>"}.join
6
- end
7
-
8
- before do
9
- @f = Forme::Form.new
10
- end
11
-
12
- it "Forme.version should return version string" do
13
- Forme.version.must_match(/\A\d+\.\d+\.\d+\z/)
14
- end
15
-
16
- it "should create a simple input tags" do
17
- @f.input(:text).must_equal '<input type="text"/>'
18
- @f.input(:radio).must_equal '<input type="radio"/>'
19
- @f.input(:password).must_equal '<input type="password"/>'
20
- @f.input(:checkbox).must_equal '<input type="checkbox"/>'
21
- @f.input(:submit).must_equal '<input type="submit"/>'
22
- end
23
-
24
- it "should use :html option if given" do
25
- @f.input(:text, :html=>"<a>foo</a>").must_equal '<a>foo</a>'
26
- end
27
-
28
- it "should support a callable :html option" do
29
- @f.input(:text, :html=>proc{|i| "<a>#{i.type}</a>"}).must_equal '<a>text</a>'
30
- end
31
-
32
- it "should still use labeler, wrapper, error_handler, and helper if :html option is given" do
33
- @f.input(:text, :html=>"<a>foo</a>", :label=>'a', :error=>'b', :help=>'c', :wrapper=>:div).must_equal '<div><label>a: <a>foo</a></label><span class="error_message">b</span><span class="helper">c</span></div>'
34
- end
35
-
36
- it "should use :name option as attribute" do
37
- @f.input(:text, :name=>"foo").must_equal '<input name="foo" type="text"/>'
38
- end
39
-
40
- it "should use :id option as attribute" do
41
- @f.input(:text, :id=>"foo").must_equal '<input id="foo" type="text"/>'
42
- end
43
-
44
- it "should use :class option as attribute" do
45
- @f.input(:text, :class=>"foo").must_equal '<input class="foo" type="text"/>'
46
- end
47
-
48
- it "should use :value option as attribute" do
49
- @f.input(:text, :value=>"foo").must_equal '<input type="text" value="foo"/>'
50
- end
51
-
52
- it "should use :placeholder option as attribute" do
53
- @f.input(:text, :placeholder=>"foo").must_equal '<input placeholder="foo" type="text"/>'
54
- end
55
-
56
- it "should use :style option as attribute" do
57
- @f.input(:text, :style=>"foo").must_equal '<input style="foo" type="text"/>'
58
- end
59
-
60
- it "should use :key option as name and id attributes" do
61
- @f.input(:text, :key=>"foo").must_equal '<input id="foo" name="foo" type="text"/>'
62
- end
63
-
64
- it "should use :key_id option as suffix for :key option id attributes" do
65
- @f.input(:text, :key=>"foo", :key_id=>'bar').must_equal '<input id="foo_bar" name="foo" type="text"/>'
66
- end
67
-
68
- it "should have :key option respect :multiple option" do
69
- @f.input(:text, :key=>"foo", :multiple=>true).must_equal '<input id="foo" name="foo[]" type="text"/>'
70
- end
71
-
72
- it "should use :key option respect form's current namespace" do
73
- @f.with_opts(:namespace=>['bar']) do
74
- @f.input(:text, :key=>"foo").must_equal '<input id="bar_foo" name="bar[foo]" type="text"/>'
75
- @f.input(:text, :key=>"foo", :multiple=>true).must_equal '<input id="bar_foo" name="bar[foo][]" type="text"/>'
76
- @f.with_opts(:namespace=>['bar', 'baz']) do
77
- @f.input(:text, :key=>"foo").must_equal '<input id="bar_baz_foo" name="bar[baz][foo]" type="text"/>'
78
- end
79
- end
80
- end
81
-
82
- it "should consider form's :values hash for default values based on the :key option if :value is not present" do
83
- @f.opts[:values] = {'foo'=>'baz'}
84
- @f.input(:text, :key=>"foo").must_equal '<input id="foo" name="foo" type="text" value="baz"/>'
85
- @f.input(:text, :key=>"foo", :value=>'x').must_equal '<input id="foo" name="foo" type="text" value="x"/>'
86
-
87
- @f.input(:text, :key=>:foo).must_equal '<input id="foo" name="foo" type="text" value="baz"/>'
88
- @f.opts[:values] = {:foo=>'baz'}
89
- @f.input(:text, :key=>:foo).must_equal '<input id="foo" name="foo" type="text" value="baz"/>'
90
- end
91
-
92
- it "should consider form's :values hash for default values based on the :key option when using namespaces" do
93
- @f.opts[:values] = {'bar'=>{'foo'=>'baz'}}
94
- @f.with_opts(:namespace=>['bar']) do
95
- @f.input(:text, :key=>"foo").must_equal '<input id="bar_foo" name="bar[foo]" type="text" value="baz"/>'
96
- @f.input(:text, :key=>"foo", :value=>'x').must_equal '<input id="bar_foo" name="bar[foo]" type="text" value="x"/>'
97
- @f.input(:text, :key=>:foo).must_equal '<input id="bar_foo" name="bar[foo]" type="text" value="baz"/>'
98
- end
99
-
100
- @f.with_opts(:namespace=>[:bar]) do
101
- @f.input(:text, :key=>:foo).must_equal '<input id="bar_foo" name="bar[foo]" type="text" value="baz"/>'
102
-
103
- @f.opts[:values] = {:bar=>{:foo=>'baz'}}
104
- @f.input(:text, :key=>:foo).must_equal '<input id="bar_foo" name="bar[foo]" type="text" value="baz"/>'
105
- @f.opts[:values] = {:bar=>{}}
106
- @f.input(:text, :key=>:foo).must_equal '<input id="bar_foo" name="bar[foo]" type="text"/>'
107
- @f.opts[:values] = {}
108
- @f.input(:text, :key=>:foo).must_equal '<input id="bar_foo" name="bar[foo]" type="text"/>'
109
-
110
- @f.opts[:values] = {'bar'=>{'quux'=>{'foo'=>'baz'}}}
111
- @f.with_opts(:namespace=>['bar', 'quux']) do
112
- @f.input(:text, :key=>"foo").must_equal '<input id="bar_quux_foo" name="bar[quux][foo]" type="text" value="baz"/>'
113
- end
114
- end
115
- end
116
-
117
- it "should consider form's :errors hash based on the :key option" do
118
- @f.opts[:errors] = { 'foo' => 'must be present' }
119
- @f.input(:text, :key=>"foo").must_equal "<input aria-describedby=\"foo_error_message\" aria-invalid=\"true\" class=\"error\" id=\"foo\" name=\"foo\" type=\"text\"/><span class=\"error_message\" id=\"foo_error_message\">must be present</span>"
120
- end
121
-
122
- it "should consider form's :errors hash based on the :key option when using namespaces" do
123
- @f.opts[:errors] = { 'bar' => { 'foo' => 'must be present' } }
124
- @f.with_opts(:namespace=>['bar']) do
125
- @f.input(:text, :key=>"foo").must_equal "<input aria-describedby=\"bar_foo_error_message\" aria-invalid=\"true\" class=\"error\" id=\"bar_foo\" name=\"bar[foo]\" type=\"text\"/><span class=\"error_message\" id=\"bar_foo_error_message\">must be present</span>"
126
- end
127
- end
128
-
129
- it "should handle case where form has errors not for the input" do
130
- @f.opts[:errors] = { 'baz' => { 'foo' => 'must be present' } }
131
- @f.input(:text, :key=>"foo").must_equal '<input id="foo" name="foo" type="text"/>'
132
- @f.with_opts(:namespace=>['bar']) do
133
- @f.input(:text, :key=>"foo").must_equal '<input id="bar_foo" name="bar[foo]" type="text"/>'
134
- end
135
- end
136
-
137
- it "should support a with_obj method that changes the object and namespace for the given block" do
138
- @f.with_obj([:a, :c], 'bar') do
139
- @f.input(:first).must_equal '<input id="bar_first" name="bar[first]" type="text" value="a"/>'
140
- @f.with_obj([:b], 'baz') do
141
- @f.input(:first).must_equal '<input id="bar_baz_first" name="bar[baz][first]" type="text" value="b"/>'
142
- end
143
- @f.with_obj([:b], %w'baz quux') do
144
- @f.input(:first).must_equal '<input id="bar_baz_quux_first" name="bar[baz][quux][first]" type="text" value="b"/>'
145
- end
146
- @f.with_obj([:b]) do
147
- @f.input(:first).must_equal '<input id="bar_first" name="bar[first]" type="text" value="b"/>'
148
- end
149
- @f.input(:last).must_equal '<input id="bar_last" name="bar[last]" type="text" value="c"/>'
150
- end
151
- end
152
-
153
- it "should support a each_obj method that changes the object and namespace for multiple objects for the given block" do
154
- @f.tag(:form) do
155
- @f.each_obj([[:a, :c], [:b, :d]], 'bar') do
156
- @f.input(:first)
157
- @f.input(:last)
158
- end
159
- end.must_equal '<form><input id="bar_0_first" name="bar[0][first]" type="text" value="a"/><input id="bar_0_last" name="bar[0][last]" type="text" value="c"/><input id="bar_1_first" name="bar[1][first]" type="text" value="b"/><input id="bar_1_last" name="bar[1][last]" type="text" value="d"/></form>'
160
-
161
- @f.tag(:form) do
162
- @f.each_obj([[:a, :c], [:b, :d]], %w'bar baz') do
163
- @f.input(:first)
164
- @f.input(:last)
165
- end
166
- end.must_equal '<form><input id="bar_baz_0_first" name="bar[baz][0][first]" type="text" value="a"/><input id="bar_baz_0_last" name="bar[baz][0][last]" type="text" value="c"/><input id="bar_baz_1_first" name="bar[baz][1][first]" type="text" value="b"/><input id="bar_baz_1_last" name="bar[baz][1][last]" type="text" value="d"/></form>'
167
-
168
- @f.tag(:form) do
169
- @f.each_obj([[:a, :c], [:b, :d]]) do
170
- @f.input(:first)
171
- @f.input(:last)
172
- end
173
- end.must_equal '<form><input id="0_first" name="0[first]" type="text" value="a"/><input id="0_last" name="0[last]" type="text" value="c"/><input id="1_first" name="1[first]" type="text" value="b"/><input id="1_last" name="1[last]" type="text" value="d"/></form>'
174
- end
175
-
176
- it "should allow overriding form inputs on a per-block basis" do
177
- @f.input(:text).must_equal '<input type="text"/>'
178
- @f.with_opts(:wrapper=>:div){@f.input(:text)}.must_equal '<div><input type="text"/></div>'
179
- @f.with_opts(:wrapper=>:div){@f.input(:text).must_equal '<div><input type="text"/></div>'}
180
- @f.with_opts(:wrapper=>:div) do
181
- @f.input(:text).must_equal '<div><input type="text"/></div>'
182
- @f.with_opts(:wrapper=>:li){@f.input(:text).must_equal '<li><input type="text"/></li>'}
183
- @f.input(:text).must_equal '<div><input type="text"/></div>'
184
- end
185
- @f.input(:text).must_equal '<input type="text"/>'
186
- end
187
-
188
- it "should handle delayed formatting when overriding form inputs on a per-block basis" do
189
- @f.form do
190
- @f.input(:text)
191
- @f.with_opts(:wrapper=>:div) do
192
- @f.input(:text)
193
- @f.with_opts(:wrapper=>:li){@f.input(:text)}
194
- @f.input(:text)
195
- end
196
- @f.input(:text)
197
- end.must_equal '<form><input type="text"/><div><input type="text"/></div><li><input type="text"/></li><div><input type="text"/></div><input type="text"/></form>'
198
- end
199
-
200
- it "should support :obj method to with_opts for changing the obj inside the block" do
201
- @f.form do
202
- @f.with_opts(:obj=>[:a, :c]) do
203
- @f.input(:first)
204
- @f.with_opts(:obj=>[:b]){@f.input(:first)}
205
- @f.input(:last)
206
- end
207
- end.must_equal '<form><input id="first" name="first" type="text" value="a"/><input id="first" name="first" type="text" value="b"/><input id="last" name="last" type="text" value="c"/></form>'
208
- end
209
-
210
- it "should allow arbitrary attributes using the :attr option" do
211
- @f.input(:text, :attr=>{:bar=>"foo"}).must_equal '<input bar="foo" type="text"/>'
212
- end
213
-
214
- it "should convert the :data option into attributes" do
215
- @f.input(:text, :data=>{:bar=>"foo"}).must_equal '<input data-bar="foo" type="text"/>'
216
- end
217
-
218
- it "should replace underscores with hyphens in symbol :data keys when :dasherize_data is set" do
219
- @f.input(:text, :data=>{:foo_bar=>"baz"}).must_equal '<input data-foo_bar="baz" type="text"/>'
220
- @f.input(:text, :data=>{"foo_bar"=>"baz"}).must_equal '<input data-foo_bar="baz" type="text"/>'
221
-
222
- @f.input(:text, :data=>{:foo_bar=>"baz"}, :dasherize_data => true).must_equal '<input data-foo-bar="baz" type="text"/>'
223
- @f.input(:text, :data=>{"foo_bar"=>"baz"}, :dasherize_data => true).must_equal '<input data-foo_bar="baz" type="text"/>'
224
- end
225
-
226
- it "should not have standard options override the :attr option" do
227
- @f.input(:text, :name=>:bar, :attr=>{:name=>"foo"}).must_equal '<input name="foo" type="text"/>'
228
- end
229
-
230
- it "should combine :class standard option with :attr option" do
231
- @f.input(:text, :class=>:bar, :attr=>{:class=>"foo"}).must_equal '<input class="foo bar" type="text"/>'
232
- end
233
-
234
- it "should not have :data options override the :attr option" do
235
- @f.input(:text, :data=>{:bar=>"baz"}, :attr=>{:"data-bar"=>"foo"}).must_equal '<input data-bar="foo" type="text"/>'
236
- end
237
-
238
- it "should use :size and :maxlength options as attributes for text inputs" do
239
- @f.input(:text, :size=>5, :maxlength=>10).must_equal '<input maxlength="10" size="5" type="text"/>'
240
- @f.input(:textarea, :size=>5, :maxlength=>10).must_equal '<textarea></textarea>'
241
- end
242
-
243
- it "should create hidden input with value 0 for each checkbox with a name" do
244
- @f.input(:checkbox, :name=>"foo").must_equal '<input name="foo" type="hidden" value="0"/><input name="foo" type="checkbox"/>'
245
- end
246
-
247
- it "should not create hidden input with value 0 for readonly or disabled checkboxes" do
248
- @f.input(:checkbox, :name=>"foo", :formatter=>:disabled).must_equal '<input disabled="disabled" name="foo" type="checkbox"/>'
249
- @f.input(:checkbox, :name=>"foo", :formatter=>:readonly).must_equal '<input disabled="disabled" name="foo" type="checkbox"/>'
250
- end
251
-
252
- it "should create hidden input with value 0 for readonly or disabled checkboxes if no_hidden is explicitly given and not true" do
253
- @f.input(:checkbox, :name=>"foo", :formatter=>:disabled, :no_hidden=>false).must_equal '<input name="foo" type="hidden" value="0"/><input disabled="disabled" name="foo" type="checkbox"/>'
254
- @f.input(:checkbox, :name=>"foo", :formatter=>:readonly, :no_hidden=>false).must_equal '<input name="foo" type="hidden" value="0"/><input disabled="disabled" name="foo" type="checkbox"/>'
255
- end
256
-
257
- it "should not create hidden input with value 0 for each checkbox with a name if :no_hidden option is used" do
258
- @f.input(:checkbox, :name=>"foo", :no_hidden=>true).must_equal '<input name="foo" type="checkbox"/>'
259
- end
260
-
261
- it "should create hidden input with _hidden appened to id for each checkbox with a name and id" do
262
- @f.input(:checkbox, :name=>"foo", :id=>"bar").must_equal '<input id="bar_hidden" name="foo" type="hidden" value="0"/><input id="bar" name="foo" type="checkbox"/>'
263
- end
264
-
265
- it "should create hidden input with value f for each checkbox with a name and value t" do
266
- @f.input(:checkbox, :name=>"foo", :value=>"t").must_equal '<input name="foo" type="hidden" value="f"/><input name="foo" type="checkbox" value="t"/>'
267
- end
268
-
269
- it "should use :hidden_value option for value of hidden input for checkbox" do
270
- @f.input(:checkbox, :name=>"foo", :hidden_value=>"no").must_equal '<input name="foo" type="hidden" value="no"/><input name="foo" type="checkbox"/>'
271
- end
272
-
273
- it "should handle :checked option" do
274
- @f.input(:checkbox, :checked=>true).must_equal '<input checked="checked" type="checkbox"/>'
275
- @f.input(:checkbox, :checked=>false).must_equal '<input type="checkbox"/>'
276
- end
277
-
278
- it "should create textarea tag" do
279
- @f.input(:textarea).must_equal '<textarea></textarea>'
280
- @f.input(:textarea, :value=>'a').must_equal '<textarea>a</textarea>'
281
- end
282
-
283
- it "should use :cols and :rows options as attributes for textarea inputs" do
284
- @f.input(:text, :cols=>5, :rows=>10).must_equal '<input type="text"/>'
285
- @f.input(:textarea, :cols=>5, :rows=>10).must_equal '<textarea cols="5" rows="10"></textarea>'
286
- end
287
-
288
- it "should create select tag" do
289
- @f.input(:select).must_equal '<select></select>'
290
- end
291
-
292
- it "should respect multiple and size options in select tag" do
293
- @f.input(:select, :multiple=>true, :size=>10).must_equal '<select multiple="multiple" size="10"></select>'
294
- end
295
-
296
- it "should create date tag" do
297
- @f.input(:date).must_equal '<input type="date"/>'
298
- end
299
-
300
- it "should create datetime-local tag" do
301
- @f.input(:datetime).must_equal '<input type="datetime-local"/>'
302
- end
303
-
304
- it "should not error for input type :input" do
305
- @f.input(:input).must_equal '<input type="input"/>'
306
- end
307
-
308
- it "should use multiple select boxes for dates if the :as=>:select option is given" do
309
- @f.input(:date, :name=>"foo", :id=>"bar", :as=>:select, :value=>Date.new(2011, 6, 5)).must_equal %{<select id="bar" name="foo[year]">#{sel(1900..2050, 2011)}</select>-<select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select>}
310
- end
311
-
312
- it "should parse :value given as non-Date when using :as=>:select option for date inputs" do
313
- @f.input(:date, :name=>"foo", :id=>"bar", :as=>:select, :value=>"2011-06-05").must_equal %{<select id="bar" name="foo[year]">#{sel(1900..2050, 2011)}</select>-<select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select>}
314
- end
315
-
316
- it "should support not using :value when using :as=>:select option for date inputs" do
317
- @f.input(:date, :name=>"foo", :id=>"bar", :as=>:select).must_equal %{<select id="bar" name="foo[year]">#{sel(1900..2050, nil)}</select>-<select id="bar_month" name="foo[month]">#{sel(1..12, nil)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, nil)}</select>}
318
- end
319
-
320
- it "should use labels for select boxes for dates if the :as=>:select and :select_labels options are given" do
321
- @f.input(:date, :name=>"foo", :id=>"bar", :as=>:select, :value=>Date.new(2011, 6, 5), :select_labels=>{:year=>'Y', :month=>'M', :day=>'D'}, :labeler=>:explicit).must_equal %{<label class="label-before" for="bar">Y</label><select id="bar" name="foo[year]">#{sel(1900..2050, 2011)}</select>-<label class="label-before" for="bar_month">M</label><select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>-<label class="label-before" for="bar_day">D</label><select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select>}
322
- end
323
-
324
- it "should allow ordering date select boxes via :order" do
325
- @f.input(:date, :name=>"foo", :id=>"bar", :as=>:select, :value=>Date.new(2011, 6, 5), :order=>[:month, '/', :day, '/', :year]).must_equal %{<select id="bar" name="foo[month]">#{sel(1..12, 6)}</select>/<select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select>/<select id="bar_year" name="foo[year]">#{sel(1900..2050, 2011)}</select>}
326
- end
327
-
328
- it "should allow only using specific date select boxes via :order" do
329
- @f.input(:date, :name=>"foo", :id=>"bar", :as=>:select, :value=>Date.new(2011, 6, 5), :order=>[:month, :year]).must_equal %{<select id="bar" name="foo[month]">#{sel(1..12, 6)}</select><select id="bar_year" name="foo[year]">#{sel(1900..2050, 2011)}</select>}
330
- end
331
-
332
- it "should support :select_options for dates when :as=>:select is given" do
333
- @f.input(:date, :name=>"foo", :id=>"bar", :as=>:select, :value=>Date.new(2011, 6, 5), :select_options=>{:year=>1970..2020}).must_equal %{<select id="bar" name="foo[year]">#{sel(1970..2020, 2011)}</select>-<select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select>}
334
- end
335
-
336
- it "should support :select_options with both values and text for dates when :as=>:select is given" do
337
- @f.input(:date, :name=>"foo", :id=>"bar", :as=>:select, :value=>Date.new(2011, 6, 5), :select_options=>{:year=>[[2011, 'A'], [2012, 'B']]}).must_equal %{<select id="bar" name="foo[year]"><option selected="selected" value="2011">A</option><option value="2012">B</option></select>-<select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select>}
338
- end
339
-
340
- it "should have explicit labeler and trtd wrapper work with multiple select boxes for dates" do
341
- @f.input(:date, :name=>"foo", :id=>"bar", :as=>:select, :value=>Date.new(2011, 6, 5), :wrapper=>:trtd, :labeler=>:explicit, :label=>'Baz').must_equal %{<tr><td><label class="label-before" for="bar">Baz</label></td><td><select id="bar" name="foo[year]">#{sel(1900..2050, 2011)}</select>-<select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select></td></tr>}
342
- end
343
-
344
- it "should use multiple select boxes for datetimes if the :as=>:select option is given" do
345
- @f.input(:datetime, :name=>"foo", :id=>"bar", :as=>:select, :value=>DateTime.new(2011, 6, 5, 4, 3, 2)).must_equal %{<select id="bar" name="foo[year]">#{sel(1900..2050, 2011)}</select>-<select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select> <select id="bar_hour" name="foo[hour]">#{sel(0..23, 4)}</select>:<select id="bar_minute" name="foo[minute]">#{sel(0..59, 3)}</select>:<select id="bar_second" name="foo[second]">#{sel(0..59, 2)}</select>}
346
- end
347
-
348
- it "should parse :value given as non-DateTime when using :as=>:select option for datetime inputs" do
349
- @f.input(:datetime, :name=>"foo", :id=>"bar", :as=>:select, :value=>'2011-06-05 04:03:02').must_equal %{<select id="bar" name="foo[year]">#{sel(1900..2050, 2011)}</select>-<select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select> <select id="bar_hour" name="foo[hour]">#{sel(0..23, 4)}</select>:<select id="bar_minute" name="foo[minute]">#{sel(0..59, 3)}</select>:<select id="bar_second" name="foo[second]">#{sel(0..59, 2)}</select>}
350
- end
351
-
352
- it "should support not using :value when using :as=>:select option for datetime inputs" do
353
- @f.input(:datetime, :name=>"foo", :id=>"bar", :as=>:select).must_equal %{<select id="bar" name="foo[year]">#{sel(1900..2050, nil)}</select>-<select id="bar_month" name="foo[month]">#{sel(1..12, nil)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, nil)}</select> <select id="bar_hour" name="foo[hour]">#{sel(0..23, nil)}</select>:<select id="bar_minute" name="foo[minute]">#{sel(0..59, nil)}</select>:<select id="bar_second" name="foo[second]">#{sel(0..59, nil)}</select>}
354
- end
355
-
356
- it "should allow ordering select boxes for datetimes via :order" do
357
- @f.input(:datetime, :name=>"foo", :id=>"bar", :as=>:select, :value=>DateTime.new(2011, 6, 5, 4, 3, 2), :order=>[:day, '/', :month, 'T', :hour, ':', :minute]).must_equal %{<select id="bar" name="foo[day]">#{sel(1..31, 5)}</select>/<select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>T<select id="bar_hour" name="foo[hour]">#{sel(0..23, 4)}</select>:<select id="bar_minute" name="foo[minute]">#{sel(0..59, 3)}</select>}
358
- end
359
-
360
- it "should support :select_options for datetimes when :as=>:select option is given" do
361
- @f.input(:datetime, :name=>"foo", :id=>"bar", :as=>:select, :value=>DateTime.new(2011, 6, 5, 10, 3, 2), :select_options=>{:year=>1970..2020, :hour=>9..17}).must_equal %{<select id="bar" name="foo[year]">#{sel(1970..2020, 2011)}</select>-<select id="bar_month" name="foo[month]">#{sel(1..12, 6)}</select>-<select id="bar_day" name="foo[day]">#{sel(1..31, 5)}</select> <select id="bar_hour" name="foo[hour]">#{sel(9..17, 10)}</select>:<select id="bar_minute" name="foo[minute]">#{sel(0..59, 3)}</select>:<select id="bar_second" name="foo[second]">#{sel(0..59, 2)}</select>}
362
- end
363
-
364
- it "should create select tag with options" do
365
- @f.input(:select, :options=>[1, 2, 3], :selected=>2).must_equal '<select><option>1</option><option selected="selected">2</option><option>3</option></select>'
366
- @f.input(:select, :options=>[1, 2, 3], :value=>2).must_equal '<select><option>1</option><option selected="selected">2</option><option>3</option></select>'
367
- end
368
-
369
- it "should create select tag with options and values" do
370
- @f.input(:select, :options=>[[:a, 1], [:b, 2], [:c, 3]], :selected=>2).must_equal '<select><option value="1">a</option><option selected="selected" value="2">b</option><option value="3">c</option></select>'
371
- end
372
-
373
- it "should have select work with false values" do
374
- @f.input(:select, :options=>[[1, true], [2, false]], :value=>false).must_equal '<select><option value="true">1</option><option selected="selected" value="false">2</option></select>'
375
- end
376
-
377
- it "should create select tag with option groups" do
378
- @f.input(:select, :optgroups=>[['d', [[:a, 1], [:b, 2]]], ['e', [[:c, 3]]]], :selected=>2).must_equal '<select><optgroup label="d"><option value="1">a</option><option selected="selected" value="2">b</option></optgroup><optgroup label="e"><option value="3">c</option></optgroup></select>'
379
- end
380
-
381
- it "should create select tag with option groups with attributes" do
382
- @f.input(:select, :optgroups=>[[{:label=>'d', :class=>'f'}, [[:a, 1], [:b, 2]]], [{:label=>'e', :class=>'g'}, [[:c, 3]]]], :selected=>2).must_equal '<select><optgroup class="f" label="d"><option value="1">a</option><option selected="selected" value="2">b</option></optgroup><optgroup class="g" label="e"><option value="3">c</option></optgroup></select>'
383
- end
384
-
385
- it "should create select tag with options and values with hashes" do
386
- @f.input(:select, :options=>[[:a, {:foo=>1}], [:b, {:bar=>4, :value=>2}], [:c, {:baz=>3}]], :selected=>2).must_equal '<select><option foo="1">a</option><option bar="4" selected="selected" value="2">b</option><option baz="3">c</option></select>'
387
- end
388
-
389
- it "should create select tag with options and values using given method" do
390
- @f.input(:select, :options=>[[:a, 1], [:b, 2], [:c, 3]], :text_method=>:last, :selected=>2).must_equal '<select><option>1</option><option selected="selected">2</option><option>3</option></select>'
391
- @f.input(:select, :options=>[[:a, 1], [:b, 2], [:c, 3]], :text_method=>:last, :value_method=>:first, :selected=>:b).must_equal '<select><option value="a">1</option><option selected="selected" value="b">2</option><option value="c">3</option></select>'
392
- end
393
-
394
- it "should use html attributes specified in options" do
395
- @f.input(:text, :value=>'foo', :name=>'bar').must_equal '<input name="bar" type="text" value="foo"/>'
396
- @f.input(:textarea, :value=>'foo', :name=>'bar').must_equal '<textarea name="bar">foo</textarea>'
397
- @f.input(:select, :name=>'bar', :options=>[1, 2, 3]).must_equal '<select name="bar"><option>1</option><option>2</option><option>3</option></select>'
398
- end
399
-
400
- it "should support :add_blank option for select inputs" do
401
- @f.input(:select, :options=>[[:b, 2], [:c, 3]], :add_blank=>true, :value=>2).must_equal '<select><option value=""></option><option selected="selected" value="2">b</option><option value="3">c</option></select>'
402
- end
403
-
404
- it "should use Forme.default_add_blank_prompt value if :add_blank option is true" do
405
- begin
406
- Forme.default_add_blank_prompt = 'foo'
407
- @f.input(:select, :options=>[[:b, 2], [:c, 3]], :add_blank=>true, :value=>2).must_equal '<select><option value="">foo</option><option selected="selected" value="2">b</option><option value="3">c</option></select>'
408
- ensure
409
- Forme.default_add_blank_prompt = nil
410
- end
411
- end
412
-
413
- it "should use :add_blank option value as prompt if it is a String" do
414
- @f.input(:select, :options=>[[:b, 2], [:c, 3]], :add_blank=>"Prompt Here", :value=>2).must_equal '<select><option value="">Prompt Here</option><option selected="selected" value="2">b</option><option value="3">c</option></select>'
415
- end
416
-
417
- it "should support :add_blank option with :blank_position :after for select inputs" do
418
- @f.input(:select, :options=>[[:b, 2], [:c, 3]], :add_blank=>true, :blank_position=>:after, :value=>2).must_equal '<select><option selected="selected" value="2">b</option><option value="3">c</option><option value=""></option></select>'
419
- end
420
-
421
- it "should support :add_blank option with :blank_attr option for select inputs" do
422
- @f.input(:select, :options=>[[:b, 2], [:c, 3]], :add_blank=>true, :blank_attr=>{:foo=>:bar}, :value=>2).must_equal '<select><option foo="bar" value=""></option><option selected="selected" value="2">b</option><option value="3">c</option></select>'
423
- end
424
-
425
- it "should create set of radio buttons" do
426
- @f.input(:radioset, :options=>[1, 2, 3], :selected=>2).must_equal '<label class="option"><input type="radio" value="1"/> 1</label><label class="option"><input checked="checked" type="radio" value="2"/> 2</label><label class="option"><input type="radio" value="3"/> 3</label>'
427
- @f.input(:radioset, :options=>[1, 2, 3], :value=>2).must_equal '<label class="option"><input type="radio" value="1"/> 1</label><label class="option"><input checked="checked" type="radio" value="2"/> 2</label><label class="option"><input type="radio" value="3"/> 3</label>'
428
- end
429
-
430
- it "should handle nil option value" do
431
- @f.input(:radioset, :options=>[1, 2, nil], :selected=>2).must_equal '<label class="option"><input type="radio" value="1"/> 1</label><label class="option"><input checked="checked" type="radio" value="2"/> 2</label><input type="radio"/>'
432
- end
433
-
434
- it "should raise error for an radioset without options" do
435
- proc{@f.input(:radioset)}.must_raise Forme::Error
436
- end
437
-
438
- it "should have radioset work with false values" do
439
- @f.input(:radioset, :options=>[[1, true], [2, false]], :value=>false).must_equal '<label class="option"><input type="radio" value="true"/> 1</label><label class="option"><input checked="checked" type="radio" value="false"/> 2</label>'
440
- end
441
-
442
- it "should create set of radio buttons with options and values" do
443
- @f.input(:radioset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :selected=>2).must_equal '<label class="option"><input type="radio" value="1"/> a</label><label class="option"><input checked="checked" type="radio" value="2"/> b</label><label class="option"><input type="radio" value="3"/> c</label>'
444
- end
445
-
446
- it "should create set of radio buttons with options and values with hashes" do
447
- @f.input(:radioset, :options=>[[:a, {:attr=>{:foo=>1}}], [:b, {:class=>'foo', :value=>2}], [:c, {:id=>:baz}]], :selected=>2).must_equal '<label class="option"><input foo="1" type="radio" value="a"/> a</label><label class="option"><input checked="checked" class="foo" type="radio" value="2"/> b</label><label class="option"><input id="baz" type="radio" value="c"/> c</label>'
448
- end
449
-
450
- it "should create set of radio buttons with options and values using given method" do
451
- @f.input(:radioset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :text_method=>:last, :selected=>2).must_equal '<label class="option"><input type="radio" value="1"/> 1</label><label class="option"><input checked="checked" type="radio" value="2"/> 2</label><label class="option"><input type="radio" value="3"/> 3</label>'
452
- @f.input(:radioset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :text_method=>:last, :value_method=>:first, :selected=>:b).must_equal '<label class="option"><input type="radio" value="a"/> 1</label><label class="option"><input checked="checked" type="radio" value="b"/> 2</label><label class="option"><input type="radio" value="c"/> 3</label>'
453
- end
454
-
455
- it "should support :add_blank option for radioset inputs" do
456
- @f.input(:radioset, :options=>[[:b, 2], [:c, 3]], :add_blank=>true, :value=>2).must_equal '<label class="option"><input type="radio" value=""/> </label><label class="option"><input checked="checked" type="radio" value="2"/> b</label><label class="option"><input type="radio" value="3"/> c</label>'
457
- end
458
-
459
- it "should use :add_blank option value as prompt if it is a String" do
460
- @f.input(:radioset, :options=>[[:b, 2], [:c, 3]], :add_blank=>"Prompt Here", :value=>2).must_equal '<label class="option"><input type="radio" value=""/> Prompt Here</label><label class="option"><input checked="checked" type="radio" value="2"/> b</label><label class="option"><input type="radio" value="3"/> c</label>'
461
- end
462
-
463
- it "should respect the :key option for radio sets" do
464
- @f.input(:radioset, :options=>[1, 2, 3], :key=>:foo, :value=>2).must_equal '<label class="option"><input id="foo_1" name="foo" type="radio" value="1"/> 1</label><label class="option"><input checked="checked" id="foo_2" name="foo" type="radio" value="2"/> 2</label><label class="option"><input id="foo_3" name="foo" type="radio" value="3"/> 3</label>'
465
- end
466
-
467
- it "should create set of radio buttons with fieldsets and legends for :optgroups" do
468
- @f.input(:radioset, :optgroups=>[['d', [[:a, 1], [:b, 2]]], ['e', [[:c, 3]]]], :selected=>2).must_equal '<fieldset><legend>d</legend><label class="option"><input type="radio" value="1"/> a</label><label class="option"><input checked="checked" type="radio" value="2"/> b</label></fieldset><fieldset><legend>e</legend><label class="option"><input type="radio" value="3"/> c</label></fieldset>'
469
- end
470
-
471
- it "should create set of radio buttons with label attributes" do
472
- @f.input(:radioset, :options=>[1, 2, 3], :selected=>2, :label_attr=>{:foo=>:bar}).must_equal '<label class="option" foo="bar"><input type="radio" value="1"/> 1</label><label class="option" foo="bar"><input checked="checked" type="radio" value="2"/> 2</label><label class="option" foo="bar"><input type="radio" value="3"/> 3</label>'
473
- end
474
-
475
- it "should create set of radio buttons with :error and :error_attr options" do
476
- @f.input(:radioset, :options=>[1, 2, 3], :selected=>2, :error=>'foo', :error_attr=>{'bar'=>'baz'}).must_equal '<label class="option"><input type="radio" value="1"/> 1</label><label class="option"><input checked="checked" type="radio" value="2"/> 2</label><label class="option"><input aria-invalid="true" class="error" type="radio" value="3"/> 3</label><span bar="baz" class="error_message">foo</span>'
477
- end
478
-
479
- it "should support custom error_handler for set of radio buttons" do
480
- @f.input(:radioset, :options=>[1, 2, 3], :selected=>2, :error=>'foo', :error_attr=>{'bar'=>'baz'}, :error_handler=>lambda{|tag, input| input.tag(:div, {}, tag)}).must_equal '<div><label class="option"><input type="radio" value="1"/> 1</label><label class="option"><input checked="checked" type="radio" value="2"/> 2</label><label class="option"><input type="radio" value="3"/> 3</label></div>'
481
- end
482
-
483
- it "should create set of checkbox buttons" do
484
- @f.input(:checkboxset, :options=>[1, 2, 3], :selected=>2).must_equal '<label class="option"><input type="checkbox" value="1"/> 1</label><label class="option"><input checked="checked" type="checkbox" value="2"/> 2</label><label class="option"><input type="checkbox" value="3"/> 3</label>'
485
- @f.input(:checkboxset, :options=>[1, 2, 3], :value=>2).must_equal '<label class="option"><input type="checkbox" value="1"/> 1</label><label class="option"><input checked="checked" type="checkbox" value="2"/> 2</label><label class="option"><input type="checkbox" value="3"/> 3</label>'
486
- end
487
-
488
- it "should support :multiple option for checkboxset buttons" do
489
- @f.input(:checkboxset, :options=>[1, 2, 3], :selected=>[1,2], :multiple=>false).must_equal '<label class="option"><input type="checkbox" value="1"/> 1</label><label class="option"><input type="checkbox" value="2"/> 2</label><label class="option"><input type="checkbox" value="3"/> 3</label>'
490
- @f.input(:checkboxset, :options=>[1, 2, 3], :selected=>[1,2], :multiple=>true).must_equal '<label class="option"><input checked="checked" type="checkbox" value="1"/> 1</label><label class="option"><input checked="checked" type="checkbox" value="2"/> 2</label><label class="option"><input type="checkbox" value="3"/> 3</label>'
491
- end
492
-
493
- it "should create set of checkbox buttons with options and values" do
494
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :selected=>2).must_equal '<label class="option"><input type="checkbox" value="1"/> a</label><label class="option"><input checked="checked" type="checkbox" value="2"/> b</label><label class="option"><input type="checkbox" value="3"/> c</label>'
495
- end
496
-
497
- it "should have radioset work with false values" do
498
- @f.input(:checkboxset, :options=>[[1, true], [2, false]], :value=>false).must_equal '<label class="option"><input type="checkbox" value="true"/> 1</label><label class="option"><input checked="checked" type="checkbox" value="false"/> 2</label>'
499
- end
500
-
501
- it "should support :wrapper and :tag_wrapper for checkboxsets" do
502
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :tag_wrapper=>:div, :wrapper=>:li).must_equal '<li><div><label class="option"><input type="checkbox" value="1"/> a</label></div><div><label class="option"><input type="checkbox" value="2"/> b</label></div><div><label class="option"><input type="checkbox" value="3"/> c</label></div></li>'
503
- end
504
-
505
- it "should support :label for checkboxsets" do
506
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :label=>'foo').must_equal '<span class="label">foo</span><label class="option"><input type="checkbox" value="1"/> a</label><label class="option"><input type="checkbox" value="2"/> b</label><label class="option"><input type="checkbox" value="3"/> c</label>'
507
- end
508
-
509
- it "should support fieldset/legend for checkboxsets" do
510
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :label=>'foo', :labeler=>:legend, :wrapper=>:fieldset).must_equal '<fieldset><legend>foo</legend><label class="option"><input type="checkbox" value="1"/> a</label><label class="option"><input type="checkbox" value="2"/> b</label><label class="option"><input type="checkbox" value="3"/> c</label></fieldset>'
511
- end
512
-
513
- it "should support legend with attributes for checkboxsets" do
514
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :label=>'foo', :label_attr=>{:class=>"baz"}, :tag_label_attr=>{:class=>"bar"}, :labeler=>:legend, :wrapper=>:fieldset).must_equal '<fieldset><legend class="baz">foo</legend><label class="bar"><input type="checkbox" value="1"/> a</label><label class="bar"><input type="checkbox" value="2"/> b</label><label class="bar"><input type="checkbox" value="3"/> c</label></fieldset>'
515
- end
516
-
517
- it "should support legend with attributes for checkboxsets, handling errors with :error_handler=>:after_legend" do
518
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :id=>:quux, :label=>'foo', :label_attr=>{:class=>"baz"}, :tag_label_attr=>{:class=>"bar"}, :labeler=>:legend, :wrapper=>:fieldset, :error=>'bar2', :error_handler=>:after_legend).must_equal '<fieldset><legend class="baz">foo</legend><span class="error_message" id="quux_1_error_message">bar2</span><label class="bar"><input aria-describedby="quux_1_error_message" aria-invalid="true" class="error" id="quux_1" type="checkbox" value="1"/> a</label><label class="bar"><input id="quux_2" type="checkbox" value="2"/> b</label><label class="bar"><input id="quux_3" type="checkbox" value="3"/> c</label></fieldset>'
519
- end
520
-
521
- it "should have :error_handler=>:after_legend funfction like regular error handler if first tag is not a legend" do
522
- @f.input(:text, :error_handler=>:after_legend, :error=>'a', :id=>'b').must_equal '<input aria-describedby="b_error_message" aria-invalid="true" class="error" id="b" type="text"/><span class="error_message" id="b_error_message">a</span>'
523
- end
524
-
525
- it "should support :tag_labeler for checkboxsets" do
526
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :tag_labeler=>:explicit).must_equal '<input type="checkbox" value="1"/><label class="option label-after">a</label><input type="checkbox" value="2"/><label class="option label-after">b</label><input type="checkbox" value="3"/><label class="option label-after">c</label>'
527
- end
528
-
529
- it "should support custom :labeler for checkboxsets" do
530
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :label=>'foo', :labeler=>lambda{|tag, input| input.tag(:div, {}, tag)}).must_equal '<div><label class="option"><input type="checkbox" value="1"/> a</label><label class="option"><input type="checkbox" value="2"/> b</label><label class="option"><input type="checkbox" value="3"/> c</label></div>'
531
- end
532
-
533
- it "should create set of checkbox buttons with options and values with hashes" do
534
- @f.input(:checkboxset, :options=>[[:a, {:attr=>{:foo=>1}}], [:b, {:class=>'foo', :value=>2}], [:c, {:id=>:baz}]], :selected=>2).must_equal '<label class="option"><input foo="1" type="checkbox" value="a"/> a</label><label class="option"><input checked="checked" class="foo" type="checkbox" value="2"/> b</label><label class="option"><input id="baz" type="checkbox" value="c"/> c</label>'
535
- end
536
-
537
- it "should create set of checkbox buttons with options and values using given method" do
538
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :text_method=>:last, :selected=>2).must_equal '<label class="option"><input type="checkbox" value="1"/> 1</label><label class="option"><input checked="checked" type="checkbox" value="2"/> 2</label><label class="option"><input type="checkbox" value="3"/> 3</label>'
539
- @f.input(:checkboxset, :options=>[[:a, 1], [:b, 2], [:c, 3]], :text_method=>:last, :value_method=>:first, :selected=>:b).must_equal '<label class="option"><input type="checkbox" value="a"/> 1</label><label class="option"><input checked="checked" type="checkbox" value="b"/> 2</label><label class="option"><input type="checkbox" value="c"/> 3</label>'
540
- end
541
-
542
- it "should support :add_blank option for checkboxset inputs" do
543
- @f.input(:checkboxset, :options=>[[:b, 2], [:c, 3]], :add_blank=>true, :value=>2).must_equal '<label class="option"><input type="checkbox" value=""/> </label><label class="option"><input checked="checked" type="checkbox" value="2"/> b</label><label class="option"><input type="checkbox" value="3"/> c</label>'
544
- end
545
-
546
- it "should use :add_blank option value as prompt if it is a String" do
547
- @f.input(:checkboxset, :options=>[[:b, 2], [:c, 3]], :add_blank=>"Prompt Here", :value=>2).must_equal '<label class="option"><input type="checkbox" value=""/> Prompt Here</label><label class="option"><input checked="checked" type="checkbox" value="2"/> b</label><label class="option"><input type="checkbox" value="3"/> c</label>'
548
- end
549
-
550
- it "should respect the :key option for checkbox sets" do
551
- @f.input(:checkboxset, :options=>[1, 2, 3], :key=>:foo, :value=>2).must_equal '<label class="option"><input id="foo_1" name="foo[]" type="checkbox" value="1"/> 1</label><label class="option"><input checked="checked" id="foo_2" name="foo[]" type="checkbox" value="2"/> 2</label><label class="option"><input id="foo_3" name="foo[]" type="checkbox" value="3"/> 3</label>'
552
- end
553
-
554
- it "should prefer the :name option to :key option for checkbox sets" do
555
- @f.input(:checkboxset, :options=>[1, 2, 3], :key=>:foo, :name=>'bar[]', :value=>2).must_equal '<label class="option"><input id="foo_1" name="bar[]" type="checkbox" value="1"/> 1</label><label class="option"><input checked="checked" id="foo_2" name="bar[]" type="checkbox" value="2"/> 2</label><label class="option"><input id="foo_3" name="bar[]" type="checkbox" value="3"/> 3</label>'
556
- end
557
-
558
- it "should prefer the :name and :id option to :key option for checkbox sets" do
559
- @f.input(:checkboxset, :options=>[1, 2, 3], :key=>:foo, :name=>'bar[]', :id=>:baz, :value=>2).must_equal '<label class="option"><input id="baz_1" name="bar[]" type="checkbox" value="1"/> 1</label><label class="option"><input checked="checked" id="baz_2" name="bar[]" type="checkbox" value="2"/> 2</label><label class="option"><input id="baz_3" name="bar[]" type="checkbox" value="3"/> 3</label>'
560
- end
561
-
562
- it "should respect the :error option for checkbox sets" do
563
- @f.input(:checkboxset, :options=>[1, 2, 3], :error=>'foo', :value=>2).must_equal '<label class="option"><input type="checkbox" value="1"/> 1</label><label class="option"><input checked="checked" type="checkbox" value="2"/> 2</label><label class="option"><input aria-invalid="true" class="error" type="checkbox" value="3"/> 3</label><span class="error_message">foo</span>'
564
- end
565
-
566
- it "should create set of checkbox buttons with fieldsets and legends for optgroups" do
567
- @f.input(:checkboxset, :optgroups=>[['d', [[:a, 1], [:b, 2]]], ['e', [[:c, 3]]]], :selected=>2).must_equal '<fieldset><legend>d</legend><label class="option"><input type="checkbox" value="1"/> a</label><label class="option"><input checked="checked" type="checkbox" value="2"/> b</label></fieldset><fieldset><legend>e</legend><label class="option"><input type="checkbox" value="3"/> c</label></fieldset>'
568
- end
569
-
570
- it "should create set of checkbox buttons with label attributes" do
571
- @f.input(:checkboxset, :options=>[1, 2, 3], :selected=>2, :label_attr=>{:foo=>:bar}).must_equal '<label class="option" foo="bar"><input type="checkbox" value="1"/> 1</label><label class="option" foo="bar"><input checked="checked" type="checkbox" value="2"/> 2</label><label class="option" foo="bar"><input type="checkbox" value="3"/> 3</label>'
572
- end
573
-
574
- it "should raise an Error for empty checkbox sets" do
575
- @f.input(:checkboxset, :options=>[], :error=>'foo', :value=>2).must_equal '<span class="error_message">foo</span>'
576
- end
577
-
578
- it "radio and checkbox inputs should handle :checked option" do
579
- @f.input(:radio, :checked=>true).must_equal '<input checked="checked" type="radio"/>'
580
- @f.input(:radio, :checked=>false).must_equal '<input type="radio"/>'
581
- @f.input(:checkbox, :checked=>true).must_equal '<input checked="checked" type="checkbox"/>'
582
- @f.input(:checkbox, :checked=>false).must_equal '<input type="checkbox"/>'
583
- end
584
-
585
- it "inputs should handle :autofocus option" do
586
- @f.input(:text, :autofocus=>true).must_equal '<input autofocus="autofocus" type="text"/>'
587
- @f.input(:text, :autofocus=>false).must_equal '<input type="text"/>'
588
- end
589
-
590
- it "inputs should handle :required option" do
591
- @f.input(:text, :required=>true).must_equal '<input required="required" type="text"/>'
592
- @f.input(:text, :required=>false).must_equal '<input type="text"/>'
593
- end
594
-
595
- it "inputs should handle :disabled option" do
596
- @f.input(:text, :disabled=>true).must_equal '<input disabled="disabled" type="text"/>'
597
- @f.input(:text, :disabled=>false).must_equal '<input type="text"/>'
598
- end
599
-
600
- it "inputs should not include options with nil values" do
601
- @f.input(:text, :name=>nil).must_equal '<input type="text"/>'
602
- @f.input(:textarea, :name=>nil).must_equal '<textarea></textarea>'
603
- end
604
-
605
- it "inputs should include options with false values" do
606
- @f.input(:text, :name=>false).must_equal '<input name="false" type="text"/>'
607
- end
608
-
609
- it "should automatically create a label if a :label option is used" do
610
- @f.input(:text, :label=>'Foo', :value=>'foo').must_equal '<label>Foo: <input type="text" value="foo"/></label>'
611
- end
612
-
613
- it "should set label attributes with :label_attr option" do
614
- @f.input(:text, :label=>'Foo', :value=>'foo', :label_attr=>{:class=>'bar'}).must_equal '<label class="bar">Foo: <input type="text" value="foo"/></label>'
615
- end
616
-
617
- it "should handle implicit labels with checkboxes" do
618
- @f.input(:checkbox, :label=>'Foo', :value=>'foo', :name=>'a').must_equal '<input name="a" type="hidden" value="0"/><label><input name="a" type="checkbox" value="foo"/> Foo</label>'
619
- end
620
-
621
- it "should handle implicit labels with :label_position=>:after" do
622
- @f.input(:text, :label=>'Foo', :value=>'foo', :label_position=>:after).must_equal '<label><input type="text" value="foo"/> Foo</label>'
623
- end
624
-
625
- it "should handle implicit labels with checkboxes with :label_position=>:before" do
626
- @f.input(:checkbox, :label=>'Foo', :value=>'foo', :name=>'a', :label_position=>:before).must_equal '<input name="a" type="hidden" value="0"/><label>Foo <input name="a" type="checkbox" value="foo"/></label>'
627
- end
628
-
629
- it "should automatically note the input has errors if :error option is used" do
630
- @f.input(:text, :error=>'Bad Stuff!', :value=>'foo').must_equal '<input aria-invalid="true" class="error" type="text" value="foo"/><span class="error_message">Bad Stuff!</span>'
631
- end
632
-
633
- it "should add an error message after the label" do
634
- @f.input(:text, :error=>'Bad Stuff!', :value=>'foo', :label=>"Foo").must_equal '<label>Foo: <input aria-invalid="true" class="error" type="text" value="foo"/></label><span class="error_message">Bad Stuff!</span>'
635
- end
636
-
637
- it "should add to existing :class option if :error option is used" do
638
- @f.input(:text, :error=>'Bad Stuff!', :class=>'bar', :value=>'foo').must_equal '<input aria-invalid="true" class="bar error" type="text" value="foo"/><span class="error_message">Bad Stuff!</span>'
639
- end
640
-
641
- it "should respect :error_attr option for setting the attributes for the error message span" do
642
- @f.input(:text, :error=>'Bad Stuff!', :value=>'foo', :error_attr=>{:class=>'foo'}).must_equal '<input aria-invalid="true" class="error" type="text" value="foo"/><span class="foo error_message">Bad Stuff!</span>'
643
- end
644
-
645
- it "should use aria-describedby and aria-invalid tags for errors with where the id attribute can be determined" do
646
- @f.input(:text, :error=>'Bad Stuff!', :id=>:bar, :value=>'foo', :error_attr=>{:class=>'foo'}).must_equal '<input aria-describedby="bar_error_message" aria-invalid="true" class="error" id="bar" type="text" value="foo"/><span class="foo error_message" id="bar_error_message">Bad Stuff!</span>'
647
- end
648
-
649
- it "should support :error_id option for errors to specify id of error" do
650
- @f.input(:text, :error=>'Bad Stuff!', :error_id=>:baz, :id=>:bar, :value=>'foo', :error_attr=>{:class=>'foo'}).must_equal '<input aria-describedby="baz" aria-invalid="true" class="error" id="bar" type="text" value="foo"/><span class="foo error_message" id="baz">Bad Stuff!</span>'
651
- end
652
-
653
- it "should have :error_attr :id take precedence over :error_id option" do
654
- @f.input(:text, :error=>'Bad Stuff!', :error_id=>:baz, :id=>:bar, :value=>'foo', :error_attr=>{:class=>'foo', :id=>'q'}).must_equal '<input aria-describedby="baz" aria-invalid="true" class="error" id="bar" type="text" value="foo"/><span class="foo error_message" id="q">Bad Stuff!</span>'
655
- end
656
-
657
- it "#open should return an opening tag" do
658
- @f.open(:action=>'foo', :method=>'post').must_equal '<form action="foo" method="post">'
659
- end
660
-
661
- it "#close should return a closing tag" do
662
- @f.close.must_equal '</form>'
663
- end
664
-
665
- it "#button should return a submit tag" do
666
- @f.button.must_equal '<input type="submit"/>'
667
- end
668
-
669
- it "#button should accept an options hash" do
670
- @f.button(:name=>'foo', :value=>'bar').must_equal '<input name="foo" type="submit" value="bar"/>'
671
- end
672
-
673
- it "#button should accept a string to use as a value" do
674
- @f.button('foo').must_equal '<input type="submit" value="foo"/>'
675
- end
676
-
677
- it "#tag should return a serialized_tag" do
678
- @f.tag(:textarea).must_equal '<textarea></textarea>'
679
- @f.tag(:textarea, :name=>:foo).must_equal '<textarea name="foo"></textarea>'
680
- @f.tag(:textarea, {:name=>:foo}, :bar).must_equal '<textarea name="foo">bar</textarea>'
681
- end
682
-
683
- it "#tag should accept a block" do
684
- @f.tag(:div){@f.tag(:textarea)}.must_equal '<div><textarea></textarea></div>'
685
- @f.tag(:div, :name=>'a'){@f.tag(:textarea)}.must_equal '<div name="a"><textarea></textarea></div>'
686
- @f.tag(:div, {:name=>'a'}, ["text"]){@f.tag(:textarea)}.must_equal '<div name="a">text<textarea></textarea></div>'
687
- end
688
-
689
- it "#tag should accept children as procs" do
690
- @f.tag(:div, {:class=>"foo"}, lambda{|t| t.tag(:input, :class=>t.attr[:class])}).must_equal '<div class="foo"><input class="foo"/></div>'
691
- end
692
-
693
- it "#tag should accept children as methods" do
694
- o = Object.new
695
- def o.foo(t) t.tag(:input, :class=>t.attr[:class]) end
696
- @f.tag(:div, {:class=>"foo"}, o.method(:foo)).must_equal '<div class="foo"><input class="foo"/></div>'
697
- end
698
-
699
- it "should have an #inputs method for multiple inputs wrapped in a fieldset" do
700
- @f.inputs([:textarea, :text]).must_equal '<fieldset class="inputs"><textarea></textarea><input type="text"/></fieldset>'
701
- end
702
-
703
- it "should have an #inputs method for multiple inputs wrapped in a fieldset when using an empty block" do
704
- @f.inputs([:textarea, :text]){}.must_equal '<fieldset class="inputs"><textarea></textarea><input type="text"/></fieldset>'
705
- end
706
-
707
- it "should have default #inputs method accept an :attr option" do
708
- @f.inputs([:textarea, :text], :legend=>'Inputs', :attr=>{:class=>'foo', :bar=>'baz'}).must_equal '<fieldset bar="baz" class="foo inputs"><legend>Inputs</legend><textarea></textarea><input type="text"/></fieldset>'
709
- end
710
-
711
- it "should have default #inputs method accept a :legend option" do
712
- @f.inputs([:textarea, :text], :legend=>'Inputs').must_equal '<fieldset class="inputs"><legend>Inputs</legend><textarea></textarea><input type="text"/></fieldset>'
713
- end
714
-
715
- it "should have default #inputs method accept a :legend_attr option" do
716
- @f.inputs([:textarea, :text], :legend=>'Inputs', :legend_attr=>{:class=>'foo'}).must_equal '<fieldset class="inputs"><legend class="foo">Inputs</legend><textarea></textarea><input type="text"/></fieldset>'
717
- end
718
-
719
- it "should have an #inputs method take a block and yield to it" do
720
- @f.inputs{@f.input(:textarea); @f.input(:text)}.must_equal '<fieldset class="inputs"><textarea></textarea><input type="text"/></fieldset>'
721
- end
722
-
723
- it "should have an #inputs method work with both args and block" do
724
- @f.inputs([:textarea]){@f.input(:text)}.must_equal '<fieldset class="inputs"><textarea></textarea><input type="text"/></fieldset>'
725
- end
726
-
727
- it "should have an #inputs method support array arguments and creating inputs with the array as argument list" do
728
- @f.inputs([[:textarea, {:name=>'foo'}], [:text, {:id=>'bar'}]]).must_equal '<fieldset class="inputs"><textarea name="foo"></textarea><input id="bar" type="text"/></fieldset>'
729
- end
730
-
731
- it "should have #inputs accept transformer options to modify the options inside the inputs" do
732
- @f.inputs([:textarea, :text], :wrapper=>:div).must_equal '<fieldset class="inputs"><div><textarea></textarea></div><div><input type="text"/></div></fieldset>'
733
- end
734
-
735
- it "should have #inputs accept :nested_inputs_wrapper options to modify the :input_wrapper option inside the inputs" do
736
- @f.inputs(:nested_inputs_wrapper=>:div){@f.inputs([:textarea, :text])}.must_equal '<fieldset class="inputs"><div><textarea></textarea><input type="text"/></div></fieldset>'
737
- end
738
-
739
- it "should escape tag content" do
740
- @f.tag(:div, {}, ['<p></p>']).must_equal '<div>&lt;p&gt;&lt;/p&gt;</div>'
741
- end
742
-
743
- it "should not escape raw tag content using Forme::Raw" do
744
- @f.tag(:div, {}, ['<p></p>'.dup.extend(Forme::Raw)]).must_equal '<div><p></p></div>'
745
- end
746
-
747
- it "should not escape raw tag content using Forme.raw" do
748
- @f.tag(:div, {}, [Forme.raw('<p></p>')]).must_equal '<div><p></p></div>'
749
- end
750
-
751
- it "should not escape raw tag content using Form#raw" do
752
- @f.tag(:div, {}, [@f.raw('<p></p>')]).must_equal '<div><p></p></div>'
753
- end
754
-
755
- it "should escape tag content in attribute values" do
756
- @f.tag(:div, :foo=>'<p></p>').must_equal '<div foo="&lt;p&gt;&lt;/p&gt;"></div>'
757
- end
758
-
759
- it "should not escape raw tag content in attribute values" do
760
- @f.tag(:div, :foo=>Forme.raw('<p></p>')).must_equal '<div foo="<p></p>"></div>'
761
- end
762
-
763
- it "should format dates, times, and datetimes in ISO format" do
764
- @f.tag(:div, :foo=>Date.new(2011, 6, 5)).must_equal '<div foo="2011-06-05"></div>'
765
- @f.tag(:div, :foo=>DateTime.new(2011, 6, 5, 4, 3, 2)).must_equal '<div foo="2011-06-05T04:03:02.000"></div>'
766
- @f.tag(:div, :foo=>Time.utc(2011, 6, 5, 4, 3, 2)).must_equal '<div foo="2011-06-05T04:03:02.000"></div>'
767
- end
768
-
769
- it "should format bigdecimals in standard notation" do
770
- @f.tag(:div, :foo=>BigDecimal('10000.010')).must_equal '<div foo="10000.01"></div>'
771
- end
772
-
773
- it "inputs should accept a :wrapper option to use a custom wrapper" do
774
- @f.input(:text, :wrapper=>:li).must_equal '<li><input type="text"/></li>'
775
- end
776
-
777
- it "inputs should accept a :wrapper_attr option to use custom wrapper attributes" do
778
- @f.input(:text, :wrapper=>:li, :wrapper_attr=>{:class=>"foo"}).must_equal '<li class="foo"><input type="text"/></li>'
779
- end
780
-
781
- it "inputs should accept a :help option to use custom helper text" do
782
- @f.input(:text, :help=>"List type of foo").must_equal '<input type="text"/><span class="helper">List type of foo</span>'
783
- end
784
-
785
- it "inputs should accept a :helper_attr option for custom helper attributes" do
786
- @f.input(:text, :help=>"List type of foo", :helper_attr=>{:class=>'foo'}).must_equal '<input type="text"/><span class="foo helper">List type of foo</span>'
787
- end
788
-
789
- it "inputs should have helper displayed inside wrapper, after error" do
790
- @f.input(:text, :help=>"List type of foo", :error=>'bad', :wrapper=>:li).must_equal '<li><input aria-invalid="true" class="error" type="text"/><span class="error_message">bad</span><span class="helper">List type of foo</span></li>'
791
- end
792
-
793
- it "inputs should accept a :formatter option to use a custom formatter" do
794
- @f.input(:text, :formatter=>:readonly, :value=>'1', :label=>'Foo').must_equal '<label>Foo: <span class="readonly-text">1</span></label>'
795
- @f.input(:text, :formatter=>:default, :value=>'1', :label=>'Foo').must_equal '<label>Foo: <input type="text" value="1"/></label>'
796
- end
797
-
798
- it "inputs should accept a :labeler option to use a custom labeler" do
799
- @f.input(:textarea, :labeler=>:explicit, :label=>'bar', :id=>:foo).must_equal '<label class="label-before" for="foo">bar</label><textarea id="foo"></textarea>'
800
- end
801
-
802
- it "inputs handle explicit labels with :label_position=>:after" do
803
- @f.input(:textarea, :labeler=>:explicit, :label=>'bar', :id=>:foo, :label_position=>:after).must_equal '<textarea id="foo"></textarea><label class="label-after" for="foo">bar</label>'
804
- end
805
-
806
- it "inputs handle explicit labels with :key_id" do
807
- @f.input(:textarea, :labeler=>:explicit, :label=>'bar', :key=>:bar, :key_id=>:foo).must_equal '<label class="label-before" for="bar_foo">bar</label><textarea id="bar_foo" name="bar"></textarea>'
808
- end
809
-
810
- it "should handle explicit labels with checkboxes" do
811
- @f.input(:checkbox, :labeler=>:explicit, :label=>'Foo', :value=>'foo', :name=>'a', :id=>'bar').must_equal '<input id="bar_hidden" name="a" type="hidden" value="0"/><input id="bar" name="a" type="checkbox" value="foo"/><label class="label-after" for="bar">Foo</label>'
812
- end
813
-
814
- it "should handle explicit labels with checkboxes with :label_position=>:before" do
815
- @f.input(:checkbox, :labeler=>:explicit, :label=>'Foo', :value=>'foo', :name=>'a', :id=>'bar', :label_position=>:before).must_equal '<label class="label-before" for="bar">Foo</label><input id="bar_hidden" name="a" type="hidden" value="0"/><input id="bar" name="a" type="checkbox" value="foo"/>'
816
- end
817
-
818
- it "inputs handle implicit labels or checkboxes without hidden fields with :label_position=>:before" do
819
- @f.input(:checkbox, :label=>'Foo', :value=>'foo', :name=>'a', :id=>'bar', :label_position=>:before, :no_hidden=>true).must_equal '<label>Foo <input id="bar" name="a" type="checkbox" value="foo"/></label>'
820
- end
821
-
822
- it "inputs should accept a :error_handler option to use a custom error_handler" do
823
- @f.input(:textarea, :error_handler=>proc{|t, i| [t, "!!! #{i.opts[:error]}"]}, :error=>'bar', :id=>:foo).must_equal '<textarea aria-describedby="foo_error_message" aria-invalid="true" class="error" id="foo"></textarea>!!! bar'
824
- end
825
-
826
- it "#inputs should accept a :inputs_wrapper option to use a custom inputs_wrapper" do
827
- @f.inputs([:textarea], :inputs_wrapper=>:ol).must_equal '<ol><textarea></textarea></ol>'
828
- end
829
-
830
- it "inputs should accept a :wrapper=>nil option to not use a wrapper" do
831
- Forme::Form.new(:wrapper=>:li).input(:text, :wrapper=>nil).must_equal '<input type="text"/>'
832
- end
833
-
834
- it "inputs should accept a :labeler=>nil option to not use a labeler" do
835
- @f.input(:textarea, :labeler=>nil, :label=>'bar', :id=>:foo).must_equal '<textarea id="foo"></textarea>'
836
- end
837
-
838
- it "inputs should accept a :error_handler=>nil option to not use an error_handler" do
839
- @f.input(:textarea, :error_handler=>nil, :error=>'bar', :id=>:foo).must_equal '<textarea aria-invalid="true" class="error" id="foo"></textarea>'
840
- end
841
-
842
- it "#inputs should accept a :inputs_wrapper=>nil option to not use an inputs_wrapper" do
843
- @f.form{|f| f.inputs([:textarea], :inputs_wrapper=>nil)}.must_equal '<form><textarea></textarea></form>'
844
- end
845
-
846
- it "#inputs should treat a single hash argument as an options hash with no default inputs" do
847
- @f.inputs(:inputs_wrapper=>:ol){@f.input(:textarea)}.must_equal '<ol><textarea></textarea></ol>'
848
- end
849
-
850
- it "should support setting defaults for inputs at the form level" do
851
- f = Forme::Form.new(:input_defaults=>{'text'=>{:size=>20}, 'textarea'=>{:cols=>80, :rows=>6}})
852
- f.input(:text, :name=>"foo").must_equal '<input name="foo" size="20" type="text"/>'
853
- f.input(:textarea, :name=>"foo").must_equal '<textarea cols="80" name="foo" rows="6"></textarea>'
854
- end
855
-
856
- it "should work with input_defaults with symbol keys using using inputs with symbol keys" do
857
- f = Forme::Form.new(:input_defaults=>{:text=>{:size=>20}, 'text'=>{:size=>30}})
858
- f.input(:text, :name=>"foo").must_equal '<input name="foo" size="20" type="text"/>'
859
- f.input('text', :name=>"foo").must_equal '<input name="foo" size="30" type="text"/>'
860
- end
861
-
862
- it "invalid custom transformers should raise an Error" do
863
- proc{Forme::Form.new(:wrapper=>Object.new).input(:text)}.must_raise(Forme::Error)
864
- proc{@f.input(:textarea, :wrapper=>Object.new)}.must_raise(Forme::Error)
865
- proc{@f.input(:textarea, :formatter=>nil)}.must_raise(Forme::Error)
866
- end
867
-
868
- it "should handle :before and :after hook options" do
869
- Forme.form({}, :before=>lambda{|f| f.tag(:input, :type=>:hidden, :name=>:a, :value=>'b')}, :after=>lambda{|f| f.tag(:input, :type=>:hidden, :name=>:c, :value=>'d')}){|f| f.tag(:input)}.must_equal '<form><input name="a" type="hidden" value="b"/><input/><input name="c" type="hidden" value="d"/></form>'
870
- end
871
- end
872
-
873
- describe "Forme custom" do
874
- it "formatters can be specified as a proc" do
875
- Forme::Form.new(:formatter=>proc{|i| i.tag(:textarea, i.opts[:name]=>:name)}).input(:text, :name=>'foo').must_equal '<textarea foo="name"></textarea>'
876
- end
877
-
878
- it "serializers can be specified as a proc" do
879
- Forme::Form.new(:serializer=>proc{|t| "#{t.type} = #{t.opts[:name]}"}).input(:textarea, :name=>'foo').must_equal 'textarea = foo'
880
- end
881
-
882
- it "labelers can be specified as a proc" do
883
- Forme::Form.new(:labeler=>proc{|t, i| ["#{i.opts[:label]}: ", t]}).input(:textarea, :name=>'foo', :label=>'bar').must_equal 'bar: <textarea name="foo"></textarea>'
884
- end
885
-
886
- it "error_handlers can be specified as a proc" do
887
- Forme::Form.new(:error_handler=>proc{|t, i| [t, "!!! #{i.opts[:error]}"]}).input(:textarea, :name=>'foo', :error=>'bar').must_equal '<textarea aria-invalid="true" class="error" name="foo"></textarea>!!! bar'
888
- end
889
-
890
- it "wrappers can be specified as a proc" do
891
- Forme::Form.new(:wrapper=>proc{|t, i| t.tag(:div, {:bar=>i.opts[:name]}, t)}).input(:textarea, :name=>'foo').must_equal '<div bar="foo"><textarea name="foo"></textarea></div>'
892
- end
893
-
894
- it "inputs_wrappers can be specified as a proc" do
895
- Forme::Form.new(:inputs_wrapper=>proc{|f, opts, &block| f.tag(:div, &block)}).inputs([:textarea]).must_equal '<div><textarea></textarea></div>'
896
- end
897
-
898
- it "can use nil as value to disable default transformer" do
899
- Forme::Form.new(:labeler=>nil).input(:textarea, :label=>'foo').must_equal '<textarea></textarea>'
900
- end
901
- end
902
-
903
- describe "Forme built-in custom" do
904
- it "transformers should raise if the there is no matching transformer" do
905
- proc{Forme::Form.new(:formatter=>:foo).input(:text)}.must_raise(Forme::Error)
906
- end
907
-
908
- it "formatter: disabled disables all inputs unless :disabled=>false option" do
909
- Forme::Form.new(:formatter=>:disabled).input(:textarea).must_equal '<textarea disabled="disabled"></textarea>'
910
- Forme::Form.new(:formatter=>:disabled).input(:textarea, :disabled=>false).must_equal '<textarea></textarea>'
911
- end
912
-
913
- it "formatter: readonly uses spans for text input fields and disables radio/checkbox fields" do
914
- Forme::Form.new(:formatter=>:readonly).input(:text, :label=>"Foo", :value=>"Bar").must_equal "<label>Foo: <span class=\"readonly-text\">Bar</span></label>"
915
- Forme::Form.new(:formatter=>:readonly).input(:radio, :label=>"Foo", :value=>"Bar").must_equal "<label><input disabled=\"disabled\" type=\"radio\" value=\"Bar\"/> Foo</label>"
916
- Forme::Form.new(:formatter=>:readonly).input(:radio, :label=>"Foo", :value=>"Bar", :checked=>true).must_equal "<label><input checked=\"checked\" disabled=\"disabled\" type=\"radio\" value=\"Bar\"/> Foo</label>"
917
- Forme::Form.new(:formatter=>:readonly).input(:checkbox, :label=>"Foo", :value=>"Bar").must_equal "<label><input disabled=\"disabled\" type=\"checkbox\" value=\"Bar\"/> Foo</label>"
918
- Forme::Form.new(:formatter=>:readonly).input(:checkbox, :label=>"Foo", :value=>"Bar", :checked=>true).must_equal "<label><input checked=\"checked\" disabled=\"disabled\" type=\"checkbox\" value=\"Bar\"/> Foo</label>"
919
- Forme::Form.new(:formatter=>:readonly).input(:select, :label=>"Foo", :options=>[1, 2, 3], :value=>2).must_equal "<label>Foo: <span>2</span></label>"
920
- Forme::Form.new(:formatter=>:readonly).input(:select, :label=>"Foo").must_equal "<label>Foo: <span></span></label>"
921
- end
922
-
923
- it "formatter: readonly removes hidden inputs" do
924
- Forme::Form.new(:formatter=>:readonly).input(:hidden, :value=>"Bar").must_equal ""
925
- end
926
-
927
- it "formatter: readonly formats text into paragraphs for textarea inputs" do
928
- Forme::Form.new(:formatter=>:readonly).input(:textarea, :label=>"Foo", :value=>"\n Bar\nBaz\n\nQuuz\n\n1\n2 \n").must_equal "<label>Foo: <div class=\"readonly-textarea\"><p> Bar<br />Baz</p><p>Quuz</p><p>1<br />2 </p></div></label>"
929
- end
930
-
931
- it "formatter: readonly does not format nil, raw string, or non-string inputs" do
932
- Forme::Form.new(:formatter=>:readonly).input(:textarea, :label=>"Foo").must_equal "<label>Foo: <div class=\"readonly-textarea\"></div></label>"
933
- Forme::Form.new(:formatter=>:readonly).input(:textarea, :label=>"Foo", :value=>Forme.raw("Bar\n\nBaz")).must_equal "<label>Foo: <div class=\"readonly-textarea\">Bar\n\nBaz</div></label>"
934
- Forme::Form.new(:formatter=>:readonly).input(:textarea, :label=>"Foo", :value=>1).must_equal "<label>Foo: <div class=\"readonly-textarea\">1</div></label>"
935
- end
936
-
937
- it "formatter: readonly should ignore submit buttons" do
938
- Forme.form({}, :formatter=>:readonly, :button=>'a').must_equal '<form></form>'
939
- end
940
-
941
- it "labeler: explicit uses an explicit label with for attribute" do
942
- Forme::Form.new(:labeler=>:explicit).input(:textarea, :id=>'foo', :label=>'bar').must_equal '<label class="label-before" for="foo">bar</label><textarea id="foo"></textarea>'
943
- end
944
-
945
- it "labeler: explicit handles the key option correctly" do
946
- Forme::Form.new(:labeler=>:explicit, :namespace=>:baz).input(:textarea, :key=>'foo', :label=>'bar').must_equal '<label class="label-before" for="baz_foo">bar</label><textarea id="baz_foo" name="baz[foo]"></textarea>'
947
- end
948
-
949
- it "labeler: explicit should handle tags with errors" do
950
- Forme::Form.new(:labeler=>:explicit).input(:text, :error=>'Bad Stuff!', :value=>'f', :id=>'foo', :label=>'bar').must_equal '<label class="label-before" for="foo">bar</label><input aria-describedby="foo_error_message" aria-invalid="true" class="error" id="foo" type="text" value="f"/><span class="error_message" id="foo_error_message">Bad Stuff!</span>'
951
- end
952
-
953
- it "labeler: span should add a span with label class before the tag" do
954
- Forme::Form.new(:labeler=>:span).input(:text, :label=>'A').must_equal '<span class="label">A</span><input type="text"/>'
955
- end
956
-
957
- it "labeler: span should support :label_attr" do
958
- Forme::Form.new(:labeler=>:span).input(:text, :label=>'A', :label_attr=>{:foo=>'bar', :class=>"baz"}).must_equal '<span class="baz label" foo="bar">A</span><input type="text"/>'
959
- end
960
-
961
- it "wrapper: li wraps tag in an li" do
962
- Forme::Form.new(:wrapper=>:li).input(:textarea, :id=>'foo').must_equal '<li><textarea id="foo"></textarea></li>'
963
- Forme::Form.new(:wrapper=>:li).input(:textarea, :id=>'foo', :wrapper_attr=>{:id=>'bar'}).must_equal '<li id="bar"><textarea id="foo"></textarea></li>'
964
- end
965
-
966
- it "wrapper: p wraps tag in an p" do
967
- Forme::Form.new(:wrapper=>:p).input(:textarea, :id=>'foo').must_equal '<p><textarea id="foo"></textarea></p>'
968
- Forme::Form.new(:wrapper=>:p).input(:textarea, :id=>'foo', :wrapper_attr=>{:id=>'bar'}).must_equal '<p id="bar"><textarea id="foo"></textarea></p>'
969
- end
970
-
971
- it "wrapper: div wraps tag in an div" do
972
- Forme::Form.new(:wrapper=>:div).input(:textarea, :id=>'foo').must_equal '<div><textarea id="foo"></textarea></div>'
973
- Forme::Form.new(:wrapper=>:div).input(:textarea, :id=>'foo', :wrapper_attr=>{:id=>'bar'}).must_equal '<div id="bar"><textarea id="foo"></textarea></div>'
974
- end
975
-
976
- it "wrapper: span wraps tag in an span" do
977
- Forme::Form.new(:wrapper=>:span).input(:textarea, :id=>'foo').must_equal '<span><textarea id="foo"></textarea></span>'
978
- Forme::Form.new(:wrapper=>:span).input(:textarea, :id=>'foo', :wrapper_attr=>{:id=>'bar'}).must_equal '<span id="bar"><textarea id="foo"></textarea></span>'
979
- end
980
-
981
- it "wrapper: td wraps tag in an td" do
982
- Forme::Form.new(:wrapper=>:td).input(:textarea, :id=>'foo').must_equal '<td><textarea id="foo"></textarea></td>'
983
- Forme::Form.new(:wrapper=>:td).input(:textarea, :id=>'foo', :wrapper_attr=>{:id=>'bar'}).must_equal '<td id="bar"><textarea id="foo"></textarea></td>'
984
- end
985
-
986
- it "wrapper: trtd wraps tag in an tr/td" do
987
- Forme::Form.new(:wrapper=>:trtd).input(:textarea, :id=>'foo').must_equal '<tr><td><textarea id="foo"></textarea></td><td></td></tr>'
988
- Forme::Form.new(:wrapper=>:trtd).input(:textarea, :id=>'foo', :wrapper_attr=>{:id=>'bar'}).must_equal '<tr id="bar"><td><textarea id="foo"></textarea></td><td></td></tr>'
989
- end
990
-
991
- it "wrapper: trtd supports multiple tags in separate tds" do
992
- Forme::Form.new(:wrapper=>:trtd, :labeler=>:explicit).input(:textarea, :id=>'foo', :label=>'Foo').must_equal '<tr><td><label class="label-before" for="foo">Foo</label></td><td><textarea id="foo"></textarea></td></tr>'
993
- end
994
-
995
- it "wrapper: trtd should use at most 2 td tags" do
996
- Forme::Form.new(:wrapper=>:trtd, :labeler=>:explicit).input(:textarea, :id=>'foo', :label=>'Foo', :error=>'Bar').must_equal '<tr><td><label class="label-before" for="foo">Foo</label></td><td><textarea aria-describedby="foo_error_message" aria-invalid="true" class="error" id="foo"></textarea><span class="error_message" id="foo_error_message">Bar</span></td></tr>'
997
- end
998
-
999
- it "wrapper: trtd should handle inputs with label after" do
1000
- Forme::Form.new(:wrapper=>:trtd, :labeler=>:explicit).input(:checkbox, :id=>'foo', :name=>'foo', :label=>'Foo').must_equal '<tr><td><label class="label-after" for="foo">Foo</label></td><td><input id="foo_hidden" name="foo" type="hidden" value="0"/><input id="foo" name="foo" type="checkbox"/></td></tr>'
1001
- end
1002
-
1003
- it "wrapper: tr should use a td wrapper and tr inputs_wrapper" do
1004
- Forme::Form.new(:wrapper=>:tr).inputs([:textarea]).must_equal '<tr><td><textarea></textarea></td></tr>'
1005
- f = Forme::Form.new
1006
- f.with_opts(:wrapper=>:tr){f.inputs([:textarea])}.must_equal '<tr><td><textarea></textarea></td></tr>'
1007
- end
1008
-
1009
- it "wrapper: table should use a trtd wrapper and table inputs_wrapper" do
1010
- Forme::Form.new(:wrapper=>:table).inputs([:textarea]).must_equal '<table><tbody><tr><td><textarea></textarea></td><td></td></tr></tbody></table>'
1011
- f = Forme::Form.new
1012
- f.with_opts(:wrapper=>:table){f.inputs([:textarea])}.must_equal '<table><tbody><tr><td><textarea></textarea></td><td></td></tr></tbody></table>'
1013
- end
1014
-
1015
- it "wrapper: ol should use an li wrapper and ol inputs_wrapper" do
1016
- Forme::Form.new(:wrapper=>:ol).inputs([:textarea]).must_equal '<ol><li><textarea></textarea></li></ol>'
1017
- f = Forme::Form.new
1018
- f.with_opts(:wrapper=>:ol){f.inputs([:textarea])}.must_equal '<ol><li><textarea></textarea></li></ol>'
1019
- end
1020
-
1021
- it "wrapper: fieldset_ol should use an li wrapper and fieldset_ol inputs_wrapper" do
1022
- Forme::Form.new(:wrapper=>:fieldset_ol).inputs([:textarea]).must_equal '<fieldset class="inputs"><ol><li><textarea></textarea></li></ol></fieldset>'
1023
- f = Forme::Form.new
1024
- f.with_opts(:wrapper=>:fieldset_ol){f.inputs([:textarea])}.must_equal '<fieldset class="inputs"><ol><li><textarea></textarea></li></ol></fieldset>'
1025
- end
1026
-
1027
- it "wrapper should not override inputs_wrapper if both given" do
1028
- Forme::Form.new(:wrapper=>:tr, :inputs_wrapper=>:div).inputs([:textarea]).must_equal '<div><td><textarea></textarea></td></div>'
1029
- f = Forme::Form.new
1030
- f.with_opts(:wrapper=>:tr, :inputs_wrapper=>:div){f.inputs([:textarea])}.must_equal '<div><td><textarea></textarea></td></div>'
1031
- end
1032
-
1033
- it "inputs_wrapper: ol wraps tags in an ol" do
1034
- Forme::Form.new(:inputs_wrapper=>:ol, :wrapper=>:li).inputs([:textarea]).must_equal '<ol><li><textarea></textarea></li></ol>'
1035
- Forme::Form.new(:inputs_wrapper=>:ol, :wrapper=>:li).inputs([:textarea], :attr=>{:foo=>1}).must_equal '<ol foo="1"><li><textarea></textarea></li></ol>'
1036
- end
1037
-
1038
- it "inputs_wrapper: fieldset_ol wraps tags in a fieldset and an ol" do
1039
- Forme::Form.new(:inputs_wrapper=>:fieldset_ol, :wrapper=>:li).inputs([:textarea]).must_equal '<fieldset class="inputs"><ol><li><textarea></textarea></li></ol></fieldset>'
1040
- Forme::Form.new(:inputs_wrapper=>:fieldset_ol, :wrapper=>:li).inputs([:textarea], :attr=>{:foo=>1}).must_equal '<fieldset class="inputs" foo="1"><ol><li><textarea></textarea></li></ol></fieldset>'
1041
- end
1042
-
1043
- it "inputs_wrapper: fieldset_ol supports a :legend option" do
1044
- Forme.form({}, :inputs_wrapper=>:fieldset_ol, :wrapper=>:li, :legend=>'Foo', :inputs=>[:textarea]).must_equal '<form><fieldset class="inputs"><legend>Foo</legend><ol><li><textarea></textarea></li></ol></fieldset></form>'
1045
- end
1046
-
1047
- it "inputs_wrapper: div wraps tags in a div" do
1048
- Forme::Form.new(:inputs_wrapper=>:div, :wrapper=>:span).inputs([:textarea]).must_equal '<div><span><textarea></textarea></span></div>'
1049
- Forme::Form.new(:inputs_wrapper=>:div, :wrapper=>:span).inputs([:textarea], :attr=>{:foo=>1}).must_equal '<div foo="1"><span><textarea></textarea></span></div>'
1050
- end
1051
-
1052
- it "inputs_wrapper: tr wraps tags in an tr" do
1053
- Forme::Form.new(:inputs_wrapper=>:tr, :wrapper=>:td).inputs([:textarea]).must_equal '<tr><td><textarea></textarea></td></tr>'
1054
- Forme::Form.new(:inputs_wrapper=>:tr, :wrapper=>:td).inputs([:textarea], :attr=>{:foo=>1}).must_equal '<tr foo="1"><td><textarea></textarea></td></tr>'
1055
- end
1056
-
1057
- it "inputs_wrapper: table wraps tags in an table" do
1058
- Forme::Form.new(:inputs_wrapper=>:table, :wrapper=>:trtd).inputs([:textarea]).must_equal '<table><tbody><tr><td><textarea></textarea></td><td></td></tr></tbody></table>'
1059
- Forme::Form.new(:inputs_wrapper=>:table, :wrapper=>:trtd).inputs([:textarea], :attr=>{:foo=>1}).must_equal '<table foo="1"><tbody><tr><td><textarea></textarea></td><td></td></tr></tbody></table>'
1060
- end
1061
-
1062
- it "inputs_wrapper: table accepts a :legend option" do
1063
- Forme::Form.new(:inputs_wrapper=>:table, :wrapper=>:trtd).inputs([:textarea], :legend=>'Inputs').must_equal '<table><caption>Inputs</caption><tbody><tr><td><textarea></textarea></td><td></td></tr></tbody></table>'
1064
- end
1065
-
1066
- it "inputs_wrapper: table accepts a :legend_attr option" do
1067
- Forme::Form.new(:inputs_wrapper=>:table, :wrapper=>:trtd).inputs([:textarea], :legend=>'Inputs', :legend_attr=>{:class=>'foo'}).must_equal '<table><caption class="foo">Inputs</caption><tbody><tr><td><textarea></textarea></td><td></td></tr></tbody></table>'
1068
- end
1069
-
1070
- it "inputs_wrapper: table accepts a :labels option" do
1071
- Forme::Form.new(:inputs_wrapper=>:table).inputs(:labels=>%w'A B C').must_equal '<table><thead><tr><th>A</th><th>B</th><th>C</th></tr></thead><tbody></tbody></table>'
1072
- end
1073
-
1074
- it "inputs_wrapper: table doesn't add empty header row for :labels=>[]" do
1075
- Forme::Form.new(:inputs_wrapper=>:table).inputs(:labels=>[]).must_equal '<table><tbody></tbody></table>'
1076
- end
1077
-
1078
- it "serializer: html_usa formats dates and datetimes in American format without timezones" do
1079
- Forme::Form.new(:serializer=>:html_usa).tag(:div, :foo=>Date.new(2011, 6, 5)).must_equal '<div foo="06/05/2011"></div>'
1080
- Forme::Form.new(:serializer=>:html_usa).tag(:div, :foo=>DateTime.new(2011, 6, 5, 16, 3, 2)).must_equal '<div foo="06/05/2011 04:03:02PM"></div>'
1081
- Forme::Form.new(:serializer=>:html_usa).tag(:div, :foo=>Time.utc(2011, 6, 5, 4, 3, 2)).must_equal '<div foo="06/05/2011 04:03:02AM"></div>'
1082
- end
1083
-
1084
- it "serializer: html_usa should convert date and datetime inputs into text inputs" do
1085
- Forme::Form.new(:serializer=>:html_usa).input(:date, :value=>Date.new(2011, 6, 5)).must_equal '<input type="text" value="06/05/2011"/>'
1086
- Forme::Form.new(:serializer=>:html_usa).input(:datetime, :value=>DateTime.new(2011, 6, 5, 16, 3, 2)).must_equal '<input type="text" value="06/05/2011 04:03:02PM"/>'
1087
- end
1088
-
1089
- it "serializer: text uses plain text output instead of html" do
1090
- Forme::Form.new(:serializer=>:text).input(:textarea, :label=>"Foo", :value=>"Bar").must_equal "Foo: Bar\n\n"
1091
- Forme::Form.new(:serializer=>:text).input(:text, :label=>"Foo", :value=>"Bar").must_equal "Foo: Bar\n\n"
1092
- Forme::Form.new(:serializer=>:text).input(:radio, :label=>"Foo", :value=>"Bar").must_equal "___ Foo\n"
1093
- Forme::Form.new(:serializer=>:text).input(:radio, :label=>"Foo", :value=>"Bar", :checked=>true).must_equal "_X_ Foo\n"
1094
- Forme::Form.new(:serializer=>:text).input(:checkbox, :label=>"Foo", :value=>"Bar").must_equal "___ Foo\n"
1095
- Forme::Form.new(:serializer=>:text).input(:checkbox, :label=>"Foo", :value=>"Bar", :checked=>true).must_equal "_X_ Foo\n"
1096
- Forme::Form.new(:serializer=>:text).input(:select, :label=>"Foo", :options=>[1, 2, 3], :value=>2).must_equal "Foo: \n___ 1\n_X_ 2\n___ 3\n\n"
1097
- Forme::Form.new(:serializer=>:text).input(:password, :label=>"Pass").must_equal "Pass: ********\n\n"
1098
- Forme::Form.new(:serializer=>:text).button().must_equal ""
1099
- Forme::Form.new(:serializer=>:text).inputs([[:textarea, {:label=>"Foo", :value=>"Bar"}]], :legend=>'Baz').must_equal "Baz\n---\nFoo: Bar\n\n"
1100
- Forme::Form.new(:serializer=>:text).tag(:p){|f| f.input(:textarea, :label=>"Foo", :value=>"Bar")}.must_equal "Foo: Bar\n\n"
1101
- Forme::Form.new(:serializer=>:text).tag(:p, {}, ['a']).must_equal "a"
1102
- end
1103
-
1104
- it "Form#open and #close return empty string when using serializer: text" do
1105
- f = Forme::Form.new(:serializer=>:text)
1106
- f.open({}).must_be_nil
1107
- f.close.must_be_nil
1108
- end
1109
- end
1110
-
1111
- describe "Forme registering custom transformers" do
1112
- it "should have #register_transformer register a transformer object for later use" do
1113
- Forme.register_transformer(:wrapper, :div2, proc{|t, i| i.tag(:div2, {}, [t])})
1114
- Forme::Form.new(:wrapper=>:div2).input(:textarea).must_equal '<div2><textarea></textarea></div2>'
1115
- end
1116
-
1117
- it "should have #register_transformer register a transformer block for later use" do
1118
- Forme.register_transformer(:wrapper, :div1){|t, i| i.tag(:div1, {}, [t])}
1119
- Forme::Form.new(:wrapper=>:div1).input(:textarea).must_equal '<div1><textarea></textarea></div1>'
1120
- end
1121
-
1122
- it "should have #register_transformer raise an error if given a block and an object" do
1123
- proc do
1124
- Forme.register_transformer(:wrapper, :div1, proc{|t, i| t}){|t, i| i.tag(:div1, {}, [t])}
1125
- end.must_raise(Forme::Error)
1126
- end
1127
-
1128
- it "should have #register_transformer raise an error for invalid transformer" do
1129
- proc do
1130
- Forme.register_transformer(:foo, :div1){}
1131
- end.must_raise(Forme::Error)
1132
- end
1133
- end
1134
-
1135
- describe "Forme configurations" do
1136
- after do
1137
- Forme.default_config = :default
1138
- end
1139
-
1140
- it "config: :formastic uses fieldset_ol inputs_wrapper and li wrapper, and explicit labeler" do
1141
- Forme::Form.new(:config=>:formtastic).inputs([[:textarea, {:id=>:foo, :label=>'Foo'}]]).must_equal '<fieldset class="inputs"><ol><li><label class="label-before" for="foo">Foo</label><textarea id="foo"></textarea></li></ol></fieldset>'
1142
- end
1143
-
1144
- it "should be able to set a default configuration with Forme.default_config=" do
1145
- Forme.default_config = :formtastic
1146
- Forme::Form.new.inputs([[:textarea, {:id=>:foo, :label=>'Foo'}]]).must_equal '<fieldset class="inputs"><ol><li><label class="label-before" for="foo">Foo</label><textarea id="foo"></textarea></li></ol></fieldset>'
1147
- end
1148
-
1149
- it "should have #register_config register a configuration for later use" do
1150
- Forme.register_config(:foo, :wrapper=>:li, :labeler=>:explicit)
1151
- Forme::Form.new(:config=>:foo).input(:textarea, :id=>:foo, :label=>'Foo').must_equal '<li><label class="label-before" for="foo">Foo</label><textarea id="foo"></textarea></li>'
1152
- end
1153
-
1154
- it "should have #register_config support a :base option to base it on an existing config" do
1155
- Forme.register_config(:foo2, :labeler=>:default, :base=>:formtastic)
1156
- Forme::Form.new(:config=>:foo2).inputs([[:textarea, {:id=>:foo, :label=>'Foo'}]]).must_equal '<fieldset class="inputs"><ol><li><label>Foo: <textarea id="foo"></textarea></label></li></ol></fieldset>'
1157
- end
1158
-
1159
- end
1160
-
1161
- describe "Forme object forms" do
1162
- it "should handle a simple case" do
1163
- obj = Class.new{def forme_input(form, field, opts) form._input(:text, :name=>"obj[#{field}]", :id=>"obj_#{field}", :value=>"#{field}_foo") end}.new
1164
- Forme::Form.new(obj).input(:field).must_equal '<input id="obj_field" name="obj[field]" type="text" value="field_foo"/>'
1165
- end
1166
-
1167
- it "should handle more complex case with multiple different types and opts" do
1168
- obj = Class.new do
1169
- def self.name() "Foo" end
1170
-
1171
- attr_reader :x, :y
1172
-
1173
- def initialize(x, y)
1174
- @x, @y = x, y
1175
- end
1176
- def forme_input(form, field, opts={})
1177
- t = opts[:type]
1178
- t ||= (field == :x ? :textarea : :text)
1179
- s = field
1180
- form._input(t, {:label=>s.upcase, :name=>"foo[#{s}]", :id=>"foo_#{s}", :value=>send(field)}.merge!(opts))
1181
- end
1182
- end.new('&foo', 3)
1183
- f = Forme::Form.new(obj)
1184
- f.input(:x).must_equal '<label>X: <textarea id="foo_x" name="foo[x]">&amp;foo</textarea></label>'
1185
- f.input(:y, :attr=>{:brain=>'no'}).must_equal '<label>Y: <input brain="no" id="foo_y" name="foo[y]" type="text" value="3"/></label>'
1186
- end
1187
-
1188
- it "should handle case where obj doesn't respond to forme_input" do
1189
- Forme::Form.new([:foo]).input(:first).must_equal '<input id="first" name="first" type="text" value="foo"/>'
1190
- obj = Class.new{attr_accessor :foo}.new
1191
- obj.foo = 'bar'
1192
- Forme::Form.new(obj).input(:foo).must_equal '<input id="foo" name="foo" type="text" value="bar"/>'
1193
- end
1194
-
1195
- it "should respect opts hash when obj doesn't respond to forme_input" do
1196
- Forme::Form.new([:foo]).input(:first, :name=>'bar').must_equal '<input id="first" name="bar" type="text" value="foo"/>'
1197
- Forme::Form.new([:foo]).input(:first, :id=>'bar').must_equal '<input id="bar" name="first" type="text" value="foo"/>'
1198
- Forme::Form.new([:foo]).input(:first, :value=>'bar').must_equal '<input id="first" name="first" type="text" value="bar"/>'
1199
- Forme::Form.new([:foo]).input(:first, :attr=>{:x=>'bar'}).must_equal '<input id="first" name="first" type="text" value="foo" x="bar"/>'
1200
- end
1201
-
1202
- it "should respect current namespace" do
1203
- Forme::Form.new([:foo], :namespace=>'a').input(:first).must_equal '<input id="a_first" name="a[first]" type="text" value="foo"/>'
1204
- end
1205
-
1206
- it "should get values for hashes using #[]" do
1207
- Forme::Form.new(:obj=>{:bar=>:foo}, :namespace=>'a').input(:bar).must_equal '<input id="a_bar" name="a[bar]" type="text" value="foo"/>'
1208
- end
1209
-
1210
- it "should handle obj passed in via :obj hash key" do
1211
- Forme::Form.new(:obj=>[:foo]).input(:first).must_equal '<input id="first" name="first" type="text" value="foo"/>'
1212
- end
1213
-
1214
- it "should be able to turn off obj handling per input using :obj=>nil option" do
1215
- Forme::Form.new([:foo]).input(:checkbox, :name=>"foo", :hidden_value=>"no", :obj=>nil).must_equal '<input name="foo" type="hidden" value="no"/><input name="foo" type="checkbox"/>'
1216
- end
1217
-
1218
- it "should be able to change input type" do
1219
- Forme::Form.new([:foo]).input(:first, :type=>:email).must_equal '<input id="first" name="first" type="email" value="foo"/>'
1220
- end
1221
-
1222
- it "should not have default value for file input" do
1223
- Forme::Form.new([:foo]).input(:first, :type=>:file).must_equal '<input id="first" name="first" type="file"/>'
1224
- end
1225
-
1226
- it "should be able to set value for file input" do
1227
- Forme::Form.new([:foo]).input(:first, :type=>:file, :value=>"foo").must_equal '<input id="first" name="first" type="file" value="foo"/>'
1228
- end
1229
-
1230
- it "should respect given :key option" do
1231
- Forme::Form.new([:foo]).input(:first, :key=>'a').must_equal '<input id="a" name="a" type="text" value="foo"/>'
1232
- end
1233
-
1234
- it "should not accept 3 hash arguments" do
1235
- proc{Forme.form({:a=>'1'}, {:b=>'2'}, {:c=>'3'})}.must_raise Forme::Error
1236
- end
1237
- end
1238
-
1239
- describe "Forme.form DSL" do
1240
- it "should return a form tag" do
1241
- Forme.form.must_equal '<form></form>'
1242
- end
1243
-
1244
- it "should yield a Form object to the block" do
1245
- Forme.form{|f| f.must_be_kind_of(Forme::Form)}
1246
- end
1247
-
1248
- it "should respect an array of classes" do
1249
- Forme.form(:class=>[:foo, :bar]).must_equal '<form class="foo bar"></form>'
1250
- Forme.form(:class=>[:foo, [:bar, :baz]]).must_equal '<form class="foo bar baz"></form>'
1251
- end
1252
-
1253
- it "should have inputs called instead the block be added to the existing form" do
1254
- Forme.form{|f| f.input(:text)}.must_equal '<form><input type="text"/></form>'
1255
- end
1256
-
1257
- it "should be able to nest inputs inside tags" do
1258
- Forme.form{|f| f.tag(:div){f.input(:text)}}.must_equal '<form><div><input type="text"/></div></form>'
1259
- Forme.form{|f| f.tag(:div){f.tag(:fieldset){f.input(:text)}}}.must_equal '<form><div><fieldset><input type="text"/></fieldset></div></form>'
1260
- Forme.form{|f| f.tag(:div){f.tag(:fieldset){f.tag(:span){f.input(:text)}}}}.must_equal '<form><div><fieldset><span><input type="text"/></span></fieldset></div></form>'
1261
- Forme.form{|f| f.tag(:div){f.input(:text)}; f.input(:radio)}.must_equal '<form><div><input type="text"/></div><input type="radio"/></form>'
1262
- Forme.form{|f| f.tag(:div){f.tag(:fieldset){f.input(:text); f.input(:radio)}; f.input(:checkbox)}}.must_equal '<form><div><fieldset><input type="text"/><input type="radio"/></fieldset><input type="checkbox"/></div></form>'
1263
- end
1264
-
1265
- it "should handle an :inputs option to automatically create inputs" do
1266
- Forme.form({}, :inputs=>[:text, :textarea]).must_equal '<form><fieldset class="inputs"><input type="text"/><textarea></textarea></fieldset></form>'
1267
- end
1268
-
1269
- it "should handle a :legend option if inputs is used" do
1270
- Forme.form({}, :inputs=>[:text, :textarea], :legend=>'Foo').must_equal '<form><fieldset class="inputs"><legend>Foo</legend><input type="text"/><textarea></textarea></fieldset></form>'
1271
- end
1272
-
1273
- it "should still work with a block if :inputs is used" do
1274
- Forme.form({}, :inputs=>[:text]){|f| f.input(:textarea)}.must_equal '<form><fieldset class="inputs"><input type="text"/></fieldset><textarea></textarea></form>'
1275
- end
1276
-
1277
- it "should handle an :button option to automatically create a button" do
1278
- Forme.form({}, :button=>'Foo').must_equal '<form><input type="submit" value="Foo"/></form>'
1279
- end
1280
-
1281
- it "should allow :button option value to be a hash" do
1282
- Forme.form({}, :button=>{:value=>'Foo', :name=>'bar'}).must_equal '<form><input name="bar" type="submit" value="Foo"/></form>'
1283
- end
1284
-
1285
- it "should handle an :button option work with a block" do
1286
- Forme.form({}, :button=>'Foo'){|f| f.input(:textarea)}.must_equal '<form><textarea></textarea><input type="submit" value="Foo"/></form>'
1287
- end
1288
-
1289
- it "should have an :button and :inputs option work together" do
1290
- Forme.form({}, :inputs=>[:text, :textarea], :button=>'Foo').must_equal '<form><fieldset class="inputs"><input type="text"/><textarea></textarea></fieldset><input type="submit" value="Foo"/></form>'
1291
- end
1292
- end