Sha256: 403ea9004ed880836797cdda901837e26d58f0a25e2954f5e7fd955e5d7c6484

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 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
      validate({ number: 1 }, validations).valid?.should be_true
    end

    it 'should validate false when the number is even' do
      validate({ number: 2 }, validations).valid?.should be_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
      validate({ number: '2' }, validations).valid?.should be_false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hash_validator-0.3.0 spec/validators/lambda_spec.rb
hash_validator-0.2.7 spec/validators/lambda_spec.rb
hash_validator-0.2.6 spec/validators/lambda_spec.rb