opal-rspec 0.4.0.beta3 → 0.4.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.gitmodules +15 -0
  4. data/.travis.yml +12 -0
  5. data/.yardopts +5 -0
  6. data/CHANGELOG.md +3 -1
  7. data/Gemfile +6 -7
  8. data/README.md +2 -0
  9. data/Rakefile +12 -50
  10. data/lib/opal/rspec/version.rb +1 -1
  11. data/lib/opal/rspec.rb +14 -0
  12. data/opal/opal/rspec/async.rb +146 -11
  13. data/opal/opal/rspec/fixes.rb +18 -8
  14. data/opal/opal/rspec/requires.rb +45 -0
  15. data/opal/opal/rspec.rb +1 -24
  16. data/opal-rspec.gemspec +1 -1
  17. data/spec/async_spec.rb +4 -5
  18. data/spec/matchers_spec.rb +20 -0
  19. data/spec/named_subject_spec.rb +11 -0
  20. data/spec/should_syntax_spec.rb +17 -0
  21. data/vendor_lib/rspec/autorun.rb +2 -0
  22. data/vendor_lib/rspec/core/backport_random.rb +302 -0
  23. data/vendor_lib/rspec/core/backtrace_formatter.rb +65 -0
  24. data/vendor_lib/rspec/core/command_line.rb +36 -0
  25. data/vendor_lib/rspec/core/configuration.rb +1129 -0
  26. data/vendor_lib/rspec/core/configuration_options.rb +143 -0
  27. data/vendor_lib/rspec/core/drb_command_line.rb +26 -0
  28. data/vendor_lib/rspec/core/drb_options.rb +87 -0
  29. data/vendor_lib/rspec/core/dsl.rb +26 -0
  30. data/vendor_lib/rspec/core/example.rb +312 -0
  31. data/vendor_lib/rspec/core/example_group.rb +540 -0
  32. data/vendor_lib/rspec/core/filter_manager.rb +224 -0
  33. data/vendor_lib/rspec/core/flat_map.rb +17 -0
  34. data/vendor_lib/rspec/core/formatters/base_formatter.rb +291 -0
  35. data/vendor_lib/rspec/core/formatters/base_text_formatter.rb +307 -0
  36. data/vendor_lib/rspec/core/formatters/deprecation_formatter.rb +193 -0
  37. data/vendor_lib/rspec/core/formatters/documentation_formatter.rb +67 -0
  38. data/vendor_lib/rspec/core/formatters/helpers.rb +82 -0
  39. data/vendor_lib/rspec/core/formatters/html_formatter.rb +155 -0
  40. data/vendor_lib/rspec/core/formatters/html_printer.rb +408 -0
  41. data/vendor_lib/rspec/core/formatters/json_formatter.rb +99 -0
  42. data/vendor_lib/rspec/core/formatters/progress_formatter.rb +32 -0
  43. data/vendor_lib/rspec/core/formatters/snippet_extractor.rb +101 -0
  44. data/vendor_lib/rspec/core/formatters.rb +54 -0
  45. data/vendor_lib/rspec/core/hooks.rb +535 -0
  46. data/vendor_lib/rspec/core/memoized_helpers.rb +431 -0
  47. data/vendor_lib/rspec/core/metadata.rb +313 -0
  48. data/vendor_lib/rspec/core/mocking/with_absolutely_nothing.rb +11 -0
  49. data/vendor_lib/rspec/core/mocking/with_flexmock.rb +27 -0
  50. data/vendor_lib/rspec/core/mocking/with_mocha.rb +52 -0
  51. data/vendor_lib/rspec/core/mocking/with_rr.rb +27 -0
  52. data/vendor_lib/rspec/core/mocking/with_rspec.rb +27 -0
  53. data/vendor_lib/rspec/core/option_parser.rb +234 -0
  54. data/vendor_lib/rspec/core/ordering.rb +154 -0
  55. data/vendor_lib/rspec/core/pending.rb +110 -0
  56. data/vendor_lib/rspec/core/project_initializer.rb +88 -0
  57. data/vendor_lib/rspec/core/rake_task.rb +128 -0
  58. data/vendor_lib/rspec/core/reporter.rb +132 -0
  59. data/vendor_lib/rspec/core/ruby_project.rb +44 -0
  60. data/vendor_lib/rspec/core/runner.rb +97 -0
  61. data/vendor_lib/rspec/core/shared_context.rb +53 -0
  62. data/vendor_lib/rspec/core/shared_example_group/collection.rb +27 -0
  63. data/vendor_lib/rspec/core/shared_example_group.rb +146 -0
  64. data/vendor_lib/rspec/core/version.rb +7 -0
  65. data/vendor_lib/rspec/core/warnings.rb +22 -0
  66. data/vendor_lib/rspec/core/world.rb +131 -0
  67. data/vendor_lib/rspec/core.rb +203 -0
  68. data/vendor_lib/rspec/expectations/differ.rb +154 -0
  69. data/vendor_lib/rspec/expectations/errors.rb +9 -0
  70. data/vendor_lib/rspec/expectations/expectation_target.rb +87 -0
  71. data/vendor_lib/rspec/expectations/extensions/object.rb +29 -0
  72. data/vendor_lib/rspec/expectations/extensions.rb +1 -0
  73. data/vendor_lib/rspec/expectations/fail_with.rb +79 -0
  74. data/vendor_lib/rspec/expectations/handler.rb +68 -0
  75. data/vendor_lib/rspec/expectations/syntax.rb +182 -0
  76. data/vendor_lib/rspec/expectations/version.rb +8 -0
  77. data/vendor_lib/rspec/expectations.rb +75 -0
  78. data/vendor_lib/rspec/matchers/built_in/base_matcher.rb +68 -0
  79. data/vendor_lib/rspec/matchers/built_in/be.rb +213 -0
  80. data/vendor_lib/rspec/matchers/built_in/be_instance_of.rb +15 -0
  81. data/vendor_lib/rspec/matchers/built_in/be_kind_of.rb +11 -0
  82. data/vendor_lib/rspec/matchers/built_in/be_within.rb +55 -0
  83. data/vendor_lib/rspec/matchers/built_in/change.rb +141 -0
  84. data/vendor_lib/rspec/matchers/built_in/cover.rb +21 -0
  85. data/vendor_lib/rspec/matchers/built_in/eq.rb +22 -0
  86. data/vendor_lib/rspec/matchers/built_in/eql.rb +23 -0
  87. data/vendor_lib/rspec/matchers/built_in/equal.rb +48 -0
  88. data/vendor_lib/rspec/matchers/built_in/exist.rb +26 -0
  89. data/vendor_lib/rspec/matchers/built_in/has.rb +48 -0
  90. data/vendor_lib/rspec/matchers/built_in/include.rb +61 -0
  91. data/vendor_lib/rspec/matchers/built_in/match.rb +17 -0
  92. data/vendor_lib/rspec/matchers/built_in/match_array.rb +51 -0
  93. data/vendor_lib/rspec/matchers/built_in/raise_error.rb +154 -0
  94. data/vendor_lib/rspec/matchers/built_in/respond_to.rb +74 -0
  95. data/vendor_lib/rspec/matchers/built_in/satisfy.rb +30 -0
  96. data/vendor_lib/rspec/matchers/built_in/start_and_end_with.rb +48 -0
  97. data/vendor_lib/rspec/matchers/built_in/throw_symbol.rb +94 -0
  98. data/vendor_lib/rspec/matchers/built_in/yield.rb +297 -0
  99. data/vendor_lib/rspec/matchers/built_in.rb +39 -0
  100. data/vendor_lib/rspec/matchers/compatibility.rb +14 -0
  101. data/vendor_lib/rspec/matchers/configuration.rb +113 -0
  102. data/vendor_lib/rspec/matchers/dsl.rb +23 -0
  103. data/vendor_lib/rspec/matchers/generated_descriptions.rb +35 -0
  104. data/vendor_lib/rspec/matchers/matcher.rb +301 -0
  105. data/vendor_lib/rspec/matchers/method_missing.rb +12 -0
  106. data/vendor_lib/rspec/matchers/operator_matcher.rb +99 -0
  107. data/vendor_lib/rspec/matchers/pretty.rb +70 -0
  108. data/vendor_lib/rspec/matchers/test_unit_integration.rb +11 -0
  109. data/vendor_lib/rspec/matchers.rb +633 -0
  110. data/vendor_lib/rspec/mocks/any_instance/chain.rb +92 -0
  111. data/vendor_lib/rspec/mocks/any_instance/expectation_chain.rb +47 -0
  112. data/vendor_lib/rspec/mocks/any_instance/message_chains.rb +75 -0
  113. data/vendor_lib/rspec/mocks/any_instance/recorder.rb +200 -0
  114. data/vendor_lib/rspec/mocks/any_instance/stub_chain.rb +45 -0
  115. data/vendor_lib/rspec/mocks/any_instance/stub_chain_chain.rb +23 -0
  116. data/vendor_lib/rspec/mocks/argument_list_matcher.rb +104 -0
  117. data/vendor_lib/rspec/mocks/argument_matchers.rb +264 -0
  118. data/vendor_lib/rspec/mocks/arity_calculator.rb +66 -0
  119. data/vendor_lib/rspec/mocks/configuration.rb +111 -0
  120. data/vendor_lib/rspec/mocks/error_generator.rb +203 -0
  121. data/vendor_lib/rspec/mocks/errors.rb +12 -0
  122. data/vendor_lib/rspec/mocks/example_methods.rb +201 -0
  123. data/vendor_lib/rspec/mocks/extensions/marshal.rb +17 -0
  124. data/vendor_lib/rspec/mocks/framework.rb +36 -0
  125. data/vendor_lib/rspec/mocks/instance_method_stasher.rb +112 -0
  126. data/vendor_lib/rspec/mocks/matchers/have_received.rb +99 -0
  127. data/vendor_lib/rspec/mocks/matchers/receive.rb +112 -0
  128. data/vendor_lib/rspec/mocks/matchers/receive_messages.rb +72 -0
  129. data/vendor_lib/rspec/mocks/message_expectation.rb +643 -0
  130. data/vendor_lib/rspec/mocks/method_double.rb +209 -0
  131. data/vendor_lib/rspec/mocks/method_reference.rb +95 -0
  132. data/vendor_lib/rspec/mocks/mock.rb +7 -0
  133. data/vendor_lib/rspec/mocks/mutate_const.rb +406 -0
  134. data/vendor_lib/rspec/mocks/object_reference.rb +90 -0
  135. data/vendor_lib/rspec/mocks/order_group.rb +82 -0
  136. data/vendor_lib/rspec/mocks/proxy.rb +269 -0
  137. data/vendor_lib/rspec/mocks/proxy_for_nil.rb +37 -0
  138. data/vendor_lib/rspec/mocks/space.rb +95 -0
  139. data/vendor_lib/rspec/mocks/standalone.rb +3 -0
  140. data/vendor_lib/rspec/mocks/stub_chain.rb +51 -0
  141. data/vendor_lib/rspec/mocks/syntax.rb +374 -0
  142. data/vendor_lib/rspec/mocks/targets.rb +90 -0
  143. data/vendor_lib/rspec/mocks/test_double.rb +109 -0
  144. data/vendor_lib/rspec/mocks/verifying_double.rb +77 -0
  145. data/vendor_lib/rspec/mocks/verifying_message_expecation.rb +60 -0
  146. data/vendor_lib/rspec/mocks/verifying_proxy.rb +151 -0
  147. data/vendor_lib/rspec/mocks/version.rb +7 -0
  148. data/vendor_lib/rspec/mocks.rb +100 -0
  149. data/vendor_lib/rspec/support/caller_filter.rb +56 -0
  150. data/vendor_lib/rspec/support/spec/deprecation_helpers.rb +29 -0
  151. data/vendor_lib/rspec/support/spec/in_sub_process.rb +40 -0
  152. data/vendor_lib/rspec/support/spec/stderr_splitter.rb +50 -0
  153. data/vendor_lib/rspec/support/spec.rb +14 -0
  154. data/vendor_lib/rspec/support/version.rb +7 -0
  155. data/vendor_lib/rspec/support/warnings.rb +41 -0
  156. data/vendor_lib/rspec/support.rb +6 -0
  157. data/vendor_lib/rspec/version.rb +5 -0
  158. data/vendor_lib/rspec-expectations.rb +1 -0
  159. data/vendor_lib/rspec.rb +3 -0
  160. metadata +163 -4
  161. data/opal/opal/rspec/rspec.js +0 -20384
