Sha256: 7443c2c7dcda9343998992ce3435290a218d552aa6ef03ead7378b88827e50a6

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

class BCDD::Result::Context
  class Expectations
    require_relative 'expectations/mixin'

    def self.mixin(success: nil, failure: nil, with: nil)
      addons = Mixin::Addons.options(with)

      mod = ::BCDD::Result::Expectations::Mixin.module!
      mod.const_set(:Result, new(success: success, failure: failure).freeze)
      mod.module_eval(Mixin::METHODS)
      mod.send(:include, *addons) unless addons.empty?
      mod
    end

    def initialize(subject: nil, success: nil, failure: nil, contract: nil)
      @subject = subject

      @contract = contract if contract.is_a?(::BCDD::Result::Contract::Evaluator)

      @contract ||= ::BCDD::Result::Contract.new(success: success, failure: failure).freeze
    end

    def Success(type, **value)
      Success.new(type: type, value: value, subject: subject, expectations: contract)
    end

    def Failure(type, **value)
      Failure.new(type: type, value: value, subject: subject, expectations: contract)
    end

    def with(subject:)
      self.class.new(subject: subject, contract: contract)
    end

    private

    attr_reader :subject, :contract
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcdd-result-0.7.0 lib/bcdd/result/context/expectations.rb