Sha256: 7efd110c25e8844514d789193d7c1998d6775c46c975dea01bd76070520c6454

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: false

require 'spec_helper'

describe 'yard Sinclair::Caster.cast_with' do
  let(:my_caster) { Class.new(Sinclair::Caster) }

  it 'Casting from pre registered symbol caster' do
    my_caster.cast_with(:json, :to_json)

    expect(my_caster.cast({ key: :value }, :json)).to eq('{"key":"value"}')
  end

  it 'Casting from pre registered block caster' do
    my_caster.cast_with(:complex) do |hash|
      real = hash[:real]
      imaginary = hash[:imaginary]

      "#{real.to_f} + #{imaginary.to_f} i"
    end

    value = { real: 10, imaginary: 5 }

    expect(my_caster.cast(value, :complex)).to eq('10.0 + 5.0 i')
  end

  it 'Casting from pre registered class' do
    my_caster.cast_with(Numeric, :to_i)

    expect(my_caster.cast('10', Integer)).to eq(10)
  end

  it 'Casting from pre registered block caster from a class' do
    my_caster.cast_with(HashModel) do |value, klass:|
      klass.new(value)
    end

    my_caster.cast_with(String, &:to_json)

    values = [
      { klass: String, value: { name: 'john', age: 20, country: 'BR' } },
      { klass: HashPerson, value: { name: 'Mary', age: 22, country: 'IT' } }
    ]

    values.map! do |config|
      value = config[:value]
      klass = config[:klass]
      my_caster.cast(value, klass, klass: klass)
    end

    expect(values[0]).to eq('{"name":"john","age":20,"country":"BR"}')
    expect(values[1]).to eq(HashPerson.new(name: 'Mary', age: 22))
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sinclair-2.1.1 spec/integration/yard/sinclair/caster/cast_with_spec.rb
sinclair-2.1.0 spec/integration/yard/sinclair/caster/cast_with_spec.rb
sinclair-2.0.1 spec/integration/yard/sinclair/caster/cast_with_spec.rb
sinclair-2.0.0 spec/integration/yard/sinclair/caster/cast_with_spec.rb
sinclair-1.16.3 spec/integration/yard/sinclair/caster/cast_with_spec.rb
sinclair-1.16.2 spec/integration/yard/sinclair/caster/cast_with_spec.rb
sinclair-1.16.1 spec/integration/yard/sinclair/caster/cast_with_spec.rb
sinclair-1.16.0 spec/integration/yard/sinclair/caster/cast_with_spec.rb
sinclair-1.15.0 spec/integration/yard/sinclair/caster/cast_with_spec.rb