Sha256: 397dabc0b35246aedb3a8f7b5f6ad62a13453a93472302d73a53eff2a9093de6

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

class Fake
  include WannabeBool::Attributes

  attr_reader :main, :published, :migrated
  attr_wannabe_bool :main, :published, :migrated

  def initialize(main, published)
    @main = main
    @published = published
  end

  def migrated?
    # Don't worry about the symbol in return, it is just to help in test expectation.
    :original_method_return
  end
end

describe WannabeBool::Attributes do
  context 'when reader attribute exists' do
    let(:subject_methods) do
      fake = Fake.new(true, true)
      fake.public_methods - Object.methods
    end

    it 'creates the predicate method' do
      expect(subject_methods).to include(:main?)
      expect(subject_methods).to include(:published?)
    end

    [ { type: 'string',  value: 'true' },
      { type: 'integer', value: 1      },
      { type: 'symbol',  value: :true  },
      { type: 'boolean', value: true   }
    ].each do |info|
      context "with a #{info[:type]} value" do
        subject { Fake.new(info[:value], info[:value]) }

        it 'returns original value converted to boolean' do
          expect(subject.main?).to be true
          expect(subject.published?).to be true
        end
      end
    end
  end

  context 'when reader attribute does not exist' do
    it 'raise an ArgumentError' do
      expect { Fake.send(:attr_wannabe_bool, :not_exist) }.to raise_error(ArgumentError, 'not_exist method is not defined.')
    end
  end

  context 'when predicate method exists' do
    subject { Fake.new(true, true) }

    it 'does not overrides the original predicate method' do
      expect(subject.migrated?).to eql :original_method_return
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wannabe_bool-0.5.0 spec/wannabe_bool/attributes_spec.rb
wannabe_bool-0.4.0 spec/wannabe_bool/attributes_spec.rb
wannabe_bool-0.3.0 spec/wannabe_bool/attributes_spec.rb
wannabe_bool-0.2.0 spec/wannabe_bool/attributes_spec.rb