bureaucrat 0.0.3 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/forms_test.rb CHANGED
@@ -1,66 +1,61 @@
1
- require File.dirname(__FILE__) + "/test_helper"
1
+ require_relative 'test_helper'
2
2
 
3
- class TestForm < BureaucratTestCase
4
- describe 'inherited form with a CharField' do
3
+ module FormTests
4
+ class Test_inherited_form_with_a_CharField < BureaucratTestCase
5
5
  class OneForm < Forms::Form
6
6
  include Bureaucrat::Fields
7
7
 
8
8
  field :name, CharField.new
9
9
  end
10
10
 
11
- should 'return an instance of Media when calling #media' do
12
- form = OneForm.new
13
- assert_kind_of(Widgets::Media, form.media)
14
- end
15
-
16
- should 'have a BoundField in [:name]' do
11
+ def test_have_a_BoundField
17
12
  form = OneForm.new
18
13
  assert_kind_of(Forms::BoundField, form[:name])
19
14
  end
20
15
 
21
- should 'be bound when data is provided' do
22
- form = OneForm.new(:name => 'name')
16
+ def test_be_bound_when_data_is_provided
17
+ form = OneForm.new(name: 'name')
23
18
  assert_equal(true, form.bound?)
24
19
  end
25
20
 
26
- describe 'when calling #valid?' do
27
- should 'return false when data isn\'t valid' do
28
- form = OneForm.new(:name => nil)
21
+ class Test_when_calling_valid < BureaucratTestCase
22
+ def test_return_false_when_data_isnt_valid
23
+ form = OneForm.new(name: nil)
29
24
  assert_equal(false, form.valid?)
30
25
  end
31
26
 
32
- should 'return true when data is valid' do
33
- form = OneForm.new(:name => 'valid')
27
+ def test_return_true_when_data_is_valid
28
+ form = OneForm.new(name: 'valid')
34
29
  assert_equal(true, form.valid?)
35
30
  end
36
31
  end
37
32
 
38
- describe 'when calling #errors' do
39
- should 'have errors when invalid' do
40
- form = OneForm.new(:name => nil)
33
+ class Test_when_calling_errors < BureaucratTestCase
34
+ def test_have_errors_when_invalid
35
+ form = OneForm.new(name: nil)
41
36
  assert_operator(form.errors.size, :>, 0)
42
37
  end
43
38
 
44
- should 'not have errors when valid' do
45
- form = OneForm.new(:name => 'valid')
39
+ def test_not_have_errors_when_valid
40
+ form = OneForm.new(name: 'valid')
46
41
  assert_equal(form.errors.size, 0)
47
42
  end
48
43
  end
49
44
 
50
- describe 'when calling #changed_data' do
51
- should 'return an empty list if no field was changed' do
45
+ class Test_when_calling_changed_data < BureaucratTestCase
46
+ def test_return_an_empty_list_if_no_field_was_changed
52
47
  form = OneForm.new
53
48
  assert_equal([], form.changed_data)
54
49
  end
55
50
 
56
- should 'return a list of changed fields when modified' do
57
- form = OneForm.new(:name => 'changed')
51
+ def test_return_a_list_of_changed_fields_when_modified
52
+ form = OneForm.new(name: 'changed')
58
53
  assert_equal([:name], form.changed_data)
59
54
  end
60
55
  end
61
56
  end
62
57
 
63
- describe 'form with custom clean proc on field' do
58
+ class Test_form_with_custom_clean_proc_on_field < BureaucratTestCase
64
59
  class CustomCleanForm < Forms::Form
65
60
  include Bureaucrat::Fields
66
61
 
@@ -68,64 +63,89 @@ class TestForm < BureaucratTestCase
68
63
 
69
64
  def clean_name
70
65
  value = cleaned_data[:name]
71
- raise FieldValidationError.new("Invalid name") unless value == 'valid_name'
66
+ unless value == 'valid_name'
67
+ raise Bureaucrat::ValidationError.new("Invalid name")
68
+ end
72
69
  value.upcase
73
70
  end
74
71
  end
75
72
 
76
- should 'not be valid if clean method fails' do
77
- form = CustomCleanForm.new(:name => 'other')
73
+ def test_not_be_valid_if_clean_method_fails
74
+ form = CustomCleanForm.new(name: 'other')
78
75
  assert_equal(false, form.valid?)
79
76
  end
80
77
 
