Sha256: 9e1dfc018f4c9ff08dd0914c403974e8adf2219fb797d9f9764e764af158de47

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

require "test_helper"
require "reform/form/coercion"

class CoercionTest < BaseTest
  class Irreversible
    def self.call(value)
      value*2
    end
  end

  class Form < Reform::Form
    feature Coercion

    property :released_at, type: Types::Form::DateTime

    property :hit do
      property :length, type: Types::Form::Int
      property :good,   type: Types::Form::Bool
    end

    property :band do
      property :label do
        property :value, type: Irreversible
      end
    end
  end

  subject do
    Form.new(album)
  end

  let (:album) {
    OpenStruct.new(
      :released_at => "31/03/1981",
      :hit         => OpenStruct.new(:length => "312"),
      :band        => Band.new(OpenStruct.new(:value => "9999.99"))
    )
  }

  # it { subject.released_at.must_be_kind_of DateTime }
  it { subject.released_at.must_equal "31/03/1981" } # NO coercion in setup.
  it { subject.hit.length.must_equal "312" }
  it { subject.band.label.value.must_equal "9999.99" }


  let (:params) {
    {
      :released_at => "30/03/1981",
      :hit         => {:length => "312"},
      :band        => {:label => {:value => "9999.99"}}
    }
  }


  # validate
  describe "#validate" do
    before { subject.validate(params) }

    it { subject.released_at.must_equal DateTime.parse("30/03/1981") }
    it { subject.hit.length.must_equal 312 }
    it { subject.hit.good.must_equal nil }
    it { subject.band.label.value.must_equal "9999.999999.99" } # coercion happened once.
  end

  # save
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
reform-2.2.4 test/coercion_test.rb
reform-2.2.3 test/coercion_test.rb
reform-2.2.2 test/coercion_test.rb
reform-2.2.1 test/coercion_test.rb
reform-2.2.0 test/coercion_test.rb
reform-2.2.0.rc1 test/coercion_test.rb