reform 2.3.0.rc2 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +17 -0
  3. data/.gitignore +4 -2
  4. data/CHANGES.md +24 -1
  5. data/Gemfile +1 -7
  6. data/LICENSE.txt +1 -1
  7. data/README.md +5 -5
  8. data/Rakefile +2 -15
  9. data/lib/reform/contract/validate.rb +1 -1
  10. data/lib/reform/form/dry.rb +47 -9
  11. data/lib/reform/form/populator.rb +13 -3
  12. data/lib/reform/form/prepopulate.rb +1 -1
  13. data/lib/reform/form/validate.rb +3 -3
  14. data/lib/reform/validation/groups.rb +0 -1
  15. data/lib/reform/version.rb +1 -1
  16. data/reform.gemspec +3 -4
  17. data/test/call_test.rb +23 -0
  18. data/test/changed_test.rb +6 -6
  19. data/test/coercion_test.rb +17 -17
  20. data/test/{composition_new_api.rb → composition_test.rb} +27 -28
  21. data/test/{contract_new_api.rb → contract_test.rb} +8 -8
  22. data/test/default_test.rb +2 -2
  23. data/test/deserialize_test.rb +8 -8
  24. data/test/docs/validation_test.rb +134 -0
  25. data/test/{errors_new_api.rb → errors_test.rb} +41 -41
  26. data/test/feature_test.rb +2 -2
  27. data/test/fixtures/dry_error_messages.yml +64 -54
  28. data/test/{form_option_new_api.rb → form_option_test.rb} +1 -1
  29. data/test/{form_new_api.rb → form_test.rb} +3 -3
  30. data/test/from_test.rb +10 -10
  31. data/test/{inherit_new_api.rb → inherit_test.rb} +17 -17
  32. data/test/{module_new_api.rb → module_test.rb} +10 -10
  33. data/test/parse_option_test.rb +7 -7
  34. data/test/parse_pipeline_test.rb +1 -1
  35. data/test/{populate_new_api.rb → populate_test.rb} +136 -53
  36. data/test/populator_skip_test.rb +2 -2
  37. data/test/prepopulator_test.rb +16 -16
  38. data/test/read_only_test.rb +2 -2
  39. data/test/readable_test.rb +3 -3
  40. data/test/{reform_new_api.rb → reform_test.rb} +19 -19
  41. data/test/{save_new_api.rb → save_test.rb} +4 -4
  42. data/test/setup_test.rb +9 -9
  43. data/test/{skip_if_new_api.rb → skip_if_test.rb} +12 -12
  44. data/test/skip_setter_and_getter_test.rb +6 -6
  45. data/test/test_helper.rb +6 -7
  46. data/test/{validate_new_api.rb → validate_test.rb} +108 -76
  47. data/test/validation/{dry_validation_new_api.rb → dry_validation_test.rb} +145 -135
  48. data/test/validation/result_test.rb +14 -14
  49. data/test/virtual_test.rb +7 -7
  50. data/test/writeable_test.rb +8 -8
  51. metadata +46 -99
  52. data/.rubocop.yml +0 -30
  53. data/.rubocop_todo.yml +0 -460
  54. data/.travis.yml +0 -32
  55. data/lib/reform/form/dry/new_api.rb +0 -58
  56. data/lib/reform/form/dry/old_api.rb +0 -61
  57. data/test/call_new_api.rb +0 -23
  58. data/test/call_old_api.rb +0 -23
  59. data/test/composition_old_api.rb +0 -184
  60. data/test/contract_old_api.rb +0 -77
  61. data/test/errors_old_api.rb +0 -230
  62. data/test/fixtures/dry_new_api_error_messages.yml +0 -104
  63. data/test/form_old_api.rb +0 -57
  64. data/test/form_option_old_api.rb +0 -24
  65. data/test/inherit_old_api.rb +0 -105
  66. data/test/module_old_api.rb +0 -146
  67. data/test/populate_old_api.rb +0 -304
  68. data/test/reform_old_api.rb +0 -202
  69. data/test/save_old_api.rb +0 -101
  70. data/test/skip_if_old_api.rb +0 -92
  71. data/test/validate_old_api.rb +0 -410
  72. data/test/validation/dry_validation_old_api.rb +0 -772
@@ -21,8 +21,8 @@ class PopulatorSkipTest < MiniTest::Spec
21
21
 
22
22
  form.validate(hash)
23
23
 
24
- form.songs.size.must_equal 2
24
+ assert_equal form.songs.size, 2
25
25
  assert_nil form.songs[0].title