81
- should 'be valid if clean method passes' do
82
- form = CustomCleanForm.new(:name => 'valid_name')
78
+ def test_be_valid_if_clean_method_passes
79
+ form = CustomCleanForm.new(name: 'valid_name')
83
80
  assert_equal(true, form.valid?)
84
81
  end
85
82
 
86
- should 'set the value to the one returned by the custom clean method' do
87
- form = CustomCleanForm.new(:name => 'valid_name')
83
+ def test_set_the_value_to_the_one_returned_by_the_custom_clean_method
84
+ form = CustomCleanForm.new(name: 'valid_name')
88
85
  form.valid?
89
86
  assert_equal('VALID_NAME', form.cleaned_data[:name])
90
87
  end
91
88
 
92
89
  end
93
90
 
94
- describe 'inherited form with two charfields when rendered' do
95
- class TwoForm < Forms::Form
91
+ class Test_populating_objects < BureaucratTestCase
92
+ class PopulatorForm < Forms::Form
96
93
  include Bureaucrat::Fields
97
94
 
98
- field :name, CharField.new(:label => 'Name')
99
- field :color, CharField.new
95
+ field :name, CharField.new(required: false)
96
+ field :color, CharField.new(required: false)
97
+ field :number, IntegerField.new(required: false)
100
98
  end
101
99
 
102
- def setup
103
- @form = TwoForm.new(:name => 'name')
104
- @unbound_form = TwoForm.new
105
- end
100
+ def test_correctly_populate_an_object_with_all_fields
101
+ obj = Struct.new(:name, :color, :number).new
102
+ name_value = 'The Name'
103
+ color_value = 'Black'
104
+ number_value = 10
106
105
 
107
- should 'should correctly render as table' do
108
- expected = normalize_html("<tr><th><label for='id_name'>Name:</label></th><td><input name='name' id='id_name' type='text' value='name'/></td></tr>\n<tr><th><label for='id_color'>Color:</label></th><td><ul class='errorlist'><li>This field is required</li></ul><input name='color' id='id_color' type='text'/></td></tr>")
109
- rendered = normalize_html(@form.as_table)
110
- assert_equal(expected, rendered)
111
- end
106
+ form = PopulatorForm.new(name: name_value,
107
+ color: color_value,
108
+ number: number_value.to_s)
109
+
110
+ assert form.valid?
112
111
 
113
- should 'should correctly render as ul' do
114
- expected = normalize_html("<li><label for='id_name'>Name:</label> <input name='name' id='id_name' type='text' value='name'/></li>\n<li><ul class='errorlist'><li>This field is required</li></ul><label for='id_color'>Color:</label> <input name='color' id='id_color' type='text'/></li>")
115
- rendered = normalize_html(@form.as_ul)
116
- assert_equal(expected, rendered)
112
+ form.populate_object(obj)
113
+
114
+ assert_equal(name_value, obj.name)
115
+ assert_equal(color_value, obj.color)
116
+ assert_equal(number_value, obj.number)
117
117
  end
118
118
 
119
- should 'should correctly render as p' do
120
- expected = normalize_html("<p><label for='id_name'>Name:</label> <input name='name' id='id_name' type='text' value='name'/></p>\nThis field is required\n<p><label for='id_color'>Color:</label> <input name='color' id='id_color' type='text'/></p>")
121
- rendered = normalize_html(@form.as_p)
122
- assert_equal(expected, rendered)
119
+ def test_correctly_populate_an_object_without_all_fields
120
+ obj = Struct.new(:name, :number).new
121
+ name_value = 'The Name'
122
+ color_value = 'Black'
123
+ number_value = 10
124
+
125
+ form = PopulatorForm.new(name: name_value,
126
+ color: color_value,
127
+ number: number_value.to_s)
128
+
129
+ assert form.valid?
130
+
131
+ form.populate_object(obj)
132
+
133
+ assert_equal(name_value, obj.name)
134
+ assert_equal(number_value, obj.number)
123
135
  end
124
136
 
125
- should 'correctly render as p when not bound' do
126
- expected = normalize_html("<p><label for='id_name'>Name:</label> <input name='name' id='id_name' type='text'/></p>\n<p><label for='id_color'>Color:</label> <input name='color' id='id_color' type='text'/></p>")
127
- rendered = normalize_html(@unbound_form.as_p)
128
- assert_equal(expected, rendered)
137
+ def test_correctly_populate_an_object_with_all_fields_with_some_missing_values
138
+ obj = Struct.new(:name, :color, :number).new('a', 'b', 2)
139
+
140
+ form = PopulatorForm.new({})
141
+
142
+ assert form.valid?
143
+
144
+ form.populate_object(obj)
145
+
146
+ assert_equal('', obj.name)
147
+ assert_equal('', obj.color)
148
+ assert_equal(nil, obj.number)
129
149
  end
130
150
  end
131
151
  end
@@ -1,17 +1,28 @@
1
- require File.dirname(__FILE__) + "/test_helper"
1
+ require_relative 'test_helper'
2
2
 
3
- class SimpleForm < Bureaucrat::Forms::Form
4
- include Bureaucrat::Fields
3
+ module FormsetTests
4
+ class SimpleForm < Bureaucrat::Forms::Form
5
+ include Bureaucrat::Fields
5
6
 
6
- field :name, CharField.new
7
- end
7
+ field :name, CharField.new
8
+ end
9
+
10
+ SimpleFormFormSet =
11
+ Bureaucrat::Formsets.make_formset_class(SimpleForm, extra: 2)
12
+
13
+ class CustomFormSet < Bureaucrat::Formsets::BaseFormSet
14
+ def clean
15
+ raise Bureaucrat::ValidationError.new('This is wrong!')
16
+ end
17
+ end
8
18
 
9
- SimpleFormFormSet = Bureaucrat::Formsets.make_formset_class(SimpleForm,
10
- :extra => 2)
19
+ SimpleFormCustomFormSet =
20
+ Bureaucrat::Formsets.make_formset_class(SimpleForm,
21
+ extra: 2,
22
+ formset: CustomFormSet)
11
23
 
