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.



31
32
33
34
35
# File 'lib/rr/space.rb', line 31

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (protected)



18
19
20
# File 'lib/rr/space.rb', line 18

def method_missing(method_name, *args, **kwargs, &block)
  instance.__send__(method_name, *args, **kwargs, &block)
end

Class Attribute Details

.instanceObject



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

def instance
  @instance ||= new
end

Instance Attribute Details

#ordered_doublesObject (readonly)

Returns the value of attribute ordered_doubles.



28
29
30
# File 'lib/rr/space.rb', line 28

def ordered_doubles
  @ordered_doubles
end

#recorded_callsObject (readonly)

Returns the value of attribute recorded_calls.



28
29
30
# File 'lib/rr/space.rb', line 28

def recorded_calls
  @recorded_calls
end

#trim_backtraceObject

Returns the value of attribute trim_backtrace.



29
30
31
# File 'lib/rr/space.rb', line 29

def trim_backtrace
  @trim_backtrace
end

Instance Method Details

#blank_slate_whitelistObject



96
97
98
99
100
# File 'lib/rr/space.rb', line 96

def blank_slate_whitelist
  @blank_slate_whitelist ||= [
    "object_id", "respond_to?", "respond_to_missing?", "method_missing", "instance_eval", "instance_exec", "class_eval"
  ]
end

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



88
89
90
91
92
93
94
# File 'lib/rr/space.rb', line 88

def record_call(subject, method_name, arguments, keyword_arguments, block)
  @recorded_calls.add(subject,
                      method_name,
                      arguments,
                      keyword_arguments,
                      block)
end

#register_ordered_double(double) ⇒ Object

Registers the ordered Double to be verified.



38
39
40
# File 'lib/rr/space.rb', line 38

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

#resetObject

Resets the registered Doubles and ordered Doubles



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

def reset
  RR.trim_backtrace = false
  RR.overridden_error_class = nil
  reset_ordered_doubles
  Injections::DoubleInjection.reset
  reset_method_missing_injections
  reset_singleton_method_added_injections
  reset_recorded_calls
  reset_bound_objects
end

#reset_double(subject, method_name) ⇒ Object

Resets the DoubleInjection for the passed in subject and method_name.



84
85
86
# File 'lib/rr/space.rb', line 84

def reset_double(subject, method_name)
  Injections::DoubleInjection.reset_double(class << subject; self; end, method_name)
end

#verify_double(subject, method_name) ⇒ Object

Verifies the DoubleInjection for the passed in subject and method_name.



79
80
81
# File 'lib/rr/space.rb', line 79

def verify_double(subject, method_name)
  Injections::DoubleInjection.verify_double(class << subject; self; end, method_name)
end

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

Verifies all the DoubleInjection objects have met their TimesCalledExpectations.



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

def verify_doubles(*subjects)
  Injections::DoubleInjection.verify(*subjects)
end

#verify_ordered_double(double) ⇒ Object

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rr/space.rb', line 44

def verify_ordered_double(double)
  unless double.terminal?
    raise RR::Errors.build_error(: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 RR::Errors.build_error(:DoubleOrderError, message)
  end
  @ordered_doubles.shift unless double.attempt?
  double
end