26
- form.songs[1].title.must_equal "Bad"
26
+ assert_equal form.songs[1].title, "Bad"
27
27
  end
28
28
  end
@@ -37,25 +37,25 @@ class PrepopulatorTest < MiniTest::Spec
37
37
  it do
38
38
  form = AlbumForm.new(OpenStruct.new(length: 1)).prepopulate!(title: "Potemkin City Limits")
39
39
 
40
- form.length.must_equal 1
41
- form.title.must_equal "Another Day At Work"
42
- form.hit.model.must_equal Song.new("Potemkin City Limits")
43
- form.songs.size.must_equal 2
44
- form.songs[0].model.must_equal Song.new
45
- form.songs[1].model.must_equal Song.new
46
- form.songs[1].model.must_equal Song.new
40
+ assert_equal form.length, 1
41
+ assert_equal form.title, "Another Day At Work"
42
+ assert_equal form.hit.model, Song.new("Potemkin City Limits")
43
+ assert_equal form.songs.size, 2
44
+ assert_equal form.songs[0].model, Song.new
45
+ assert_equal form.songs[1].model, Song.new
46
+ assert_equal form.songs[1].model, Song.new
47
47
  # prepopulate works more than 1 level, recursive.
48
48
  # it also passes options properly down there.
49
- form.hit.band.model.must_equal Band.new("Potemkin City Limits")
49
+ assert_equal form.hit.band.model, Band.new("Potemkin City Limits")
50
50
  end
51
51
 
52
52
  # add to existing collection.
53
53
  it do
54
- form = AlbumForm.new(OpenStruct.new(songs: [Song.new])).prepopulate!
54
+ form = AlbumForm.new(OpenStruct.new(songs: [Song.new])).prepopulate!(title: "Potemkin City Limits")
55
55
 
56
- form.songs.size.must_equal 2
57
- form.songs[0].model.must_equal Song.new
58
- form.songs[1].model.must_equal Song.new
56
+ assert_equal form.songs.size, 2
57
+ assert_equal form.songs[0].model, Song.new
58
+ assert_equal form.songs[1].model, Song.new
59
59
  end
60
60
  end
61
61
 
@@ -75,7 +75,7 @@ class PrepopulateWithoutConfiguration < MiniTest::Spec
75
75
 
76
76
  subject { AlbumForm.new(OpenStruct.new(songs: [], hit: nil)).prepopulate! }
77
77
 
78
- it { subject.songs.size.must_equal 0 }
78
+ it { assert_equal subject.songs.size, 0 }
79
79
  end
80
80
 
81
81
  class ManualPrepopulatorOverridingTest < MiniTest::Spec
@@ -104,8 +104,8 @@ class ManualPrepopulatorOverridingTest < MiniTest::Spec
104
104
  it do
105
105
  form = AlbumForm.new(OpenStruct.new(length: 1)).prepopulate!(title: "Potemkin City Limits")
106
106
 
107
- form.length.must_equal 1
108
- form.hit.model.must_equal Song.new("Potemkin City Limits")
109
- form.hit.title.must_equal "Potemkin City Limits"
107
+ assert_equal form.length, 1
108
+ assert_equal form.hit.model, Song.new("Potemkin City Limits")
109
+ assert_equal form.hit.title, "Potemkin City Limits"
110
110
  end
111
111
  end
@@ -9,6 +9,6 @@ class ReadonlyTest < MiniTest::Spec
9
9
 
10
10
  let(:form) { SongForm.new(OpenStruct.new) }
11
11
 
12
- it { form.readonly?(:artist).must_equal false }
13
- it { form.readonly?(:title).must_equal true }
12
+ it { refute form.readonly?(:artist) }
13
+ it { assert form.readonly?(:title) }
14
14
  end
@@ -15,16 +15,16 @@ class ReadableTest < MiniTest::Spec
15
15
 
16
16
  form.validate("password" => "123")
17
17
 
18
- form.password.must_equal "123"
18
+ assert_equal form.password, "123"
19
19
 
20
20
  form.sync
21
- cred.password.must_equal "123" # password written.
21
+ assert_equal cred.password, "123" # password written.
22
22
 
23
23
  hash = {}
24
24
  form.save do |nested|
25
25
  hash = nested
26
26
  end
27
27
 
28
- hash.must_equal("password" => "123")
28
+ assert_equal hash, "password" => "123"
29
29
  }
30
30
  end
@@ -19,7 +19,7 @@ class ReformTest < Minitest::Spec
19
19
 
