Class: BeComputedByMatcher

Inherits:
Object show all
Defined in:
lib/mspec/matchers/be_computed_by.rb

Instance Method Summary collapse

Constructor Details

#initialize(sym, *args) ⇒ BeComputedByMatcher

Returns a new instance of BeComputedByMatcher.



2
3
4
5
# File 'lib/mspec/matchers/be_computed_by.rb', line 2

def initialize(sym, *args)
  @method = sym
  @args = args
end

Instance Method Details

#failure_messageObject



28
29
30
# File 'lib/mspec/matchers/be_computed_by.rb', line 28

def failure_message
  ["Expected #{@value.inspect}", "to be computed by #{method_call} (computed #{@actual.inspect} instead)"]
end

#matches?(array) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mspec/matchers/be_computed_by.rb', line 7

def matches?(array)
  array.each do |line|
    @receiver = line.shift
    @value = line.pop
    @arguments = line
    @arguments += @args
    @actual = @receiver.send(@method, *@arguments)
    return false unless @actual == @value
  end

  return true
end

#method_callObject



20
21
22
23
24
25
26
# File 'lib/mspec/matchers/be_computed_by.rb', line 20

def method_call
  method_call = "#{@receiver.inspect}.#{@method}"
  unless @arguments.empty?
    method_call << " from #{@arguments.map { |x| x.inspect }.join(", ")}"
  end
  method_call
end