12
- class TestFormset < BureaucratTestCase
13
- describe 'formset with empty data' do
14
- setup do
24
+ class Test_formset_with_empty_data < BureaucratTestCase
25
+ def setup
15
26
  management_form_data = {
16
27
  :'form-TOTAL_FORMS' => '2',
17
28
  :'form-INITIAL_FORMS' => '2'
@@ -23,29 +34,44 @@ class TestFormset < BureaucratTestCase
23
34
  @invalid_bound_set = SimpleFormFormSet.new(management_form_data.merge(invalid_data))
24
35
  end
25
36
 
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
37
+ def test_#valid?_returns_true_if_all_forms_are_valid
33
38
  assert(@valid_bound_set.valid?)
34
39
  end
35
40
 
36
- should '#valid? returns false if there is an invalid form' do
41
+ def test_#valid?_returns_false_if_there_is_an_invalid_form
37
42
  assert(!@invalid_bound_set.valid?)
38
43
  end
39
44
 
40
- should 'correctly return the list of errors' do
41
- assert_equal([{}, {:name => ["This field is required"]}],
45
+ def test_correctly_return_the_list_of_errors
46
+ assert_equal([{}, {name: ["This field is required"]}],
42
47
  @invalid_bound_set.errors)
43
48
  end
44
49
 
45
- should 'correctly return the list of cleaned data' do
46
- expected = [{:name => 'Lynch'}, {:name => 'Tio'}]
50
+ def test_correctly_return_the_list_of_cleaned_data
51
+ expected = [{'name' => 'Lynch'}, {'name' => 'Tio'}]
47
52
  result = @valid_bound_set.cleaned_data
48
53
  assert_equal(expected, result)
49
54
  end
50
55
  end
56
+
57
+ class Test_Formset_with_clean_method_raising_a_ValidationError_exception < BureaucratTestCase
58
+ def setup
59
+ management_form_data = {
60
+ :'form-TOTAL_FORMS' => '2',
61
+ :'form-INITIAL_FORMS' => '2'
62
+ }
63
+ valid_data = {:'form-0-name' => 'Lynch', :'form-1-name' => 'Tio'}
64
+ @bound_set =
65
+ SimpleFormCustomFormSet.new(management_form_data.merge(valid_data))
66
+ end
67
+
68
+ def test_not_be_valid
69
+ assert_equal(false, @bound_set.valid?)
70
+ end
71
+
72
+ def test_add_clean_errors_to_nonfield_errors
73
+ @bound_set.valid?
74
+ assert_equal(["This is wrong!"], @bound_set.non_form_errors)
75
+ end
76
+ end
51
77
  end
data/test/test_helper.rb CHANGED
@@ -1,22 +1,30 @@
1
1
  require 'bigdecimal'
2
- require 'rubygems'
3
- require "contest"
2
+ require 'minitest/autorun'
4
3
 
5
- require File.join(File.dirname(__FILE__), '..', 'lib', 'bureaucrat')
6
- require File.join(File.dirname(__FILE__), '..', 'lib', 'bureaucrat', 'formsets')
4
+ require_relative '../lib/bureaucrat'
5
+ require_relative '../lib/bureaucrat/formsets'
7
6
 
8
7
  # Used to compare rendered htmls
9
8
  require 'rexml/document'
10
9
 
11
- class BureaucratTestCase < Test::Unit::TestCase
10
+ class BureaucratTestCase < MiniTest::Unit::TestCase
12
11
  include Bureaucrat
13
- end
14
12
 
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
13
+ def assert_nothing_raised(&block)
14
+ block.call
15
+ assert true
16
+ end
17
+
18
+ def assert_not_equal(value, other)
19
+ assert value != other, "should be different from #{value}"
20
+ end
21
+
22
+ def normalize_html(html)
23
+ begin
24
+ node = REXML::Document.new("<DUMMYROOT>#{html.strip}</DUMMYROOT>")
25
+ node.to_s.gsub!(/<\/?DUMMYROOT>/, '')
26
+ rescue Exception => e
27
+ html
28
+ end
21
29
  end
22
30
  end
data/test/widgets_test.rb CHANGED
@@ -1,101 +1,9 @@
1
- require File.dirname(__FILE__) + "/test_helper"
1
+ require_relative 'test_helper'
2
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
3
+ module WidgetTests
4
+ class Test_TextInput_widget < BureaucratTestCase
5
+ class Test_with_empty_attributes < BureaucratTestCase
6
+ def test_correctly_render
99
7
  input = Widgets::TextInput.new
100
8
  expected = normalize_html('<input type="text" value="hi" name="test" />')
101
9
  rendered = normalize_html(input.render('test', 'hi'))
@@ -103,17 +11,17 @@ class TestWidgets < BureaucratTestCase
103
11
  end
104
12
  end
105
13
 
106
- describe 'with attributes' do
107
- should 'correctly render' do
108
- input = Widgets::TextInput.new(:attribute => 'value')
14
+ class Test_with_attributes < BureaucratTestCase
15
+ def test_correctly_render
16
+ input = Widgets::TextInput.new(attribute: 'value')
109
17
  expected = normalize_html('<input type="text" value="hi" name="test" attribute="value" />')
110
18
  rendered = normalize_html(input.render('test', 'hi'))
111
19
  assert_equal(expected, rendered)
112
20
  end
113
21
  end
114
22
 
115
- describe 'without value' do
116
- should 'not render a value' do
23
+ class Test_without_value < BureaucratTestCase
24
+ def test_not_render_a_value
117
25
  input = Widgets::TextInput.new
118
26
  expected = normalize_html('<input type="text" name="test" />')
119
27
  rendered = normalize_html(input.render('test', nil))
@@ -121,208 +29,320 @@ class TestWidgets < BureaucratTestCase
121
29
  end
122
30
  end
123
31
 
124
- describe 'when copied' do
125
- should 'have a copy of the attributes' do
126
- input1 = Widgets::TextInput.new(:attribute => 2)
32
+ class Test_when_copied < BureaucratTestCase
33
+ def test_have_a_copy_of_the_attributes
34
+ input1 = Widgets::TextInput.new(attribute: 2)
127
35
  input2 = input1.dup
128
36
  assert_not_equal(input1.attrs.object_id, input2.attrs.object_id)
129
37
  end
130
38
  end
131
39
  end
132
40
 
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'/>")
41
+ class Test_PasswordInput_widget < BureaucratTestCase
42
+ class Test_with_render_value_true < BureaucratTestCase
43
+ def test_render_correctly_including_value
44
+ input = Widgets::PasswordInput.new(nil, true)
45
+ expected = normalize_html("<input name='test' type='password' value='secret'/>")
138
46
  rendered = normalize_html(input.render('test', 'secret'))
139
- assert_equal(excepted, rendered)
47
+ assert_equal(expected, rendered)
140
48
  end
141
49
  end
142
50
 
143
- describe 'with render_value=false' do
144
- should 'render correctly not including value' do
51
+ class Test_with_render_value_false < BureaucratTestCase
52
+ def test_render_correctly_not_including_value
145
53
  input = Widgets::PasswordInput.new(nil, false)
146
- excepted = normalize_html("<input name='test' type='password'/>")
54
+ expected = normalize_html("<input name='test' type='password'/>")
147
55
  rendered = normalize_html(input.render('test', 'secret'))
148
- assert_equal(excepted, rendered)
56
+ assert_equal(expected, rendered)
149
57
  end
150
58
  end
151
59
  end
152
60
 
153
- describe 'HiddenInput widget' do
154
- should 'correctly render' do
61
+ class Test_HiddenInput_widget < BureaucratTestCase
62
+ def test_correctly_render
155
63
  input = Widgets::HiddenInput.new
156
- excepted = normalize_html("<input name='test' type='hidden' value='secret'/>")
64
+ expected = normalize_html("<input name='test' type='hidden' value='secret'/>")
157
65
  rendered = normalize_html(input.render('test', 'secret'))
158
- assert_equal(excepted, rendered)
66
+ assert_equal(expected, rendered)
159
67
  end
160
68
  end
161
69
 
162
- describe 'MultipleHiddenInput widget' do
163
- should 'correctly render' do
70
+ class Test_MultipleHiddenInput_widget < BureaucratTestCase
71
+ def test_correctly_render
164
72
  input = Widgets::MultipleHiddenInput.new
165
- excepted = normalize_html("<input name='test' type='hidden' value='v1'/>\n<input name='test' type='hidden' value='v2'/>")
73
+ expected = normalize_html("<input name='test[]' type='hidden' value='v1'/>\n<input name='test[]' type='hidden' value='v2'/>")
166
74
  rendered = normalize_html(input.render('test', ['v1', 'v2']))
167
- assert_equal(excepted, rendered)
75
+ assert_equal(expected, rendered)
168
76
  end
169
77
  # TODO: value_from_datahash
170
78
  end
171
79
 
172
- describe 'FileInput widget' do
173
- should 'correctly render' do
80
+ class Test_FileInput_widget < BureaucratTestCase
81
+ def test_correctly_render
174
82
  input = Widgets::FileInput.new
175
- excepted = normalize_html("<input name='test' type='file'/>")
83
+ expected = normalize_html("<input name='test' type='file'/>")
176
84
  rendered = normalize_html(input.render('test', "anything"))
177
- assert_equal(excepted, rendered)
85
+ assert_equal(expected, rendered)
178
86
  end
179
87
  # TODO: value_from_datahash, has_changed?
180
88
  end
181
89
 
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>")
90
+ class Test_Textarea_widget < BureaucratTestCase
91
+ def test_correctly_render
92
+ input = Widgets::Textarea.new(cols: '50', rows: '15')
93
+ expected = normalize_html("<textarea name='test' rows='15' cols='50'>hello</textarea>")
186
94
  rendered = normalize_html(input.render('test', "hello"))
187
- assert_equal(excepted, rendered)
95
+ assert_equal(expected, rendered)
96
+ end
97
+
98
+ def test_correctly_render_multiline
99
+ input = Widgets::Textarea.new(cols: '50', rows: '15')
100
+ expected = normalize_html("<textarea name='test' rows='15' cols='50'>hello\n\ntest</textarea>")
101
+ rendered = normalize_html(input.render('test', "hello\n\ntest"))
102
+ assert_equal(expected, rendered)
188
103
  end
189
104
  end
190
105
 
191
- describe 'CheckboxInput widget' do
192
- should 'correctly render with a false value' do
106
+ class Test_CheckboxInput_widget < BureaucratTestCase
107
+ def test_correctly_render_with_a_false_value
193
108
  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)
109
+ expected = normalize_html("<input name='test' type='checkbox'/>")
110
+ rendered = normalize_html(input.render('test', false))
111
+ assert_equal(expected, rendered)
197
112
  end
198
113
 
199
- should 'correctly render with a true value' do
114
+ def test_correctly_render_with_a_true_value
200
115
  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)
