Sha256: 2b8fd5f735ba23f6dc4d4d7af9c8d782ecbf615eabd34b6e686f074ce93c79a6

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

describe Sinclair::Caster do
  subject(:caster) { caster_class.new(&method_name) }

  let(:caster_class) { Class.new(described_class) }

  describe '.cast' do
    context 'when no options are given and the block accepts none' do
      let(:method_name) { :to_s }

      it 'uses the block to transform the value' do
        expect(caster.cast(10)).to eq('10')
      end
    end

    context 'when options are given and the block accepts none' do
      let(:method_name) { :to_i }

      it 'uses the block to transform the value' do
        expect(caster.cast('10', extra: true)).to eq(10)
      end
    end

    context 'when no options are given and the block accepts options' do
      subject(:caster) do
        caster_class.new do |value, sum: 5|
          (value.to_i + sum).to_s
        end
      end

      it 'uses the block to transform the value' do
        expect(caster.cast('10')).to eq('15')
      end
    end

    context 'when options are given and the block accepts options' do
      subject(:caster) do
        caster_class.new do |value, sum:|
          (value.to_i + sum).to_s
        end
      end

      it 'uses the options in the block' do
        expect(caster.cast('10', sum: 5)).to eq('15')
      end
    end

    context 'when no options are given and the block requires options' do
      subject(:caster) do
        caster_class.new do |value, sum:|
          (value.to_i + sum).to_s
        end
      end

      it do
        expect { caster.cast('10') }.to raise_error(ArgumentError)
      end
    end

    context 'when extra options are given and the block accepts options' do
      subject(:caster) do
        caster_class.new do |value, sum:|
          (value.to_i + sum).to_s
        end
      end

      it 'ignores extra options' do
        expect(caster.cast('10', sum: 5, extra: true)).to eq('15')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sinclair-1.16.1 spec/lib/sinclair/caster_spec.rb
sinclair-1.16.0 spec/lib/sinclair/caster_spec.rb
sinclair-1.15.0 spec/lib/sinclair/caster_spec.rb