20
20
  it "returns empty fields" do
21
21
  assert_nil form.title
22
- form.name.must_be_nil
22
+ assert_nil form.name
23
23
  end
24
24
 
25
25
  describe "and submitted values" do
@@ -27,15 +27,15 @@ class ReformTest < Minitest::Spec
27
27
  form.validate("name" => "Duran Duran")
28
28
 
29
29
  assert_nil form.title
30
- form.name.must_equal "Duran Duran"
30
+ assert_equal form.name, "Duran Duran"
31
31
  end
32
32
  end
33
33
  end
34
34
 
35
35
  describe "(edit) form with existing models" do
36
36
  it "returns filled-out fields" do
37
- form.name.must_equal "Duran Duran"
38
- form.title.must_equal "Rio"
37
+ assert_equal form.name, "Duran Duran"
38
+ assert_equal form.title, "Rio"
39
39
  end
40
40
  end
41
41
 
@@ -50,13 +50,13 @@ class ReformTest < Minitest::Spec
50
50
  end
51
51
 
52
52
  it "returns true when valid" do
53
- form.validate("name" => "Duran Duran").must_equal true
53
+ assert_equal form.validate("name" => "Duran Duran"), true
54
54
  end
55
55
 
56
56
  it "exposes input via property accessors" do
57
57
  form.validate("name" => "Duran Duran")
58
58
 
59
- form.name.must_equal "Duran Duran"
59
+ assert_equal form.name, "Duran Duran"
60
60
  end
61
61
 
62
62
  it "doesn't change model properties" do
@@ -81,12 +81,12 @@ class ReformTest < Minitest::Spec
81
81
  let(:form) { ValidatingForm.new(comp) }
82
82
 
83
83
  it "returns false when invalid" do
84
- form.validate({}).must_equal false
84
+ assert_equal form.validate({}), false
85
85
  end
86
86
 
87
87
  it "populates errors" do
88
88
  form.validate({})
89
- form.errors.messages.must_equal(name: ["must be filled"], title: ["must be filled"])
89
+ assert_equal form.errors.messages, name: ["must be filled"], title: ["must be filled"]
90
90
  end
91
91
  end
92
92
  end
@@ -100,7 +100,7 @@ class ReformTest < Minitest::Spec
100
100
  it "xxpushes data to models" do
101
101
  form.save
102
102
 
103
- comp.name.must_equal "Diesel Boy"
103
+ assert_equal comp.name, "Diesel Boy"
104
104
  assert_nil comp.title
105
105
  end
106
106
 
@@ -112,13 +112,13 @@ class ReformTest < Minitest::Spec
112
112
  hash = map
113
113
  end
114
114
 
115
- hash.must_equal("name" => "Diesel Boy", "title" => nil)
115
+ assert_equal hash, "name" => "Diesel Boy", "title" => nil
116
116
  end
117
117
  end
118
118
  end
119
119
 
120
120
  describe "#model" do
121
- it { form.model.must_equal comp }
121
+ it { assert_equal form.model, comp }
122
122
  end
123
123
 
124
124
  describe "inheritance" do
@@ -132,9 +132,9 @@ class ReformTest < Minitest::Spec
132
132
  let(:form) { HitForm.new(OpenStruct.new()) }
133
133
  it do
134
134
  form.validate("title" => "The Body")
135
- form.title.must_equal "The Body"
135
+ assert_equal form.title, "The Body"
136
136
  assert_nil form.position
137
- form.errors.messages.must_equal(name: ["must be filled"], position: ["must be filled"])
137
+ assert_equal form.errors.messages, name: ["must be filled"], position: ["must be filled"]
138
138
  end
139
139
  end
140
140
  end
@@ -156,18 +156,18 @@ class OverridingAccessorsTest < BaseTest
156
156
  subject { SongForm.new(song) }
157
157
 
158
158
  # override reader for presentation.
159
- it { subject.title.must_equal "pray" }
159
+ it { assert_equal subject.title, "pray" }
160
160
 
161
161
  describe "#save" do
162
162
  before { subject.validate("title" => "Hey Little World") }
163
163
 
164
164
  # reader always used
165
- it { subject.title.must_equal "hey little worldhey little world" }
165
+ it { assert_equal subject.title, "hey little worldhey little world" }
166
166
 
167
167
  # the reader is not used when saving/syncing.
168
168
  it do
169
169
  subject.save do |hash|
170
- hash["title"].must_equal "Hey Little WorldHey Little World"
170
+ assert_equal hash["title"], "Hey Little WorldHey Little World"
171
171
  end
