Sha256: 0804e59338b602ab8f2621a7d51b3a663c3b8a837bf610b91f9ba45844fc522f

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 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 lambda (new syntax)' do
      validate({}, { foo: -> (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: -> (arg) {} }) }.to_not raise_error
    end

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

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

  describe '#validate' do
    let(:validations) {{ number: -> (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: -> (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

1 entries across 1 versions & 1 rubygems

Version Path
hash_validator-0.2.5 spec/validators/lambda_spec.rb