rr 0.3.11 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/CHANGES +8 -3
  2. data/README +130 -39
  3. data/Rakefile +6 -5
  4. data/lib/rr.rb +8 -8
  5. data/lib/rr/adapters/rr_methods.rb +228 -0
  6. data/lib/rr/adapters/rspec.rb +1 -1
  7. data/lib/rr/adapters/test_unit.rb +1 -1
  8. data/lib/rr/double.rb +294 -89
  9. data/lib/rr/{scenario_creator.rb → double_creator.rb} +65 -66
  10. data/lib/rr/{scenario_definition.rb → double_definition.rb} +36 -36
  11. data/lib/rr/{scenario_definition_builder.rb → double_definition_builder.rb} +3 -3
  12. data/lib/rr/double_insertion.rb +132 -0
  13. data/lib/rr/{scenario_matches.rb → double_matches.rb} +2 -2
  14. data/lib/rr/{scenario_method_proxy.rb → double_method_proxy.rb} +2 -2
  15. data/lib/rr/errors/argument_equality_error.rb +3 -3
  16. data/lib/rr/errors/rr_error.rb +13 -13
  17. data/lib/rr/errors/scenario_definition_error.rb +3 -3
  18. data/lib/rr/errors/scenario_not_found_error.rb +3 -3
  19. data/lib/rr/errors/scenario_order_error.rb +3 -3
  20. data/lib/rr/errors/times_called_error.rb +3 -3
  21. data/lib/rr/expectations/any_argument_expectation.rb +1 -1
  22. data/lib/rr/expectations/argument_equality_expectation.rb +1 -1
  23. data/lib/rr/expectations/times_called_expectation.rb +1 -1
  24. data/lib/rr/hash_with_object_id_key.rb +1 -1
  25. data/lib/rr/space.rb +40 -40
  26. data/lib/rr/times_called_matchers/any_times_matcher.rb +13 -13
  27. data/lib/rr/times_called_matchers/at_least_matcher.rb +11 -11
  28. data/lib/rr/times_called_matchers/at_most_matcher.rb +16 -16
  29. data/lib/rr/times_called_matchers/integer_matcher.rb +13 -13
  30. data/lib/rr/times_called_matchers/non_terminal.rb +21 -21
  31. data/lib/rr/times_called_matchers/proc_matcher.rb +7 -7
  32. data/lib/rr/times_called_matchers/range_matcher.rb +14 -14
  33. data/lib/rr/times_called_matchers/terminal.rb +16 -16
  34. data/lib/rr/times_called_matchers/times_called_matcher.rb +32 -32
  35. data/spec/core_spec_suite.rb +18 -0
  36. data/{examples → spec}/environment_fixture_setup.rb +0 -1
  37. data/{examples/high_level_example.rb → spec/high_level_spec.rb} +11 -11
  38. data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +67 -0
  39. data/spec/rr/adapters/rr_methods_creator_spec.rb +365 -0
  40. data/spec/rr/adapters/rr_methods_space_spec.rb +134 -0
  41. data/spec/rr/adapters/rr_methods_spec_helper.rb +11 -0
  42. data/{examples/rr/extensions/instance_methods_times_matcher_example.rb → spec/rr/adapters/rr_methods_times_matcher_spec.rb} +4 -4
  43. data/spec/rr/double/double_insertion_bind_spec.rb +78 -0
  44. data/spec/rr/double/double_insertion_dispatching_spec.rb +221 -0
  45. data/spec/rr/double/double_insertion_has_original_method_spec.rb +56 -0
  46. data/spec/rr/double/double_insertion_register_scenario_spec.rb +24 -0
  47. data/spec/rr/double/double_insertion_reset_spec.rb +89 -0
  48. data/spec/rr/double/double_insertion_spec.rb +66 -0
  49. data/spec/rr/double/double_insertion_verify_spec.rb +23 -0
  50. data/spec/rr/double_creator_spec.rb +454 -0
  51. data/{examples/rr/scenario_definition_example.rb → spec/rr/double_definition_spec.rb} +143 -143
  52. data/spec/rr/double_method_proxy_spec.rb +71 -0
  53. data/spec/rr/double_spec.rb +654 -0
  54. data/spec/rr/errors/rr_error_spec.rb +65 -0
  55. data/spec/rr/expectations/any_argument_expectation_spec.rb +47 -0
  56. data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +38 -0
  57. data/spec/rr/expectations/argument_equality_expectation_spec.rb +58 -0
  58. data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +53 -0
  59. data/spec/rr/expectations/duck_type_argument_equality_expectation_spec.rb +71 -0
  60. data/spec/rr/expectations/is_a_argument_equality_expectation_spec.rb +51 -0
  61. data/spec/rr/expectations/numeric_argument_equality_expectation_spec.rb +47 -0
  62. data/spec/rr/expectations/range_argument_equality_expectation_spec.rb +59 -0
  63. data/spec/rr/expectations/regexp_argument_equality_expectation_spec.rb +72 -0
  64. data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +43 -0
  65. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +67 -0
  66. data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +71 -0
  67. data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +21 -0
  68. data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +103 -0
  69. data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +79 -0
  70. data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +81 -0
  71. data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +40 -0
  72. data/spec/rr/rspec/rspec_adapter_spec.rb +65 -0
  73. data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +52 -0
  74. data/spec/rr/rspec/rspec_usage_spec.rb +67 -0
  75. data/spec/rr/space/hash_with_object_id_key_spec.rb +88 -0
  76. data/spec/rr/space/space_create_spec.rb +278 -0
  77. data/spec/rr/space/space_helper.rb +7 -0
  78. data/spec/rr/space/space_register_spec.rb +32 -0
  79. data/spec/rr/space/space_reset_spec.rb +131 -0
  80. data/spec/rr/space/space_spec.rb +32 -0
  81. data/spec/rr/space/space_verify_spec.rb +181 -0
  82. data/{examples → spec}/rr/test_unit/test_helper.rb +1 -1
  83. data/{examples → spec}/rr/test_unit/test_unit_backtrace_test.rb +0 -0
  84. data/{examples → spec}/rr/test_unit/test_unit_integration_test.rb +4 -4
  85. data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +47 -0
  86. data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +55 -0
  87. data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +70 -0
  88. data/spec/rr/times_called_matchers/integer_matcher_spec.rb +70 -0
  89. data/spec/rr/times_called_matchers/proc_matcher_spec.rb +55 -0
  90. data/spec/rr/times_called_matchers/range_matcher_spec.rb +76 -0
  91. data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +118 -0
  92. data/spec/rspec_spec_suite.rb +16 -0
  93. data/spec/spec_helper.rb +9 -0
  94. data/{examples/example_suite.rb → spec/spec_suite.rb} +3 -3
  95. data/{examples/test_unit_example_suite.rb → spec/test_unit_spec_suite.rb} +0 -0
  96. metadata +93 -93
  97. data/examples/core_example_suite.rb +0 -31
  98. data/examples/example_helper.rb +0 -9
  99. data/examples/rr/double/double_bind_example.rb +0 -70
  100. data/examples/rr/double/double_dispatching_example.rb +0 -236
  101. data/examples/rr/double/double_example.rb +0 -66
  102. data/examples/rr/double/double_has_original_method_example.rb +0 -56
  103. data/examples/rr/double/double_register_scenario_example.rb +0 -24
  104. data/examples/rr/double/double_reset_example.rb +0 -89
  105. data/examples/rr/double/double_verify_example.rb +0 -23
  106. data/examples/rr/errors/rr_error_example.rb +0 -65
  107. data/examples/rr/expectations/any_argument_expectation_example.rb +0 -52
  108. data/examples/rr/expectations/anything_argument_equality_expectation_example.rb +0 -38
  109. data/examples/rr/expectations/argument_equality_expectation_example.rb +0 -62
  110. data/examples/rr/expectations/boolean_argument_equality_expectation_example.rb +0 -48
  111. data/examples/rr/expectations/duck_type_argument_equality_expectation_example.rb +0 -67
  112. data/examples/rr/expectations/is_a_argument_equality_expectation_example.rb +0 -50
  113. data/examples/rr/expectations/numeric_argument_equality_expectation_example.rb +0 -46
  114. data/examples/rr/expectations/range_argument_equality_expectation_example.rb +0 -59
  115. data/examples/rr/expectations/regexp_argument_equality_expectation_example.rb +0 -67
  116. data/examples/rr/expectations/times_called_expectation/times_called_expectation_any_times_example.rb +0 -50
  117. data/examples/rr/expectations/times_called_expectation/times_called_expectation_at_least_example.rb +0 -73
  118. data/examples/rr/expectations/times_called_expectation/times_called_expectation_at_most_example.rb +0 -77
  119. data/examples/rr/expectations/times_called_expectation/times_called_expectation_example.rb +0 -42
  120. data/examples/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +0 -20
  121. data/examples/rr/expectations/times_called_expectation/times_called_expectation_integer_example.rb +0 -111
  122. data/examples/rr/expectations/times_called_expectation/times_called_expectation_proc_example.rb +0 -88
  123. data/examples/rr/expectations/times_called_expectation/times_called_expectation_range_example.rb +0 -90
  124. data/examples/rr/extensions/instance_methods_argument_matcher_example.rb +0 -65
  125. data/examples/rr/extensions/instance_methods_creator_example.rb +0 -363
  126. data/examples/rr/extensions/instance_methods_example_helper.rb +0 -11
  127. data/examples/rr/extensions/instance_methods_space_example.rb +0 -122
  128. data/examples/rr/rspec/rspec_adapter_example.rb +0 -63
  129. data/examples/rr/rspec/rspec_backtrace_tweaking_example.rb +0 -36
  130. data/examples/rr/rspec/rspec_usage_example.rb +0 -65
  131. data/examples/rr/scenario_creator_example.rb +0 -459
  132. data/examples/rr/scenario_example.rb +0 -701
  133. data/examples/rr/scenario_method_proxy_example.rb +0 -71
  134. data/examples/rr/space/hash_with_object_id_key_example.rb +0 -86
  135. data/examples/rr/space/space_create_example.rb +0 -278
  136. data/examples/rr/space/space_example.rb +0 -29
  137. data/examples/rr/space/space_helper.rb +0 -7
  138. data/examples/rr/space/space_register_example.rb +0 -32
  139. data/examples/rr/space/space_reset_example.rb +0 -120
  140. data/examples/rr/space/space_verify_example.rb +0 -169
  141. data/examples/rr/times_called_matchers/any_times_matcher_example.rb +0 -63
  142. data/examples/rr/times_called_matchers/at_least_matcher_example.rb +0 -70
  143. data/examples/rr/times_called_matchers/at_most_matcher_example.rb +0 -85
  144. data/examples/rr/times_called_matchers/integer_matcher_example.rb +0 -91
  145. data/examples/rr/times_called_matchers/proc_matcher_example.rb +0 -77
  146. data/examples/rr/times_called_matchers/range_matcher_example.rb +0 -97
  147. data/examples/rr/times_called_matchers/times_called_matcher_example.rb +0 -53
  148. data/examples/rspec_example_suite.rb +0 -25
  149. data/lib/rr/extensions/instance_methods.rb +0 -228
  150. data/lib/rr/scenario.rb +0 -337
