1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
require File.dirname(__FILE__) + '/../abstract_unit'
class FormHelperTest < Test::Unit::TestCase
include ActionView::Helpers::FormHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
silence_warnings do
Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost)
Post.class_eval do
alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
end
end
def setup
@post = Post.new
def @post.errors() Class.new{ def on(field) field == "author_name" end }.new end
def @post.id; 123; end
def @post.id_before_type_cast; 123; end
@post.title = "Hello World"
@post.author_name = ""
@post.body = "Back to the hill and over it again!"
@post.secret = 1
@post.written_on = Date.new(2004, 6, 15)
@controller = Class.new do
def url_for(options, *parameters_for_method_reference)
"http://www.example.com"
end
end
@controller = @controller.new
end
def test_text_field
assert_dom_equal(
'<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
)
assert_dom_equal(
'<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title")
)
assert_dom_equal(
'<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name")
)
end
def test_text_field_with_escapes
@post.title = "<b>Hello World</b>"
assert_dom_equal(
'<input id="post_title" name="post[title]" size="30" type="text" value="<b>Hello World</b>" />', text_field("post", "title")
)
end
def test_text_field_with_options
expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />'
assert_dom_equal expected, text_field("post", "title", "size" => 35)
assert_dom_equal expected, text_field("post", "title", :size => 35)
end
def test_text_field_assuming_size
expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />'
assert_dom_equal expected, text_field("post", "title", "maxlength" => 35)
assert_dom_equal expected, text_field("post", "title", :maxlength => 35)
end
def test_check_box
assert_dom_equal(
'<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
check_box("post", "secret")
)
@post.secret = 0
assert_dom_equal(
'<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
check_box("post", "secret")
)
assert_dom_equal(
'<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
check_box("post", "secret" ,{"checked"=>"checked"})
)
@post.secret = true
assert_dom_equal(
'<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
check_box("post", "secret")
)
end
def test_check_box_with_explicit_checked_and_unchecked_values
@post.secret = "on"
assert_dom_equal(
'<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" /><input name="post[secret]" type="hidden" value="off" />',
check_box("post", "secret", {}, "on", "off")
)
end
def test_radio_button
assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
radio_button("post", "title", "Hello World")
)
assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
radio_button("post", "title", "Goodbye World")
)
end
def test_radio_button_is_checked_with_integers
assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
radio_button("post", "secret", "1")
)
end
def test_text_area
assert_dom_equal(
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
text_area("post", "body")
)
end
def test_text_area_with_escapes
@post.body = "Back to <i>the</i> hill and over it again!"
assert_dom_equal(
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to <i>the</i> hill and over it again!</textarea>',
text_area("post", "body")
)
end
def test_date_selects
assert_dom_equal(
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
text_area("post", "body")
)
end
def test_explicit_name
assert_dom_equal(
'<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
)
assert_dom_equal(
'<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
text_area("post", "body", "name" => "really!")
)
assert_dom_equal(
'<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />',
check_box("post", "secret", "name" => "i mean it")
)
assert_dom_equal text_field("post", "title", "name" => "dont guess"),
text_field("post", "title", :name => "dont guess")
assert_dom_equal text_area("post", "body", "name" => "really!"),
text_area("post", "body", :name => "really!")
assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
check_box("post", "secret", :name => "i mean it")
end
def test_explicit_id
assert_dom_equal(
'<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
)
assert_dom_equal(
'<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
text_area("post", "body", "id" => "really!")
)
assert_dom_equal(
'<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
check_box("post", "secret", "id" => "i mean it")
)
assert_dom_equal text_field("post", "title", "id" => "dont guess"),
text_field("post", "title", :id => "dont guess")
assert_dom_equal text_area("post", "body", "id" => "really!"),
text_area("post", "body", :id => "really!")
assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
check_box("post", "secret", :id => "i mean it")
end
def test_auto_index
pid = @post.id
assert_dom_equal(
"<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
)
assert_dom_equal(
"<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
text_area("post[]", "body")
)
assert_dom_equal(
"<input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" /><input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" />",
check_box("post[]", "secret")
)
assert_dom_equal(
"<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
radio_button("post[]", "title", "Hello World")
)
assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
radio_button("post[]", "title", "Goodbye World")
)
end
def test_form_for
_erbout = ''
form_for(:post => @post) do |f|
_erbout.concat f.text_field(:title)
_erbout.concat f.text_area(:body)
_erbout.concat f.check_box(:secret)
end
expected =
"<form action='http://www.example.com' method='post'>" +
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
"<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='post[secret]' type='hidden' value='0' />" +
"</form>"
assert_dom_equal expected, _erbout
end
def test_fields_for
_erbout = ''
fields_for(:post => @post) do |f|
_erbout.concat f.text_field(:title)
_erbout.concat f.text_area(:body)
_erbout.concat f.check_box(:secret)
end
expected =
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
"<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
"<input name='post[secret]' type='hidden' value='0' />"
assert_dom_equal expected, _erbout
end
def test_form_for_and_fields_for
_erbout = ''
form_for(:post => @post) do |post_form|
_erbout.concat post_form.text_field(:title)
_erbout.concat post_form.text_area(:body)
fields_for(:parent_post => @post) do |parent_fields|
_erbout.concat parent_fields.check_box(:secret)
end
end
expected =
"<form action='http://www.example.com' method='post'>" +
"<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
"<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
"<input name='parent_post[secret]' type='hidden' value='0' />" +
"</form>"
assert_dom_equal expected, _erbout
end
end
|