Sha256: 7755682d036b74d99f65d270aad8873cc1dbce330ba6e62180a521f247e398da

Contents?: true

Size: 823 Bytes

Versions: 5

Compression:

Stored size: 823 Bytes

Contents

require 'spec_helper'
describe Domain, "coerce" do

  class Point
    extend Domain::Scalar.new(:x, :y)

    coercions do |c|
      c.coercion(String){|v,_| Point.new(*v.split(',').map(&:to_i)) }
      c.coercion(Array) {|v,_| Point.new(*v) }
    end

    EMPTY = Point.new(0, 0)
  end

  let(:point_1_1){ Point.new(1, 1) }
  let(:point_2_2){ Point.new(2, 2) }

  it 'should delegate to coercions' do
    Point.coerce("1, 1").should eq(point_1_1)
  end

  it 'raises a TypeError if something goes wrong' do
    lambda{
      Point.coerce(12)
    }.should raise_error(TypeError, /Can't convert `12`/)
  end

  describe "the [] alias" do

    it 'delegates to coerce when one argument' do
      Point["1,1"].should eq(point_1_1)
    end

    it 'supports Array' do
      Point[[2, 2]].should eq(point_2_2)
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
myrrha-3.1.0 spec/ext/domain/test_coerce.rb
myrrha-3.0.0 spec/ext/domain/test_coerce.rb
myrrha-3.0.0.rc7 spec/ext/domain/test_coerce.rb
myrrha-3.0.0.rc6 spec/ext/domain/test_coerce.rb
myrrha-3.0.0.rc5 spec/ext/domain/test_coerce.rb