Sha256: 9ec44b83ccbdcdedd76c5ac1dd3864cb88fe37c65d147c9851a99335c33e4ccd

Contents?: true

Size: 1.8 KB

Versions: 5

Compression:

Stored size: 1.8 KB

Contents

require 'spec_helper'
require 'standalone_validator'

describe "core features" do
  let(:valid_object)   { double(:valid? => true) }
  let(:invalid_object) { double(:valid? => false) }

  it "accepts inline lambdas via 'include_validation'" do
    validator_class = StandaloneValidator.create do
      include_validation do |object, result|
        unless object.valid?
          result.add_violation(:base, "is invalid")
        end
      end
    end

    validator = validator_class.new

    result = validator.violations_of(valid_object)
    expect(result).to be_ok

    result = validator.violations_of(invalid_object)
    expect(result).to_not be_ok
  end

  specify "the methods added to the class are accessible inside the lambda" do
    validator_class = StandaloneValidator.create do
      include_validation do |object, result|
        unless valid?(object)
          result.add_violation(:base, "is invalid")
        end
      end

      def valid?(object)
        object.valid?
      end
    end

    validator = validator_class.new

    result = validator.violations_of(valid_object)
    expect(result).to be_ok

    result = validator.violations_of(invalid_object)
    expect(result).to_not be_ok
  end

  specify "initialization options can be accepted by defining a constructor" do
    validator_class = StandaloneValidator.create do
      def initialize(bias)
        @bias = bias
      end

      include_validation do |object, result|
        unless @bias
          result.add_violation(:base, "is invalid")
        end
      end
    end

    biased_validator = validator_class.new(true)
    result = biased_validator.violations_of(invalid_object)
    expect(result).to be_ok

    biased_validator = validator_class.new(false)
    result = biased_validator.violations_of(valid_object)
    expect(result).to_not be_ok
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
standalone_validator-0.2.3 spec/integration_spec.rb
standalone_validator-0.2.2 spec/integration_spec.rb
standalone_validator-0.2.1 spec/integration_spec.rb
standalone_validator-0.2.0 spec/integration_spec.rb
standalone_validator-0.1.0 spec/integration_spec.rb