Class: ObjectProtocol::SatisfactionAttempt

Inherits:
Object
  • Object
show all
Defined in:
lib/object_protocol/satisfaction_attempt.rb

Instance Method Summary collapse

Constructor Details

#initialize(protocol, &blk) ⇒ SatisfactionAttempt

Returns a new instance of SatisfactionAttempt.



6
7
8
9
# File 'lib/object_protocol/satisfaction_attempt.rb', line 6

def initialize(protocol, &blk)
  @protocol = protocol
  @blk      = blk
end

Instance Method Details

#to_boolObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/object_protocol/satisfaction_attempt.rb', line 11

def to_bool
  execution.call(protocol)

  unsatisfied_steps = protocol.steps.map do |step|
    SatisfiableStep.new(protocol: protocol, step: step)
  end

  execution.messages.each do |sent_message|
    next_step = unsatisfied_steps.first

    next_step.attempt_to_apply_sent_message(sent_message)

    if next_step.satisfied?
      unsatisfied_steps.shift
    end
  end

  unsatisfied_steps.empty?
end

#to_rspec_matcher_failure_message_linesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/object_protocol/satisfaction_attempt.rb', line 31

def to_rspec_matcher_failure_message_lines
  return ["<empty execution>"] if execution.messages.empty?

  execution.messages.map do |sent_message|
    fragment_base = "#{protocol.name_of_participant(sent_message.sender)}"\
      ".sent(:#{sent_message.name})"\
      ".to(#{protocol.name_of_participant(sent_message.receiver)})"

    if sent_message.arguments_passed?
      "#{fragment_base}.with(#{sent_message.arguments})"
    else
      fragment_base
    end
  end
end