test/twin/coercion_test.rb in disposable-0.4.3 vs test/twin/coercion_test.rb in disposable-0.4.4

- old
+ new

@@ -1,20 +1,19 @@ require "test_helper" require "disposable/twin/coercion" class CoercionTest < MiniTest::Spec - class TwinWithSkipSetter < Disposable::Twin feature Coercion feature Setup::SkipSetter property :id - property :released_at, type: Types::Form::DateTime + property :released_at, type: DRY_TYPES_CONSTANT::DateTime property :hit do - property :length, type: Types::Coercible::Int + property :length, type: const_get("Types::Coercible::#{DRY_TYPES_INT_CONSTANT}") property :good, type: Types::Bool end property :band do property :label do @@ -22,11 +21,10 @@ end end end describe "with Setup::SkipSetter" do - subject do TwinWithSkipSetter.new(album) end let (:album) { @@ -52,18 +50,18 @@ subject.band.label.value = "9999.99" subject.released_at.must_be_kind_of DateTime subject.released_at.must_equal DateTime.parse("30/03/1981") subject.hit.length.must_equal 312 - subject.hit.good.must_equal nil + subject.hit.good.must_be_nil subject.band.label.value.must_equal 9999.99 end end class TwinWithoutSkipSetter < Disposable::Twin feature Coercion - property :id, type: Types::Coercible::Int + property :id, type: const_get("Types::Coercible::#{DRY_TYPES_INT_CONSTANT}") end describe "without Setup::SkipSetter" do subject do @@ -79,13 +77,13 @@ class TwinWithNilify < Disposable::Twin feature Coercion property :date_of_birth, - type: Types::Form::Date, nilify: true + type: DRY_TYPES_CONSTANT::Date, nilify: true property :date_of_death_by_unicorns, - type: Types::Form::Nil | Types::Form::Date + type: DRY_TYPES_CONSTANT::Nil | DRY_TYPES_CONSTANT::Date property :id, nilify: true end describe "with Nilify" do @@ -100,37 +98,37 @@ subject.date_of_death_by_unicorns.must_equal Date.parse('2037-02-18') end it "coerce empty values to nil when using option nilify: true" do subject.date_of_birth = "" - subject.date_of_birth.must_equal nil + subject.date_of_birth.must_be_nil end it "coerce empty values to nil when using dry-types | operator" do subject.date_of_death_by_unicorns = "" - subject.date_of_death_by_unicorns.must_equal nil + subject.date_of_death_by_unicorns.must_be_nil end it "converts blank string to nil, without :type option" do subject.id = "" - subject.id.must_equal nil + subject.id.must_be_nil end end end # this converts "" to nil and then breaks because it's strict. -# Types::Strict::String.constructor(Dry::Types::Coercions::Form.method(:to_nil)) +# Types::Strict::String.constructor(Dry::Types::Params.method(:to_nil)) class CoercionTypingTest < MiniTest::Spec class Song < Disposable::Twin include Coercion include Setup::SkipSetter - # property :title, type: Dry::Types::Strict::String.constructor(Dry::Types::Coercions::Form.method(:to_nil)) + # property :title, type: Dry::Types::Strict::String.constructor(Dry::Types::Params.method(:to_nil)) property :title, type: Types::Strict::String.optional # this is the behavior of the "DB" data twin. this is NOT the form. - # property :name, type: Types::Form::String + # property :name, type: Types::Params::String end it do twin = Song.new(Struct.new(:title, :name).new) @@ -153,15 +151,17 @@ class Form < Disposable::Twin include Coercion include Setup::SkipSetter - # property :title, type: Dry::Types::Strict::String.constructor(Dry::Types::Coercions::Form.method(:to_nil)) - property :title, type: Types::Strict::String.optional.constructor(Dry::Types::Coercions::Form.method(:to_nil)) # this is the behavior of the "DB" data twin. this is NOT the form. + # property :title, type: Dry::Types::Strict::String.constructor(Dry::Types::Params.method(:to_nil)) - # property :name, type: Types::Form::String + constructor = Disposable::Twin::Coercion::DRY_TYPES_VERSION < 13 ? 'form.nil' : 'params.nil' + property :title, type: Types::Strict::String.optional.constructor(Dry::Types[constructor]) # this is the behavior of the "DB" data twin. this is NOT the form. - property :enabled, type: Types::Form::Bool + # property :name, type: Types::Params::String + + property :enabled, type: DRY_TYPES_CONSTANT::Bool # property :enabled, Bool.constructor(:trim!) end it do twin =Form.new(Struct.new(:title, :enabled).new)