bureaucrat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + "/test_helper"
2
+
3
+ class SimpleForm < Bureaucrat::Forms::Form
4
+ include Bureaucrat::Fields
5
+
6
+ field :name, CharField.new
7
+ end
8
+
9
+ SimpleFormFormSet = Bureaucrat::Formsets.make_formset_class(SimpleForm,
10
+ :extra => 2)
11
+
12
+ class TestFormset < BureaucratTestCase
13
+ describe 'formset with empty data' do
14
+ setup do
15
+ management_form_data = {
16
+ :'form-TOTAL_FORMS' => '2',
17
+ :'form-INITIAL_FORMS' => '2'
18
+ }
19
+ valid_data = {:'form-0-name' => 'Lynch', :'form-1-name' => 'Tio'}
20
+ invalid_data = {:'form-0-name' => 'Lynch', :'form-1-name' => ''}
21
+ @set = SimpleFormFormSet.new
22
+ @valid_bound_set = SimpleFormFormSet.new(management_form_data.merge(valid_data))
23
+ @invalid_bound_set = SimpleFormFormSet.new(management_form_data.merge(invalid_data))
24
+ end
25
+
26
+ should 'correctly render when calling as_table' do
27
+ expected = normalize_html("<input name='form-TOTAL_FORMS' id='id_form-TOTAL_FORMS' type='hidden' value='2'/><input name='form-INITIAL_FORMS' id='id_form-INITIAL_FORMS' type='hidden' value='0'/>\n<tr><th><label for='id_form-0-name'>Name:</label></th><td><input name='form-0-name' id='id_form-0-name' type='text'/></td></tr> <tr><th><label for='id_form-1-name'>Name:</label></th><td><input name='form-1-name' id='id_form-1-name' type='text'/></td></tr>")
28
+ rendered = normalize_html(@set.as_table)
29
+ assert_equal(expected, rendered)
30
+ end
31
+
32
+ should '#valid? returns true if all forms are valid' do
33
+ assert(@valid_bound_set.valid?)
34
+ end
35
+
36
+ should '#valid? returns false if there is an invalid form' do
37
+ assert(!@invalid_bound_set.valid?)
38
+ end
39
+
40
+ should 'correctly return the list of errors' do
41
+ assert_equal([{}, {:name => ["This field is required"]}],
42
+ @invalid_bound_set.errors)
43
+ end
44
+
45
+ should 'correctly return the list of cleaned data' do
46
+ expected = [{:name => 'Lynch'}, {:name => 'Tio'}]
47
+ result = @valid_bound_set.cleaned_data
48
+ assert_equal(expected, result)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,22 @@
1
+ require 'bigdecimal'
2
+ require 'rubygems'
3
+ require "contest"
4
+
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'bureaucrat')
6
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'bureaucrat', 'formsets')
7
+
8
+ # Used to compare rendered htmls
9
+ require 'rexml/document'
10
+
11
+ class BureaucratTestCase < Test::Unit::TestCase
12
+ include Bureaucrat
13
+ end
14
+
15
+ def normalize_html(html)
16
+ begin
17
+ node = REXML::Document.new("<DUMMYROOT>#{html}</DUMMYROOT>")
18
+ node.to_s.gsub!(/<\/?DUMMYROOT>/, '')
19
+ rescue Exception => e
20
+ html
21
+ end
22
+ end
@@ -0,0 +1,328 @@
1
+ require File.dirname(__FILE__) + "/test_helper"
2
+
3
+ class TestWidgets < BureaucratTestCase
4
+ describe 'Media' do
5
+ describe 'empty' do
6
+ setup do
7
+ @media = Widgets::Media.new
8
+ end
9
+
10
+ should 'render an empty string' do
11
+ rendered = @media.render
12
+ assert_equal('', rendered)
13
+ end
14
+ end
15
+
16
+ describe 'with only javascript' do
17
+ setup do
18
+ @media = Widgets::Media.new(:js => ['test.js', 'test2.js'])
19
+ end
20
+
21
+ should 'render correctly and in same order' do
22
+ rendered = @media.render
23
+ expected = "<script type=\"text/javascript\" src=\"http://localhost/test.js\"></script>\n<script type=\"text/javascript\" src=\"http://localhost/test2.js\"></script>"
24
+ assert_equal(expected, rendered)
25
+ end
26
+ end
27
+
28
+ describe 'with only css' do
29
+ setup do
30
+ @media = Widgets::Media.new(:css => {:screen => ['test.css',
31
+ 'test2.css']})
32
+ end
33
+
34
+ should 'render correctly' do
35
+ rendered = @media.render
36
+ expected = "<link href=\"http://localhost/test.css\" type=\"text/css\" media=\"screen\" rel=\"stylesheet\" />\n<link href=\"http://localhost/test2.css\" type=\"text/css\" media=\"screen\" rel=\"stylesheet\" />"
37
+ assert_equal(expected, rendered)
38
+ end
39
+ end
40
+
41
+ describe 'with both js and css' do
42
+ setup do
43
+ @media = Widgets::Media.new(:js => ['test.js', 'test2.js'],
44
+ :css => {:screen => ['test.css',
45
+ 'test2.css']})
46
+ end
47
+
48
+ should 'render correctly' do
49
+ rendered = @media.render
50
+ expected = "<link href=\"http://localhost/test.css\" type=\"text/css\" media=\"screen\" rel=\"stylesheet\" />\n<link href=\"http://localhost/test2.css\" type=\"text/css\" media=\"screen\" rel=\"stylesheet\" />\n<script type=\"text/javascript\" src=\"http://localhost/test.js\"></script>\n<script type=\"text/javascript\" src=\"http://localhost/test2.js\"></script>"
51
+ assert_equal(expected, rendered)
52
+ end
53
+ end
54
+
55
+ describe 'the result of summing two instances of Media' do
56
+ setup do
57
+ @media1 = Widgets::Media.new(:js => ['test1.js', 'test2.js'],
58
+ :css => {:screen => ['test1.css',
59
+ 'test2.css']})
60
+ @media2 = Widgets::Media.new(:js => ['test3.js', 'test4.js'],
61
+ :css => {:screen => ['test3.css',
62
+ 'test4.css']})
63
+ @combined = @media1 + @media2
64
+ end
65
+
66
+ should 'be an instance of Media' do
67
+ assert_kind_of(Widgets::Media, @combined)
68
+ end
69
+
70
+ should 'contain a combined list of js' do
71
+ expected = @media1.to_hash[:js] + @media2.to_hash[:js]
72
+ result = @combined.to_hash[:js]
73
+ assert_equal(expected, result)
74
+ end
75
+
76
+ should 'contain a combined list of css' do
77
+ expected = @media1.to_hash[:css]
78
+ @media2.to_hash[:css].each do |k, v|
79
+ expected[k] ||= []
80
+ expected[k] += v
81
+ end
82
+ result = @combined.to_hash[:css]
83
+ assert_equal(expected, result)
84
+ end
85
+ end
86
+ end
87
+
88
+ describe 'Widget instance' do
89
+ should 'return an instance of Media when calling #media' do
90
+ widget = Widgets::Widget.new
91
+ assert_kind_of(Widgets::Media, widget.media)
92
+ end
93
+ end
94
+
95
+ describe 'TextInput widget' do
96
+
97
+ describe 'with empty attributes' do
98
+ should 'correctly render' do
99
+ input = Widgets::TextInput.new
100
+ expected = normalize_html('<input type="text" value="hi" name="test" />')
101
+ rendered = normalize_html(input.render('test', 'hi'))
102
+ assert_equal(expected, rendered)
103
+ end
104
+ end
105
+
106
+ describe 'with attributes' do
107
+ should 'correctly render' do
108
+ input = Widgets::TextInput.new(:attribute => 'value')
109
+ expected = normalize_html('<input type="text" value="hi" name="test" attribute="value" />')
110
+ rendered = normalize_html(input.render('test', 'hi'))
111
+ assert_equal(expected, rendered)
112
+ end
113
+ end
114
+
115
+ describe 'without value' do
116
+ should 'not render a value' do
117
+ input = Widgets::TextInput.new
118
+ expected = normalize_html('<input type="text" name="test" />')
119
+ rendered = normalize_html(input.render('test', nil))
120
+ assert_equal(expected, rendered)
121
+ end
122
+ end
123
+
124
+ describe 'when copied' do
125
+ should 'have a copy of the attributes' do
126
+ input1 = Widgets::TextInput.new(:attribute => 2)
127
+ input2 = input1.dup
128
+ assert_not_equal(input1.attrs.object_id, input2.attrs.object_id)
129
+ end
130
+ end
131
+ end
132
+
133
+ describe 'PasswordInput widget' do
134
+ describe 'with render_value=true' do
135
+ should 'render correctly including value' do
136
+ input = Widgets::PasswordInput.new
137
+ excepted = normalize_html("<input name='test' type='password' value='secret'/>")
138
+ rendered = normalize_html(input.render('test', 'secret'))
139
+ assert_equal(excepted, rendered)
140
+ end
141
+ end
142
+
143
+ describe 'with render_value=false' do
144
+ should 'render correctly not including value' do
145
+ input = Widgets::PasswordInput.new(nil, false)
146
+ excepted = normalize_html("<input name='test' type='password'/>")
147
+ rendered = normalize_html(input.render('test', 'secret'))
148
+ assert_equal(excepted, rendered)
149
+ end
150
+ end
151
+ end
152
+
153
+ describe 'HiddenInput widget' do
154
+ should 'correctly render' do
155
+ input = Widgets::HiddenInput.new
156
+ excepted = normalize_html("<input name='test' type='hidden' value='secret'/>")
157
+ rendered = normalize_html(input.render('test', 'secret'))
158
+ assert_equal(excepted, rendered)
159
+ end
160
+ end
161
+
162
+ describe 'MultipleHiddenInput widget' do
163
+ should 'correctly render' do
164
+ input = Widgets::MultipleHiddenInput.new
165
+ excepted = normalize_html("<input name='test' type='hidden' value='v1'/>\n<input name='test' type='hidden' value='v2'/>")
166
+ rendered = normalize_html(input.render('test', ['v1', 'v2']))
167
+ assert_equal(excepted, rendered)
168
+ end
169
+ # TODO: value_from_datahash
170
+ end
171
+
172
+ describe 'FileInput widget' do
173
+ should 'correctly render' do
174
+ input = Widgets::FileInput.new
175
+ excepted = normalize_html("<input name='test' type='file'/>")
176
+ rendered = normalize_html(input.render('test', "anything"))
177
+ assert_equal(excepted, rendered)
178
+ end
179
+ # TODO: value_from_datahash, has_changed?
180
+ end
181
+
182
+ describe 'Textarea widget' do
183
+ should 'correctly render' do
184
+ input = Widgets::Textarea.new(:cols => '50', :rows => '15')
185
+ excepted = normalize_html("<textarea name='test' rows='15' cols='50'>hello</textarea>")
186
+ rendered = normalize_html(input.render('test', "hello"))
187
+ assert_equal(excepted, rendered)
188
+ end
189
+ end
190
+
191
+ describe 'CheckboxInput widget' do
192
+ should 'correctly render with a false value' do
193
+ input = Widgets::CheckboxInput.new
194
+ excepted = normalize_html("<input name='test' type='checkbox' value='false'/>")
195
+ rendered = normalize_html(input.render('test', 'false'))
196
+ assert_equal(excepted, rendered)
197
+ end
198
+
199
+ should 'correctly render with a true value' do
200
+ input = Widgets::CheckboxInput.new
201
+ excepted = normalize_html("<input name='test' type='checkbox' value='true'/>")
202
+ rendered = normalize_html(input.render('test', 'true'))
203
+ assert_equal(excepted, rendered)
204
+ end
205
+
206
+ should 'correctly render with a non boolean value' do
207
+ input = Widgets::CheckboxInput.new
208
+ excepted = normalize_html("<input name='test' type='checkbox' value='anything'/>")
209
+ rendered = normalize_html(input.render('test', 'anything'))
210
+ assert_equal(excepted, rendered)
211
+ end
212
+ # TODO: value_from_datahash, has_changed?
213
+ end
214
+
215
+ describe 'Select widget' do
216
+ def setup
217
+ @choices = [['1', 'One'], ['2', 'Two']]
218
+ @groupchoices = [['numbers', ['1', 'One'], ['2', 'Two']],
219
+ ['words', [['spoon', 'Spoon'], ['banana', 'Banana']]]]
220
+ end
221
+
222
+ describe 'with empty choices' do
223
+ should 'correctly render' do
224
+ input = Widgets::Select.new
225
+ excepted = normalize_html("<select name='test'>\n</select>")
226
+ rendered = normalize_html(input.render('test', 'hello'))
227
+ assert_equal(excepted, rendered)
228
+ end
229
+ end
230
+
231
+ describe 'with flat choices' do
232
+ should 'correctly render (none selected)' do
233
+ input = Widgets::Select.new(nil, @choices)
234
+ excepted = normalize_html("<select name='test'>\n<option value='1'>One</option>\n<option value='2'>Two</option>\n</select>")
235
+ rendered = normalize_html(input.render('test', 'hello'))
236
+ assert_equal(excepted, rendered)
237
+ end
238
+
239
+ should 'correctly render (with selected)' do
240
+ input = Widgets::Select.new(nil, @choices)
241
+ excepted = normalize_html("<select name='test'>\n<option value='1'>One</option>\n<option value='2' selected='selected'>Two</option>\n</select>")
242
+ rendered = normalize_html(input.render('test', '2'))
243
+ assert_equal(excepted, rendered)
244
+ end
245
+ end
246
+
247
+ describe 'with group choices' do
248
+ should 'correctly render (none selected)' do
249
+ input = Widgets::Select.new(nil, @groupchoices)
250
+ excepted = normalize_html("<select name='test'>\n<optgroup label='numbers'>\n<option value='1'/>\n<option value='One'/>\n</optgroup>\n<optgroup label='words'>\n<option value='spoon'>Spoon</option>\n<option value='banana'>Banana</option>\n</optgroup>\n</select>")
251
+ rendered = normalize_html(input.render('test', 'hello'))
252
+ assert_equal(excepted, rendered)
253
+ end
254
+
255
+ should 'correctly render (with selected)' do
256
+ input = Widgets::Select.new(nil, @groupchoices)
257
+ excepted = normalize_html("<select name='test'>\n<optgroup label='numbers'>\n<option value='1'/>\n<option value='One'/>\n</optgroup>\n<optgroup label='words'>\n<option value='spoon'>Spoon</option>\n<option value='banana' selected='selected'>Banana</option>\n</optgroup>\n</select>")
258
+ rendered = normalize_html(input.render('test', 'banana'))
259
+ assert_equal(excepted, rendered)
260
+ end
261
+ end
262
+ end
263
+
264
+ describe 'NullBooleanSelect widget' do
265
+ should 'correctly render with "Unknown" as the default value when none is selected' do
266
+ input = Widgets::NullBooleanSelect.new
267
+ excepted = normalize_html("<select name='test'>\n<option selected='selected' value='1'>Unknown</option>\n<option value='2'>Yes</option>\n<option value='3'>No</option>\n</select>")
268
+ rendered = normalize_html(input.render('test', nil))
269
+ assert_equal(excepted, rendered)
270
+ end
271
+
272
+ should 'correctly render (with selected)' do
273
+ input = Widgets::NullBooleanSelect.new
274
+ excepted = normalize_html("<select name='test'>\n<option value='1'>Unknown</option>\n<option selected='selected' value='2'>Yes</option>\n<option value='3'>No</option>\n</select>")
275
+ rendered = normalize_html(input.render('test', '2'))
276
+ assert_equal(excepted, rendered)
277
+ end
278
+ end
279
+
280
+ describe 'SelectMultiple widget' do
281
+ def setup
282
+ @choices = [['1', 'One'], ['2', 'Two']]
283
+ @groupchoices = [['numbers', ['1', 'One'], ['2', 'Two']],
284
+ ['words', [['spoon', 'Spoon'], ['banana', 'Banana']]]]
285
+ end
286
+
287
+ describe 'with empty choices' do
288
+ should 'correctly render' do
289
+ input = Widgets::SelectMultiple.new
290
+ excepted = normalize_html("<select name='test' multiple='multiple'>\n</select>")
291
+ rendered = normalize_html(input.render('test', 'hello'))
292
+ assert_equal(excepted, rendered)
293
+ end
294
+ end
295
+
296
+ describe 'with flat choices' do
297
+ should 'correctly render (none selected)' do
298
+ input = Widgets::SelectMultiple.new(nil, @choices)
299
+ excepted = normalize_html("<select name='test' multiple='multiple'>\n<option value='1'>One</option>\n<option value='2'>Two</option>\n</select>")
300
+ rendered = normalize_html(input.render('test', 'hello'))
301
+ assert_equal(excepted, rendered)
302
+ end
303
+
304
+ should 'correctly render (with selected)' do
305
+ input = Widgets::SelectMultiple.new(nil, @choices)
306
+ excepted = normalize_html("<select name='test' multiple='multiple'>\n<option value='1' selected='selected'>One</option>\n<option value='2' selected='selected'>Two</option>\n</select>")
307
+ rendered = normalize_html(input.render('test', ['1', '2']))
308
+ assert_equal(excepted, rendered)
309
+ end
310
+ end
311
+
312
+ describe 'with group choices' do
313
+ should 'correctly render (none selected)' do
314
+ input = Widgets::SelectMultiple.new(nil, @groupchoices)
315
+ excepted = normalize_html("<select name='test' multiple='multiple'>\n<optgroup label='numbers'>\n<option value='1'/>\n<option value='One'/>\n</optgroup>\n<optgroup label='words'>\n<option value='spoon'>Spoon</option>\n<option value='banana'>Banana</option>\n</optgroup>\n</select>")
316
+ rendered = normalize_html(input.render('test', 'hello'))
317
+ assert_equal(excepted, rendered)
318
+ end
319
+
320
+ should 'correctly render (with selected)' do
321
+ input = Widgets::SelectMultiple.new(nil, @groupchoices)
322
+ excepted = normalize_html("<select name='test' multiple='multiple'>\n<optgroup label='numbers'>\n<option value='1'/>\n<option value='One'/>\n</optgroup>\n<optgroup label='words'>\n<option value='spoon' selected='selected'>Spoon</option>\n<option value='banana' selected='selected'>Banana</option>\n</optgroup>\n</select>")
323
+ rendered = normalize_html(input.render('test', ['banana', 'spoon']))
324
+ assert_equal(excepted, rendered)
325
+ end
326
+ end
327
+ end
328
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bureaucrat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Deferrari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-03 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: utizoc@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/bureaucrat/fields.rb
26
+ - lib/bureaucrat/forms.rb
27
+ - lib/bureaucrat/formsets.rb
28
+ - lib/bureaucrat/quickfields.rb
29
+ - lib/bureaucrat/utils.rb
30
+ - lib/bureaucrat/validation.rb
31
+ - lib/bureaucrat/validation_old.rb
32
+ - lib/bureaucrat/widgets.rb
33
+ - lib/bureaucrat/wizard.rb
34
+ - lib/bureaucrat.rb
35
+ - README.md
36
+ - LICENSE
37
+ - test/fields_test.rb
38
+ - test/forms_test.rb
39
+ - test/formsets_test.rb
40
+ - test/test_helper.rb
41
+ - test/widgets_test.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/tizoc/bureaucrat
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Form handling for Ruby inspired by Django forms.
70
+ test_files: []
71
+