Sha256: 602c6d515f6440b9044e134a6fa130a42f2f454ab74cf2cb686823a980c5f14a

Contents?: true

Size: 1.52 KB

Versions: 8

Compression:

Stored size: 1.52 KB

Contents

require 'spec_helper'

describe 'Functional validator' do
  describe 'Accepting Lambdas in validations' do
    it 'should accept a lambda' do
      validate({}, { foo: lambda { |arg| } })
    end

    it 'should accept a proc' do
      validate({}, { foo: Proc.new { |arg| } })
    end
  end

  describe 'Correct number of arguments for lambads in validations' do
    it 'should accept a lambda with one argument' do
      expect { validate({}, { foo: lambda { |a| } }) }.to_not raise_error
    end

    it 'should not accept a lambda with no arguments' do
      expect { validate({}, { foo: lambda { } }) }.to raise_error(HashValidator::Validator::LambdaValidator::InvalidArgumentCount)
    end

    it 'should not accept a lambda with two arguments' do
      expect { validate({}, { foo: lambda { |a,b| } }) }.to raise_error(HashValidator::Validator::LambdaValidator::InvalidArgumentCount)
    end
  end

  describe '#validate' do
    let(:validations) {{ number: lambda { |n| n.odd? } }}

    it 'should validate true when the number is odd' do
      expect(validate({ number: 1 }, validations).valid?).to eq true
    end

    it 'should validate false when the number is even' do
      expect(validate({ number: 2 }, validations).valid?).to eq false
    end
  end

  describe 'Thrown exceptions from within the lambda' do
    let(:validations) {{ number: lambda { |n| n.odd? } }}

    it 'should validate false when an exception occurs within the lambda' do
      expect(validate({ number: '2' }, validations).valid?).to eq false
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hash_validator-1.1.0 spec/validators/lambda_spec.rb
hash_validator-1.0.0 spec/validators/lambda_spec.rb
hash_validator-0.8.0 spec/validators/lambda_spec.rb
hash_validator-0.7.1 spec/validators/lambda_spec.rb
hash_validator-0.7.0 spec/validators/lambda_spec.rb
hash_validator-0.6.0 spec/validators/lambda_spec.rb
hash_validator-0.5.0 spec/validators/lambda_spec.rb
hash_validator-0.4.0 spec/validators/lambda_spec.rb