Class: Compact::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/compact/contract.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContract

Returns a new instance of Contract.



8
9
10
11
# File 'lib/compact/contract.rb', line 8

def initialize
  @collaborator_invocations = Set.new
  @test_double_invocations = Set.new
end

Instance Attribute Details

#specsObject (readonly)

Returns the value of attribute specs.



6
7
8
# File 'lib/compact/contract.rb', line 6

def specs
  @specs
end

Instance Method Details

#add_invocation(invocation) ⇒ Object



64
65
66
# File 'lib/compact/contract.rb', line 64

def add_invocation(invocation)
  @test_double_invocations.add(invocation)
end

#describe_failing_specsObject



48
49
50
51
52
53
54
55
56
# File 'lib/compact/contract.rb', line 48

def describe_failing_specs
  headline = "Attempts to verify the following method invocations failed:"
  messages = failing_invocations.map do |invocation|
    bad_results = unspecified_invocations.select{|p| p.matches_call(invocation) }
    invocation.describe.gsub("returns", "expected") +
        "\nMatching invocations returned the following values: #{bad_results.map(&:returns).inspect}"
  end
  print_banner_separated(headline, messages)
end

#describe_pending_specsObject



42
43
44
45
46
# File 'lib/compact/contract.rb', line 42

def describe_pending_specs
  headline = "No test doubles mirror the following verified invocations:"
  messages = pending_invocations.map(&:describe)
  print_banner_separated(headline, messages)
end

#describe_untested_specsObject



36
37
38
39
40
# File 'lib/compact/contract.rb', line 36

def describe_untested_specs
  headline = "The following methods were invoked on test doubles without corresponding contract tests:"
  messages = untested_invocations.map(&:describe)
  print_banner_separated(headline, messages)
end

#has_failing?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/compact/contract.rb', line 32

def has_failing?
  !failing_invocations.empty?
end

#has_pending?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/compact/contract.rb', line 24

def has_pending?
  !pending_invocations.empty?
end

#has_untested?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/compact/contract.rb', line 28

def has_untested?
  !untested_invocations.empty?
end

#prepare_double(block = Proc.new) ⇒ Object



13
14
15
16
17
18
# File 'lib/compact/contract.rb', line 13

def prepare_double(block = Proc.new)
  double = block.call
  interceptor = ArgumentInterceptor.new(double)
  interceptor.register(self)
  interceptor
end

#verified?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/compact/contract.rb', line 20

def verified?
  @test_double_invocations == @collaborator_invocations
end

#verify(collaborator, block = Proc.new) ⇒ Object



58
59
60
61
62
# File 'lib/compact/contract.rb', line 58

def verify(collaborator, block = Proc.new)
  interceptor = ArgumentInterceptor.new(collaborator)
  block.call(interceptor)
  interceptor.invocations.each{|inv| @collaborator_invocations.add(inv) }
end