116
+ expected ="<input checked='checked' name='test' type='checkbox'/>"
117
+ rendered = normalize_html(input.render('test', true))
118
+ assert_equal(expected, rendered)
204
119
  end
205
120
 
206
- should 'correctly render with a non boolean value' do
121
+ def test_correctly_render_with_a_non_boolean_value
207
122
  input = Widgets::CheckboxInput.new
208
- excepted = normalize_html("<input name='test' type='checkbox' value='anything'/>")
123
+ expected = "<input checked='checked' name='test' type='checkbox' value='anything'/>"
209
124
  rendered = normalize_html(input.render('test', 'anything'))
210
- assert_equal(excepted, rendered)
125
+ assert_equal(expected, rendered)
211
126
  end
212
127
  # TODO: value_from_datahash, has_changed?
213
128
  end
214
129
 
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
130
+ module SelectWidgetTests
131
+ class Test_with_empty_choices < BureaucratTestCase
132
+ def test_correctly_render
224
133
  input = Widgets::Select.new
225
- excepted = normalize_html("<select name='test'>\n</select>")
134
+ expected = normalize_html("<select name='test'>\n</select>")
226
135
  rendered = normalize_html(input.render('test', 'hello'))
227
- assert_equal(excepted, rendered)
136
+ assert_equal(expected, rendered)
228
137
  end
