Sha256: e73f0160e99fea7e72894f2f886439a8b7f1b573d00c64202657bae343f0f482

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

class BCDD::Result::Expectations
  class Contract::Evaluator
    include Contract::Interface

    attr_reader :allowed_types, :success, :failure

    def initialize(success, failure)
      @success = success
      @failure = failure

      @allowed_types = (success.allowed_types | failure.allowed_types).freeze
    end

    def type?(type)
      success_disabled = success == Contract::Disabled
      failure_disabled = failure == Contract::Disabled

      return Contract::Disabled.type?(type) if success_disabled && failure_disabled

      (!success_disabled && success.type?(type)) || (!failure_disabled && failure.type?(type))
    end

    def type!(type)
      return type if type?(type)

      raise Error::UnexpectedType.build(type: type, allowed_types: allowed_types)
    end

    def type_and_value!(data)
      self.for(data).type_and_value!(data)
    end

    private

    def for(data)
      case data.name
      when :unknown then Contract::Disabled
      when :success then success
      else failure
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bcdd-result-0.6.0 lib/bcdd/result/expectations/contract/evaluator.rb
bcdd-result-0.5.0 lib/bcdd/result/expectations/contract/evaluator.rb