Class: RR::Space

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/space.rb

Overview

RR::Space.instance is the global state subject for the RR framework.

Defined Under Namespace

Modules: Reader

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpace

Returns a new instance of Space.



24
25
26
27
28
29
# File 'lib/rr/space.rb', line 24

def initialize
  @double_injections = HashWithObjectIdKey.new
  @ordered_doubles = []
  @trim_backtrace = false
  @recorded_calls = RR::RecordedCalls.new
end

Class Attribute Details

.instanceObject



11
12
13
# File 'lib/rr/space.rb', line 11

def instance
  @instance ||= new
end

Instance Attribute Details

#double_injectionsObject (readonly)

Returns the value of attribute double_injections.



22
23
24
# File 'lib/rr/space.rb', line 22

def double_injections
  @double_injections
end

#ordered_doublesObject (readonly)

Returns the value of attribute ordered_doubles.



22
23
24
# File 'lib/rr/space.rb', line 22

def ordered_doubles
  @ordered_doubles
end

#recorded_callsObject (readonly)

Returns the value of attribute recorded_calls.



22
23
24
# File 'lib/rr/space.rb', line 22

def recorded_calls
  @recorded_calls
end

#trim_backtraceObject

Returns the value of attribute trim_backtrace.



23
24
25
# File 'lib/rr/space.rb', line 23

def trim_backtrace
  @trim_backtrace
end

Instance Method Details

#double_injection(subject, method_name) ⇒ Object

Reuses or creates, if none exists, a DoubleInjection for the passed in subject and method_name. When a DoubleInjection is created, it binds the dispatcher to the subject.



35
36
37
38
39
# File 'lib/rr/space.rb', line 35

def double_injection(subject, method_name)
  @double_injections[subject][method_name.to_sym] ||= begin
    DoubleInjection.new(subject, method_name.to_sym, (class << subject; self; end)).bind
  end
end

#double_injection_exists?(subject, method_name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rr/space.rb', line 41

def double_injection_exists?(subject, method_name)
  @double_injections.include?(subject) && @double_injections[subject].include?(method_name.to_sym)
end

#record_call(subject, method_name, arguments, block) ⇒ Object



100
101
102
# File 'lib/rr/space.rb', line 100

def record_call(subject, method_name, arguments, block)
  @recorded_calls << [subject, method_name, arguments, block]
end

#register_ordered_double(double) ⇒ Object

Registers the ordered Double to be verified.



46
47
48
# File 'lib/rr/space.rb', line 46

def register_ordered_double(double)
  @ordered_doubles << double unless ordered_doubles.include?(double)
end

#resetObject

Resets the registered Doubles and ordered Doubles



80
81
82
83
84
# File 'lib/rr/space.rb', line 80

def reset
  reset_ordered_doubles
  reset_double_injections
  reset_recorded_calls
end

#reset_double(subject, method_name) ⇒ Object

Resets the DoubleInjection for the passed in subject and method_name.



94
95
96
97
98
# File 'lib/rr/space.rb', line 94

def reset_double(subject, method_name)
  double_injection = @double_injections[subject].delete(method_name)
  @double_injections.delete(subject) if @double_injections[subject].empty?
  double_injection.reset
end

#verify_double(subject, method_name) ⇒ Object

Verifies the DoubleInjection for the passed in subject and method_name.



87
88
89
90
91
# File 'lib/rr/space.rb', line 87

def verify_double(subject, method_name)
  @double_injections[subject][method_name].verify
ensure
  reset_double subject, method_name
end

#verify_doubles(*objects) ⇒ Object Also known as: verify

Verifies all the DoubleInjection objects have met their TimesCalledExpectations.



69
70
71
72
73
74
75
76
# File 'lib/rr/space.rb', line 69

def verify_doubles(*objects)
  objects = @double_injections.keys if objects.empty?
  objects.each do |subject|
    @double_injections[subject].keys.each do |method_name|
      verify_double(subject, method_name)
    end
  end
end

#verify_ordered_double(double) ⇒ Object

Verifies that the passed in ordered Double is being called in the correct position.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rr/space.rb', line 52

def verify_ordered_double(double)
  unless double.terminal?
    raise Errors::DoubleOrderError,
          "Ordered Doubles cannot have a NonTerminal TimesCalledExpectation"
  end
  unless @ordered_doubles.first == double
    message = Double.formatted_name(double.method_name, double.expected_arguments)
    message << " called out of order in list\n"
    message << Double.list_message_part(@ordered_doubles)
    raise Errors::DoubleOrderError, message
  end
  @ordered_doubles.shift unless double.attempt?
  double
end