229
138
  end
230
139
 
231
- describe 'with flat choices' do
232
- should 'correctly render (none selected)' do
140
+ class Test_with_flat_choices < BureaucratTestCase
141
+ def setup
142
+ @choices = [['1', 'One'], ['2', 'Two']]
143
+ end
144
+
145
+ def test_correctly_render_none_selected
233
146
  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>")
147
+ expected = normalize_html("<select name='test'>\n<option value='1'>One</option>\n<option value='2'>Two</option>\n</select>")
235
148
  rendered = normalize_html(input.render('test', 'hello'))
236
- assert_equal(excepted, rendered)
149
+ assert_equal(expected, rendered)
237
150
  end
238
151
 
239
- should 'correctly render (with selected)' do
152
+ def test_correctly_render_with_selected
240
153
  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>")
154
+ expected = normalize_html("<select name='test'>\n<option value='1'>One</option>\n<option value='2' selected='selected'>Two</option>\n</select>")
242
155
  rendered = normalize_html(input.render('test', '2'))
243
- assert_equal(excepted, rendered)
156
+ assert_equal(expected, rendered)
244
157
  end
245
158
  end
246
159
 
247
- describe 'with group choices' do
248
- should 'correctly render (none selected)' do
160
+ class Test_with_group_choices < BureaucratTestCase
161
+ def setup
162
+ @groupchoices = [['numbers', ['1', 'One'], ['2', 'Two']],
163
+ ['words', [['spoon', 'Spoon'], ['banana', 'Banana']]]]
164
+ end
165
+
166
+ def test_correctly_render_none_selected
249
167
  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>")
168
+ expected = 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
169
  rendered = normalize_html(input.render('test', 'hello'))
252
- assert_equal(excepted, rendered)
170
+ assert_equal(expected, rendered)
253
171
  end
254
172
 
255
- should 'correctly render (with selected)' do
173
+ def test_correctly_render_with_selected
256
174
  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>")
175
+ expected = 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
176
  rendered = normalize_html(input.render('test', 'banana'))
