Sha256: 4e47078cabb9ab9d5e91e179e3c3afdca228bd7aa2c477e9bb525cfaa69e372f

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

require 'spec_helper'

describe 'Enumerable validator' do
  describe 'Accepting Enumerables in validations' do
    it 'should accept an empty array' do
      validate({}, { foo: [] })
    end

    it 'should accept an array' do
      validate({}, { foo: [ 'apple', 'banana', 'carrot' ] })
    end

    it 'should accept a hash' do
      validate({}, { foo: { apple: 'apple', banana: 'banana', carrot: 'cattot' } })
    end
  end

  describe '#validate' do
    describe 'Simple Array' do
      let(:validations) {{ fruit: [ 'apple', 'banana', 'carrot' ] }}

      it 'should validate true if the value is present' do
        validate({ fruit: 'apple' }, validations).valid?.should be_true
      end

      it 'should validate false if the value is not present' do
        validate({ fruit: 'pear' }, validations).valid?.should be_false
      end

      it 'should validate false if the key is not present' do
        validate({ something: 'apple' }, validations).valid?.should be_false
      end

      it 'should provide an appropriate error message is the value is not present' do
        validate({ fruit: 'pear' }, validations).errors.should eq({ fruit: 'value from list required' })
      end
    end

    describe 'Range' do
      let(:validations) {{ number: 1..10 }}

      it 'should validate true if the value is present' do
        validate({ number: 5 }, validations).valid?.should be_true
      end
      it 'should validate false if the value is not present' do
        validate({ number: 15 }, validations).valid?.should be_false
      end
    end

    describe 'Infinite Range' do
      let(:validations) {{ number: 1..Float::INFINITY }}

      it 'should validate true if the value is present' do
        validate({ number: 5 }, validations).valid?.should be_true
      end
      it 'should validate false if the value is not present' do
        validate({ number: -5 }, validations).valid?.should be_false
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hash_validator-0.2.6 spec/validators/in_enumerable_spec.rb
hash_validator-0.2.5 spec/validators/in_enumerable_spec.rb
hash_validator-0.2.4 spec/validators/in_enumerable_spec.rb