Sha256: 30e8127165c17bb26f00d5c33de1b3f3f98251b394e57390750ae101c9ba1fae

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Bogus
  class Interaction < Struct.new(:method, :args, :return_value, :error, :has_result)
    def initialize(method, args, &block)
      self.method = method
      self.args = args

      if block_given?
        evaluate_return_value(block)
        self.has_result = true
      end
    end

    def ==(other)
      method == other.method && same_args?(other) && same_result?(other)
    end

    def any_args?
      [AnyArgs] == args
    end

    def args
      super.reject { |arg| arg.eql?(Bogus::DefaultValue) }
    end

    private

    def same_args?(other)
      return true if any_args? || other.any_args?
      return false unless args.size == other.args.size
      args.zip(other.args).all?{|a1, a2| a1 == a2 || a2 == a1}
    end

    def same_result?(other)
      return true unless has_result && other.has_result
      return_value == other.return_value && error == other.error
    end

    def evaluate_return_value(block)
      self.return_value = block.call
    rescue => e
      self.error = e.class
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bogus-0.0.4 lib/bogus/interaction.rb