259
- assert_equal(excepted, rendered)
177
+ assert_equal(expected, rendered)
178
+ end
179
+ end
180
+
181
+ class Test_with_simple_choices < BureaucratTestCase
182
+ def setup
183
+ @simplechoices = [ "able", "baker", "charlie" ]
184
+ end
185
+
186
+ def test_correctly_render_none_selected
187
+ input = Widgets::Select.new(nil, @simplechoices)
188
+ expected = normalize_html("<select name='test'>\n<option value='able'>able</option>\n<option value='baker'>baker</option>\n<option value='charlie'>charlie</option>\n</select>")
189
+ rendered = normalize_html(input.render('test', 'hello'))
190
+ assert_equal(expected, rendered)
191
+ end
192
+
193
+ def test_correctly_render_with_selected
194
+ input = Widgets::Select.new(nil, @simplechoices)
195
+ expected = normalize_html("<select name='test'>\n<option value='able'>able</option>\n<option value='baker' selected='selected'>baker</option>\n<option value='charlie'>charlie</option>\n</select>")
196
+ rendered = normalize_html(input.render('test', 'baker'))
197
+ assert_equal(expected, rendered)
198
+ end
199
+ end
200
+
201
+ class Test_with_option_choices < BureaucratTestCase
202
+ def setup
203
+ @optionchoices = [[{ value: "foo", disabled: "disabled", onSelect: "doSomething();" }, "Foo"],
204
+ [{ value: "bar" }, "Bar"]]
205
+ @optionchoicesselected = [[{ value: "foo", disabled: "disabled" }, "Foo"],
206
+ [{ value: "bar", selected: "selected" }, "Bar"]]
207
+ end
208
+
209
+ def test_correctly_render_none_selected
210
+ input = Widgets::Select.new(nil, @optionchoices)
211
+ expected = normalize_html("<select name='test'>\n<option value='foo' disabled='disabled' onSelect='doSomething();'>Foo</option>\n<option value='bar'>Bar</option>\n</select>")
212
+ rendered = normalize_html(input.render('test', 'hello'))
213
+ assert_equal(expected, rendered)
214
+ end
215
+
216
+ def test_correctly_render_traditional_selected
217
+ input = Widgets::Select.new(nil, @optionchoices)
218
+ expected = normalize_html("<select name='test'>\n<option value='foo' disabled='disabled' onSelect='doSomething();'>Foo</option>\n<option value='bar' selected='selected'>Bar</option>\n</select>")
219
+ rendered = normalize_html(input.render('test', 'bar'))
220
+ assert_equal(expected, rendered)
221
+ end
222
+
223
+ def test_correctly_render_option_selected
224
+ input = Widgets::Select.new(nil, @optionchoicesselected)
225
+ expected = normalize_html("<select name='test'>\n<option value='foo' disabled='disabled'>Foo</option>\n<option value='bar' selected='selected'>Bar</option>\n</select>")
226
+ rendered = normalize_html(input.render('test', 'hello'))
227
+ assert_equal(expected, rendered)
260
228
  end
261
229
  end
262
230
  end
263
231
 
264
- describe 'NullBooleanSelect widget' do
265
- should 'correctly render with "Unknown" as the default value when none is selected' do
232
+ class Test_NullBooleanSelect_widget < BureaucratTestCase
233
+ def test_correctly_render_with_Unknown_as_the_default_value_when_none_is_selected
266
234
  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>")
235
+ expected = 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
236
  rendered = normalize_html(input.render('test', nil))
269
- assert_equal(excepted, rendered)
237
+ assert_equal(expected, rendered)
270
238
  end
271
239
 
272
- should 'correctly render (with selected)' do
240
+ def test_correctly_render_with_selected
273
241
  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>")
242
+ expected = 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
243
  rendered = normalize_html(input.render('test', '2'))
276
- assert_equal(excepted, rendered)
244
+ assert_equal(expected, rendered)
277
245
  end
278
246
  end
279
247
 
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
248
+ module SelectMultipleTests
249
+ class Test_with_empty_choices < BureaucratTestCase
250
+ def test_correctly_render
289
251
  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)
252
+ expected = normalize_html("<select name='test[]' multiple='multiple'>\n</select>")
253
+ rendered = normalize_html(input.render('test', ['hello']))
254
+ assert_equal(expected, rendered)
293
255
  end
294
256
  end
295
257
 
296
- describe 'with flat choices' do
297
- should 'correctly render (none selected)' do
258
+ class Test_with_flat_choices < BureaucratTestCase
259
+ def setup
260
+ @choices = [['1', 'One'], ['2', 'Two']]
261
+ end
262
+
263
+ def test_correctly_render_none_selected
298
264
  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)
265
+ expected = normalize_html("<select name='test[]' multiple='multiple'>\n<option value='1'>One</option>\n<option value='2'>Two</option>\n</select>")
266
+ rendered = normalize_html(input.render('test', ['hello']))
267
+ assert_equal(expected, rendered)
302
268
  end
303
269
 
304
- should 'correctly render (with selected)' do
270
+ def test_correctly_render_with_selected
305
271
  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>")
272
+ expected = 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
273
  rendered = normalize_html(input.render('test', ['1', '2']))
308
- assert_equal(excepted, rendered)
274
+ assert_equal(expected, rendered)
309
275
  end
310
276
  end
311
277
 
312
- describe 'with group choices' do
313
- should 'correctly render (none selected)' do
278
+ class Test_with_group_choices < BureaucratTestCase
279
+ def setup
280
+ @groupchoices = [['numbers', ['1', 'One'], ['2', 'Two']],
281
+ ['words', [['spoon', 'Spoon'], ['banana', 'Banana']]]]
282
+ end
283
+
284
+ def test_correctly_render_none_selected
314
285
  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)
