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.



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

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.



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

def ordered_doubles
  @ordered_doubles
end

#recorded_callsObject (readonly)

Returns the value of attribute recorded_calls.



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

def recorded_calls
  @recorded_calls
end

#trim_backtraceObject

Returns the value of attribute trim_backtrace.



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

def trim_backtrace
  @trim_backtrace
end

Instance Method Details

#blank_slate_whitelistObject



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

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



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

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.



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

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

#resetObject

Resets the registered Doubles and ordered Doubles



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

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.



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

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.



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

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.



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

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.



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

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