Sha256: 4559edd7a2d6b70a6de28526037186d92acb2386f374e9acc73c958ab875f260

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

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

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

  class Form < TestForm
    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 { assert_nil subject.hit.good }
    it { subject.band.label.value.must_equal "9999.999999.99" } # coercion happened once.
  end

  # save
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reform-2.3.0.rc1 test/coercion_test.rb