Sha256: 21e653db62dbdadc7e8b35248aa397fa5d37dc52d8ea505d525146e84fbfb9c8

Contents?: true

Size: 1.87 KB

Versions: 4

Compression:

Stored size: 1.87 KB

Contents

require 'minitest_helper'

describe Rasti::Types::Hash do

  it "{'a' => '123'} -> {a: 123}" do
    Rasti::Types::Hash[Rasti::Types::Symbol, Rasti::Types::Integer].cast('a' => '123').must_equal a: 123
  end

  it "{'1' => :abc} -> {1 => 'abc'}" do
    Rasti::Types::Hash[Rasti::Types::Integer, Rasti::Types::String].cast('1' => :abc).must_equal 1 => 'abc'
  end

  [nil, 1, 'text', :symbol, Object.new].each do |value|
    it "#{value.inspect} -> CastError" do
      error = proc { Rasti::Types::Hash[Rasti::Types::Symbol, Rasti::Types::Integer].cast(value) }.must_raise Rasti::Types::CastError
      error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Hash[Rasti::Types::Symbol, Rasti::Types::Integer]"
    end
  end

  describe 'Multi cast errors' do

    it 'Simple types' do
      value = {
        true => 1,
        2 => false
      }

      error = proc { Rasti::Types::Hash[Rasti::Types::Integer, Rasti::Types::Integer].cast(value) }.must_raise Rasti::Types::MultiCastError

      error.errors.must_equal true => ['Invalid cast: true -> Rasti::Types::Integer'],
                              2 => ['Invalid cast: false -> Rasti::Types::Integer']

      error.message.must_equal "Cast errors:\n- true: [\"Invalid cast: true -> Rasti::Types::Integer\"]\n- 2: [\"Invalid cast: false -> Rasti::Types::Integer\"]"
    end

    it 'Models' do
      value = {
        'a' => {x: 1, y: 2},
        'b' => {x: 1},
        'c' => true
      }

      error = proc { Rasti::Types::Hash[Rasti::Types::String, Rasti::Types::Model[Point]].cast(value) }.must_raise Rasti::Types::MultiCastError

      error.errors.must_equal 'b.y' => ['not present'],
                              'c' => ['Invalid cast: true -> Rasti::Types::Model[Point]']

      error.message.must_equal "Cast errors:\n- b.y: [\"not present\"]\n- c: [\"Invalid cast: true -> Rasti::Types::Model[Point]\"]"
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rasti-types-1.1.2 spec/hash_spec.rb
rasti-types-1.1.1 spec/hash_spec.rb
rasti-types-1.1.0 spec/hash_spec.rb
rasti-types-1.0.0 spec/hash_spec.rb