Sha256: 5c8ac0a8786c085a60732949c9d61f4a45a29848d4c57155d12f4b4281dc8bf2

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

require 'spec_helper'

describe Id::Coercion do

  it 'can coerce basic types' do
    coerced = Id::Coercion.coerce("2", Integer)
    expect(coerced).to eq 2
  end

  it 'can coerce strings into dates' do
    coerced = Id::Coercion.coerce("01/01/2001", Date)
    expect(coerced).to eq Date.new(2001, 1, 1)
  end

  it 'can coerce strings into times' do
    coerced = Id::Coercion.coerce("01/01/2001", Time)
    expect(coerced).to eq Time.new(2001, 1, 1)
  end

  it 'can coerce strings into booleans' do
    coerced = Id::Coercion.coerce("yes", Id::Boolean)
    expect(coerced).to be_true
  end

  it 'can have new coercions added, even stupid ones' do
    Id::Coercion.register(Fixnum, String) { |value| (value + 1).to_s }
    coerced = Id::Coercion.coerce(2, String)
    expect(coerced).to eq "3"
  end

  it 'throws an error if there is no registered coercion for the given types' do
    expect { Id::Coercion.coerce(2, Proc) }.to raise_error Id::CoercionError
  end

  it 'can coerce data into id models' do
    c = Class.new { include Id::Model ; field :cats }
    coerced = Id::Coercion.coerce({cats: 3}, c)
    expect(coerced.cats).to eq 3
  end

  it 'can coerce arrays of a type' do
    c = Class.new { include Id::Model }
    coerced = Id::Coercion.coerce([{},{},{}], Array[c])
    expect(coerced).to have(3).items
    coerced.each { |item| expect(item).to be_a c }
  end

  it 'can coerce optional types' do
    c = Class.new { include Id::Model; field :foot, optional: true, type: Integer }
    expect(c.new.foot).to be_none
    expect(c.new(foot: '4').foot).to eq Some[4]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
id-0.1.1 spec/lib/id/coercion_spec.rb
id-0.1 spec/lib/id/coercion_spec.rb