reform 2.3.2 → 2.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +17 -0
- data/.gitignore +1 -1
- data/CHANGES.md +23 -0
- data/Gemfile +1 -1
- data/LICENSE.txt +1 -1
- data/README.md +5 -5
- data/Rakefile +1 -12
- data/lib/reform/contract/validate.rb +1 -1
- data/lib/reform/form/dry.rb +47 -9
- data/lib/reform/form/populator.rb +13 -3
- data/lib/reform/form/prepopulate.rb +1 -1
- data/lib/reform/form/validate.rb +3 -3
- data/lib/reform/validation/groups.rb +0 -1
- data/lib/reform/version.rb +1 -1
- data/reform.gemspec +2 -2
- data/test/call_test.rb +23 -0
- data/test/changed_test.rb +6 -6
- data/test/coercion_test.rb +17 -17
- data/test/{composition_new_api.rb → composition_test.rb} +27 -28
- data/test/{contract_new_api.rb → contract_test.rb} +8 -8
- data/test/default_test.rb +2 -2
- data/test/deserialize_test.rb +8 -8
- data/test/docs/validation_test.rb +134 -0
- data/test/{errors_new_api.rb → errors_test.rb} +41 -41
- data/test/feature_test.rb +2 -2
- data/test/fixtures/dry_error_messages.yml +64 -54
- data/test/{form_option_new_api.rb → form_option_test.rb} +1 -1
- data/test/{form_new_api.rb → form_test.rb} +3 -3
- data/test/from_test.rb +10 -10
- data/test/{inherit_new_api.rb → inherit_test.rb} +17 -17
- data/test/{module_new_api.rb → module_test.rb} +10 -10
- data/test/parse_option_test.rb +7 -7
- data/test/parse_pipeline_test.rb +1 -1
- data/test/{populate_new_api.rb → populate_test.rb} +136 -53
- data/test/populator_skip_test.rb +2 -2
- data/test/prepopulator_test.rb +16 -16
- data/test/read_only_test.rb +2 -2
- data/test/readable_test.rb +3 -3
- data/test/{reform_new_api.rb → reform_test.rb} +19 -19
- data/test/{save_new_api.rb → save_test.rb} +4 -4
- data/test/setup_test.rb +9 -9
- data/test/{skip_if_new_api.rb → skip_if_test.rb} +12 -12
- data/test/skip_setter_and_getter_test.rb +6 -6
- data/test/test_helper.rb +5 -6
- data/test/{validate_new_api.rb → validate_test.rb} +65 -78
- data/test/validation/{dry_validation_new_api.rb → dry_validation_test.rb} +128 -128
- data/test/validation/result_test.rb +14 -14
- data/test/virtual_test.rb +7 -7
- data/test/writeable_test.rb +8 -8
- metadata +42 -75
- data/.travis.yml +0 -16
- data/Appraisals +0 -8
- data/gemfiles/0.13.0.gemfile +0 -8
- data/gemfiles/1.5.0.gemfile +0 -9
- data/lib/reform/form/dry/new_api.rb +0 -47
- data/lib/reform/form/dry/old_api.rb +0 -61
- data/test/call_new_api.rb +0 -23
- data/test/call_old_api.rb +0 -23
- data/test/composition_old_api.rb +0 -184
- data/test/contract_old_api.rb +0 -77
- data/test/errors_old_api.rb +0 -230
- data/test/fixtures/dry_new_api_error_messages.yml +0 -104
- data/test/form_old_api.rb +0 -57
- data/test/form_option_old_api.rb +0 -24
- data/test/inherit_old_api.rb +0 -105
- data/test/module_old_api.rb +0 -146
- data/test/populate_old_api.rb +0 -304
- data/test/reform_old_api.rb +0 -202
- data/test/save_old_api.rb +0 -101
- data/test/skip_if_old_api.rb +0 -92
- data/test/validate_old_api.rb +0 -410
- data/test/validation/dry_validation_old_api.rb +0 -772
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'reform/form/dry'
|
3
|
+
|
4
|
+
class DocsDryVTest < Minitest::Spec
|
5
|
+
#:basic
|
6
|
+
class AlbumForm < Reform::Form
|
7
|
+
feature Reform::Form::Dry
|
8
|
+
|
9
|
+
property :name
|
10
|
+
|
11
|
+
validation do
|
12
|
+
params do
|
13
|
+
required(:name).filled
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
#:basic end
|
18
|
+
|
19
|
+
it 'validates correctly' do
|
20
|
+
form = DocsDryVTest::AlbumForm.new(Album.new(nil, nil, nil))
|
21
|
+
result = form.call(name: nil)
|
22
|
+
|
23
|
+
refute result.success?
|
24
|
+
assert_equal({ name: ['must be filled'] }, form.errors.messages)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class DocsDryVWithRulesTest < Minitest::Spec
|
29
|
+
#:basic_with_rules
|
30
|
+
class AlbumForm < Reform::Form
|
31
|
+
feature Reform::Form::Dry
|
32
|
+
|
33
|
+
property :name
|
34
|
+
|
35
|
+
validation name: :default do
|
36
|
+
option :form
|
37
|
+
|
38
|
+
params do
|
39
|
+
required(:name).filled
|
40
|
+
end
|
41
|
+
|
42
|
+
rule(:name) do
|
43
|
+
key.failure('must be unique') if Album.where.not(id: form.model.id).where(name: value).exists?
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
#:basic_with_rules end
|
48
|
+
|
49
|
+
it 'validates correctly' do
|
50
|
+
Album = Struct.new(:name, :songs, :artist, :user)
|
51
|
+
form = DocsDryVWithRulesTest::AlbumForm.new(Album.new(nil, nil, nil, nil))
|
52
|
+
result = form.call(name: nil)
|
53
|
+
|
54
|
+
refute result.success?
|
55
|
+
assert_equal({ name: ['must be filled'] }, form.errors.messages)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class DryVWithNestedTest < Minitest::Spec
|
60
|
+
#:nested
|
61
|
+
class AlbumForm < Reform::Form
|
62
|
+
feature Reform::Form::Dry
|
63
|
+
|
64
|
+
property :name
|
65
|
+
|
66
|
+
validation do
|
67
|
+
params { required(:name).filled }
|
68
|
+
end
|
69
|
+
|
70
|
+
property :artist do
|
71
|
+
property :name
|
72
|
+
|
73
|
+
validation do
|
74
|
+
params { required(:name).filled }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
#:nested end
|
79
|
+
|
80
|
+
it 'validates correctly' do
|
81
|
+
form = DryVWithNestedTest::AlbumForm.new(Album.new(nil, nil, Artist.new(nil)))
|
82
|
+
result = form.call(name: nil, artist: { name: '' })
|
83
|
+
|
84
|
+
refute result.success?
|
85
|
+
assert_equal({ name: ['must be filled'], 'artist.name': ['must be filled'] }, form.errors.messages)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class DryVValGroupTest < Minitest::Spec
|
90
|
+
class AlbumForm < Reform::Form
|
91
|
+
feature Reform::Form::Dry
|
92
|
+
|
93
|
+
property :name
|
94
|
+
property :artist
|
95
|
+
#:validation_groups
|
96
|
+
validation name: :default do
|
97
|
+
params { required(:name).filled }
|
98
|
+
end
|
99
|
+
|
100
|
+
validation name: :artist, if: :default do
|
101
|
+
params { required(:artist).filled }
|
102
|
+
end
|
103
|
+
|
104
|
+
validation name: :famous, after: :default do
|
105
|
+
params { optional(:artist) }
|
106
|
+
|
107
|
+
rule(:artist) do
|
108
|
+
if value
|
109
|
+
key.failure('only famous artist') unless value =~ /famous/
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
#:validation_groups end
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'validates correctly' do
|
117
|
+
form = DryVValGroupTest::AlbumForm.new(Album.new(nil, nil, nil))
|
118
|
+
result = form.call(name: nil)
|
119
|
+
|
120
|
+
refute result.success?
|
121
|
+
assert_equal({ name: ['must be filled'] }, result.errors.messages)
|
122
|
+
|
123
|
+
result = form.call(name: 'Title')
|
124
|
+
refute result.success?
|
125
|
+
assert_equal({ artist: ['must be filled'] }, result.errors.messages)
|
126
|
+
|
127
|
+
result = form.call(name: 'Title', artist: 'Artist')
|
128
|
+
refute result.success?
|
129
|
+
assert_equal({ artist: ['only famous artist'] }, result.errors.messages)
|
130
|
+
|
131
|
+
result = form.call(name: 'Title', artist: 'Artist famous')
|
132
|
+
assert result.success?
|
133
|
+
end
|
134
|
+
end
|
@@ -37,7 +37,7 @@ class ErrorsTest < MiniTest::Spec
|
|
37
37
|
# TODO: make band a required object.
|
38
38
|
|
39
39
|
validation do
|
40
|
-
config.messages.load_paths << "test/fixtures/
|
40
|
+
config.messages.load_paths << "test/fixtures/dry_error_messages.yml"
|
41
41
|
|
42
42
|
params { required(:name).filled }
|
43
43
|
|
@@ -72,22 +72,22 @@ class ErrorsTest < MiniTest::Spec
|
|
72
72
|
|
73
73
|
describe "#validate with invalid array property" do
|
74
74
|
it do
|
75
|
-
form.validate(
|
75
|
+
refute form.validate(
|
76
76
|
title: "Swimming Pool - EP",
|
77
77
|
band: {
|
78
78
|
name: "Marie Madeleine",
|
79
79
|
label: {name: "Ekler'o'shocK"}
|
80
80
|
},
|
81
81
|
artists: [42, "Good Charlotte", 43]
|
82
|
-
)
|
83
|
-
form.errors.messages
|
84
|
-
form.errors.size
|
82
|
+
)
|
83
|
+
assert_equal form.errors.messages, artists: {0 => ["must be a string"], 2 => ["must be a string"]}
|
84
|
+
assert_equal form.errors.size, 1
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
88
|
describe "#errors without #validate" do
|
89
89
|
it do
|
90
|
-
form.errors.size
|
90
|
+
assert_equal form.errors.size, 0
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
@@ -102,13 +102,13 @@ class ErrorsTest < MiniTest::Spec
|
|
102
102
|
end
|
103
103
|
|
104
104
|
it do
|
105
|
-
form.errors.messages
|
105
|
+
assert_equal form.errors.messages,{
|
106
106
|
title: ["must be filled"],
|
107
107
|
"hit.title": ["must be filled"],
|
108
108
|
"songs.title": ["must be filled"],
|
109
109
|
"band.label.name": ["must be filled"],
|
110
110
|
"producer.name": ["must be filled"]
|
111
|
-
|
111
|
+
}
|
112
112
|
end
|
113
113
|
|
114
114
|
# it do
|
@@ -117,60 +117,60 @@ class ErrorsTest < MiniTest::Spec
|
|
117
117
|
# end
|
118
118
|
|
119
119
|
# nested forms keep their own Errors:
|
120
|
-
it { form.producer.errors.messages
|
121
|
-
it { form.hit.errors.messages
|
122
|
-
it { form.songs[0].errors.messages
|
120
|
+
it { assert_equal form.producer.errors.messages, name: ["must be filled"] }
|
121
|
+
it { assert_equal form.hit.errors.messages, title: ["must be filled"] }
|
122
|
+
it { assert_equal form.songs[0].errors.messages, title: ["must be filled"] }
|
123
123
|
|
124
124
|
it do
|
125
|
-
form.errors.messages
|
125
|
+
assert_equal form.errors.messages, {
|
126
126
|
title: ["must be filled"],
|
127
127
|
"hit.title": ["must be filled"],
|
128
128
|
"songs.title": ["must be filled"],
|
129
129
|
"band.label.name": ["must be filled"],
|
130
130
|
"producer.name": ["must be filled"]
|
131
|
-
|
132
|
-
form.errors.size
|
131
|
+
}
|
132
|
+
assert_equal form.errors.size, 5
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
136
136
|
describe "#validate with main form invalid" do
|
137
137
|
it do
|
138
|
-
form.validate("title" => "", "band" => {"label" => {name: "Fat Wreck"}}, "producer" => nil)
|
139
|
-
form.errors.messages
|
140
|
-
form.errors.size
|
138
|
+
refute form.validate("title" => "", "band" => {"label" => {name: "Fat Wreck"}}, "producer" => nil)
|
139
|
+
assert_equal form.errors.messages, title: ["must be filled"], producer: ["must be a hash"]
|
140
|
+
assert_equal form.errors.size, 2
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
144
144
|
describe "#validate with middle nested form invalid" do
|
145
145
|
before { @result = form.validate("hit" => {"title" => ""}, "band" => {"label" => {name: "Fat Wreck"}}) }
|
146
146
|
|
147
|
-
it { @result
|
148
|
-
it { form.errors.messages
|
149
|
-
it { form.errors.size
|
147
|
+
it { refute @result }
|
148
|
+
it { assert_equal form.errors.messages, "hit.title": ["must be filled"] }
|
149
|
+
it { assert_equal form.errors.size, 1 }
|
150
150
|
end
|
151
151
|
|
152
152
|
describe "#validate with collection form invalid" do
|
153
153
|
before { @result = form.validate("songs" => [{"title" => ""}], "band" => {"label" => {name: "Fat Wreck"}}) }
|
154
154
|
|
155
|
-
it { @result
|
156
|
-
it { form.errors.messages
|
157
|
-
it { form.errors.size
|
155
|
+
it { refute @result }
|
156
|
+
it { assert_equal form.errors.messages, "songs.title": ["must be filled"] }
|
157
|
+
it { assert_equal form.errors.size, 1 }
|
158
158
|
end
|
159
159
|
|
160
160
|
describe "#validate with collection and 2-level-nested invalid" do
|
161
161
|
before { @result = form.validate("songs" => [{"title" => ""}], "band" => {"label" => {}}) }
|
162
162
|
|
163
|
-
it { @result
|
164
|
-
it { form.errors.messages
|
165
|
-
it { form.errors.size
|
163
|
+
it { refute @result }
|
164
|
+
it { assert_equal form.errors.messages, "songs.title": ["must be filled"], "band.label.name": ["must be filled"] }
|
165
|
+
it { assert_equal form.errors.size, 2 }
|
166
166
|
end
|
167
167
|
|
168
168
|
describe "#validate with nested form using :base invalid" do
|
169
169
|
it do
|
170
170
|
result = form.validate("songs" => [{"title" => "Someday"}], "band" => {"name" => "Nickelback", "label" => {"name" => "Roadrunner Records"}})
|
171
|
-
result
|
172
|
-
form.errors.messages
|
173
|
-
form.errors.size
|
171
|
+
refute result
|
172
|
+
assert_equal form.errors.messages, "band.name": ["you're a bad person"]
|
173
|
+
assert_equal form.errors.size, 1
|
174
174
|
end
|
175
175
|
end
|
176
176
|
|
@@ -178,17 +178,17 @@ class ErrorsTest < MiniTest::Spec
|
|
178
178
|
let(:album_title) { nil }
|
179
179
|
it do
|
180
180
|
result = form.validate("songs" => [{"title" => "Someday"}], "band" => {"name" => "Nickelback", "label" => {"name" => "Roadrunner Records"}})
|
181
|
-
result
|
182
|
-
form.errors.messages
|
181
|
+
refute result
|
182
|
+
assert_equal form.errors.messages, title: ["must be filled"], "band.name": ["you're a bad person"]
|
183
183
|
# add a new custom error
|
184
184
|
form.errors.add(:policy, "error_text")
|
185
|
-
form.errors.messages
|
185
|
+
assert_equal form.errors.messages, title: ["must be filled"], "band.name": ["you're a bad person"], policy: ["error_text"]
|
186
186
|
# does not duplicate errors
|
187
187
|
form.errors.add(:title, "must be filled")
|
188
|
-
form.errors.messages
|
188
|
+
assert_equal form.errors.messages, title: ["must be filled"], "band.name": ["you're a bad person"], policy: ["error_text"]
|
189
189
|
# merge existing errors
|
190
190
|
form.errors.add(:policy, "another error")
|
191
|
-
form.errors.messages
|
191
|
+
assert_equal form.errors.messages, title: ["must be filled"], "band.name": ["you're a bad person"], policy: ["error_text", "another error"]
|
192
192
|
end
|
193
193
|
end
|
194
194
|
|
@@ -202,14 +202,14 @@ class ErrorsTest < MiniTest::Spec
|
|
202
202
|
)
|
203
203
|
end
|
204
204
|
|
205
|
-
it { @result
|
206
|
-
it { form.hit.title
|
207
|
-
it { form.title
|
208
|
-
it { form.songs.first.title
|
205
|
+
it { assert @result }
|
206
|
+
it { assert_equal form.hit.title, "Sacrifice" }
|
207
|
+
it { assert_equal form.title, "Second Heat" }
|
208
|
+
it { assert_equal form.songs.first.title, "Heart Of A Lion" }
|
209
209
|
it do
|
210
210
|
skip "WE DON'T NEED COUNT AND EMPTY? ON THE CORE ERRORS OBJECT"
|
211
|
-
form.errors.size
|
212
|
-
form.errors.empty
|
211
|
+
assert_equal form.errors.size, 0
|
212
|
+
assert form.errors.empty
|
213
213
|
end
|
214
214
|
end
|
215
215
|
|
@@ -219,7 +219,7 @@ class ErrorsTest < MiniTest::Spec
|
|
219
219
|
# to_s is aliased to messages
|
220
220
|
it {
|
221
221
|
skip "why do we need Errors#to_s ?"
|
222
|
-
form.errors.to_s
|
222
|
+
assert_equal form.errors.to_s, "{:\"songs.title\"=>[\"must be filled\"], :\"band.label.name\"=>[\"must be filled\"]}"
|
223
223
|
}
|
224
224
|
end
|
225
225
|
end
|
data/test/feature_test.rb
CHANGED
@@ -45,8 +45,8 @@ class FeatureInheritanceTest < BaseTest
|
|
45
45
|
let(:form) { AlbumForm.new(album) }
|
46
46
|
|
47
47
|
it do
|
48
|
-
form.date
|
49
|
-
form.songs[0].date
|
48
|
+
assert_equal form.date, "May 16"
|
49
|
+
assert_equal form.songs[0].date, "May 16"
|
50
50
|
end
|
51
51
|
|
52
52
|
# it { subject.class.include?(Reform::Form::ActiveModel) }
|
@@ -1,94 +1,104 @@
|
|
1
1
|
en:
|
2
|
-
|
3
|
-
|
2
|
+
dry_validation:
|
3
|
+
errors:
|
4
|
+
array?: "must be an array"
|
4
5
|
|
5
|
-
|
6
|
+
empty?: "must be empty"
|
6
7
|
|
7
|
-
|
8
|
+
excludes?: "must not include %{value}"
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
excluded_from?:
|
11
|
+
arg:
|
12
|
+
default: "must not be one of: %{list}"
|
13
|
+
range: "must not be one of: %{list_left} - %{list_right}"
|
13
14
|
|
14
|
-
|
15
|
+
eql?: "must be equal to %{left}"
|
15
16
|
|
16
|
-
|
17
|
+
not_eql?: "must not be equal to %{left}"
|
17
18
|
|
18
|
-
|
19
|
+
filled?: "must be filled"
|
19
20
|
|
20
|
-
|
21
|
+
format?: "is in invalid format"
|
21
22
|
|
22
|
-
|
23
|
+
number?: "must be a number"
|
23
24
|
|
24
|
-
|
25
|
+
odd?: "must be odd"
|
25
26
|
|
26
|
-
|
27
|
+
even?: "must be even"
|
27
28
|
|
28
|
-
|
29
|
+
gt?: "must be greater than %{num}"
|
29
30
|
|
30
|
-
|
31
|
+
gteq?: "must be greater than or equal to %{num}"
|
31
32
|
|
32
|
-
|
33
|
+
hash?: "must be a hash"
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
included_in?:
|
36
|
+
arg:
|
37
|
+
default: "must be one of: %{list}"
|
38
|
+
range: "must be one of: %{list_left} - %{list_right}"
|
38
39
|
|
39
|
-
|
40
|
+
includes?: "must include %{value}"
|
40
41
|
|
41
|
-
|
42
|
+
bool?: "must be boolean"
|
42
43
|
|
43
|
-
|
44
|
+
true?: "must be true"
|
44
45
|
|
45
|
-
|
46
|
+
false?: "must be false"
|
46
47
|
|
47
|
-
|
48
|
+
int?: "must be an integer"
|
48
49
|
|
49
|
-
|
50
|
+
float?: "must be a float"
|
50
51
|
|
51
|
-
|
52
|
+
decimal?: "must be a decimal"
|
52
53
|
|
53
|
-
|
54
|
+
date?: "must be a date"
|
54
55
|
|
55
|
-
|
56
|
+
date_time?: "must be a date time"
|
56
57
|
|
57
|
-
|
58
|
+
time?: "must be a time"
|
58
59
|
|
59
|
-
|
60
|
+
key?: "is missing"
|
60
61
|
|
61
|
-
|
62
|
+
attr?: "is missing"
|
62
63
|
|
63
|
-
|
64
|
+
lt?: "must be less than %{num}"
|
64
65
|
|
65
|
-
|
66
|
+
lteq?: "must be less than or equal to %{num}"
|
66
67
|
|
67
|
-
|
68
|
+
max_size?: "size cannot be greater than %{num}"
|
68
69
|
|
69
|
-
|
70
|
+
min_size?: "size cannot be less than %{num}"
|
70
71
|
|
71
|
-
|
72
|
+
none?: "cannot be defined"
|
72
73
|
|
73
|
-
|
74
|
+
str?: "must be a string"
|
74
75
|
|
75
|
-
|
76
|
+
type?: "must be %{type}"
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
size?:
|
79
|
+
arg:
|
80
|
+
default: "size must be %{size}"
|
81
|
+
range: "size must be within %{size_left} - %{size_right}"
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
83
|
+
value:
|
84
|
+
string:
|
85
|
+
arg:
|
86
|
+
default: "length must be %{size}"
|
87
|
+
range: "length must be within %{size_left} - %{size_right}"
|
87
88
|
|
88
|
-
good_musical_taste?: "you're a bad person"
|
89
89
|
|
90
|
-
|
91
|
-
|
90
|
+
|
91
|
+
rules:
|
92
|
+
name:
|
93
|
+
good_musical_taste?: "you're a bad person"
|
94
|
+
title:
|
95
|
+
good_musical_taste?: "you're a bad person"
|
96
|
+
songs:
|
97
|
+
a_song?: "must have at least one enabled song"
|
98
|
+
artist:
|
99
|
+
with_last_name?: "must have last name"
|
100
|
+
|
92
101
|
de:
|
93
|
-
|
102
|
+
dry_validation:
|
103
|
+
errors:
|
94
104
|
filled?: "muss abgefüllt sein"
|
@@ -17,7 +17,7 @@ class FormOptionTest < MiniTest::Spec
|
|
17
17
|
|
18
18
|
it do
|
19
19
|
form = AlbumForm.new(Album.new(Song.new("When It Comes To You")))
|
20
|
-
|
20
|
+
assert_equal "When It Comes To You", form.song.title
|
21
21
|
|
22
22
|
form.validate(song: {title: "Run For Cover"})
|
23
23
|
end
|
@@ -25,8 +25,8 @@ class FormTest < MiniTest::Spec
|
|
25
25
|
let(:cloned) { AlbumForm.clone }
|
26
26
|
|
27
27
|
# #dup is called in Op.inheritable_attr(:contract_class), it must be subclass of the original one.
|
28
|
-
it { cloned
|
29
|
-
it { AlbumForm.definitions
|
28
|
+
it { refute_equal cloned, AlbumForm }
|
29
|
+
it { refute_equal AlbumForm.definitions, cloned.definitions }
|
30
30
|
|
31
31
|
it do
|
32
32
|
# currently, forms need a name for validation, even without AM.
|
@@ -51,7 +51,7 @@ class FormTest < MiniTest::Spec
|
|
51
51
|
end
|
52
52
|
|
53
53
|
it "allows injecting :virtual options" do
|
54
|
-
ArtistForm.new(Artist.new, current_user: Object).current_user
|
54
|
+
assert_equal ArtistForm.new(Artist.new, current_user: Object).current_user, Object
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
data/test/from_test.rb
CHANGED
@@ -31,19 +31,19 @@ class AsTest < BaseTest
|
|
31
31
|
|
32
32
|
subject { AlbumForm.new(Album.new("Best Of", hit, [Song.new("Fallout"), song2])) }
|
33
33
|
|
34
|
-
it { subject.name
|
35
|
-
it { subject.single.title
|
36
|
-
it { subject.tracks[0].name
|
37
|
-
it { subject.tracks[1].name
|
34
|
+
it { assert_equal subject.name, "Best Of" }
|
35
|
+
it { assert_equal subject.single.title, "Roxanne" }
|
36
|
+
it { assert_equal subject.tracks[0].name, "Fallout" }
|
37
|
+
it { assert_equal subject.tracks[1].name, "Roxanne" }
|
38
38
|
|
39
39
|
describe "#validate" do
|
40
40
|
|
41
41
|
before { subject.validate(params) }
|
42
42
|
|
43
|
-
it { subject.name
|
44
|
-
it { subject.single.title
|
45
|
-
it { subject.tracks[0].name
|
46
|
-
it { subject.tracks[1].name
|
43
|
+
it { assert_equal subject.name, "Best Of The Police" }
|
44
|
+
it { assert_equal subject.single.title, "So Lonely" }
|
45
|
+
it { assert_equal subject.tracks[0].name, "Message In A Bottle" }
|
46
|
+
it { assert_equal subject.tracks[1].name, "Roxanne" }
|
47
47
|
end
|
48
48
|
|
49
49
|
describe "#sync" do
|
@@ -52,7 +52,7 @@ class AsTest < BaseTest
|
|
52
52
|
subject.sync
|
53
53
|
end
|
54
54
|
|
55
|
-
it { song2.title
|
55
|
+
it { assert_equal song2.title, "Livin' Ain't No Crime" }
|
56
56
|
end
|
57
57
|
|
58
58
|
describe "#save (nested hash)" do
|
@@ -65,7 +65,7 @@ class AsTest < BaseTest
|
|
65
65
|
hash = nested_hash
|
66
66
|
end
|
67
67
|
|
68
|
-
hash
|
68
|
+
assert_equal hash, "title" => "Best Of The Police", "hit" => {"title" => "So Lonely"}, "songs" => [{"title" => "Message In A Bottle"}, {"title" => "Roxanne"}], "band" => nil
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
@@ -52,46 +52,46 @@ class InheritTest < BaseTest
|
|
52
52
|
|
53
53
|
it do
|
54
54
|
subject.validate("hit" => {"title" => "LA Drone", "rating" => 10})
|
55
|
-
subject.hit.title
|
56
|
-
subject.hit.rating
|
57
|
-
subject.errors.messages
|
55
|
+
assert_equal subject.hit.title, "LA Drone"
|
56
|
+
assert_equal subject.hit.rating, 10
|
57
|
+
assert_equal subject.errors.messages, {}
|
58
58
|
end
|
59
59
|
|
60
60
|
it do
|
61
61
|
subject.validate({})
|
62
62
|
assert_nil subject.model.hit.title
|
63
63
|
assert_nil subject.model.hit.rating
|
64
|
-
subject.errors.messages
|
64
|
+
assert_equal subject.errors.messages, "hit.title": ["must be filled"], "hit.rating": ["must be filled"]
|
65
65
|
end
|
66
66
|
|
67
67
|
it "xxx" do
|
68
68
|
# sub hashes like :deserializer must be properly cloned when inheriting.
|
69
|
-
AlbumForm.options_for(:title)[:deserializer].object_id
|
69
|
+
refute_equal AlbumForm.options_for(:title)[:deserializer].object_id, CompilationForm.options_for(:title)[:deserializer].object_id
|
70
70
|
|
71
71
|
# don't overwrite direct deserializer: {} configuration.
|
72
|
-
AlbumForm.options_for(:title)[:internal_populator].
|
73
|
-
AlbumForm.options_for(:title)[:deserializer][:skip_parse]
|
72
|
+
assert AlbumForm.options_for(:title)[:internal_populator].is_a? Reform::Form::Populator::Sync
|
73
|
+
assert_equal AlbumForm.options_for(:title)[:deserializer][:skip_parse], "skip_if in AlbumForm"
|
74
74
|
|
75
75
|
# AlbumForm.options_for(:hit)[:internal_populator].inspect.must_match /Reform::Form::Populator:.+ @user_proc="Populator"/
|
76
76
|
# AlbumForm.options_for(:hit)[:deserializer][:instance].inspect.must_be_instance_with Reform::Form::Populator, user_proc: "Populator"
|
77
77
|
|
78
|
-
AlbumForm.options_for(:songs)[:internal_populator].
|
79
|
-
AlbumForm.options_for(:songs)[:deserializer][:skip_parse].
|
78
|
+
assert AlbumForm.options_for(:songs)[:internal_populator].is_a? Reform::Form::Populator::IfEmpty
|
79
|
+
assert AlbumForm.options_for(:songs)[:deserializer][:skip_parse].is_a? Reform::Form::Validate::Skip::AllBlank
|
80
80
|
|
81
|
-
AlbumForm.options_for(:band)[:internal_populator].
|
81
|
+
assert AlbumForm.options_for(:band)[:internal_populator].is_a? Reform::Form::Populator::IfEmpty
|
82
82
|
|
83
|
-
CompilationForm.options_for(:title)[:deserializer][:skip_parse]
|
83
|
+
assert_equal CompilationForm.options_for(:title)[:deserializer][:skip_parse], "skip_if from CompilationForm"
|
84
84
|
# pp CompilationForm.options_for(:songs)
|
85
|
-
CompilationForm.options_for(:songs)[:internal_populator].
|
85
|
+
assert CompilationForm.options_for(:songs)[:internal_populator].is_a? Reform::Form::Populator::IfEmpty
|
86
86
|
|
87
|
-
CompilationForm.options_for(:band)[:internal_populator].
|
87
|
+
assert CompilationForm.options_for(:band)[:internal_populator].is_a? Reform::Form::Populator::IfEmpty
|
88
88
|
|
89
89
|
# completely overwrite inherited.
|
90
|
-
CompilationForm.options_for(:hit)[:deserializer][:skip_parse].
|
90
|
+
assert CompilationForm.options_for(:hit)[:deserializer][:skip_parse].is_a? SkipParse
|
91
91
|
|
92
92
|
# inherit: true with block will still inherit the original class.
|
93
|
-
AlbumForm.new(OpenStruct.new(band: OpenStruct.new)).band.band_id
|
94
|
-
CompilationForm.new(OpenStruct.new(band: OpenStruct.new)).band.band_id
|
93
|
+
assert_equal AlbumForm.new(OpenStruct.new(band: OpenStruct.new)).band.band_id, 1
|
94
|
+
assert_equal CompilationForm.new(OpenStruct.new(band: OpenStruct.new)).band.band_id, 1
|
95
95
|
end
|
96
96
|
|
97
97
|
class CDForm < AlbumForm
|
@@ -101,5 +101,5 @@ class InheritTest < BaseTest
|
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
104
|
-
it { CDForm.options_for(:band)[:internal_populator].instance_variable_get(:@user_proc)
|
104
|
+
it { assert_equal CDForm.options_for(:band)[:internal_populator].instance_variable_get(:@user_proc), "CD Populator" }
|
105
105
|
end
|