286
+ expected = 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>")
287
+ rendered = normalize_html(input.render('test', ['hello']))
288
+ assert_equal(expected, rendered)
318
289
  end
319
290
 
320
- should 'correctly render (with selected)' do
291
+ def test_correctly_render_with_selected
321
292
  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>")
293
+ expected = 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
294
  rendered = normalize_html(input.render('test', ['banana', 'spoon']))
324
- assert_equal(excepted, rendered)
295
+ assert_equal(expected, rendered)
325
296
  end
326
297
  end
327
298
  end
299
+
300
+ class Test_RadioSelect_widget < BureaucratTestCase
301
+ def test_correctly_render_none_selected
302
+ input = Widgets::RadioSelect.new(nil, [['1', 'One'], ['2', 'Two']])
303
+ expected = normalize_html("<ul>\n<li><label for='id_radio_0'><input name='radio' id='id_radio_0' type='radio' value='1'/> One</label></li>\n<li><label for='id_radio_1'><input name='radio' id='id_radio_1' type='radio' value='2'/> Two</label></li>\n</ul>")
304
+ rendered = normalize_html(input.render('radio', '', id: 'id_radio'))
305
+ assert_equal(expected, rendered)
306
+ end
307
+
308
+ def test_correctly_render_with_selected
309
+ input = Widgets::RadioSelect.new(nil, [['1', 'One'], ['2', 'Two']])
310
+ expected = normalize_html("<ul>\n<li><label for='id_radio_0'><input checked='checked' name='radio' id='id_radio_0' type='radio' value='1'/> One</label></li>\n<li><label for='id_radio_1'><input name='radio' id='id_radio_1' type='radio' value='2'/> Two</label></li>\n</ul>")
311
+ rendered = normalize_html(input.render('radio', '1', id: 'id_radio'))
312
+ assert_equal(expected, rendered)
313
+ end
314
+ end
315
+
316
+ module CheckboxSelectMultipleTests
317
+ class Test_with_empty_choices < BureaucratTestCase
318
+ def test_render_an_empty_ul
319
+ input = Widgets::CheckboxSelectMultiple.new
320
+ expected = normalize_html("<ul>\n</ul>")
321
+ rendered = normalize_html(input.render('test', ['hello'], id: 'id_checkboxes'))
322
+ assert_equal(expected, rendered)
323
+ end
324
+ end
325
+
326
+ class Test_with_choices < BureaucratTestCase
327
+ def setup
328
+ @choices = [['1', 'One'], ['2', 'Two'], ['3', 'Three']]
329
+ end
330
+
331
+ def test_correctly_renders_none_selected
332
+ input = Widgets::CheckboxSelectMultiple.new(nil, @choices)
333
+ expected = normalize_html("<ul>\n<li><label for='id_checkboxes_0'><input name='test[]' id='id_checkboxes_0' type='checkbox' value='1'/> One</label></li>\n<li><label for='id_checkboxes_1'><input name='test[]' id='id_checkboxes_1' type='checkbox' value='2'/> Two</label></li>\n<li><label for='id_checkboxes_2'><input name='test[]' id='id_checkboxes_2' type='checkbox' value='3'/> Three</label></li>\n</ul>")
334
+ rendered = normalize_html(input.render('test', ['hello'], id: 'id_checkboxes'))
335
+ assert_equal(expected, rendered)
336
+ end
337
+
338
+ def test_correctly_renders_with_selected
339
+ input = Widgets::CheckboxSelectMultiple.new(nil, @choices)
340
+ expected = normalize_html("<ul>\n<li><label for='id_checkboxes_0'><input checked='checked' name='test[]' id='id_checkboxes_0' type='checkbox' value='1'/> One</label></li>\n<li><label for='id_checkboxes_1'><input checked='checked' name='test[]' id='id_checkboxes_1' type='checkbox' value='2'/> Two</label></li>\n<li><label for='id_checkboxes_2'><input name='test[]' id='id_checkboxes_2' type='checkbox' value='3'/> Three</label></li>\n</ul>")
341
+ rendered = normalize_html(input.render('test', ['1', '2'],
342
+ id: 'id_checkboxes'))
343
+ assert_equal(expected, rendered)
344
+ end
345
+ end
346
+ end
347
+
328
348
  end