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,92 @@
1
+ module RSpec
2
+ module Mocks
3
+ module AnyInstance
4
+ class Chain
5
+ def initialize(recorder, *args, &block)
6
+ @recorder = recorder
7
+ @expectation_args = args
8
+ @expectation_block = block
9
+ end
10
+
11
+ module Customizations
12
+ # @macro [attach] record
13
+ # @method $1(*args, &block)
14
+ # Records the `$1` message for playback against an instance that
15
+ # invokes a method stubbed or mocked using `any_instance`.
16
+ #
17
+ # @see RSpec::Mocks::MessageExpectation#$1
18
+ #
19
+ def self.record(method_name)
20
+ define_method(method_name) do |*args, &block|
21
+ record(method_name, *args, &block)
22
+ end
23
+ end
24
+
25
+ record :and_return
26
+ record :and_raise
27
+ record :and_throw
28
+ record :and_yield
29
+ record :and_call_original
30
+ record :with
31
+ record :once
32
+ record :twice
33
+ record :any_number_of_times
34
+ record :exactly
35
+ record :times
36
+ record :never
37
+ record :at_least
38
+ record :at_most
39
+ end
40
+
41
+ include Customizations
42
+
43
+ # @private
44
+ def playback!(instance)
45
+ message_expectation = create_message_expectation_on(instance)
46
+ messages.inject(message_expectation) do |object, message|
47
+ object.__send__(*message.first, &message.last)
48
+ end
49
+ end
50
+
51
+ # @private
52
+ def constrained_to_any_of?(*constraints)
53
+ constraints.any? do |constraint|
54
+ messages.any? do |message|
55
+ message.first.first == constraint
56
+ end
57
+ end
58
+ end
59
+
60
+ # @private
61
+ def expectation_fulfilled!
62
+ @expectation_fulfilled = true
63
+ end
64
+
65
+ def never
66
+ ErrorGenerator.raise_double_negation_error("expect_any_instance_of(MyClass)") if negated?
67
+ super
68
+ end
69
+
70
+ private
71
+
72
+ def negated?
73
+ messages.any? { |(message, *_), _| message == :never }
74
+ end
75
+
76
+ def messages
77
+ @messages ||= []
78
+ end
79
+
80
+ def last_message
81
+ messages.last.first.first unless messages.empty?
82
+ end
83
+
84
+ def record(rspec_method_name, *args, &block)
85
+ verify_invocation_order(rspec_method_name, *args, &block)
86
+ messages << [args.unshift(rspec_method_name), block]
87
+ self
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,47 @@
1
+ module RSpec
2
+ module Mocks
3
+ module AnyInstance
4
+ # @api private
5
+ class ExpectationChain < Chain
6
+ def expectation_fulfilled?
7
+ @expectation_fulfilled || constrained_to_any_of?(:never, :any_number_of_times)
8
+ end
9
+
10
+ def initialize(*args, &block)
11
+ @expectation_fulfilled = false
12
+ super
13
+ end
14
+
15
+ private
16
+ def verify_invocation_order(rspec_method_name, *args, &block)
17
+ end
18
+ end
19
+
20
+ # @api private
21
+ class PositiveExpectationChain < ExpectationChain
22
+
23
+ private
24
+
25
+ def create_message_expectation_on(instance)
26
+ proxy = ::RSpec::Mocks.proxy_for(instance)
27
+ expected_from = IGNORED_BACKTRACE_LINE
28
+ me = proxy.add_message_expectation(expected_from, *@expectation_args, &@expectation_block)
29
+ if RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks?
30
+ me.and_yield_receiver_to_implementation
31
+ end
32
+
33
+ me
34
+ end
35
+
36
+ def invocation_order
37
+ @invocation_order ||= {
38
+ :with => [nil],
39
+ :and_return => [:with, nil],
40
+ :and_raise => [:with, nil]
41
+ }
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,75 @@
1
+ module RSpec
2
+ module Mocks
3
+ module AnyInstance
4
+ # @private
5
+ class MessageChains
6
+ def initialize
7
+ @chains_by_method_name = Hash.new { |h, k| h[k] = [] }
8
+ end
9
+
10
+ # @private
11
+ def [](method_name)
12
+ @chains_by_method_name[method_name]
13
+ end
14
+
15
+ # @private
16
+ def add(method_name, chain)
17
+ @chains_by_method_name[method_name] << chain
18
+ chain
19
+ end
20
+
21
+ # @private
22
+ def remove_stub_chains_for!(method_name)
23
+ @chains_by_method_name[method_name].reject! do |chain|
24
+ StubChain === chain
25
+ end
26
+ end
27
+
28
+ # @private
29
+ def has_expectation?(method_name)
30
+ @chains_by_method_name[method_name].find do |chain|
31
+ ExpectationChain === chain
32
+ end
33
+ end
34
+
35
+ # @private
36
+ def all_expectations_fulfilled?
37
+ @chains_by_method_name.all? do |method_name, chains|
38
+ chains.all? { |chain| chain.expectation_fulfilled? }
39
+ end
40
+ end
41
+
42
+ # @private
43
+ def unfulfilled_expectations
44
+ @chains_by_method_name.map do |method_name, chains|
45
+ method_name.to_s if ExpectationChain === chains.last unless chains.last.expectation_fulfilled?
46
+ end.compact
47
+ end
48
+
49
+ # @private
50
+ def received_expected_message!(method_name)
51
+ @chains_by_method_name[method_name].each do |chain|
52
+ chain.expectation_fulfilled!
53
+ end
54
+ end
55
+
56
+ # @private
57
+ def playback!(instance, method_name)
58
+ raise_if_second_instance_to_receive_message(instance)
59
+ @chains_by_method_name[method_name].each do |chain|
60
+ chain.playback!(instance)
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def raise_if_second_instance_to_receive_message(instance)
67
+ @instance_with_expectation ||= instance if ExpectationChain === instance
68
+ if ExpectationChain === instance && !@instance_with_expectation.equal?(instance)
69
+ raise RSpec::Mocks::MockExpectationError, "Exactly one instance should have received the following message(s) but didn't: #{unfulfilled_expectations.sort.join(', ')}"
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,200 @@
1
+ module RSpec
2
+ module Mocks
3
+ module AnyInstance
4
+ # Given a class `TheClass`, `TheClass.any_instance` returns a `Recorder`,
5
+ # which records stubs and message expectations for later playback on
6
+ # instances of `TheClass`.
7
+ #
8
+ # Further constraints are stored in instances of [Chain](Chain).
9
+ #
10
+ # @see AnyInstance
11
+ # @see Chain
12
+ class Recorder
13
+ # @private
14
+ attr_reader :message_chains, :stubs, :klass
15
+
16
+ def initialize(klass)
17
+ @message_chains = MessageChains.new
18
+ @stubs = Hash.new { |hash,key| hash[key] = [] }
19
+ @observed_methods = []
20
+ @played_methods = {}
21
+ @klass = klass
22
+ @expectation_set = false
23
+ end
24
+
25
+ # Initializes the recording a stub to be played back against any
26
+ # instance of this object that invokes the submitted method.
27
+ #
28
+ # @see Methods#stub
29
+ def stub(method_name_or_method_map, &block)
30
+ if Hash === method_name_or_method_map
31
+ method_name_or_method_map.each do |method_name, return_value|
32
+ stub(method_name).and_return(return_value)
33
+ end
34
+ else
35
+ observe!(method_name_or_method_map)
36
+ message_chains.add(method_name_or_method_map, StubChain.new(self, method_name_or_method_map, &block))
37
+ end
38
+ end
39
+
40
+ # Initializes the recording a stub chain to be played back against any
41
+ # instance of this object that invokes the method matching the first
42
+ # argument.
43
+ #
44
+ # @see Methods#stub_chain
45
+ def stub_chain(*method_names_and_optional_return_values, &block)
46
+ normalize_chain(*method_names_and_optional_return_values) do |method_name, args|
47
+ observe!(method_name)
48
+ message_chains.add(method_name, StubChainChain.new(self, *args, &block))
49
+ end
50
+ end
51
+
52
+ # Initializes the recording a message expectation to be played back
53
+ # against any instance of this object that invokes the submitted
54
+ # method.
55
+ #
56
+ # @see Methods#should_receive
57
+ def should_receive(method_name, &block)
58
+ @expectation_set = true
59
+ observe!(method_name)
60
+ message_chains.add(method_name, PositiveExpectationChain.new(self, method_name, &block))
61
+ end
62
+
63
+ def should_not_receive(method_name, &block)
64
+ should_receive(method_name, &block).never
65
+ end
66
+
67
+ # Removes any previously recorded stubs, stub_chains or message
68
+ # expectations that use `method_name`.
69
+ #
70
+ # @see Methods#unstub
71
+ def unstub(method_name)
72
+ unless @observed_methods.include?(method_name.to_sym)
73
+ raise RSpec::Mocks::MockExpectationError, "The method `#{method_name}` was not stubbed or was already unstubbed"
74
+ end
75
+ message_chains.remove_stub_chains_for!(method_name)
76
+ ::RSpec::Mocks.proxies_of(@klass).each do |proxy|
77
+ stubs[method_name].each { |stub| proxy.remove_single_stub(method_name, stub) }
78
+ end
79
+ stubs[method_name].clear
80
+ stop_observing!(method_name) unless message_chains.has_expectation?(method_name)
81
+ end
82
+
83
+ # @api private
84
+ #
85
+ # Used internally to verify that message expectations have been
86
+ # fulfilled.
87
+ def verify
88
+ if @expectation_set && !message_chains.all_expectations_fulfilled?
89
+ raise RSpec::Mocks::MockExpectationError, "Exactly one instance should have received the following message(s) but didn't: #{message_chains.unfulfilled_expectations.sort.join(', ')}"
90
+ end
91
+ ensure
92
+ stop_all_observation!
93
+ ::RSpec::Mocks.space.remove_any_instance_recorder_for(@klass)
94
+ end
95
+
96
+ # @private
97
+ def stop_all_observation!
98
+ @observed_methods.each {|method_name| restore_method!(method_name)}
99
+ end
100
+
101
+ # @private
102
+ def playback!(instance, method_name)
103
+ RSpec::Mocks.space.ensure_registered(instance)
104
+ message_chains.playback!(instance, method_name)
105
+ @played_methods[method_name] = instance
106
+ received_expected_message!(method_name) if message_chains.has_expectation?(method_name)
107
+ end
108
+
109
+ # @private
110
+ def instance_that_received(method_name)
111
+ @played_methods[method_name]
112
+ end
113
+
114
+ def build_alias_method_name(method_name)
115
+ "__#{method_name}_without_any_instance__"
116
+ end
117
+
118
+ def already_observing?(method_name)
119
+ @observed_methods.include?(method_name)
120
+ end
121
+
122
+ private
123
+
124
+ def normalize_chain(*args)
125
+ args.shift.to_s.split('.').map {|s| s.to_sym}.reverse.each {|a| args.unshift a}
126
+ yield args.first, args
127
+ end
128
+
129
+ def received_expected_message!(method_name)
130
+ message_chains.received_expected_message!(method_name)
131
+ restore_method!(method_name)
132
+ mark_invoked!(method_name)
133
+ end
134
+
135
+ def restore_method!(method_name)
136
+ if public_protected_or_private_method_defined?(build_alias_method_name(method_name))
137
+ restore_original_method!(method_name)
138
+ else
139
+ remove_dummy_method!(method_name)
140
+ end
141
+ end
142
+
143
+ def restore_original_method!(method_name)
144
+ alias_method_name = build_alias_method_name(method_name)
145
+ @klass.class_exec do
146
+ remove_method method_name
147
+ alias_method method_name, alias_method_name
148
+ remove_method alias_method_name
149
+ end
150
+ end
151
+
152
+ def remove_dummy_method!(method_name)
153
+ @klass.class_exec do
154
+ remove_method method_name
155
+ end
156
+ end
157
+
158
+ def backup_method!(method_name)
159
+ alias_method_name = build_alias_method_name(method_name)
160
+ @klass.class_exec do
161
+ alias_method alias_method_name, method_name
162
+ end if public_protected_or_private_method_defined?(method_name)
163
+ end
164
+
165
+ def public_protected_or_private_method_defined?(method_name)
166
+ @klass.method_defined?(method_name) || @klass.private_method_defined?(method_name)
167
+ end
168
+
169
+ def stop_observing!(method_name)
170
+ restore_method!(method_name)
171
+ @observed_methods.delete(method_name)
172
+ end
173
+
174
+ def observe!(method_name)
175
+ if RSpec::Mocks.configuration.verify_partial_doubles?
176
+ raise MockExpectationError unless @klass.method_defined?(method_name)
177
+ end
178
+
179
+ stop_observing!(method_name) if already_observing?(method_name)
180
+ @observed_methods << method_name
181
+ backup_method!(method_name)
182
+ @klass.__send__(:define_method, method_name) do |*args, &blk|
183
+ klass = ::RSpec::Mocks.method_handle_for(self, method_name).owner
184
+ ::RSpec::Mocks.any_instance_recorder_for(klass).playback!(self, method_name)
185
+ self.__send__(method_name, *args, &blk)
186
+ end
187
+ end
188
+
189
+ def mark_invoked!(method_name)
190
+ backup_method!(method_name)
191
+ @klass.__send__(:define_method, method_name) do |*args, &blk|
192
+ klass = ::RSpec::Mocks.method_handle_for(self, method_name).owner
193
+ invoked_instance = ::RSpec::Mocks.any_instance_recorder_for(klass).instance_that_received(method_name)
194
+ raise RSpec::Mocks::MockExpectationError, "The message '#{method_name}' was received by #{self.inspect} but has already been received by #{invoked_instance}"
195
+ end
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,45 @@
1
+ module RSpec
2
+ module Mocks
3
+ module AnyInstance
4
+ # @private
5
+ class StubChain < Chain
6
+
7
+ # @private
8
+ def expectation_fulfilled?
9
+ true
10
+ end
11
+
12
+ private
13
+
14
+ def create_message_expectation_on(instance)
15
+ proxy = ::RSpec::Mocks.proxy_for(instance)
16
+ expected_from = IGNORED_BACKTRACE_LINE
17
+ stub = proxy.add_stub(expected_from, *@expectation_args, &@expectation_block)
18
+ @recorder.stubs[stub.message] << stub
19
+
20
+ if RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks?
21
+ stub.and_yield_receiver_to_implementation
22
+ end
23
+
24
+ stub
25
+ end
26
+
27
+ def invocation_order
28
+ @invocation_order ||= {
29
+ :with => [nil],
30
+ :and_return => [:with, nil],
31
+ :and_raise => [:with, nil],
32
+ :and_yield => [:with, nil],
33
+ :and_call_original => [:with, nil]
34
+ }
35
+ end
36
+
37
+ def verify_invocation_order(rspec_method_name, *args, &block)
38
+ unless invocation_order[rspec_method_name].include?(last_message)
39
+ raise(NoMethodError, "Undefined method #{rspec_method_name}")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ module RSpec
2
+ module Mocks
3
+ module AnyInstance
4
+ # @private
5
+ class StubChainChain < StubChain
6
+
7
+ private
8
+
9
+ def create_message_expectation_on(instance)
10
+ ::RSpec::Mocks::StubChain.stub_chain_on(instance, *@expectation_args, &@expectation_block)
11
+ end
12
+
13
+ def invocation_order
14
+ @invocation_order ||= {
15
+ :and_return => [nil],
16
+ :and_raise => [nil],
17
+ :and_yield => [nil]
18
+ }
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,104 @@
1
+ require 'rspec/mocks/argument_matchers'
2
+
3
+ module RSpec
4
+ module Mocks
5
+ # Wrapper for matching arguments against a list of expected values. Used by
6
+ # the `with` method on a `MessageExpectation`:
7
+ #
8
+ # object.should_receive(:message).with(:a, 'b', 3)
9
+ # object.message(:a, 'b', 3)
10
+ #
11
+ # Values passed to `with` can be literal values or argument matchers that
12
+ # match against the real objects .e.g.
13
+ #
14
+ # object.should_receive(:message).with(hash_including(:a => 'b'))
15
+ #
16
+ # Can also be used directly to match the contents of any `Array`. This
17
+ # enables 3rd party mocking libs to take advantage of rspec's argument
18
+ # matching without using the rest of rspec-mocks.
19
+ #
20
+ # require 'rspec/mocks/argument_list_matcher'
21
+ # include RSpec::Mocks::ArgumentMatchers
22
+ #
23
+ # arg_list_matcher = RSpec::Mocks::ArgumentListMatcher.new(123, hash_including(:a => 'b'))
24
+ # arg_list_matcher.args_match?(123, :a => 'b')
25
+ #
26
+ # This class is immutable.
27
+ #
28
+ # @see ArgumentMatchers
29
+ class ArgumentListMatcher
30
+ # @private
31
+ attr_reader :expected_args
32
+
33
+ # @api public
34
+ # @param [Array] *expected_args a list of expected literals and/or argument matchers
35
+ # @param [Block] block a block with arity matching the expected
36
+ #
37
+ # Initializes an `ArgumentListMatcher` with a collection of literal
38
+ # values and/or argument matchers, or a block that handles the evaluation
39
+ # for you.
40
+ #
41
+ # @see ArgumentMatchers
42
+ # @see #args_match?
43
+ def initialize(*expected_args, &block)
44
+ @expected_args = expected_args
45
+ @block = expected_args.empty? ? block : nil
46
+ @match_any_args = false
47
+ @matchers = nil
48
+
49
+ case expected_args.first
50
+ when ArgumentMatchers::AnyArgsMatcher
51
+ @match_any_args = true
52
+ when ArgumentMatchers::NoArgsMatcher
53
+ @matchers = []
54
+ else
55
+ @matchers = expected_args.collect {|arg| matcher_for(arg)}
56
+ end
57
+ end
58
+
59
+ # @api public
60
+ # @param [Array] *args
61
+ #
62
+ # Matches each element in the `expected_args` against the element in the same
63
+ # position of the arguments passed to `new`.
64
+ #
65
+ # @see #initialize
66
+ def args_match?(*args)
67
+ match_any_args? || block_passes?(*args) || matchers_match?(*args)
68
+ end
69
+
70
+ private
71
+
72
+ def matcher_for(arg)
73
+ return ArgumentMatchers::MatcherMatcher.new(arg) if is_matcher?(arg)
74
+ return ArgumentMatchers::RegexpMatcher.new(arg) if Regexp === arg
75
+ return ArgumentMatchers::EqualityProxy.new(arg)
76
+ end
77
+
78
+ def is_matcher?(object)
79
+ return false if object.respond_to?(:i_respond_to_everything_so_im_not_really_a_matcher)
80
+
81
+ [:failure_message_for_should, :failure_message].any? do |msg|
82
+ object.respond_to?(msg)
83
+ end && object.respond_to?(:matches?)
84
+ end
85
+
86
+ def block_passes?(*args)
87
+ @block.call(*args) if @block
88
+ end
89
+
90
+ def matchers_match?(*args)
91
+ @matchers == args
92
+ end
93
+
94
+ def match_any_args?
95
+ @match_any_args
96
+ end
97
+
98
+ # Value that will match all argument lists.
99
+ #
100
+ # @private
101
+ MATCH_ALL = new(ArgumentMatchers::AnyArgsMatcher.new)
102
+ end
103
+ end
104
+ end