Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / template / form_tag_helper_test.rb
blobd0f9e9903e321bbb87878449585b45ab5939f212
1 require "#{File.dirname(__FILE__)}/../abstract_unit"
3 class FormTagHelperTest < Test::Unit::TestCase
4   include ActionView::Helpers::UrlHelper
5   include ActionView::Helpers::TagHelper
6   include ActionView::Helpers::FormTagHelper
7   include ActionView::Helpers::TextHelper
8   include ActionView::Helpers::CaptureHelper
10   def setup
11     @controller = Class.new do
12       def url_for(options)
13         "http://www.example.com"
14       end
15     end
16     @controller = @controller.new
17   end
19   def test_check_box_tag
20     actual = check_box_tag "admin"
21     expected = %(<input id="admin" name="admin" type="checkbox" value="1" />)
22     assert_dom_equal expected, actual
23   end
25   def test_form_tag
26     actual = form_tag
27     expected = %(<form action="http://www.example.com" method="post">)
28     assert_dom_equal expected, actual
29   end
31   def test_form_tag_multipart
32     actual = form_tag({}, { 'multipart' => true })
33     expected = %(<form action="http://www.example.com" enctype="multipart/form-data" method="post">)
34     assert_dom_equal expected, actual
35   end
37   def test_form_tag_with_method_put
38     actual = form_tag({}, { :method => :put })
39     expected = %(<form action="http://www.example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>)
40     assert_dom_equal expected, actual
41   end
42   
43   def test_form_tag_with_method_delete
44     actual = form_tag({}, { :method => :delete })
45     expected = %(<form action="http://www.example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="delete" /></div>)
46     assert_dom_equal expected, actual
47   end
49   def test_form_tag_with_block
50     _erbout = ''
51     form_tag("http://example.com") { _erbout.concat "Hello world!" }
53     expected = %(<form action="http://example.com" method="post">Hello world!</form>)
54     assert_dom_equal expected, _erbout
55   end
57   def test_form_tag_with_block_and_method
58     _erbout = ''
59     form_tag("http://example.com", :method => :put) { _erbout.concat "Hello world!" }
61     expected = %(<form action="http://example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>Hello world!</form>)
62     assert_dom_equal expected, _erbout
63   end
65   def test_hidden_field_tag
66     actual = hidden_field_tag "id", 3
67     expected = %(<input id="id" name="id" type="hidden" value="3" />)
68     assert_dom_equal expected, actual
69   end
71   def test_file_field_tag
72     assert_dom_equal "<input name=\"picsplz\" type=\"file\" id=\"picsplz\" />", file_field_tag("picsplz")
73   end
75   def test_file_field_tag_with_options
76     assert_dom_equal "<input name=\"picsplz\" type=\"file\" id=\"picsplz\" class=\"pix\"/>", file_field_tag("picsplz", :class => "pix")
77   end
79   def test_password_field_tag
80     actual = password_field_tag
81     expected = %(<input id="password" name="password" type="password" />)
82     assert_dom_equal expected, actual
83   end
85   def test_radio_button_tag
86     actual = radio_button_tag "people", "david"
87     expected = %(<input id="people_david" name="people" type="radio" value="david" />)
88     assert_dom_equal expected, actual
90     actual = radio_button_tag("num_people", 5)
91     expected = %(<input id="num_people_5" name="num_people" type="radio" value="5" />)
92     assert_dom_equal expected, actual
94     actual = radio_button_tag("gender", "m") + radio_button_tag("gender", "f")
95     expected = %(<input id="gender_m" name="gender" type="radio" value="m" /><input id="gender_f" name="gender" type="radio" value="f" />)
96     assert_dom_equal expected, actual
97     
98     actual = radio_button_tag("opinion", "-1") + radio_button_tag("opinion", "1")
99     expected = %(<input id="opinion_-1" name="opinion" type="radio" value="-1" /><input id="opinion_1" name="opinion" type="radio" value="1" />)
100     assert_dom_equal expected, actual
101     
102     actual = radio_button_tag("person[gender]", "m")
103     expected = %(<input id="person_gender_m" name="person[gender]" type="radio" value="m" />)
104     assert_dom_equal expected, actual
105   end
107   def test_select_tag
108     actual = select_tag "people", "<option>david</option>"
109     expected = %(<select id="people" name="people"><option>david</option></select>)
110     assert_dom_equal expected, actual
111   end
112   
113   def test_select_tag_with_multiple
114     actual = select_tag "colors", "<option>Red</option><option>Blue</option><option>Green</option>", :multiple => :true
115     expected = %(<select id="colors" multiple="multiple" name="colors"><option>Red</option><option>Blue</option><option>Green</option></select>)
116     assert_dom_equal expected, actual
117   end
118   
119   def test_select_tag_disabled
120     actual = select_tag "places", "<option>Home</option><option>Work</option><option>Pub</option>", :disabled => :true
121     expected = %(<select id="places" disabled="disabled" name="places"><option>Home</option><option>Work</option><option>Pub</option></select>)
122     assert_dom_equal expected, actual
123   end
125   def test_text_area_tag_size_string
126     actual = text_area_tag "body", "hello world", "size" => "20x40"
127     expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>)
128     assert_dom_equal expected, actual
129   end
131   def test_text_area_tag_size_symbol
132     actual = text_area_tag "body", "hello world", :size => "20x40"
133     expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>)
134     assert_dom_equal expected, actual
135   end
137   def test_text_area_tag_should_disregard_size_if_its_given_as_an_integer
138     actual = text_area_tag "body", "hello world", :size => 20
139     expected = %(<textarea id="body" name="body">hello world</textarea>)
140     assert_dom_equal expected, actual
141   end
143   def test_text_field_tag
144     actual = text_field_tag "title", "Hello!"
145     expected = %(<input id="title" name="title" type="text" value="Hello!" />)
146     assert_dom_equal expected, actual
147   end
149   def test_text_field_tag_class_string
150     actual = text_field_tag "title", "Hello!", "class" => "admin"
151     expected = %(<input class="admin" id="title" name="title" type="text" value="Hello!" />)
152     assert_dom_equal expected, actual
153   end
154   
155   def test_text_field_tag_size_symbol
156     actual = text_field_tag "title", "Hello!", :size => 75
157     expected = %(<input id="title" name="title" size="75" type="text" value="Hello!" />)
158     assert_dom_equal expected, actual
159   end
160   
161   def test_text_field_tag_size_string
162     actual = text_field_tag "title", "Hello!", "size" => "75"
163     expected = %(<input id="title" name="title" size="75" type="text" value="Hello!" />)
164     assert_dom_equal expected, actual
165   end
166   
167   def test_text_field_tag_maxlength_symbol
168     actual = text_field_tag "title", "Hello!", :maxlength => 75
169     expected = %(<input id="title" name="title" maxlength="75" type="text" value="Hello!" />)
170     assert_dom_equal expected, actual
171   end
172   
173   def test_text_field_tag_maxlength_string
174     actual = text_field_tag "title", "Hello!", "maxlength" => "75"
175     expected = %(<input id="title" name="title" maxlength="75" type="text" value="Hello!" />)
176     assert_dom_equal expected, actual
177   end
178   
179   def test_text_field_disabled
180     actual = text_field_tag "title", "Hello!", :disabled => :true
181     expected = %(<input id="title" name="title" disabled="disabled" type="text" value="Hello!" />)
182     assert_dom_equal expected, actual
183   end
184   
185   def test_text_field_tag_with_multiple_options
186     actual = text_field_tag "title", "Hello!", :size => 70, :maxlength => 80
187     expected = %(<input id="title" name="title" size="70" maxlength="80" type="text" value="Hello!" />)
188     assert_dom_equal expected, actual
189   end
191   def test_boolean_optios
192     assert_dom_equal %(<input checked="checked" disabled="disabled" id="admin" name="admin" readonly="readonly" type="checkbox" value="1" />), check_box_tag("admin", 1, true, 'disabled' => true, :readonly => "yes")
193     assert_dom_equal %(<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />), check_box_tag("admin", 1, true, :disabled => false, :readonly => nil)
194     assert_dom_equal %(<select id="people" multiple="multiple" name="people"><option>david</option></select>), select_tag("people", "<option>david</option>", :multiple => true)
195     assert_dom_equal %(<select id="people" name="people"><option>david</option></select>), select_tag("people", "<option>david</option>", :multiple => nil)
196   end
198   def test_stringify_symbol_keys
199     actual = text_field_tag "title", "Hello!", :id => "admin"
200     expected = %(<input id="admin" name="title" type="text" value="Hello!" />)
201     assert_dom_equal expected, actual
202   end
204   def test_submit_tag
205     assert_dom_equal(
206       %(<input name='commit' type='submit' value='Save' onclick="this.setAttribute('originalValue', this.value);this.disabled=true;this.value='Saving...';alert('hello!');result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute('originalValue'); this.disabled = false };return result" />),
207       submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!')")
208     )
209   end
211   def test_pass
212     assert_equal 1, 1
213   end
215   def test_field_set_tag
216     _erbout = ''
217     field_set_tag("Your details") { _erbout.concat "Hello world!" }
219     expected = %(<fieldset><legend>Your details</legend>Hello world!</fieldset>)
220     assert_dom_equal expected, _erbout
222     _erbout = ''
223     field_set_tag { _erbout.concat "Hello world!" }
225     expected = %(<fieldset>Hello world!</fieldset>)
226     assert_dom_equal expected, _erbout
227     
228     _erbout = ''
229     field_set_tag('') { _erbout.concat "Hello world!" }
231     expected = %(<fieldset>Hello world!</fieldset>)
232     assert_dom_equal expected, _erbout
233   end
235   def protect_against_forgery?
236     false
237   end