172
172
  end
173
173
 
@@ -175,7 +175,7 @@ class OverridingAccessorsTest < BaseTest
175
175
  it do
176
176
  song.extend(Saveable)
177
177
  subject.save
178
- song.title.must_equal "Hey Little WorldHey Little World"
178
+ assert_equal song.title, "Hey Little WorldHey Little World"
179
179
  end
180
180
  end
181
181
  end
@@ -199,6 +199,6 @@ class MethodInFormTest < MiniTest::Spec
199
199
 
200
200
  # methods can be used instead of created accessors.
201
201
  subject { AlbumForm.new(OpenStruct.new(hit: OpenStruct.new)) }
202
- it { subject.title.must_equal "The Suffer And The Witness" }
203
- it { subject.hit.title.must_equal "Drones" }
202
+ it { assert_equal subject.title, "The Suffer And The Witness" }
203
+ it { assert_equal subject.hit.title, "Drones" }
204
204
  end
@@ -53,9 +53,9 @@ class SaveTest < BaseTest
53
53
 
54
54
  form.save
55
55
 
56
- album.saved?.must_equal true
57
- album.songs[0].title.must_equal "Fixed"
58
- album.songs[0].saved?.must_equal true
56
+ assert album.saved?
57
+ assert_equal album.songs[0].title, "Fixed"
58
+ assert album.songs[0].saved?
59
59
  assert_nil album.artist.saved?
60
60
  end
61
61
 
@@ -70,7 +70,7 @@ class SaveTest < BaseTest
70
70
  nested_hash = hash
71
71
  end
72
72
 
73
- nested_hash.must_equal({"name" => nil, "artist" => nil})
73
+ assert_equal nested_hash, "name" => nil, "artist" => nil
74
74
  end
75
75
  end
76
76
  end
data/test/setup_test.rb CHANGED
@@ -31,18 +31,18 @@ class SetupTest < MiniTest::Spec
31
31
  it do
32
32
  form = AlbumForm.new(album)
33
33
 
34
- form.name.must_equal "The Dissent Of Man"
35
- form.songs[0].title.must_equal "Broken"
34
+ assert_equal form.name, "The Dissent Of Man"
35
+ assert_equal form.songs[0].title, "Broken"
36
36
  assert_nil form.songs[0].composer
37
- form.songs[1].title.must_equal "Resist Stance"
38
- form.songs[1].composer.name.must_equal "Greg Graffin"
39
- form.artist.name.must_equal "Bad Religion"
37
+ assert_equal form.songs[1].title, "Resist Stance"
38
+ assert_equal form.songs[1].composer.name, "Greg Graffin"
39
+ assert_equal form.artist.name, "Bad Religion"
40
40
 
41
41
  # make sure all is wrapped in forms.
42
- form.songs[0].must_be_kind_of Reform::Form
43
- form.songs[1].must_be_kind_of Reform::Form
44
- form.songs[1].composer.must_be_kind_of Reform::Form
45
- form.artist.must_be_kind_of Reform::Form
42
+ assert form.songs[0].is_a? Reform::Form
43
+ assert form.songs[1].is_a? Reform::Form
44
+ assert form.songs[1].composer.is_a? Reform::Form
45
+ assert form.artist.is_a? Reform::Form
46
46
  end
47
47
  end
48
48
  end
@@ -26,23 +26,23 @@ class SkipIfTest < BaseTest
26
26
  # deserializes when present.
27
27
  it do
28
28
  form = AlbumForm.new(album)
29
- form.validate("hit" => {"title" => "Altar Of Sacrifice"}).must_equal true
30
- form.hit.title.must_equal "Altar Of Sacrifice"
29
+ assert form.validate("hit" => {"title" => "Altar Of Sacrifice"})
30
+ assert_equal form.hit.title, "Altar Of Sacrifice"
31
31
  end
32
32
 
33
33
  # skips deserialization when not present.
34
34
  it do
35
35
  form = AlbumForm.new(Album.new)
36
- form.validate("hit" => {"title" => ""}).must_equal true
36
+ assert form.validate("hit" => {"title" => ""})
37
37
  assert_nil form.hit # hit hasn't been deserialised.
38
38
  end
39
39
 
40
40
  # skips deserialization when not present.
41
41
  it do
42
42
  form = AlbumForm.new(Album.new(nil, nil, []))
