Sha256: d00d3b33dc609692444cf12590666555de21af77cefd63b3c073e7b51129ec0b

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

require 'spec_helper'

describe Coercer::Object, '.to_array' do
  subject { object.to_array(value) }

  let(:object)  { described_class.new }
  let(:value)   { Object.new      }
  let(:coerced) { [ value ]       }

  context 'when the value responds to #to_ary' do
    before do
      value.should_receive(:to_ary).with().and_return(coerced)
    end

    it { should be(coerced) }

    it 'does not call #to_a if #to_ary is available' do
      value.should_not_receive(:to_a)
      subject
    end
  end

  context 'when the value responds to #to_a but not #to_ary' do
    before do
      value.should_receive(:to_a).with().and_return(coerced)
    end

    it { should be(coerced) }
  end

  context 'when the value does not respond to #to_ary or #to_a' do
    it { should be_instance_of(Array) }

    it { should == coerced }
  end

  context 'when the value returns nil from #to_ary' do
    before do
      value.should_receive(:to_ary).with().and_return(nil)
    end

    it 'calls #to_a as a fallback' do
      value.should_receive(:to_a).with().and_return(coerced)
      should be(coerced)
    end

    it 'wraps the value in an Array if #to_a is not available' do
      should == coerced
    end
  end
end

Version data entries

14 entries across 12 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/object/to_array_spec.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/object/to_array_spec.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/object/to_array_spec.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/object/to_array_spec.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/object/to_array_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/object/to_array_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/object/to_array_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/object/to_array_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/object/to_array_spec.rb
coercible-1.0.0 spec/unit/coercible/coercer/object/to_array_spec.rb
coercible-0.2.0 spec/unit/coercible/coercer/object/to_array_spec.rb
coercible-0.1.0 spec/unit/coercible/coercer/object/to_array_spec.rb
coercible-0.0.2 spec/unit/coercible/coercer/object/to_array_spec.rb
coercible-0.0.1 spec/unit/coercible/coercer/object/to_array_spec.rb