Class: ObjectProtocol

Inherits:
Object
  • Object
show all
Defined in:
lib/object_protocol.rb,
lib/object_protocol/version.rb,
lib/object_protocol/stand_in.rb,
lib/object_protocol/execution.rb,
lib/object_protocol/sent_message.rb,
lib/object_protocol/message_expectation.rb,
lib/object_protocol/satisfaction_attempt.rb,
lib/object_protocol/satisfiable_message_expectation.rb,
lib/object_protocol/unordered_message_sequence_expectation.rb,
lib/object_protocol/satisfiable_unordered_message_sequence_expectation.rb

Defined Under Namespace

Modules: RSpecMatchers Classes: Execution, MessageExpectation, SatisfactionAttempt, SatisfiableMessageExpectation, SatisfiableUnorderedMessageSequenceExpectation, SentMessage, StandIn, UnorderedMessageSequenceExpectation

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*participant_names, &expectations) ⇒ ObjectProtocol



9
10
11
12
13
14
15
# File 'lib/object_protocol.rb', line 9

def initialize(*participant_names, &expectations)
  @participant_names = participant_names.map(&:to_sym)

  participant_names.each(&method(:define_stand_in))
  instance_exec(&expectations)
  participant_names.each(&method(:undefine_stand_in))
end

Instance Attribute Details

#participants_by_nameObject (readonly)

Returns the value of attribute participants_by_name.



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

def participants_by_name
  @participants_by_name
end

Instance Method Details

#add_expectation(expectation) ⇒ Object



57
58
59
# File 'lib/object_protocol.rb', line 57

def add_expectation(expectation)
  expectation_sequence_stack.last.expectations << expectation
end

#bind(**participants_by_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/object_protocol.rb', line 26

def bind(**participants_by_name)
  bind_attempt_participant_names = participants_by_name.keys.map(&:to_sym)

  missing_participant_names = participant_names - bind_attempt_participant_names
  extra_participant_names   = bind_attempt_participant_names - participant_names

  if missing_participant_names.empty? && extra_participant_names.empty?
    @participants_by_name = participants_by_name

    @participants_by_name.each(&method(:define_participant))

    self
  else
    key_error_message_parts = []

    if missing_participant_names.any?
      key_error_message_parts << "These keys are required by this protocol but weren't provided: #{missing_participant_names.join(', ')}"
    end

    if extra_participant_names.any?
      key_error_message_parts << "These keys aren't used in this protocol but were provided: #{extra_participant_names.join(', ')}"
    end

    raise KeyError, key_error_message_parts.join("\n          ") # makes the second line indent correctly
  end
end

#expectationsObject



61
62
63
# File 'lib/object_protocol.rb', line 61

def expectations
  @expectations ||= []
end

#in_any_order(&expectations) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/object_protocol.rb', line 17

def in_any_order(&expectations)
  unordered_message_sequence_expectation = UnorderedMessageSequenceExpectation.new(protocol: self)
  self.expectations << unordered_message_sequence_expectation

  expectation_sequence_stack.push(unordered_message_sequence_expectation)
  instance_exec(&expectations)
  expectation_sequence_stack.pop
end

#name_of_participant(participant) ⇒ Object



69
70
71
# File 'lib/object_protocol.rb', line 69

def name_of_participant(participant)
  participants_by_name.invert[participant]
end

#participant_by_name(name) ⇒ Object



65
66
67
# File 'lib/object_protocol.rb', line 65

def participant_by_name(name)
  participants_by_name[name]
end

#participantsObject



73
74
75
# File 'lib/object_protocol.rb', line 73

def participants
  participants_by_name.values
end

#satisfied_by?(&blk) ⇒ Boolean



53
54
55
# File 'lib/object_protocol.rb', line 53

def satisfied_by?(&blk)
  SatisfactionAttempt.new(self, &blk).to_bool
end

#to_rspec_matcher_failure_message_linesObject



77
78
79
# File 'lib/object_protocol.rb', line 77

def to_rspec_matcher_failure_message_lines
  expectations.flat_map(&:to_rspec_matcher_failure_message_lines)
end