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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68e69ea827ccacd9338da23633e8a375a8a1d8e5d1c1ba19d6c3024d700d0cdd
|
4
|
+
data.tar.gz: 70d9ae61c5ff37307e8e94c66f048e876fd13056558cc173c4f9b5543192aa52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7410df891945423b07d34bbca31976037306ad90c9e935108101e2ec2fbf3e11ba4f115f23bb4fae3ae72e0360f1111eaee152c4e85d630d344505b27f8ddf79
|
7
|
+
data.tar.gz: 6f838dfb0b78180526545e7b54378a8cb3b62c4cfa437bd951afdcfa58a3f3e19445963501edb1a601b588a44201573eb5adaad51ac3ceeb61cda93db282d788
|
@@ -0,0 +1,17 @@
|
|
1
|
+
name: CI
|
2
|
+
on: [push, pull_request]
|
3
|
+
jobs:
|
4
|
+
test:
|
5
|
+
strategy:
|
6
|
+
fail-fast: false
|
7
|
+
matrix:
|
8
|
+
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
|
9
|
+
ruby: [2.4, 2.5, 2.6, 2.7, '3.0', head]
|
10
|
+
runs-on: ubuntu-latest
|
11
|
+
steps:
|
12
|
+
- uses: actions/checkout@v2
|
13
|
+
- uses: ruby/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: ${{ matrix.ruby }}
|
16
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
17
|
+
- run: bundle exec rake
|
data/.gitignore
CHANGED
data/CHANGES.md
CHANGED
@@ -1,3 +1,26 @@
|
|
1
|
+
## 2.6.1
|
2
|
+
|
3
|
+
* Loosen `disposable` dependency to `>= 0.5.0`.
|
4
|
+
|
5
|
+
## 2.6.0
|
6
|
+
|
7
|
+
* Support ruby-3 by using `Representable::Option` to handle `keyword_arguments` forwarding :tada:
|
8
|
+
* Upgrade `representable` and `disposable` dependencies which uses `trailblazer-option` over `declarative-option`.
|
9
|
+
* Deprecate populator's callable signature which accepts `form` as a separate positional argument. Make all callable (proc, method, `Uber::Callable`) signatures identical.
|
10
|
+
|
11
|
+
## 2.5.0
|
12
|
+
* fix memory leak with Dry validation (#525)
|
13
|
+
|
14
|
+
## 2.4.0
|
15
|
+
|
16
|
+
* [BREAKING] Dropping compatibility of dry-validation < 1.x
|
17
|
+
[* Removed `Reform::Contract` ?]
|
18
|
+
[* Move Form#deserializer to Form::deserializer]
|
19
|
+
|
20
|
+
## 2.3.3
|
21
|
+
|
22
|
+
* Rename validation option for dry-v 1+ to `contract` instead of `schema`
|
23
|
+
|
1
24
|
## 2.3.2
|
2
25
|
|
3
26
|
* Fix Validation block option :form incorrectly memoized between tests
|
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -10,11 +10,11 @@ _Form objects decoupled from your models._
|
|
10
10
|
|
11
11
|
Reform gives you a form object with validations and nested setup of models. It is completely framework-agnostic and doesn't care about your database.
|
12
12
|
|
13
|
-
Although reform can be used in any Ruby framework, it comes with [Rails support](#rails-integration), works with
|
13
|
+
Although reform can be used in any Ruby framework, it comes with [Rails support](#rails-integration), works with simple_form and other form gems, allows nesting forms to implement has_one and has_many relationships, can [compose a form](#compositions) from multiple objects and gives you coercion.
|
14
14
|
|
15
15
|
## Full Documentation
|
16
16
|
|
17
|
-
Reform is part of the [Trailblazer](http://trailblazer.to) framework. [Full documentation](http://trailblazer.to/
|
17
|
+
Reform is part of the [Trailblazer](http://trailblazer.to) framework. [Full documentation](http://trailblazer.to/2.1/docs/reform.html) is available on the project site.
|
18
18
|
|
19
19
|
## Reform 2.2
|
20
20
|
|
@@ -250,7 +250,7 @@ The manual saving with block is not encouraged. You should rather check the Disp
|
|
250
250
|
|
251
251
|
## Populating Forms
|
252
252
|
|
253
|
-
Very often, you need to give Reform some information how to create or find nested objects when `validate`ing. This directive is called _populator_ and [documented here](http://trailblazer.to/
|
253
|
+
Very often, you need to give Reform some information how to create or find nested objects when `validate`ing. This directive is called _populator_ and [documented here](http://trailblazer.to/2.1/docs/reform.html#reform-populators).
|
254
254
|
|
255
255
|
## Installation
|
256
256
|
|
@@ -313,7 +313,7 @@ AlbumForm.new(album: album, cd: CD.find(1))
|
|
313
313
|
|
314
314
|
## More
|
315
315
|
|
316
|
-
Reform comes many more optional features, like hash fields, coercion, virtual fields, and so on. Check the [full documentation here](http://trailblazer.to/
|
316
|
+
Reform comes many more optional features, like hash fields, coercion, virtual fields, and so on. Check the [full documentation here](http://trailblazer.to/2.1/docs/reform.html).
|
317
317
|
|
318
318
|
[![](http://trailblazer.to/images/3dbuch-freigestellt.png)](https://leanpub.com/trailblazer)
|
319
319
|
|
@@ -330,7 +330,7 @@ Temporary note: This is the README and API for Reform 2. On the public API, only
|
|
330
330
|
|
331
331
|
Anyway, please upgrade and _report problems_ and do not simply assume that we will magically find out what needs to get fixed. When in trouble, join us on [Gitter](https://gitter.im/trailblazer/chat).
|
332
332
|
|
333
|
-
[Full documentation for Reform](http://trailblazer.to/
|
333
|
+
[Full documentation for Reform](http://trailblazer.to/2.1/docs/reform.html) is available online, or support us and grab the [Trailblazer book](https://leanpub.com/trailblazer). There is an [Upgrading Guide](http://trailblazer.to/2.1/docs/reform.html#reform-upgrading-guide) to help you migrate through versions.
|
334
334
|
|
335
335
|
### Attributions!!!
|
336
336
|
|
data/Rakefile
CHANGED
@@ -4,19 +4,8 @@ require "dry/types/version"
|
|
4
4
|
|
5
5
|
task default: %i[test]
|
6
6
|
|
7
|
-
TEST_WITH_OLD_AND_NEW_API = %w[
|
8
|
-
validation/dry_validation call composition contract errors inherit module reform
|
9
|
-
save skip_if populate validate form
|
10
|
-
].freeze
|
11
|
-
|
12
|
-
def dry_v_test_files
|
13
|
-
api = Gem::Version.new(Dry::Types::VERSION).to_s.split('.').first.to_i >= 1 ? "new" : "old"
|
14
|
-
TEST_WITH_OLD_AND_NEW_API.map { |file| "test/#{file}_#{api}_api.rb" }
|
15
|
-
end
|
16
|
-
|
17
7
|
Rake::TestTask.new(:test) do |test|
|
18
8
|
test.libs << "test"
|
19
|
-
test.test_files = FileList["test
|
9
|
+
test.test_files = FileList["test/**/*_test.rb"]
|
20
10
|
test.verbose = true
|
21
11
|
end
|
22
|
-
|
@@ -11,7 +11,7 @@ class Reform::Contract < Disposable::Twin
|
|
11
11
|
validate!(nil).success?
|
12
12
|
end
|
13
13
|
|
14
|
-
# The #errors method will be removed in Reform
|
14
|
+
# The #errors method will be removed in Reform 3.0 core.
|
15
15
|
def errors(*args)
|
16
16
|
Result::Errors.new(@result, self)
|
17
17
|
end
|
data/lib/reform/form/dry.rb
CHANGED
@@ -1,19 +1,57 @@
|
|
1
|
+
gem 'dry-validation', '~> 1.5'
|
1
2
|
require "dry-validation"
|
2
|
-
require "dry/validation/version"
|
3
3
|
require "reform/validation"
|
4
4
|
require "reform/form/dry/input_hash"
|
5
5
|
|
6
|
+
::Dry::Validation.load_extensions(:hints)
|
7
|
+
|
6
8
|
module Reform::Form::Dry
|
9
|
+
class Contract < Dry::Validation::Contract
|
10
|
+
end
|
11
|
+
|
7
12
|
def self.included(includer)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
includer.send :include, Validations
|
14
|
+
includer.extend Validations::ClassMethods
|
15
|
+
end
|
16
|
+
|
17
|
+
module Validations
|
18
|
+
module ClassMethods
|
19
|
+
def validation_group_class
|
20
|
+
Group
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.included(includer)
|
25
|
+
includer.extend(ClassMethods)
|
14
26
|
end
|
15
27
|
|
16
|
-
|
17
|
-
|
28
|
+
class Group
|
29
|
+
include InputHash
|
30
|
+
|
31
|
+
def initialize(options)
|
32
|
+
@validator = options.fetch(:contract, Contract)
|
33
|
+
@schema_inject_params = options.fetch(:with, {})
|
34
|
+
end
|
35
|
+
|
36
|
+
attr_reader :validator, :schema_inject_params, :block
|
37
|
+
|
38
|
+
def instance_exec(&block)
|
39
|
+
@block = block
|
40
|
+
end
|
41
|
+
|
42
|
+
def call(form)
|
43
|
+
# when passing options[:schema] the class instance is already created so we just need to call
|
44
|
+
# "call"
|
45
|
+
return validator.call(input_hash(form)) unless validator.is_a?(Class) && @validator <= ::Dry::Validation::Contract
|
46
|
+
|
47
|
+
dynamic_options = { form: form }
|
48
|
+
inject_options = schema_inject_params.merge(dynamic_options)
|
49
|
+
contract.new(**inject_options).call(input_hash(form))
|
50
|
+
end
|
51
|
+
|
52
|
+
def contract
|
53
|
+
@contract ||= Class.new(validator, &block)
|
54
|
+
end
|
55
|
+
end
|
18
56
|
end
|
19
57
|
end
|
@@ -7,7 +7,7 @@
|
|
7
7
|
class Reform::Form::Populator
|
8
8
|
def initialize(user_proc)
|
9
9
|
@user_proc = user_proc # the actual `populator: ->{}` block from the user, via ::property.
|
10
|
-
@value =
|
10
|
+
@value = ::Representable::Option(user_proc) # we can now process Callable, procs, :symbol.
|
11
11
|
end
|
12
12
|
|
13
13
|
def call(input, options)
|
@@ -30,7 +30,17 @@ class Reform::Form::Populator
|
|
30
30
|
|
31
31
|
def call!(options)
|
32
32
|
form = options[:represented]
|
33
|
-
|
33
|
+
evaluate_option(form, options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def evaluate_option(form, options)
|
37
|
+
if @user_proc.is_a?(Uber::Callable) && @user_proc.method(:call).arity == 2 # def call(form, options)
|
38
|
+
warn %{[Reform] Accepting `form` as a positional argument in `:populator` will be deprecated. Please use `def call(form:, **options)` signature instead.}
|
39
|
+
|
40
|
+
return @value.(form, exec_context: form, keyword_arguments: options)
|
41
|
+
end
|
42
|
+
|
43
|
+
@value.(exec_context: form, keyword_arguments: options.merge(form: form)) # Representable::Option call
|
34
44
|
end
|
35
45
|
|
36
46
|
def handle_fail(twin, options)
|
@@ -66,7 +76,7 @@ class Reform::Form::Populator
|
|
66
76
|
return @user_proc.new if @user_proc.is_a?(Class) # handle populate_if_empty: Class. this excludes using Callables, though.
|
67
77
|
|
68
78
|
deprecate_positional_args(form, @user_proc, options) do
|
69
|
-
|
79
|
+
evaluate_option(form, options)
|
70
80
|
end
|
71
81
|
end
|
72
82
|
|
@@ -13,7 +13,7 @@ module Reform::Form::Prepopulate
|
|
13
13
|
def prepopulate_local!(options)
|
14
14
|
schema.each do |dfn|
|
15
15
|
next unless block = dfn[:prepopulator]
|
16
|
-
|
16
|
+
::Representable::Option(block).(exec_context: self, keyword_arguments: options)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
data/lib/reform/form/validate.rb
CHANGED
@@ -4,15 +4,15 @@ module Reform::Form::Validate
|
|
4
4
|
class AllBlank
|
5
5
|
include Uber::Callable
|
6
6
|
|
7
|
-
def call(
|
7
|
+
def call(input:, binding:, **)
|
8
8
|
# TODO: Schema should provide property names as plain list.
|
9
9
|
# ensure param keys are strings.
|
10
|
-
params =
|
10
|
+
params = input.each_with_object({}) { |(k, v), hash|
|
11
11
|
hash[k.to_s] = v
|
12
12
|
}
|
13
13
|
|
14
14
|
# return false if any property inputs are populated.
|
15
|
-
|
15
|
+
binding[:nested].definitions.each do |definition|
|
16
16
|
value = params[definition.name.to_s]
|
17
17
|
return false if (!value.nil? && value != '')
|
18
18
|
end
|
data/lib/reform/version.rb
CHANGED
data/reform.gemspec
CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_dependency "disposable", ">= 0.
|
21
|
-
spec.add_dependency "representable", ">=
|
20
|
+
spec.add_dependency "disposable", ">= 0.5.0", "< 1.0.0"
|
21
|
+
spec.add_dependency "representable", ">= 3.1.1", "< 3.2.0"
|
22
22
|
spec.add_dependency "uber", "< 0.2.0"
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler"
|
data/test/call_test.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class CallTest < Minitest::Spec
|
4
|
+
Song = Struct.new(:title)
|
5
|
+
|
6
|
+
class SongForm < TestForm
|
7
|
+
property :title
|
8
|
+
|
9
|
+
validation do
|
10
|
+
params { required(:title).filled }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:form) { SongForm.new(Song.new) }
|
15
|
+
|
16
|
+
it { assert form.(title: "True North").success? }
|
17
|
+
it { refute form.(title: "True North").failure? }
|
18
|
+
it { refute form.(title: "").success? }
|
19
|
+
it { assert form.(title: "").failure? }
|
20
|
+
|
21
|
+
it { assert_equal form.(title: "True North").errors.messages, {} }
|
22
|
+
it { assert_equal form.(title: "").errors.messages, title: ["must be filled"] }
|
23
|
+
end
|
data/test/changed_test.rb
CHANGED
@@ -26,16 +26,16 @@ class ChangedTest < MiniTest::Spec
|
|
26
26
|
|
27
27
|
# nothing changed after setup.
|
28
28
|
it do
|
29
|
-
form.changed?(:name)
|
30
|
-
form.songs[0].changed?(:title)
|
31
|
-
form.songs[0].composer.changed?(:name)
|
29
|
+
refute form.changed?(:name)
|
30
|
+
refute form.songs[0].changed?(:title)
|
31
|
+
refute form.songs[0].composer.changed?(:name)
|
32
32
|
end
|
33
33
|
|
34
34
|
# after validate, things might have changed.
|
35
35
|
it do
|
36
36
|
form.validate("name" => "Out Of Bounds", "songs" => [{"composer" => {"name" => "Ingemar Jansson & Mikael Danielsson"}}])
|
37
|
-
form.changed?(:name)
|
38
|
-
form.songs[0].changed?(:title)
|
39
|
-
form.songs[0].composer.changed?(:name)
|
37
|
+
assert form.changed?(:name)
|
38
|
+
refute form.songs[0].changed?(:title)
|
39
|
+
assert form.songs[0].composer.changed?(:name)
|
40
40
|
end
|
41
41
|
end
|
data/test/coercion_test.rb
CHANGED
@@ -13,11 +13,11 @@ class CoercionTest < BaseTest
|
|
13
13
|
feature Coercion
|
14
14
|
include Disposable::Twin::Property::Hash
|
15
15
|
|
16
|
-
property :released_at, type:
|
16
|
+
property :released_at, type: Types::Params::DateTime
|
17
17
|
|
18
18
|
property :hit do
|
19
|
-
property :length, type:
|
20
|
-
property :good, type:
|
19
|
+
property :length, type: Types::Params::Integer
|
20
|
+
property :good, type: Types::Params::Bool
|
21
21
|
end
|
22
22
|
|
23
23
|
property :band do
|
@@ -28,7 +28,7 @@ class CoercionTest < BaseTest
|
|
28
28
|
|
29
29
|
property :metadata, field: :hash do
|
30
30
|
property :publication_settings do
|
31
|
-
property :featured, type:
|
31
|
+
property :featured, type: Types::Params::Bool
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -47,9 +47,9 @@ class CoercionTest < BaseTest
|
|
47
47
|
end
|
48
48
|
|
49
49
|
# it { subject.released_at.must_be_kind_of DateTime }
|
50
|
-
it { subject.released_at
|
51
|
-
it { subject.hit.length
|
52
|
-
it { subject.band.label.value
|
50
|
+
it { assert_equal subject.released_at, "31/03/1981" } # NO coercion in setup.
|
51
|
+
it { assert_equal subject.hit.length, "312" }
|
52
|
+
it { assert_equal subject.band.label.value, "9999.99" }
|
53
53
|
|
54
54
|
let(:params) do
|
55
55
|
{
|
@@ -75,24 +75,24 @@ class CoercionTest < BaseTest
|
|
75
75
|
describe "#validate" do
|
76
76
|
before { subject.validate(params) }
|
77
77
|
|
78
|
-
it { subject.released_at
|
79
|
-
it { subject.hit.length
|
80
|
-
it { subject.hit.good
|
81
|
-
it { subject.band.label.value
|
82
|
-
it { subject.metadata.publication_settings.featured
|
78
|
+
it { assert_equal subject.released_at, DateTime.parse("30/03/1981") }
|
79
|
+
it { assert_equal subject.hit.length, 312 }
|
80
|
+
it { assert_equal subject.hit.good, false }
|
81
|
+
it { assert_equal subject.band.label.value, "9999.999999.99" } # coercion happened once.
|
82
|
+
it { assert_equal subject.metadata.publication_settings.featured, false }
|
83
83
|
end
|
84
84
|
|
85
85
|
# sync
|
86
86
|
describe "#sync" do
|
87
87
|
before do
|
88
|
-
subject.validate(params)
|
88
|
+
assert subject.validate(params)
|
89
89
|
subject.sync
|
90
90
|
end
|
91
91
|
|
92
|
-
it { album.released_at
|
93
|
-
it { album.hit.length
|
94
|
-
it { album.hit.good
|
92
|
+
it { assert_equal album.released_at, DateTime.parse("30/03/1981") }
|
93
|
+
it { assert_equal album.hit.length, 312 }
|
94
|
+
it { assert_equal album.hit.good, false }
|
95
95
|
it { assert_nil album.metadata[:publication_settings] }
|
96
|
-
it { album.metadata["publication_settings"]["featured"]
|
96
|
+
it { assert_equal album.metadata["publication_settings"]["featured"], false }
|
97
97
|
end
|
98
98
|
end
|
@@ -33,8 +33,8 @@ class FormCompositionInheritanceTest < MiniTest::Spec
|
|
33
33
|
Tshirt = Struct.new(:price, :size)
|
34
34
|
Measurement = Struct.new(:size)
|
35
35
|
|
36
|
-
it { form.price
|
37
|
-
it { form.price(for_size: :s)
|
36
|
+
it { assert_equal form.price, 6 }
|
37
|
+
it { assert_equal form.price(for_size: :s), 2 }
|
38
38
|
end
|
39
39
|
|
40
40
|
class FormCompositionTest < MiniTest::Spec
|
@@ -71,24 +71,24 @@ class FormCompositionTest < MiniTest::Spec
|
|
71
71
|
let(:band) { Band.new("Duran^2") }
|
72
72
|
|
73
73
|
# delegation form -> composition works
|
74
|
-
it { form.id
|
75
|
-
it { form.title
|
76
|
-
it { form.name
|
77
|
-
it { form.requester_id
|
74
|
+
it { assert_equal form.id, 1 }
|
75
|
+
it { assert_equal form.title, "Rio" }
|
76
|
+
it { assert_equal form.name, "Duran Duran" }
|
77
|
+
it { assert_equal form.requester_id, 2 }
|
78
78
|
it { assert_nil form.channel }
|
79
|
-
it { form.requester
|
79
|
+
it { assert_equal form.requester, "MCP" } # same name as composed model.
|
80
80
|
it { assert_nil form.captcha }
|
81
81
|
|
82
82
|
# #model just returns <Composition>.
|
83
|
-
it { form.mapper.
|
83
|
+
it { assert form.mapper.is_a? Disposable::Composition }
|
84
84
|
|
85
85
|
# #model[] -> composed models
|
86
|
-
it { form.model[:requester]
|
87
|
-
it { form.model[:song]
|
86
|
+
it { assert_equal form.model[:requester], requester }
|
87
|
+
it { assert_equal form.model[:song], song }
|
88
88
|
|
89
89
|
it "creates Composition for you" do
|
90
|
-
form.validate("title" => "Greyhound", "name" => "Frenzal Rhomb")
|
91
|
-
form.validate("title" => "", "name" => "Frenzal Rhomb")
|
90
|
+
assert_equal form.validate("title" => "Greyhound", "name" => "Frenzal Rhomb"), true
|
91
|
+
assert_equal form.validate("title" => "", "name" => "Frenzal Rhomb"), false
|
92
92
|
end
|
93
93
|
|
94
94
|
describe "#save" do
|
@@ -101,7 +101,7 @@ class FormCompositionTest < MiniTest::Spec
|
|
101
101
|
hash[:title] = form.title
|
102
102
|
end
|
103
103
|
|
104
|
-
hash
|
104
|
+
assert_equal hash, name: "Duran Duran", title: "Rio"
|
105
105
|
end
|
106
106
|
|
107
107
|
it "provides nested symbolized hash as second block argument" do
|
@@ -113,11 +113,10 @@ class FormCompositionTest < MiniTest::Spec
|
|
113
113
|
hash = map
|
114
114
|
end
|
115
115
|
|
116
|
-
hash
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
)
|
116
|
+
assert_equal hash, {
|
117
|
+
song: {"title" => "Greyhound", "id" => 1, "channel" => "JJJ", "captcha" => "wonderful", "band" => {"title" => "Duran^2"}},
|
118
|
+
requester: {"name" => "Frenzal Rhomb", "id" => 2, "requester" => "MCP"}
|
119
|
+
}
|
121
120
|
end
|
122
121
|
|
123
122
|
it "xxx pushes data to models and calls #save when no block passed" do
|
@@ -126,16 +125,16 @@ class FormCompositionTest < MiniTest::Spec
|
|
126
125
|
band.extend(Saveable)
|
127
126
|
|
128
127
|
form.validate("title" => "Greyhound", "name" => "Frenzal Rhomb", "captcha" => "1337")
|
129
|
-
form.captcha
|
128
|
+
assert_equal form.captcha, "1337" # TODO: move to separate test.
|
130
129
|
|
131
130
|
form.save
|
132
131
|
|
133
|
-
requester.name
|
134
|
-
requester.saved
|
135
|
-
song.title
|
136
|
-
song.saved
|
137
|
-
song.band.title
|
138
|
-
song.band.saved
|
132
|
+
assert_equal requester.name, "Frenzal Rhomb"
|
133
|
+
assert_equal requester.saved?, true
|
134
|
+
assert_equal song.title, "Greyhound"
|
135
|
+
assert_equal song.saved?, true
|
136
|
+
assert_equal song.band.title, "Duran^2"
|
137
|
+
assert_equal song.band.saved?, true
|
139
138
|
end
|
140
139
|
|
141
140
|
it "returns true when models all save successfully" do
|
@@ -143,7 +142,7 @@ class FormCompositionTest < MiniTest::Spec
|
|
143
142
|
requester.extend(Saveable)
|
144
143
|
band.extend(Saveable)
|
145
144
|
|
146
|
-
form.save
|
145
|
+
assert_equal form.save, true
|
147
146
|
end
|
148
147
|
|
149
148
|
it "returns false when one or more models don't save successfully" do
|
@@ -157,7 +156,7 @@ class FormCompositionTest < MiniTest::Spec
|
|
157
156
|
requester.extend(Saveable)
|
158
157
|
band.extend(Saveable)
|
159
158
|
|
160
|
-
form.save
|
159
|
+
assert_equal form.save, false
|
161
160
|
end
|
162
161
|
end
|
163
162
|
end
|
@@ -182,5 +181,5 @@ class FormCompositionCollectionTest < MiniTest::Spec
|
|
182
181
|
let(:form) { LibraryForm.new(library: library) }
|
183
182
|
let(:library) { Library.new(2) }
|
184
183
|
|
185
|
-
it { form.save { |hash| hash
|
184
|
+
it { form.save { |hash| assert_equal hash, { library: { "books" => [{ "id" => 1, "name" => "My book" }] } } } }
|
186
185
|
end
|
@@ -46,32 +46,32 @@ class ContractTest < MiniTest::Spec
|
|
46
46
|
|
47
47
|
# accept `property form: SongForm`.
|
48
48
|
it do
|
49
|
-
form.artist.
|
49
|
+
assert form.artist.is_a? ArtistForm
|
50
50
|
end
|
51
51
|
|
52
52
|
describe ".properties" do
|
53
53
|
it "defines a property when called with one argument" do
|
54
|
-
form
|
54
|
+
assert_respond_to form, :duration
|
55
55
|
end
|
56
56
|
|
57
57
|
it "defines several properties when called with multiple arguments" do
|
58
|
-
form
|
59
|
-
form
|
58
|
+
assert_respond_to form, :year
|
59
|
+
assert_respond_to form, :style
|
60
60
|
end
|
61
61
|
|
62
62
|
it "passes options to each property when options are provided" do
|
63
63
|
readable = AlbumForm.new(album).options_for(:style)[:readable]
|
64
|
-
readable
|
64
|
+
assert_equal readable, false
|
65
65
|
end
|
66
66
|
|
67
67
|
it "returns the list of defined properties" do
|
68
68
|
returned_value = AlbumForm.properties(:hello, :world, virtual: true)
|
69
|
-
returned_value
|
69
|
+
assert_equal returned_value, %i[hello world]
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
73
|
describe "#options_for" do
|
74
|
-
it { AlbumForm.options_for(:name).extend(Declarative::Inspect).inspect
|
75
|
-
it { AlbumForm.new(album).options_for(:name).extend(Declarative::Inspect).inspect
|
74
|
+
it { assert_equal AlbumForm.options_for(:name).extend(Declarative::Inspect).inspect, "#<Disposable::Twin::Definition: @options={:private_name=>:name, :name=>\"name\"}>" }
|
75
|
+
it { assert_equal AlbumForm.new(album).options_for(:name).extend(Declarative::Inspect).inspect, "#<Disposable::Twin::Definition: @options={:private_name=>:name, :name=>\"name\"}>" }
|
76
76
|
end
|
77
77
|
end
|
data/test/default_test.rb
CHANGED
@@ -16,7 +16,7 @@ class DefaultTest < Minitest::Spec
|
|
16
16
|
it do
|
17
17
|
form = AlbumForm.new(Album.new(nil, [Song.new]))
|
18
18
|
|
19
|
-
form.name
|
20
|
-
form.songs[0].title
|
19
|
+
assert_equal form.name, "Wrong"
|
20
|
+
assert_equal form.songs[0].title, "It's Catching Up"
|
21
21
|
end
|
22
22
|
end
|
data/test/deserialize_test.rb
CHANGED
@@ -41,9 +41,9 @@ class DeserializeTest < MiniTest::Spec
|
|
41
41
|
|
42
42
|
form.validate(json)
|
43
43
|
|
44
|
-
form.title
|
45
|
-
form.artist.name
|
46
|
-
form.artist.model.object_id
|
44
|
+
assert_equal form.title, "Apocalypse Soon"
|
45
|
+
assert_equal form.artist.name, "Mute"
|
46
|
+
assert_equal form.artist.model.object_id, artist_id
|
47
47
|
end
|
48
48
|
|
49
49
|
describe "infering the deserializer from another form should NOT copy its populators" do
|
@@ -62,7 +62,7 @@ class DeserializeTest < MiniTest::Spec
|
|
62
62
|
form = CompilationForm.new(Album.new)
|
63
63
|
form.validate("artist" => {"name" => "Horowitz"}) # the deserializer doesn't know symbols.
|
64
64
|
form.sync
|
65
|
-
form.artist.model
|
65
|
+
assert_equal form.artist.model, Artist.new("Horowitz", %{{"name"=>"Horowitz"}})
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
@@ -91,11 +91,11 @@ class ValidateWithBlockTest < MiniTest::Spec
|
|
91
91
|
options_from: :deserializer
|
92
92
|
)
|
93
93
|
|
94
|
-
form.validate(json) { |params|
|
94
|
+
assert form.validate(json) { |params|
|
95
95
|
deserializer.new(form).from_json(params)
|
96
|
-
}
|
96
|
+
} # with block must return result, too.
|
97
97
|
|
98
|
-
form.title
|
99
|
-
form.artist.name
|
98
|
+
assert_equal form.title, "Apocalypse Soon"
|
99
|
+
assert_equal form.artist.name, "Mute"
|
100
100
|
end
|
101
101
|
end
|