43
- form.validate("songs" => [{"title" => "Waste Of Breath"}, {"title" => nil}]).must_equal true
44
- form.songs.size.must_equal 1
45
- form.songs[0].title.must_equal "Waste Of Breath"
43
+ assert form.validate("songs" => [{"title" => "Waste Of Breath"}, {"title" => nil}])
44
+ assert_equal form.songs.size, 1
45
+ assert_equal form.songs[0].title, "Waste Of Breath"
46
46
  end
47
47
  end
48
48
 
@@ -58,16 +58,16 @@ class SkipIfAllBlankTest < BaseTest
58
58
  # create only one object.
59
59
  it do
60
60
  form = AlbumForm.new(OpenStruct.new(songs: []))
61
- form.validate("songs" => [{"title" => "Apathy"}, {"title" => "", "length" => ""}]).must_equal true
62
- form.songs.size.must_equal 1
63
- form.songs[0].title.must_equal "Apathy"
61
+ assert form.validate("songs" => [{"title" => "Apathy"}, {"title" => "", "length" => ""}])
62
+ assert_equal form.songs.size, 1
63
+ assert_equal form.songs[0].title, "Apathy"
64
64
  end
65
65
 
66
66
  it do
67
67
  form = AlbumForm.new(OpenStruct.new(songs: []))
68
- form.validate("songs" => [{"title" => "", "length" => ""}, {"title" => "Apathy"}]).must_equal true
69
- form.songs.size.must_equal 1
70
- form.songs[0].title.must_equal "Apathy"
68
+ assert form.validate("songs" => [{"title" => "", "length" => ""}, {"title" => "Apathy"}])
69
+ assert_equal form.songs.size, 1
70
+ assert_equal form.songs[0].title, "Apathy"
71
71
  end
72
72
  end
73
73
 
@@ -37,17 +37,17 @@ class SetupSkipSetterAndGetterTest < MiniTest::Spec
37
37
  album = Album.new("Greatest Hits", artist)
38
38
  form = AlbumForm.new(album)
39
39
 
40
- form.title.must_equal "GREATEST HITS"
41
- form.artist.name.must_equal "bad religion"
40
+ assert_equal form.title, "GREATEST HITS"
41
+ assert_equal form.artist.name, "bad religion"
42
42
 
43
43
  form.validate("title" => "Resiststance", "artist" => {"name" => "Greg Graffin"})
44
44
 
45
- form.title.must_equal "ECNATSTSISER" # first, setter called, then getter.
46
- form.artist.name.must_equal "greg graffi"
45
+ assert_equal form.title, "ECNATSTSISER" # first, setter called, then getter.
46
+ assert_equal form.artist.name, "greg graffi"
47
47
 
48
48
  form.sync
49
49
 
50
- album.title.must_equal "ecnatstsiseR" # setter called, but not getter.
51
- album.artist.name.must_equal "Greg Graffi"
50
+ assert_equal album.title, "ecnatstsiseR" # setter called, but not getter.
51
+ assert_equal album.artist.name, "Greg Graffi"
52
52
  end
53
53
  end
data/test/test_helper.rb CHANGED
@@ -3,7 +3,7 @@ require "minitest/autorun"
3
3
  require "representable/debug"
4
4
  require "declarative/testing"
5
5
  require "pp"
6
- require "byebug"
6
+ require "pry-byebug"
7
7
 
8
8
  require "reform/form/dry"
9
9
 
@@ -17,14 +17,9 @@ class TestContract < Reform::Contract
17
17
  end
18
18
 
19
19
  module Types
20
- DRY_MODULE = Gem::Version.new(Dry::Types::VERSION) < Gem::Version.new("0.15.0") ? Dry::Types.module : Dry.Types()
21
- include DRY_MODULE
20
+ include Dry.Types()
22
21
  end
23
22
 
24
- DRY_TYPES_VERSION = Gem::Version.new(Dry::Types::VERSION)
25
- DRY_TYPES_CONSTANT = DRY_TYPES_VERSION < Gem::Version.new("0.13.0") ? Types::Form : Types::Params
26
- DRY_TYPES_INT_CONSTANT = DRY_TYPES_VERSION < Gem::Version.new("0.13.0") ? Types::Form::Int : Types::Params::Integer
27
-
28
23
  class BaseTest < MiniTest::Spec
29
24
  class AlbumForm < TestForm
30
25
  property :title
@@ -48,6 +43,10 @@ class BaseTest < MiniTest::Spec
48
43
  end
49
44
 
50
45
  MiniTest::Spec.class_eval do
