Sha256: 222703c0cbbd54148530ed0b99546bc91c84c0d401d1b7daa8bf5684a963e3a9

Contents?: true

Size: 1.81 KB

Versions: 12

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

RSpec.describe 'Mapper Hash' do
  link :build_mapper_factory, from: :ree_mapper
  link :build_mapper_strategy, from: :ree_mapper

  let(:mapper_factory) {
    build_mapper_factory(
      strategies: [
        build_mapper_strategy(method: :cast, dto: Hash)
      ]
    )
  }

  let(:mapper) {
    mapper_factory.call.use(:cast) {
      hash :point do
        integer :x
        integer :y
      end
    }
  }

  describe '#cast' do
    it {
      expect(mapper.cast({ point: { x: 1, y: 1 } })).to eq({ point: { x: 1, y: 1 } })
    }

    it {
      expect { mapper.cast({ point: 1 }) }.to raise_error(ReeMapper::TypeError, /`point\[x\]` is missing required field/)
    }

    it {
      expect { mapper.cast({ point: { x: 1, y: 'not integer' } }) }.to raise_error(ReeMapper::CoercionError, /`point\[y\]` is invalid integer, got `"not integer"`/)
    }
  end

  describe 'dto: option' do
    it {
      expect(
        mapper_factory.call.use(:cast) {
          hash :point, dto: OpenStruct do
            integer :x
            integer :y
          end
        }.cast({ point: { x: 1, y: 1 } })
      ).to eq({ point: OpenStruct.new({ x: 1, y: 1 }) })
    }
  end

  describe 'parent strategy object output' do
    let(:mapper_factory) {
      build_mapper_factory(
        strategies: [
          build_mapper_strategy(method: :cast, dto: Object)
        ]
      )
    }

    let(:mapper) {
      mapper_factory.call.use(:cast, dto: dto) {
        hash :point do
          integer :x
          integer :y
        end
      }
    }

    let(:dto) {
      Class.new do
        def ==(other)
          instance_variables == other.instance_variables
        end
      end
    }

    it {
      expect(mapper.cast({ point: { x: 1, y: 1 } })).to eq(Object.new.tap { _1.instance_exec { @point = { x: 1, y: 1 } } })
    }
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ree_lib-1.0.105 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.104 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.103 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.102 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.101 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.100 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.99 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.98 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.97 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.96 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.95 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb
ree_lib-1.0.94 lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb