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