46
+ Song = Struct.new(:title, :album, :composer)
47
+ Album = Struct.new(:name, :songs, :artist)
48
+ Artist = Struct.new(:name)
49
+
51
50
  module Saveable
52
51
  def save
53
52
  @saved = true
@@ -1,10 +1,6 @@
1
1
  require "test_helper"
2
2
 
3
3
  class ContractValidateTest < MiniTest::Spec
4
- Song = Struct.new(:title, :album, :composer)
5
- Album = Struct.new(:name, :songs, :artist)
6
- Artist = Struct.new(:name)
7
-
8
4
  class AlbumForm < TestContract
9
5
  property :name
10
6
  validation do
@@ -40,8 +36,8 @@ class ContractValidateTest < MiniTest::Spec
40
36
 
41
37
  # valid
42
38
  it do
43
- form.validate.must_equal true
44
- form.errors.messages.inspect.must_equal "{}"
39
+ assert form.validate
40
+ assert_equal form.errors.messages.inspect, "{}"
45
41
  end
46
42
 
47
43
  # invalid
@@ -49,17 +45,13 @@ class ContractValidateTest < MiniTest::Spec
49
45
  album.songs[1].composer.name = nil
50
46
  album.name = nil
51
47
 
52
- form.validate.must_equal false
53
- form.errors.messages.inspect.must_equal "{:name=>[\"must be filled\"], :\"songs.composer.name\"=>[\"must be filled\"]}"
48
+ assert_equal form.validate, false
49
+ assert_equal form.errors.messages.inspect, "{:name=>[\"must be filled\"], :\"songs.composer.name\"=>[\"must be filled\"]}"
54
50
  end
55
51
  end
56
52
 
57
53
  # no configuration results in "sync" (formerly known as parse_strategy: :sync).
58
54
  class ValidateWithoutConfigurationTest < MiniTest::Spec
59
- Song = Struct.new(:title, :album, :composer)
60
- Album = Struct.new(:name, :songs, :artist)
61
- Artist = Struct.new(:name)
62
-
63
55
  class AlbumForm < TestForm
64
56
  property :name
65
57
  validation do
@@ -100,48 +92,48 @@ class ValidateWithoutConfigurationTest < MiniTest::Spec
100
92
  artist: form.artist.object_id, composer: form.songs[1].composer.object_id
101
93
  }
102
94
 
103
- form.validate(
95
+ assert form.validate(
104
96
  "name" => "Best Of",
105
97
  "songs" => [{"title" => "Fallout"}, {"title" => "Roxanne", "composer" => {"name" => "Sting"}}],
106
98
  "artist" => {"name" => "The Police"}
107
- ).must_equal true
99
+ )
108
100
 
109
- form.errors.messages.inspect.must_equal "{}"
101
+ assert_equal form.errors.messages.inspect, "{}"
110
102
 
111
103
  # form has updated.
112
- form.name.must_equal "Best Of"
113
- form.songs[0].title.must_equal "Fallout"
114
- form.songs[1].title.must_equal "Roxanne"
115
- form.songs[1].composer.name.must_equal "Sting"
116
- form.artist.name.must_equal "The Police"
104
+ assert_equal form.name, "Best Of"
105
+ assert_equal form.songs[0].title, "Fallout"
106
+ assert_equal form.songs[1].title, "Roxanne"
107
+ assert_equal form.songs[1].composer.name, "Sting"
108
+ assert_equal form.artist.name, "The Police"
117
109
 
118
110
  # objects are still the same.
119
- form.songs[0].object_id.must_equal object_ids[:song]
120
- form.songs[1].object_id.must_equal object_ids[:song_with_composer]
121
- form.songs[1].composer.object_id.must_equal object_ids[:composer]
122
- form.artist.object_id.must_equal object_ids[:artist]
111
+ assert_equal form.songs[0].object_id, object_ids[:song]
112
+ assert_equal form.songs[1].object_id, object_ids[:song_with_composer]
113
+ assert_equal form.songs[1].composer.object_id, object_ids[:composer]
114
+ assert_equal form.artist.object_id, object_ids[:artist]
123
115
 
124
116
  # model has not changed, yet.
125
- album.name.must_equal "The Dissent Of Man"
126
- album.songs[0].title.must_equal "Broken"
127
- album.songs[1].title.must_equal "Resist Stance"
128
- album.songs[1].composer.name.must_equal "Greg Graffin"
129
- album.artist.name.must_equal "Bad Religion"
117
+ assert_equal album.name, "The Dissent Of Man"
118
+ assert_equal album.songs[0].title, "Broken"
119
+ assert_equal album.songs[1].title, "Resist Stance"
120
+ assert_equal album.songs[1].composer.name, "Greg Graffin"
121
+ assert_equal album.artist.name, "Bad Religion"
130
122
  end