@@ -0,0 +1,71 @@
1
+ require "spec/spec_helper"
2
+
3
+ module RR
4
+ describe DoubleMethodProxy, "initializes proxy with passed in creator", :shared => true do
5
+ it "initializes proxy with passed in creator" do
6
+ class << the_proxy
7
+ attr_reader :creator
8
+ end
9
+ the_proxy.creator.should === creator
10
+ end
11
+ end
12
+
13
+ describe DoubleMethodProxy do
14
+ attr_reader :space, :subject, :creator, :the_proxy
15
+ before(:each) do
16
+ @space = Space.new
17
+ @subject = Object.new
18
+ @creator = space.scenario_creator
19
+ creator.mock
20
+ end
21
+
22
+ describe ".new without block" do
23
+ it_should_behave_like "RR::DoubleMethodProxy initializes proxy with passed in creator"
24
+ before do
25
+ @the_proxy = DoubleMethodProxy.new(space, creator, subject)
26
+ end
27
+
28
+ it "clears out all methods from proxy" do
29
+ proxy_subclass = Class.new(DoubleMethodProxy) do
30
+ def i_should_be_a_scenario
31
+ end
32
+ end
33
+ proxy_subclass.instance_methods.should include('i_should_be_a_scenario')
34
+
35
+ proxy = proxy_subclass.new(space, creator, subject)
36
+ proxy.i_should_be_a_scenario.should be_instance_of(DoubleDefinition)
37
+ end
38
+ end
39
+
40
+ describe ".new with block" do
41
+ it_should_behave_like "RR::DoubleMethodProxy initializes proxy with passed in creator"
42
+ before do
43
+ @the_proxy = DoubleMethodProxy.new(space, creator, subject) do |b|
44
+ b.foobar(1, 2) {:one_two}
45
+ b.foobar(1) {:one}
46
+ b.foobar.with_any_args {:default}
47
+ b.baz() {:baz_result}
48
+ end
49
+ end
50
+
51
+ it "creates double_insertions" do
52
+ subject.foobar(1, 2).should == :one_two
53
+ subject.foobar(1).should == :one
54
+ subject.foobar(:something).should == :default
55
+ subject.baz.should == :baz_result
56
+ end
57
+
58
+ it "clears out all methods from proxy" do
59
+ proxy_subclass = Class.new(DoubleMethodProxy) do
60
+ def i_should_be_a_scenario
61
+ end
62
+ end
63
+ proxy_subclass.instance_methods.should include('i_should_be_a_scenario')
64
+
65
+ proxy_subclass.new(space, creator, subject) do |m|
66
+ m.i_should_be_a_scenario.should be_instance_of(DoubleDefinition)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,654 @@
1
+ require "spec/spec_helper"
2
+
3
+ module RR
4
+ describe Double do
5
+ attr_reader :space, :object, :double_insertion, :scenario
6
+ before do
7
+ @space = Space.new
8
+ @object = Object.new
9
+ def object.foobar(a, b)
10
+ [b, a]
11
+ end
12
+ @double_insertion = space.double_insertion(object, :foobar)
13
+ @scenario = space.scenario(double_insertion)
14
+ end
15
+
16
+ describe "#with" do
17
+ it "returns DoubleDefinition" do
18
+ scenario.with(1).should === scenario.definition
19
+ end
20
+
21
+ it "sets an ArgumentEqualityExpectation" do
22
+ scenario.with(1)
23
+ scenario.should be_exact_match(1)
24
+ scenario.should_not be_exact_match(2)
25
+ end
26
+
27
+ it "sets return value when block passed in" do
28
+ scenario.with(1) {:return_value}
29
+ object.foobar(1).should == :return_value
30
+ end
31
+ end
32
+
33
+ describe "#with_any_args" do
34
+ before do
35
+ scenario.with_any_args {:return_value}
36
+ end
37
+
38
+ it "returns DoubleDefinition" do
39
+ scenario.with_no_args.should === scenario.definition
40
+ end
41
+
42
+ it "sets an AnyArgumentExpectation" do
43
+ scenario.should_not be_exact_match(1)
44
+ scenario.should be_wildcard_match(1)
45
+ end
46
+
47
+ it "sets return value when block passed in" do
48
+ object.foobar(:anything).should == :return_value
49
+ end
50
+ end
51
+
52
+ describe "#with_no_args" do
53
+ before do
54
+ scenario.with_no_args {:return_value}
55
+ end
56
+
57
+ it "returns DoubleDefinition" do
58
+ scenario.with_no_args.should === scenario.definition
59
+ end
60
+
61
+ it "sets an ArgumentEqualityExpectation with no arguments" do
62
+ scenario.argument_expectation.should == Expectations::ArgumentEqualityExpectation.new()
63
+ end
64
+
65
+ it "sets return value when block passed in" do
66
+ object.foobar().should == :return_value
67
+ end
68
+ end
69
+
70
+ describe "#never" do
71
+ it "returns DoubleDefinition" do
72
+ scenario.never.should === scenario.definition
73
+ end
74
+
75
+ it "sets up a Times Called Expectation with 0" do
76
+ scenario.never
77
+ proc {scenario.call(double_insertion)}.should raise_error(Errors::TimesCalledError)
78
+ end
79
+
80
+ it "sets return value when block passed in" do
81
+ scenario.with_any_args.never
82
+ proc {scenario.call(double_insertion)}.should raise_error(Errors::TimesCalledError)
83
+ end
84
+ end
85
+
86
+ describe "#once" do
87
+ it "returns DoubleDefinition" do
88
+ scenario.once.should === scenario.definition
89
+ end
90
+
91
+ it "sets up a Times Called Expectation with 1" do
92
+ scenario.once
93
+ scenario.call(double_insertion)
94
+ proc {scenario.call(double_insertion)}.should raise_error(Errors::TimesCalledError)
95
+ end
96
+
97
+ it "sets return value when block passed in" do
98
+ scenario.with_any_args.once {:return_value}
99
+ object.foobar.should == :return_value
100
+ end
101
+ end
102
+
103
+ describe "#twice" do
104
+ it "returns DoubleDefinition" do
105
+ scenario.twice.should === scenario.definition
106
+ end
107
+
108
+ it "sets up a Times Called Expectation with 2" do
109
+ scenario.twice
110
+ scenario.call(double_insertion)
111
+ scenario.call(double_insertion)
112
+ proc {scenario.call(double_insertion)}.should raise_error(Errors::TimesCalledError)
113
+ end
114
+
115
+ it "sets return value when block passed in" do
116
+ scenario.with_any_args.twice {:return_value}
117
+ object.foobar.should == :return_value
118
+ end
119
+ end
120
+
121
+ describe "#at_least" do
122
+ it "returns DoubleDefinition" do
123
+ scenario.with_any_args.at_least(2).should === scenario.definition
124
+ end
125
+
126
+ it "sets up a AtLeastMatcher with 2" do
127
+ scenario.at_least(2)
128
+ scenario.definition.times_matcher.should == TimesCalledMatchers::AtLeastMatcher.new(2)
129
+ end
130
+
131
+ it "sets return value when block passed in" do
132
+ scenario.with_any_args.at_least(2) {:return_value}
133
+ object.foobar.should == :return_value
134
+ end
135
+ end
136
+
137
+ describe "#at_most" do
138
+ it "returns DoubleDefinition" do
139
+ scenario.with_any_args.at_most(2).should === scenario.definition
140
+ end
141
+
142
+ it "sets up a Times Called Expectation with 1" do
143
+ scenario.at_most(2)
144
+ scenario.call(double_insertion)
145
+ scenario.call(double_insertion)
146
+ proc do
147
+ scenario.call(double_insertion)
148
+ end.should raise_error(
149
+ Errors::TimesCalledError,
150
+ "foobar()\nCalled 3 times.\nExpected at most 2 times."
151
+ )
152
+ end
153
+
154
+ it "sets return value when block passed in" do
155
+ scenario.with_any_args.at_most(2) {:return_value}
156
+ object.foobar.should == :return_value
157
+ end
158
+ end
159
+
160
+ describe "#times" do
161
+ it "returns DoubleDefinition" do
162
+ scenario.times(3).should === scenario.definition
163
+ end
164
+
165
+ it "sets up a Times Called Expectation with passed in times" do
166
+ scenario.times(3)
167
+ scenario.call(double_insertion)
168
+ scenario.call(double_insertion)
169
+ scenario.call(double_insertion)
170
+ proc {scenario.call(double_insertion)}.should raise_error(Errors::TimesCalledError)
171
+ end
172
+
173
+ it "sets return value when block passed in" do
174
+ scenario.with_any_args.times(3) {:return_value}
175
+ object.foobar.should == :return_value
176
+ end
177
+ end
178
+
179
+ describe "#any_number_of_times" do
180
+ it "returns DoubleDefinition" do
181
+ scenario.any_number_of_times.should === scenario.definition
182
+ end
183
+
184
+ it "sets up a Times Called Expectation with AnyTimes matcher" do
185
+ scenario.any_number_of_times
186
+ scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
187
+ end
188
+
189
+ it "sets return value when block passed in" do
190
+ scenario.with_any_args.any_number_of_times {:return_value}
191
+ object.foobar.should == :return_value
192
+ end
193
+ end
194
+
195
+ describe "#ordered" do
196
+ it "adds itself to the ordered scenarios list" do
197
+ scenario.ordered
198
+ space.ordered_scenarios.should include(scenario)
199
+ end
200
+
201
+ it "does not double_insertion add itself" do
202
+ scenario.ordered
203
+ scenario.ordered
204
+ space.ordered_scenarios.should == [scenario ]
205
+ end
206
+
207
+ it "sets ordered? to true" do
208
+ scenario.ordered
209
+ scenario.should be_ordered
210
+ end
211
+
212
+ it "sets return value when block passed in" do
213
+ scenario.with_any_args.once.ordered {:return_value}
214
+ object.foobar.should == :return_value
215
+ end
216
+ end
217
+
218
+ describe "#ordered?" do
219
+ it "defaults to false" do
220
+ scenario.should_not be_ordered
221
+ end
222
+ end
223
+
224
+ describe "#yields" do
225
+ it "returns DoubleDefinition" do
226
+ scenario.yields(:baz).should === scenario.definition
227
+ end
228
+
229
+ it "yields the passed in argument to the call block when there is no returns value set" do
230
+ scenario.with_any_args.yields(:baz)
231
+ passed_in_block_arg = nil
232
+ object.foobar {|arg| passed_in_block_arg = arg}.should == nil
233
+ passed_in_block_arg.should == :baz
234
+ end
235
+
236
+ it "yields the passed in argument to the call block when there is a no returns value set" do
237
+ scenario.with_any_args.yields(:baz).returns(:return_value)
238
+
239
+ passed_in_block_arg = nil
240
+ object.foobar {|arg| passed_in_block_arg = arg}.should == :return_value
241
+ passed_in_block_arg.should == :baz
242
+ end
243
+
244
+ it "sets return value when block passed in" do
245
+ scenario.with_any_args.yields {:return_value}
246
+ object.foobar {}.should == :return_value
247
+ end
248
+ end
249
+
250
+ describe "#after_call" do
251
+ it "returns DoubleDefinition" do
252
+ scenario.after_call {}.should === scenario.definition
253
+ end
254
+
255
+ it "sends return value of Double implementation to after_call" do
256
+ return_value = {}
257
+ scenario.returns(return_value).after_call do |value|
258
+ value[:foo] = :bar
259
+ value
260
+ end
261
+
262
+ actual_value = scenario.call(double_insertion)
263
+ actual_value.should === return_value
264
+ actual_value.should == {:foo => :bar}
265
+ end
266
+
267
+ it "receives the return value in the after_call callback" do
268
+ return_value = :returns_value
269
+ scenario.returns(return_value).after_call do |value|
270
+ :after_call_value
271
+ end
272
+
273
+ actual_value = scenario.call(double_insertion)
274
+ actual_value.should == :after_call_value
275
+ end
276
+
277
+ it "allows after_call to mock the return value" do
278
+ return_value = Object.new
279
+ scenario.with_any_args.returns(return_value).after_call do |value|
280
+ mock(value).inner_method(1) {:baz}
281
+ value
282
+ end
283
+
284
+ object.foobar.inner_method(1).should == :baz
285
+ end
286
+
287
+ it "raises an error when not passed a block" do
288
+ proc do
289
+ scenario.after_call
290
+ end.should raise_error(ArgumentError, "after_call expects a block")
291
+ end
292
+ end
293
+
294
+ describe "#returns" do
295
+ it "returns DoubleDefinition" do
296
+ scenario.returns {:baz}.should === scenario.definition
297
+ scenario.returns(:baz).should === scenario.definition
298
+ end
299
+
300
+ it "sets the value of the method when passed a block" do
301
+ scenario.returns {:baz}
302
+ scenario.call(double_insertion).should == :baz
303
+ end
304
+
305
+ it "sets the value of the method when passed an argument" do
306
+ scenario.returns(:baz)
307
+ scenario.call(double_insertion).should == :baz
308
+ end
309
+
310
+ it "returns false when passed false" do
311
+ scenario.returns(false)
312
+ scenario.call(double_insertion).should == false
313
+ end
314
+
315
+ it "raises an error when both argument and block is passed in" do
316
+ proc do
317
+ scenario.returns(:baz) {:another}
318
+ end.should raise_error(ArgumentError, "returns cannot accept both an argument and a block")
319
+ end
320
+ end
321
+
322
+ describe "#implemented_by" do
323
+ it "returns the DoubleDefinition" do
324
+ scenario.implemented_by(proc{:baz}).should === scenario.definition
325
+ end
326
+
327
+ it "sets the implementation to the passed in proc" do
328
+ scenario.implemented_by(proc{:baz})
329
+ scenario.call(double_insertion).should == :baz
330
+ end
331
+
332
+ it "sets the implementation to the passed in method" do
333
+ def object.foobar(a, b)
334
+ [b, a]
335
+ end
336
+ scenario.implemented_by(object.method(:foobar))
337
+ scenario.call(double_insertion, 1, 2).should == [2, 1]
338
+ end
339
+ end
340
+
341
+ describe "#implemented_by_original_method" do
342
+ it "returns the DoubleDefinition object" do
343
+ scenario.implemented_by_original_method.should === scenario.definition
344
+ end
345
+
346
+ it "sets the implementation to the original method" do
347
+ scenario.implemented_by_original_method
348
+ scenario.call(double_insertion, 1, 2).should == [2, 1]
349
+ end
350
+
351
+ it "calls methods when respond_to? is true and methods does not contain original method" do
352
+ method_name = nil
353
+ class << object
354
+ def methods
355
+ []
356
+ end
357
+ def method(name)
358
+ raise "We should not be here"
359
+ end
360
+ def respond_to?(name)
361
+ true
362
+ end
363
+ def method_missing(method_name, *args, &block)
364
+ raise "We should not be here"
365
+ end
366
+ end
367
+
368
+ double_insertion = space.double_insertion(object, :foobar)
369
+ scenario = space.scenario(double_insertion)
370
+ scenario.with_any_args
371
+ scenario.implemented_by_original_method
372
+
373
+ object.foobar(1, 2).should == [2, 1]
374
+ end
375
+
376
+ it "calls method when original_method does not exist" do
377
+ class << object
378
+ def method_missing(method_name, *args, &block)
379
+ "method_missing for #{method_name}(#{args.inspect})"
380
+ end
381
+ end
382
+ double_insertion = space.double_insertion(object, :does_not_exist)
383
+ scenario = space.scenario(double_insertion)
384
+ scenario.with_any_args
385
+ scenario.implemented_by_original_method
386
+
387
+ return_value = object.does_not_exist(1, 2)
388
+ return_value.should == "method_missing for does_not_exist([1, 2])"
389
+ end
390
+ end
391
+
392
+ describe "#call implemented by a proc" do
393
+ it "calls the return proc when implemented by a proc" do
394
+ scenario.returns {|arg| "returning #{arg}"}
395
+ scenario.call(double_insertion, :foobar).should == "returning foobar"
396
+ end
397
+
398
+ it "calls and returns the after_call when after_call is set" do
399
+ scenario.returns {|arg| "returning #{arg}"}.after_call do |value|
400
+ "#{value} after call"
401
+ end
402
+ scenario.call(double_insertion, :foobar).should == "returning foobar after call"
403
+ end
404
+
405
+ it "returns nil when to returns is not set" do
406
+ scenario.call(double_insertion).should be_nil
407
+ end
408
+
409
+ it "works when times_called is not set" do
410
+ scenario.returns {:value}
411
+ scenario.call(double_insertion)
412
+ end
413
+
414
+ it "verifes the times_called does not exceed the TimesCalledExpectation" do
415
+ scenario.times(2).returns {:value}
416
+
417
+ scenario.call(double_insertion, :foobar)
418
+ scenario.call(double_insertion, :foobar)
419
+ proc {scenario.call(double_insertion, :foobar)}.should raise_error(Errors::TimesCalledError)
420
+ end
421
+
422
+ it "raises DoubleOrderError when ordered and called out of order" do
423
+ scenario1 = scenario
424
+ scenario2 = space.scenario(double_insertion)
425
+
426
+ scenario1.with(1).returns {:return_1}.ordered.once
427
+ scenario2.with(2).returns {:return_2}.ordered.once
428
+
429
+ proc do
430
+ object.foobar(2)
431
+ end.should raise_error(
432
+ Errors::DoubleOrderError,
433
+ "foobar(2) called out of order in list\n" <<
434
+ "- foobar(1)\n" <<
435
+ "- foobar(2)"
436
+ )
437
+ end
438
+
439
+ it "dispatches to Space#verify_ordered_scenario when ordered" do
440
+ verify_ordered_scenario_called = false
441
+ passed_in_scenario = nil
442
+ space.method(:verify_ordered_scenario).arity.should == 1
443
+ (
444
+ class << space;
445
+ self;
446
+ end).class_eval do
447
+ define_method :verify_ordered_scenario do |scenario|
448
+ passed_in_scenario = scenario
449
+ verify_ordered_scenario_called = true
450
+ end
451
+ end
452
+
453
+ scenario.returns {:value}.ordered
454
+ scenario.call(double_insertion, :foobar)
455
+ verify_ordered_scenario_called.should be_true
456
+ passed_in_scenario.should === scenario
457
+ end
458
+
459
+ it "does not dispatche to Space#verify_ordered_scenario when not ordered" do
460
+ verify_ordered_scenario_called = false
461
+ space.method(:verify_ordered_scenario).arity.should == 1
462
+ (
463
+ class << space;
464
+ self;
465
+ end).class_eval do
466
+ define_method :verify_ordered_scenario do |scenario|
467
+ verify_ordered_scenario_called = true
468
+ end
469
+ end
470
+
471
+ scenario.returns {:value}
472
+ scenario.call(double_insertion, :foobar)
473
+ verify_ordered_scenario_called.should be_false
474
+ end
475
+
476
+ it "does not add block argument if no block passed in" do
477
+ scenario.with(1, 2).returns {|*args| args}
478
+
479
+ args = object.foobar(1, 2)
480
+ args.should == [1, 2]
481
+ end
482
+
483
+ it "makes the block the last argument" do
484
+ scenario.with(1, 2).returns {|a, b, blk| blk}
485
+
486
+ block = object.foobar(1, 2) {|a, b| [b, a]}
487
+ block.call(3, 4).should == [4, 3]
488
+ end
489
+
490
+ it "raises ArgumentError when yields was called and no block passed in" do
491
+ scenario.with(1, 2).yields(55)
492
+
493
+ proc do
494
+ object.foobar(1, 2)
495
+ end.should raise_error(ArgumentError, "A Block must be passed into the method call when using yields")
496
+ end
497
+ end
498
+
499
+ describe "#call implemented by a method" do
500
+ it "sends block to the method" do
501
+ def object.foobar(a, b)
502
+ yield(a, b)
503
+ end
504
+
505
+ scenario.with(1, 2).implemented_by(object.method(:foobar))
506
+
507
+ object.foobar(1, 2) {|a, b| [b, a]}.should == [2, 1]
508
+ end
509
+ end
510
+
511
+ describe "#exact_match?" do
512
+ it "returns false when no expectation set" do
513
+ scenario.should_not be_exact_match()
514
+ scenario.should_not be_exact_match(nil)
515
+ scenario.should_not be_exact_match(Object.new)
516
+ scenario.should_not be_exact_match(1, 2, 3)
517
+ end
518
+
519
+ it "returns false when arguments are not an exact match" do
520
+ scenario.with(1, 2, 3)
521
+ scenario.should_not be_exact_match(1, 2)
522
+ scenario.should_not be_exact_match(1)
523
+ scenario.should_not be_exact_match()
524
+ scenario.should_not be_exact_match("does not match")
525
+ end
526
+
527
+ it "returns true when arguments are an exact match" do
528
+ scenario.with(1, 2, 3)
529
+ scenario.should be_exact_match(1, 2, 3)
530
+ end
531
+ end
532
+
533
+ describe "#wildcard_match?" do
534
+ it "returns false when no expectation set" do
535
+ scenario.should_not be_wildcard_match()
536
+ scenario.should_not be_wildcard_match(nil)
537
+ scenario.should_not be_wildcard_match(Object.new)
538
+ scenario.should_not be_wildcard_match(1, 2, 3)
539
+ end
540
+
541
+ it "returns true when arguments are an exact match" do
542
+ scenario.with(1, 2, 3)
543
+ scenario.should be_wildcard_match(1, 2, 3)
544
+ scenario.should_not be_wildcard_match(1, 2)
545
+ scenario.should_not be_wildcard_match(1)
546
+ scenario.should_not be_wildcard_match()
547
+ scenario.should_not be_wildcard_match("does not match")
548
+ end
549
+
550
+ it "returns true when with_any_args" do
551
+ scenario.with_any_args
552
+
553
+ scenario.should be_wildcard_match(1, 2, 3)
554
+ scenario.should be_wildcard_match(1, 2)
555
+ scenario.should be_wildcard_match(1)
556
+ scenario.should be_wildcard_match()
557
+ scenario.should be_wildcard_match("does not match")
558
+ end
559
+ end
560
+
561
+ describe "#attempt?" do
562
+ it "returns true when TimesCalledExpectation#attempt? is true" do
563
+ scenario.with(1, 2, 3).twice
564
+ scenario.call(double_insertion, 1, 2, 3)
565
+ scenario.times_called_expectation.should be_attempt
566
+ scenario.should be_attempt
567
+ end
568
+
569
+ it "returns false when TimesCalledExpectation#attempt? is true" do
570
+ scenario.with(1, 2, 3).twice
571
+ scenario.call(double_insertion, 1, 2, 3)
572
+ scenario.call(double_insertion, 1, 2, 3)
573
+ scenario.times_called_expectation.should_not be_attempt
574
+ scenario.should_not be_attempt
575
+ end
576
+
577
+ it "returns true when there is no Times Called expectation" do
578
+ scenario.with(1, 2, 3)
579
+ scenario.definition.times_matcher.should be_nil
580
+ scenario.should be_attempt
581
+ end
582
+ end
583
+
584
+ describe "#verify" do
585
+ it "verifies that times called expectation was met" do
586
+ scenario.twice.returns {:return_value}
587
+
588
+ proc {scenario.verify}.should raise_error(Errors::TimesCalledError)
589
+ scenario.call(double_insertion)
590
+ proc {scenario.verify}.should raise_error(Errors::TimesCalledError)
591
+ scenario.call(double_insertion)
592
+
593
+ proc {scenario.verify}.should_not raise_error
594
+ end
595
+
596
+ it "does not raise an error when there is no times called expectation" do
597
+ proc {scenario.verify}.should_not raise_error
598
+ scenario.call(double_insertion)
599
+ proc {scenario.verify}.should_not raise_error
600
+ scenario.call(double_insertion)
601
+ proc {scenario.verify}.should_not raise_error
602
+ end
603
+ end
604
+
605
+ describe "#terminal?" do
606
+ it "returns true when times_called_expectation's terminal? is true" do
607
+ scenario.once
608
+ scenario.times_called_expectation.should be_terminal
609
+ scenario.should be_terminal
610
+ end
611
+
612
+ it "returns false when times_called_expectation's terminal? is false" do
613
+ scenario.any_number_of_times
614
+ scenario.times_called_expectation.should_not be_terminal
615
+ scenario.should_not be_terminal
616
+ end
617
+
618
+ it "returns false when there is no times_matcher" do
619
+ scenario.definition.times_matcher.should be_nil
620
+ scenario.should_not be_terminal
621
+ end
622
+ end
623
+
624
+ describe "#method_name" do
625
+ it "returns the DoubleInsertion's method_name" do
626
+ double_insertion.method_name.should == :foobar
627
+ scenario.method_name.should == :foobar
628
+ end
629
+ end
630
+
631
+ describe "#expected_arguments" do
632
+ it "returns argument expectation's expected_arguments when there is a argument expectation" do
633
+ scenario.with(1, 2)
634
+ scenario.expected_arguments.should == [1, 2]
635
+ end
636
+
637
+ it "returns an empty array when there is no argument expectation" do
638
+ scenario.argument_expectation.should be_nil
639
+ scenario.expected_arguments.should == []
640
+ end
641
+ end
642
+
643
+ describe "#formatted_name" do
644
+ it "renders the formatted name of the Double with no arguments" do
645
+ scenario.formatted_name.should == "foobar()"
646
+ end
647
+
648
+ it "renders the formatted name of the Double with arguments" do
649
+ scenario.with(1, 2)
650
+ scenario.formatted_name.should == "foobar(1, 2)"
651
+ end
652
+ end
653
+ end
654
+ end