Sha256: 792e491ac8b71305b67d07c9c74624a3f662862b54b380ca1f50e8d0f915a9e7

Contents?: true

Size: 1.62 KB

Versions: 9

Compression:

Stored size: 1.62 KB

Contents

require 'spec_helper'

describe HashValidator::Validator::SimpleValidator do
  describe '#initialize' do
    it 'accepts blocks with one argument' do
      expect {
        HashValidator::Validator::SimpleValidator.new('name', lambda { |a| true })
      }.to_not raise_error
    end

    it 'does not accept blocks with no arguments' do
      expect {
        HashValidator::Validator::SimpleValidator.new('name', lambda { true })
      }.to raise_error(StandardError, 'lambda should take only one argument - passed lambda takes 0')
    end

    it 'does not accept blocks with two arguments' do
      expect {
        HashValidator::Validator::SimpleValidator.new('name', lambda { |a,b| true })
      }.to raise_error(StandardError, 'lambda should take only one argument - passed lambda takes 2')
    end
  end

  describe '#validate' do
    # Lambda that accepts strings that are 4 characters or shorter
    let(:short_string_lambda)    { lambda { |v| v.is_a?(String) && v.size < 5 } }
    let(:short_string_validator) { HashValidator::Validator::SimpleValidator.new('short_string', short_string_lambda) }
    let(:errors)                 { Hash.new }

    [ '', '1', '12', '123', '1234' ].each do |value|
      it "validates the string '#{value}'" do
        short_string_validator.validate(:key, value, {}, errors)

        expect(errors).to be_empty
      end
    end

    [ nil, '12345', '123456', 123, 123456, Time.now, (1..5) ].each do |value|
      it "does not validate bad value '#{value}'" do
        short_string_validator.validate(:key, value, {}, errors)

        expect(errors).to eq({ key: 'short_string required' })
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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