131
123
 
132
124
  # with symbols.
133
125
  it do
134
- form.validate(
126
+ assert form.validate(
135
127
  name: "Best Of",
136
128
  songs: [{title: "The X-Creep"}, {title: "Trudging", composer: {name: "SNFU"}}],
137
129
  artist: {name: "The Police"}
138
- ).must_equal true
130
+ )
139
131
 
140
- form.name.must_equal "Best Of"
141
- form.songs[0].title.must_equal "The X-Creep"
142
- form.songs[1].title.must_equal "Trudging"
143
- form.songs[1].composer.name.must_equal "SNFU"
144
- form.artist.name.must_equal "The Police"
132
+ assert_equal form.name, "Best Of"
133
+ assert_equal form.songs[0].title, "The X-Creep"
134
+ assert_equal form.songs[1].title, "Trudging"
135
+ assert_equal form.songs[1].composer.name, "SNFU"
136
+ assert_equal form.artist.name, "The Police"
145
137
  end
146
138
 
147
139
  # throws exception when no populators.
@@ -155,10 +147,6 @@ class ValidateWithoutConfigurationTest < MiniTest::Spec
155
147
  end
156
148
 
157
149
  class ValidateWithInternalPopulatorOptionTest < MiniTest::Spec
158
- Song = Struct.new(:title, :album, :composer)
159
- Album = Struct.new(:name, :songs, :artist)
160
- Artist = Struct.new(:name)
161
-
162
150
  class AlbumForm < TestForm
163
151
  property :name
164
152
  validation do
@@ -201,66 +189,66 @@ class ValidateWithInternalPopulatorOptionTest < MiniTest::Spec
201
189
 
202
190
  # valid.
203
191
  it("xxx") do
204
- form.validate(
192
+ assert form.validate(
205
193
  "name" => "Best Of",
206
194
  "songs" => [{"title" => "Fallout"}, {"title" => "Roxanne", "composer" => {"name" => "Sting"}}],
207
- "artist" => {"name" => "The Police"},
208
- ).must_equal true
195
+ "artist" => {"name" => "The Police"}
196
+ )
209
197
 
210
- form.errors.messages.inspect.must_equal "{}"
198
+ assert_equal form.errors.messages.inspect, "{}"
211
199
 
212
200
  # form has updated.
213
- form.name.must_equal "Best Of"
214
- form.songs[0].title.must_equal "Fallout"
215
- form.songs[1].title.must_equal "Roxanne"
216
- form.songs[1].composer.name.must_equal "Sting"
217
- form.artist.name.must_equal "The Police"
201
+ assert_equal form.name, "Best Of"
202
+ assert_equal form.songs[0].title, "Fallout"
203
+ assert_equal form.songs[1].title, "Roxanne"
204
+ assert_equal form.songs[1].composer.name, "Sting"
205
+ assert_equal form.artist.name, "The Police"
218
206
 
219
207
  # model has not changed, yet.
220
- album.name.must_equal "The Dissent Of Man"
221
- album.songs[0].title.must_equal "Broken"
222
- album.songs[1].title.must_equal "Resist Stance"
223
- album.songs[1].composer.name.must_equal "Greg Graffin"
224
- album.artist.name.must_equal "Bad Religion"
208
+ assert_equal album.name, "The Dissent Of Man"
209
+ assert_equal album.songs[0].title, "Broken"
210
+ assert_equal album.songs[1].title, "Resist Stance"
211
+ assert_equal album.songs[1].composer.name, "Greg Graffin"
212
+ assert_equal album.artist.name, "Bad Religion"
225
213
  end
226
214
 
227
215
  # invalid.
228
216
  it do
229
- form.validate(
217
+ assert_equal form.validate(
230
218
  "name" => "",
231
219
  "songs" => [{"title" => "Fallout"}, {"title" => "Roxanne", "composer" => {"name" => ""}}],
232
220
  "artist" => {"name" => ""},
233
- ).must_equal false
221
+ ), false
234
222
 
235
- form.errors.messages.inspect.must_equal "{:name=>[\"must be filled\"], :\"songs.composer.name\"=>[\"must be filled\"], :\"artist.name\"=>[\"must be filled\"]}"
223
+ assert_equal form.errors.messages.inspect, "{:name=>[\"must be filled\"], :\"songs.composer.name\"=>[\"must be filled\"], :\"artist.name\"=>[\"must be filled\"]}"
236
224
  end