@@ -0,0 +1,269 @@
1
+ module RSpec
2
+ module Mocks
3
+ # @private
4
+ class Proxy
5
+ SpecificMessage = Struct.new(:object,:message) do
6
+ def ==(expectation)
7
+ expectation.orig_object == object && expectation.message == message
8
+ end
9
+ end
10
+
11
+ # @private
12
+ def initialize(object, order_group, name=nil, options={})
13
+ @object = object
14
+ @order_group = order_group
15
+ @name = name
16
+ @error_generator = ErrorGenerator.new(object, name)
17
+ @expectation_ordering = RSpec::Mocks::space.expectation_ordering
18
+ @messages_received = []
19
+ @options = options
20
+ @null_object = false
21
+ @method_doubles = Hash.new { |h, k| h[k] = MethodDouble.new(@object, k, self) }
22
+ end
23
+
24
+ # @private
25
+ attr_reader :object
26
+
27
+ # @private
28
+ def null_object?
29
+ @null_object
30
+ end
31
+
32
+ # @private
33
+ # Tells the object to ignore any messages that aren't explicitly set as
34
+ # stubs or message expectations.
35
+ def as_null_object
36
+ @null_object = true
37
+ @object
38
+ end
39
+
40
+ # @private
41
+ def method_handle_for(message)
42
+ nil
43
+ end
44
+
45
+ # @private
46
+ def add_message_expectation(location, method_name, opts={}, &block)
47
+ meth_double = method_double_for(method_name)
48
+
49
+ if null_object? && !block
50
+ meth_double.add_default_stub(@error_generator, @expectation_ordering, location, opts) do
51
+ @object
52
+ end
53
+ end
54
+
55
+ meth_double.add_expectation @error_generator, @expectation_ordering, location, opts, &block
56
+ end
57
+
58
+ # @private
59
+ def add_simple_expectation(method_name, response, location)
60
+ method_double_for(method_name).add_simple_expectation method_name, response, @error_generator, location
61
+ end
62
+
63
+ # @private
64
+ def build_expectation(method_name)
65
+ meth_double = method_double_for(method_name)
66
+
67
+ meth_double.build_expectation(
68
+ @error_generator,
69
+ @expectation_ordering
70
+ )
71
+ end
72
+
73
+ # @private
74
+ def replay_received_message_on(expectation, &block)
75
+ expected_method_name = expectation.message
76
+ meth_double = method_double_for(expected_method_name)
77
+
78
+ if meth_double.expectations.any?
79
+ @error_generator.raise_expectation_on_mocked_method(expected_method_name)
80
+ end
81
+
82
+ unless null_object? || meth_double.stubs.any?
83
+ @error_generator.raise_expectation_on_unstubbed_method(expected_method_name)
84
+ end
85
+
86
+ @messages_received.each do |(actual_method_name, args, _)|
87
+ if expectation.matches?(actual_method_name, *args)
88
+ expectation.invoke(nil)
89
+ block.call(*args) if block
90
+ end
91
+ end
92
+
93
+ end
94
+
95
+ # @private
96
+ def check_for_unexpected_arguments(expectation)
97
+ @messages_received.each do |(method_name, args, _)|
98
+ if expectation.matches_name_but_not_args(method_name, *args)
99
+ raise_unexpected_message_args_error(expectation, *args)
100
+ end
101
+ end
102
+ end
103
+
104
+ # @private
105
+ def add_stub(location, method_name, opts={}, &implementation)
106
+ method_double_for(method_name).add_stub @error_generator, @expectation_ordering, location, opts, &implementation
107
+ end
108
+
109
+ # @private
110
+ def add_simple_stub(method_name, response)
111
+ method_double_for(method_name).add_simple_stub method_name, response
112
+ end
113
+
114
+ # @private
115
+ def remove_stub(method_name)
116
+ method_double_for(method_name).remove_stub
117
+ end
118
+
119
+ # @private
120
+ def remove_single_stub(method_name, stub)
121
+ method_double_for(method_name).remove_single_stub(stub)
122
+ end
123
+
124
+ # @private
125
+ def verify
126
+ @method_doubles.each_value {|d| d.verify}
127
+ ensure
128
+ reset
129
+ end
130
+
131
+ # @private
132
+ def reset
133
+ @method_doubles.each_value {|d| d.reset}
134
+ @messages_received.clear
135
+ end
136
+
137
+ # @private
138
+ def received_message?(method_name, *args, &block)
139
+ @messages_received.any? {|array| array == [method_name, args, block]}
140
+ end
141
+
142
+ # @private
143
+ def has_negative_expectation?(message)
144
+ method_double_for(message).expectations.detect {|expectation| expectation.negative_expectation_for?(message)}
145
+ end
146
+
147
+ # @private
148
+ def record_message_received(message, *args, &block)
149
+ @order_group.invoked SpecificMessage.new(object, message)
150
+ @messages_received << [message, args, block]
151
+ end
152
+
153
+ # @private
154
+ def message_received(message, *args, &block)
155
+ record_message_received message, *args, &block
156
+ expectation = find_matching_expectation(message, *args)
157
+ stub = find_matching_method_stub(message, *args)
158
+
159
+ if (stub && expectation && expectation.called_max_times?) || (stub && !expectation)
160
+ expectation.increase_actual_received_count! if expectation && expectation.actual_received_count_matters?
161
+ if expectation = find_almost_matching_expectation(message, *args)
162
+ expectation.advise(*args) unless expectation.expected_messages_received?
163
+ end
164
+ stub.invoke(nil, *args, &block)
165
+ elsif expectation
166
+ expectation.invoke(stub, *args, &block)
167
+ elsif expectation = find_almost_matching_expectation(message, *args)
168
+ expectation.advise(*args) if null_object? unless expectation.expected_messages_received?
169
+ raise_unexpected_message_args_error(expectation, *args) unless (has_negative_expectation?(message) or null_object?)
170
+ elsif stub = find_almost_matching_stub(message, *args)
171
+ stub.advise(*args)
172
+ raise_missing_default_stub_error(stub, *args)
173
+ elsif Class === @object
174
+ @object.superclass.__send__(message, *args, &block)
175
+ else
176
+ @object.__send__(:method_missing, message, *args, &block)
177
+ end
178
+ end
179
+
180
+ # @private
181
+ def raise_unexpected_message_error(method_name, *args)
182
+ @error_generator.raise_unexpected_message_error method_name, *args
183
+ end
184
+
185
+ # @private
186
+ def raise_unexpected_message_args_error(expectation, *args)
187
+ @error_generator.raise_unexpected_message_args_error(expectation, *args)
188
+ end
189
+
190
+ # @private
191
+ def raise_missing_default_stub_error(expectation, *args)
192
+ @error_generator.raise_missing_default_stub_error(expectation, *args)
193
+ end
194
+
195
+ private
196
+
197
+ def method_double_for(message)
198
+ @method_doubles[message.to_sym]
199
+ end
200
+
201
+ def find_matching_expectation(method_name, *args)
202
+ find_best_matching_expectation_for(method_name) do |expectation|
203
+ expectation.matches?(method_name, *args)
204
+ end
205
+ end
206
+
207
+ def find_almost_matching_expectation(method_name, *args)
208
+ find_best_matching_expectation_for(method_name) do |expectation|
209
+ expectation.matches_name_but_not_args(method_name, *args)
210
+ end
211
+ end
212
+
213
+ def find_best_matching_expectation_for(method_name)
214
+ first_match = nil
215
+
216
+ method_double_for(method_name).expectations.each do |expectation|
217
+ next unless yield expectation
218
+ return expectation unless expectation.called_max_times?
219
+ first_match ||= expectation
220
+ end
221
+
222
+ first_match
223
+ end
224
+
225
+ def find_matching_method_stub(method_name, *args)
226
+ method_double_for(method_name).stubs.find {|stub| stub.matches?(method_name, *args)}
227
+ end
228
+
229
+ def find_almost_matching_stub(method_name, *args)
230
+ method_double_for(method_name).stubs.find {|stub| stub.matches_name_but_not_args(method_name, *args)}
231
+ end
232
+ end
233
+
234
+ class PartialMockProxy < Proxy
235
+ def method_handle_for(message)
236
+ if any_instance_class_recorder_observing_method?(@object.class, message)
237
+ message = ::RSpec::Mocks.
238
+ any_instance_recorder_for(@object.class).
239
+ build_alias_method_name(message)
240
+ end
241
+
242
+ ::RSpec::Mocks.method_handle_for(@object, message)
243
+ rescue NameError
244
+ nil
245
+ end
246
+
247
+ # @private
248
+ def add_simple_expectation(method_name, response, location)
249
+ method_double_for(method_name).configure_method
250
+ super
251
+ end
252
+
253
+ # @private
254
+ def add_simple_stub(method_name, response)
255
+ method_double_for(method_name).configure_method
256
+ super
257
+ end
258
+
259
+ private
260
+
261
+ def any_instance_class_recorder_observing_method?(klass, method_name)
262
+ return true if ::RSpec::Mocks.any_instance_recorder_for(klass).already_observing?(method_name)
263
+ superklass = klass.superclass
264
+ return false if superklass.nil?
265
+ any_instance_class_recorder_observing_method?(superklass, method_name)
266
+ end
267
+ end
268
+ end
269
+ end
@@ -0,0 +1,37 @@
1
+ module RSpec
2
+ module Mocks
3
+ # @private
4
+ class ProxyForNil < Proxy
5
+
6
+ def initialize(order_group)
7
+ @warn_about_expectations = true
8
+ super nil, order_group
9
+ end
10
+ attr_accessor :warn_about_expectations
11
+ alias warn_about_expectations? warn_about_expectations
12
+
13
+ def add_message_expectation(location, method_name, opts={}, &block)
14
+ warn(method_name) if warn_about_expectations?
15
+ super
16
+ end
17
+
18
+ def add_negative_message_expectation(location, method_name, &implementation)
19
+ warn(method_name) if warn_about_expectations?
20
+ super
21
+ end
22
+
23
+ def add_stub(location, method_name, opts={}, &implementation)
24
+ warn(method_name) if warn_about_expectations?
25
+ super
26
+ end
27
+
28
+ private
29
+
30
+ def warn method_name
31
+ source = CallerFilter.first_non_rspec_line
32
+ Kernel.warn("An expectation of :#{method_name} was set on nil. Called from #{source}. Use allow_message_expectations_on_nil to disable warnings.")
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,95 @@
1
+ module RSpec
2
+ module Mocks
3
+ # @api private
4
+ class Space
5
+ attr_reader :proxies, :any_instance_recorders
6
+
7
+ def initialize
8
+ @proxies = {}
9
+ @any_instance_recorders = {}
10
+ end
11
+
12
+ def verify_all
13
+ proxies.each_value do |object|
14
+ object.verify
15
+ end
16
+
17
+ any_instance_recorders.each_value do |recorder|
18
+ recorder.verify
19
+ end
20
+ end
21
+
22
+ def reset_all
23
+ ConstantMutator.reset_all
24
+
25
+ proxies.each_value do |object|
26
+ object.reset
27
+ end
28
+
29
+ proxies.clear
30
+ any_instance_recorders.clear
31
+ expectation_ordering.clear
32
+ end
33
+
34
+ def expectation_ordering
35
+ @expectation_ordering ||= OrderGroup.new
36
+ end
37
+
38
+ def any_instance_recorder_for(klass)
39
+ id = klass.__id__
40
+ any_instance_recorders.fetch(id) do
41
+ any_instance_recorders[id] = AnyInstance::Recorder.new(klass)
42
+ end
43
+ end
44
+
45
+ def remove_any_instance_recorder_for(klass)
46
+ any_instance_recorders.delete(klass.__id__)
47
+ end
48
+
49
+ def proxies_of(klass)
50
+ proxies.values.select { |proxy| klass === proxy.object }
51
+ end
52
+
53
+ def proxy_for(object)
54
+ id = id_for(object)
55
+ proxies.fetch(id) do
56
+ proxies[id] = case object
57
+ when NilClass then ProxyForNil.new(expectation_ordering)
58
+ when TestDouble then object.__build_mock_proxy(expectation_ordering)
59
+ else
60
+ if RSpec::Mocks.configuration.verify_partial_doubles?
61
+ VerifyingPartialMockProxy.new(object, expectation_ordering)
62
+ else
63
+ PartialMockProxy.new(object, expectation_ordering)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ alias ensure_registered proxy_for
70
+
71
+ def registered?(object)
72
+ proxies.has_key?(id_for object)
73
+ end
74
+
75
+ if defined?(::BasicObject) && !::BasicObject.method_defined?(:__id__) # for 1.9.2
76
+ require 'securerandom'
77
+
78
+ def id_for(object)
79
+ id = object.__id__
80
+
81
+ return id if object.equal?(::ObjectSpace._id2ref(id))
82
+ # this suggests that object.__id__ is proxying through to some wrapped object
83
+
84
+ object.instance_exec do
85
+ @__id_for_rspec_mocks_space ||= ::SecureRandom.uuid
86
+ end
87
+ end
88
+ else
89
+ def id_for(object)
90
+ object.__id__
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec/mocks'
2
+
3
+ RSpec::Mocks.setup(self)
@@ -0,0 +1,51 @@
1
+ module RSpec
2
+ module Mocks
3
+ # @private
4
+ class StubChain
5
+ def self.stub_chain_on(object, *chain, &blk)
6
+ new(object, *chain, &blk).stub_chain
7
+ end
8
+
9
+ attr_reader :object, :chain, :block
10
+
11
+ def initialize(object, *chain, &blk)
12
+ @object = object
13
+ @chain, @block = format_chain(*chain, &blk)
14
+ end
15
+
16
+ def stub_chain
17
+ if chain.length > 1
18
+ if matching_stub = find_matching_stub
19
+ chain.shift
20
+ matching_stub.invoke(nil).stub_chain(*chain, &block)
21
+ else
22
+ next_in_chain = Mock.new
23
+ object.stub(chain.shift) { next_in_chain }
24
+ StubChain.stub_chain_on(next_in_chain, *chain, &block)
25
+ end
26
+ else
27
+ object.stub(chain.shift, &block)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def format_chain(*chain, &blk)
34
+ if Hash === chain.last
35
+ hash = chain.pop
36
+ hash.each do |k,v|
37
+ chain << k
38
+ blk = lambda { v }
39
+ end
40
+ end
41
+ return chain.join('.').split('.'), blk
42
+ end
43
+
44
+ def find_matching_stub
45
+ ::RSpec::Mocks.proxy_for(object).
46
+ __send__(:find_matching_method_stub, chain.first.to_sym)
47
+ end
48
+ end
49
+ end
50
+ end
51
+