237
225
 
238
226
  # adding to collection via :instance.
239
227
  # valid.
240
228
  it do
241
- form.validate(
242
- "songs" => [{"title" => "Fallout"}, {"title" => "Roxanne"}, {"title" => "Rime Of The Ancient Mariner"}],
243
- ).must_equal true
229
+ assert form.validate(
230
+ "songs" => [{"title" => "Fallout"}, {"title" => "Roxanne"}, {"title" => "Rime Of The Ancient Mariner"}]
231
+ )
244
232
 
245
- form.errors.messages.inspect.must_equal "{}"
233
+ assert_equal form.errors.messages.inspect, "{}"
246
234
 
247
235
  # form has updated.
248
- form.name.must_equal "The Dissent Of Man"
249
- form.songs[0].title.must_equal "Fallout"
250
- form.songs[1].title.must_equal "Roxanne"
251
- form.songs[1].composer.name.must_equal "Greg Graffin"
252
- form.songs[1].title.must_equal "Roxanne"
253
- form.songs[2].title.must_equal "Rime Of The Ancient Mariner" # new song added.
254
- form.songs.size.must_equal 3
255
- form.artist.name.must_equal "Bad Religion"
236
+ assert_equal form.name, "The Dissent Of Man"
237
+ assert_equal form.songs[0].title, "Fallout"
238
+ assert_equal form.songs[1].title, "Roxanne"
239
+ assert_equal form.songs[1].composer.name, "Greg Graffin"
240
+ assert_equal form.songs[1].title, "Roxanne"
241
+ assert_equal form.songs[2].title, "Rime Of The Ancient Mariner" # new song added.
242
+ assert_equal form.songs.size, 3
243
+ assert_equal form.artist.name, "Bad Religion"
256
244
 
257
245
  # model has not changed, yet.
258
- album.name.must_equal "The Dissent Of Man"
259
- album.songs[0].title.must_equal "Broken"
260
- album.songs[1].title.must_equal "Resist Stance"
261
- album.songs[1].composer.name.must_equal "Greg Graffin"
262
- album.songs.size.must_equal 2
263
- album.artist.name.must_equal "Bad Religion"
246
+ assert_equal album.name, "The Dissent Of Man"
247
+ assert_equal album.songs[0].title, "Broken"
248
+ assert_equal album.songs[1].title, "Resist Stance"
249
+ assert_equal album.songs[1].composer.name, "Greg Graffin"
250
+ assert_equal album.songs.size, 2
251
+ assert_equal album.artist.name, "Bad Religion"
264
252
  end
265
253
 
266
254
  # allow writeable: false even in the deserializer.
@@ -274,7 +262,51 @@ class ValidateWithInternalPopulatorOptionTest < MiniTest::Spec
274
262
  assert_nil form.title
275
263
  form.title = "Unopened"
276
264
  form.sync # only the deserializer is marked as not-writeable.
277
- song.title.must_equal "Unopened"
265
+ assert_equal song.title, "Unopened"
266
+ end
267
+ end
268
+
269
+ # memory leak test
270
+ class ValidateUsingDifferentFormObject < MiniTest::Spec
271
+ class AlbumForm < TestForm
272
+ property :name
273
+
274
+ validation do
275
+ option :form
276
+
277
+ params { required(:name).filled(:str?) }
278
+
279
+ rule(:name) do
280
+ if form.name == 'invalid'
281
+ key.failure('Invalid name')
282
+ end
283
+ end
284
+ end
285
+ end
286
+
287
+ let(:album) { Album.new }
288
+
289
+ let(:form) { AlbumForm.new(album) }
290
+
291
+ it 'sets name correctly' do
292
+ assert form.validate(name: 'valid')
293
+ form.sync
294
+ assert_equal form.model.name, 'valid'
295
+ end
296
+
297
+ it 'validates presence of name' do
298
+ refute form.validate(name: nil)
299
+ assert_equal form.errors[:name], ["must be filled"]
300
+ end
301
+
302
+ it 'validates type of name' do
303
+ refute form.validate(name: 1)
304
+ assert_equal form.errors[:name], ["must be a string"]
305
+ end
306
+
307
+ it 'when name is invalid' do
308
+ refute form.validate(name: 'invalid')
309
+ assert_equal form.errors[:name], ["Invalid name"]
278
310
  end
279
311
  end
280
312