google-geo 1.0 → 2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. data/CHANGELOG +5 -0
  2. data/README +12 -8
  3. data/lib/google/geo.rb +10 -6
  4. data/test/fixtures/invalid_map_key.xml +10 -0
  5. data/test/fixtures/missing_address.xml +10 -0
  6. data/test/fixtures/server_error.xml +10 -0
  7. data/test/fixtures/success.xml +36 -0
  8. data/test/fixtures/success_with_multiple_addresses.xml +62 -0
  9. data/test/fixtures/too_many_queries.xml +10 -0
  10. data/test/fixtures/unavailable_address.xml +10 -0
  11. data/test/fixtures/unknown_address.xml +10 -0
  12. data/test/geo_test.rb +19 -5
  13. data/vendor/mocha-0.4.0/COPYING +3 -0
  14. data/vendor/mocha-0.4.0/MIT-LICENSE +7 -0
  15. data/vendor/mocha-0.4.0/README +35 -0
  16. data/vendor/mocha-0.4.0/RELEASE +98 -0
  17. data/vendor/mocha-0.4.0/Rakefile +126 -0
  18. data/vendor/mocha-0.4.0/examples/misc.rb +36 -0
  19. data/vendor/mocha-0.4.0/examples/mocha.rb +26 -0
  20. data/vendor/mocha-0.4.0/examples/stubba.rb +65 -0
  21. data/vendor/mocha-0.4.0/lib/mocha.rb +19 -0
  22. data/vendor/mocha-0.4.0/lib/mocha/any_instance_method.rb +35 -0
  23. data/vendor/mocha-0.4.0/lib/mocha/auto_verify.rb +113 -0
  24. data/vendor/mocha-0.4.0/lib/mocha/central.rb +35 -0
  25. data/vendor/mocha-0.4.0/lib/mocha/class_method.rb +62 -0
  26. data/vendor/mocha-0.4.0/lib/mocha/expectation.rb +295 -0
  27. data/vendor/mocha-0.4.0/lib/mocha/expectation_error.rb +6 -0
  28. data/vendor/mocha-0.4.0/lib/mocha/infinite_range.rb +27 -0
  29. data/vendor/mocha-0.4.0/lib/mocha/inspect.rb +37 -0
  30. data/vendor/mocha-0.4.0/lib/mocha/instance_method.rb +8 -0
  31. data/vendor/mocha-0.4.0/lib/mocha/metaclass.rb +7 -0
  32. data/vendor/mocha-0.4.0/lib/mocha/mock.rb +20 -0
  33. data/vendor/mocha-0.4.0/lib/mocha/mock_methods.rb +122 -0
  34. data/vendor/mocha-0.4.0/lib/mocha/object.rb +100 -0
  35. data/vendor/mocha-0.4.0/lib/mocha/pretty_parameters.rb +28 -0
  36. data/vendor/mocha-0.4.0/lib/mocha/setup_and_teardown.rb +23 -0
  37. data/vendor/mocha-0.4.0/lib/mocha/standalone.rb +30 -0
  38. data/vendor/mocha-0.4.0/lib/mocha/test_case_adapter.rb +49 -0
  39. data/vendor/mocha-0.4.0/lib/mocha_standalone.rb +2 -0
  40. data/vendor/mocha-0.4.0/lib/stubba.rb +2 -0
  41. data/vendor/mocha-0.4.0/test/active_record_test_case.rb +36 -0
  42. data/vendor/mocha-0.4.0/test/all_tests.rb +75 -0
  43. data/vendor/mocha-0.4.0/test/execution_point.rb +34 -0
  44. data/vendor/mocha-0.4.0/test/method_definer.rb +18 -0
  45. data/vendor/mocha-0.4.0/test/mocha/any_instance_method_test.rb +124 -0
  46. data/vendor/mocha-0.4.0/test/mocha/auto_verify_test.rb +163 -0
  47. data/vendor/mocha-0.4.0/test/mocha/central_test.rb +124 -0
  48. data/vendor/mocha-0.4.0/test/mocha/class_method_test.rb +196 -0
  49. data/vendor/mocha-0.4.0/test/mocha/expectation_test.rb +357 -0
  50. data/vendor/mocha-0.4.0/test/mocha/infinite_range_test.rb +50 -0
  51. data/vendor/mocha-0.4.0/test/mocha/inspect_test.rb +90 -0
  52. data/vendor/mocha-0.4.0/test/mocha/metaclass_test.rb +22 -0
  53. data/vendor/mocha-0.4.0/test/mocha/mock_methods_test.rb +235 -0
  54. data/vendor/mocha-0.4.0/test/mocha/mock_test.rb +84 -0
  55. data/vendor/mocha-0.4.0/test/mocha/object_test.rb +165 -0
  56. data/vendor/mocha-0.4.0/test/mocha/pretty_parameters_test.rb +32 -0
  57. data/vendor/mocha-0.4.0/test/mocha/setup_and_teardown_test.rb +76 -0
  58. data/vendor/mocha-0.4.0/test/mocha_acceptance_test.rb +98 -0
  59. data/vendor/mocha-0.4.0/test/mocha_test_result_integration_test.rb +105 -0
  60. data/vendor/mocha-0.4.0/test/standalone_acceptance_test.rb +110 -0
  61. data/vendor/mocha-0.4.0/test/stubba_acceptance_test.rb +102 -0
  62. data/vendor/mocha-0.4.0/test/stubba_integration_test.rb +89 -0
  63. data/vendor/mocha-0.4.0/test/stubba_test_result_integration_test.rb +85 -0
  64. data/vendor/mocha-0.4.0/test/test_helper.rb +4 -0
  65. metadata +73 -3
@@ -0,0 +1,196 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'method_definer'
3
+ require 'mocha/mock'
4
+
5
+ require 'mocha/class_method'
6
+
7
+ class ClassMethodTest < Test::Unit::TestCase
8
+
9
+ include Mocha
10
+
11
+ def test_should_provide_hidden_version_of_method_name_starting_with_prefix
12
+ method = ClassMethod.new(nil, :original_method_name)
13
+ assert_match /^__stubba__/, method.hidden_method
14
+ end
15
+
16
+ def test_should_provide_hidden_version_of_method_name_ending_with_suffix
17
+ method = ClassMethod.new(nil, :original_method_name)
18
+ assert_match /__stubba__$/, method.hidden_method
19
+ end
20
+
21
+ def test_should_provide_hidden_version_of_method_name_including_original_method_name
22
+ method = ClassMethod.new(nil, :original_method_name)
23
+ assert_match /original_method_name/, method.hidden_method
24
+ end
25
+
26
+ def test_should_provide_hidden_version_of_method_name_substituting_question_mark
27
+ method = ClassMethod.new(nil, :question_mark?)
28
+ assert_no_match /\?/, method.hidden_method
29
+ assert_match /question_mark_substituted_character_63/, method.hidden_method
30
+ end
31
+
32
+ def test_should_provide_hidden_version_of_method_name_substituting_exclamation_mark
33
+ method = ClassMethod.new(nil, :exclamation_mark!)
34
+ assert_no_match /!/, method.hidden_method
35
+ assert_match /exclamation_mark_substituted_character_33/, method.hidden_method
36
+ end
37
+
38
+ def test_should_provide_hidden_version_of_method_name_substituting_equals_sign
39
+ method = ClassMethod.new(nil, :equals_sign=)
40
+ assert_no_match /\=/, method.hidden_method
41
+ assert_match /equals_sign_substituted_character_61/, method.hidden_method
42
+ end
43
+
44
+ def test_should_provide_hidden_version_of_method_name_substituting_brackets
45
+ method = ClassMethod.new(nil, :[])
46
+ assert_no_match /\[\]/, method.hidden_method
47
+ assert_match /substituted_character_91__substituted_character_93/, method.hidden_method
48
+ end
49
+
50
+ def test_should_provide_hidden_version_of_method_name_substituting_plus_sign
51
+ method = ClassMethod.new(nil, :+)
52
+ assert_no_match /\+/, method.hidden_method
53
+ assert_match /substituted_character_43/, method.hidden_method
54
+ end
55
+
56
+ def test_should_hide_original_method
57
+ klass = Class.new { def self.method_x; end }
58
+ method = ClassMethod.new(klass, :method_x)
59
+ hidden_method_x = method.hidden_method
60
+
61
+ method.hide_original_method
62
+
63
+ assert klass.respond_to?(hidden_method_x)
64
+ end
65
+
66
+ def test_should_respond_to_original_method_name_after_original_method_has_been_hidden
67
+ klass = Class.new { def self.original_method_name; end }
68
+ method = ClassMethod.new(klass, :original_method_name)
69
+ hidden_method_x = method.hidden_method
70
+
71
+ method.hide_original_method
72
+
73
+ assert klass.respond_to?(:original_method_name)
74
+ end
75
+
76
+ def test_should_not_hide_original_method_if_method_not_defined
77
+ klass = Class.new
78
+ method = ClassMethod.new(klass, :method_x)
79
+ hidden_method_x = method.hidden_method
80
+
81
+ method.hide_original_method
82
+
83
+ assert_equal false, klass.respond_to?(hidden_method_x)
84
+ end
85
+
86
+ def test_should_define_a_new_method_which_should_call_mocha_method_missing
87
+ klass = Class.new { def self.method_x; end }
88
+ mocha = Mocha::Mock.new
89
+ klass.define_instance_method(:mocha) { mocha }
90
+ mocha.expects(:method_x).with(:param1, :param2).returns(:result)
91
+ method = ClassMethod.new(klass, :method_x)
92
+
93
+ method.define_new_method
94
+ result = klass.method_x(:param1, :param2)
95
+
96
+ assert_equal :result, result
97
+ mocha.verify
98
+ end
99
+
100
+ def test_should_remove_new_method
101
+ klass = Class.new { def self.method_x; end }
102
+ method = ClassMethod.new(klass, :method_x)
103
+
104
+ method.remove_new_method
105
+
106
+ assert_equal false, klass.respond_to?(:method_x)
107
+ end
108
+
109
+ def test_should_restore_original_method
110
+ klass = Class.new { def self.method_x; end }
111
+ method = ClassMethod.new(klass, :method_x)
112
+ hidden_method_x = method.hidden_method.to_sym
113
+ klass.define_instance_method(hidden_method_x) { :original_result }
114
+
115
+ method.restore_original_method
116
+
117
+ assert_equal :original_result, klass.method_x
118
+ assert_equal false, klass.respond_to?(hidden_method_x)
119
+ end
120
+
121
+ def test_should_not_restore_original_method_if_hidden_method_is_not_defined
122
+ klass = Class.new { def self.method_x; :new_result; end }
123
+ method = ClassMethod.new(klass, :method_x)
124
+
125
+ method.restore_original_method
126
+
127
+ assert_equal :new_result, klass.method_x
128
+ end
129
+
130
+ def test_should_call_hide_original_method
131
+ klass = Class.new { def self.method_x; end }
132
+ method = ClassMethod.new(klass, :method_x)
133
+ method.define_instance_accessor(:hide_called)
134
+ method.replace_instance_method(:hide_original_method) { self.hide_called = true }
135
+
136
+ method.stub
137
+
138
+ assert method.hide_called
139
+ end
140
+
141
+ def test_should_call_define_new_method
142
+ klass = Class.new { def self.method_x; end }
143
+ method = ClassMethod.new(klass, :method_x)
144
+ method.define_instance_accessor(:define_called)
145
+ method.replace_instance_method(:define_new_method) { self.define_called = true }
146
+
147
+ method.stub
148
+
149
+ assert method.define_called
150
+ end
151
+
152
+ def test_should_call_remove_new_method
153
+ klass = Class.new { def self.method_x; end }
154
+ klass.define_instance_method(:reset_mocha) { }
155
+ method = ClassMethod.new(klass, :method_x)
156
+ method.define_instance_accessor(:remove_called)
157
+ method.replace_instance_method(:remove_new_method) { self.remove_called = true }
158
+
159
+ method.unstub
160
+
161
+ assert method.remove_called
162
+ end
163
+
164
+ def test_should_call_restore_original_method
165
+ klass = Class.new { def self.method_x; end }
166
+ klass.define_instance_method(:reset_mocha) { }
167
+ method = ClassMethod.new(klass, :method_x)
168
+ method.define_instance_accessor(:restore_called)
169
+ method.replace_instance_method(:restore_original_method) { self.restore_called = true }
170
+
171
+ method.unstub
172
+
173
+ assert method.restore_called
174
+ end
175
+
176
+ def test_should_call_reset_mocha
177
+ klass = Class.new { def self.method_x; end }
178
+ klass.define_instance_accessor(:reset_called)
179
+ klass.define_instance_method(:reset_mocha) { self.reset_called = true }
180
+ method = ClassMethod.new(klass, :method_x)
181
+ method.replace_instance_method(:restore_original_method) { }
182
+
183
+ method.unstub
184
+
185
+ assert klass.reset_called
186
+ end
187
+
188
+ def test_should_return_mock_for_stubbee
189
+ mocha = Object.new
190
+ stubbee = Object.new
191
+ stubbee.define_instance_accessor(:mocha) { mocha }
192
+ method = ClassMethod.new(stubbee, :method_name)
193
+ assert_equal stubbee.mocha, method.mock
194
+ end
195
+
196
+ end
@@ -0,0 +1,357 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'method_definer'
3
+ require 'mocha/expectation'
4
+ require 'execution_point'
5
+
6
+ class ExpectationTest < Test::Unit::TestCase
7
+
8
+ include Mocha
9
+
10
+ def new_expectation
11
+ Expectation.new(nil, :expected_method)
12
+ end
13
+
14
+ def test_should_match_calls_to_same_method_with_any_parameters
15
+ assert new_expectation.match?(:expected_method, 1, 2, 3)
16
+ end
17
+
18
+ def test_should_match_calls_to_same_method_with_exactly_zero_parameters
19
+ expectation = new_expectation.with()
20
+ assert expectation.match?(:expected_method)
21
+ end
22
+
23
+ def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
24
+ expectation = new_expectation.with()
25
+ assert !expectation.match?(:expected_method, 1, 2, 3)
26
+ end
27
+
28
+ def test_should_match_calls_to_same_method_with_expected_parameter_values
29
+ expectation = new_expectation.with(1, 2, 3)
30
+ assert expectation.match?(:expected_method, 1, 2, 3)
31
+ end
32
+
33
+ def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
34
+ expectation = new_expectation.with() {|x, y, z| x + y == z}
35
+ assert expectation.match?(:expected_method, 1, 2, 3)
36
+ end
37
+
38
+ def test_should_not_match_calls_to_different_methods_with_no_parameters
39
+ assert !new_expectation.match?(:unexpected_method)
40
+ end
41
+
42
+ def test_should_not_match_calls_to_same_method_with_too_few_parameters
43
+ expectation = new_expectation.with(1, 2, 3)
44
+ assert !expectation.match?(:unexpected_method, 1, 2)
45
+ end
46
+
47
+ def test_should_not_match_calls_to_same_method_with_too_many_parameters
48
+ expectation = new_expectation.with(1, 2)
49
+ assert !expectation.match?(:unexpected_method, 1, 2, 3)
50
+ end
51
+
52
+ def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
53
+ expectation = new_expectation.with(1, 2, 3)
54
+ assert !expectation.match?(:unexpected_method, 1, 0, 3)
55
+ end
56
+
57
+ def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
58
+ expectation = new_expectation.with() {|x, y, z| x + y == z}
59
+ assert !expectation.match?(:expected_method, 1, 0, 3)
60
+ end
61
+
62
+ def test_should_store_provided_backtrace
63
+ backtrace = Object.new
64
+ expectation = Expectation.new(nil, :expected_method, backtrace)
65
+ assert_equal backtrace, expectation.backtrace
66
+ end
67
+
68
+ def test_should_default_backtrace_to_caller
69
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
70
+ assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
71
+ end
72
+
73
+ def test_should_not_yield
74
+ yielded = false
75
+ new_expectation.invoke() { yielded = true }
76
+ assert_equal false, yielded
77
+ end
78
+
79
+ def test_should_yield_no_parameters
80
+ expectation = new_expectation.yields
81
+ yielded_parameters = nil
82
+ expectation.invoke() { |*parameters| yielded_parameters = parameters }
83
+ assert_equal Array.new, yielded_parameters
84
+ end
85
+
86
+ def test_should_yield_with_specified_parameters
87
+ parameters_for_yield = [1, 2, 3]
88
+ expectation = new_expectation.yields(*parameters_for_yield)
89
+ yielded_parameters = nil
90
+ expectation.invoke() { |*parameters| yielded_parameters = parameters }
91
+ assert_equal parameters_for_yield, yielded_parameters
92
+ end
93
+
94
+ def test_should_return_specified_value
95
+ expectation = new_expectation.returns(99)
96
+ assert_equal 99, expectation.invoke
97
+ end
98
+
99
+ def test_should_return_same_specified_value_multiple_times
100
+ expectation = new_expectation.returns(99)
101
+ assert_equal 99, expectation.invoke
102
+ assert_equal 99, expectation.invoke
103
+ end
104
+
105
+ def test_should_return_specified_values_on_consecutive_calls
106
+ expectation = new_expectation.returns(99, 100, 101)
107
+ assert_equal 99, expectation.invoke
108
+ assert_equal 100, expectation.invoke
109
+ assert_equal 101, expectation.invoke
110
+ end
111
+
112
+ def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
113
+ values = [99, 100, 101]
114
+ expectation = new_expectation.returns(*values)
115
+ values.shift
116
+ assert_equal 99, expectation.invoke
117
+ assert_equal 100, expectation.invoke
118
+ assert_equal 101, expectation.invoke
119
+ end
120
+
121
+ def test_should_return_nil_by_default
122
+ assert_nil new_expectation.invoke
123
+ end
124
+
125
+ def test_should_return_nil_if_no_value_specified
126
+ expectation = new_expectation.returns()
127
+ assert_nil expectation.invoke
128
+ end
129
+
130
+ def test_should_return_evaluated_proc
131
+ proc = lambda { 99 }
132
+ expectation = new_expectation.returns(proc)
133
+ assert_equal 99, expectation.invoke
134
+ end
135
+
136
+ def test_should_return_evaluated_proc_without_using_is_a_method
137
+ proc = lambda { 99 }
138
+ proc.define_instance_accessor(:called)
139
+ proc.called = false
140
+ proc.replace_instance_method(:is_a?) { self.called = true; true}
141
+ expectation = new_expectation.returns(proc)
142
+ expectation.invoke
143
+ assert_equal false, proc.called
144
+ end
145
+
146
+ def test_should_raise_runtime_exception
147
+ expectation = new_expectation.raises
148
+ assert_raise(RuntimeError) { expectation.invoke }
149
+ end
150
+
151
+ def test_should_raise_custom_exception
152
+ exception = Class.new(Exception)
153
+ expectation = new_expectation.raises(exception)
154
+ assert_raise(exception) { expectation.invoke }
155
+ end
156
+
157
+ def test_should_raise_same_instance_of_custom_exception
158
+ exception_klass = Class.new(StandardError)
159
+ expected_exception = exception_klass.new
160
+ expectation = new_expectation.raises(expected_exception)
161
+ actual_exception = assert_raise(exception_klass) { expectation.invoke }
162
+ assert_same expected_exception, actual_exception
163
+ end
164
+
165
+ def test_should_use_the_default_exception_message
166
+ expectation = new_expectation.raises(Exception)
167
+ exception = assert_raise(Exception) { expectation.invoke }
168
+ assert_equal Exception.new.message, exception.message
169
+ end
170
+
171
+ def test_should_raise_custom_exception_with_message
172
+ exception_msg = "exception message"
173
+ expectation = new_expectation.raises(Exception, exception_msg)
174
+ exception = assert_raise(Exception) { expectation.invoke }
175
+ assert_equal exception_msg, exception.message
176
+ end
177
+
178
+ def test_should_not_raise_error_on_verify_if_expected_call_was_made
179
+ expectation = new_expectation
180
+ expectation.invoke
181
+ assert_nothing_raised(ExpectationError) {
182
+ expectation.verify
183
+ }
184
+ end
185
+
186
+ def test_should_not_raise_error_on_verify_if_expected_call_was_made_at_least_once
187
+ expectation = new_expectation.at_least_once
188
+ 3.times {expectation.invoke}
189
+ assert_nothing_raised(ExpectationError) {
190
+ expectation.verify
191
+ }
192
+ end
193
+
194
+ def test_should_raise_error_on_verify_if_expected_call_was_not_made_at_least_once
195
+ expectation = new_expectation.with(1, 2, 3).at_least_once
196
+ e = assert_raise(ExpectationError) {
197
+ expectation.verify
198
+ }
199
+ assert_match(/expected calls: at least 1, actual calls: 0/i, e.message)
200
+ end
201
+
202
+ def test_should_not_raise_error_on_verify_if_expected_call_was_made_expected_number_of_times
203
+ expectation = new_expectation.times(2)
204
+ 2.times {expectation.invoke}
205
+ assert_nothing_raised(ExpectationError) {
206
+ expectation.verify
207
+ }
208
+ end
209
+
210
+ def test_should_expect_call_not_to_be_made
211
+ expectation = new_expectation
212
+ expectation.define_instance_accessor(:how_many_times)
213
+ expectation.replace_instance_method(:times) { |how_many_times| self.how_many_times = how_many_times }
214
+ expectation.never
215
+ assert_equal 0, expectation.how_many_times
216
+ end
217
+
218
+ def test_should_raise_error_on_verify_if_expected_call_was_made_too_few_times
219
+ expectation = new_expectation.times(2)
220
+ 1.times {expectation.invoke}
221
+ e = assert_raise(ExpectationError) {
222
+ expectation.verify
223
+ }
224
+ assert_match(/expected calls: 2, actual calls: 1/i, e.message)
225
+ end
226
+
227
+ def test_should_raise_error_on_verify_if_expected_call_was_made_too_many_times
228
+ expectation = new_expectation.times(2)
229
+ 3.times {expectation.invoke}
230
+ assert_raise(ExpectationError) {
231
+ expectation.verify
232
+ }
233
+ end
234
+
235
+ def test_should_yield_self_to_block
236
+ expectation = new_expectation
237
+ expectation.invoke
238
+ yielded_expectation = nil
239
+ expectation.verify { |x| yielded_expectation = x }
240
+ assert_equal expectation, yielded_expectation
241
+ end
242
+
243
+ def test_should_yield_to_block_before_raising_exception
244
+ yielded = false
245
+ assert_raise(ExpectationError) {
246
+ new_expectation.verify { |x| yielded = true }
247
+ }
248
+ assert yielded
249
+ end
250
+
251
+ def test_should_store_backtrace_from_point_where_expectation_was_created
252
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
253
+ assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
254
+ end
255
+
256
+ def test_should_set_backtrace_on_assertion_failed_error_to_point_where_expectation_was_created
257
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
258
+ error = assert_raise(ExpectationError) {
259
+ expectation.verify
260
+ }
261
+ assert_equal execution_point, ExecutionPoint.new(error.backtrace)
262
+ end
263
+
264
+ def test_should_display_expectation_message_in_exception_message
265
+ options = [:a, :b, {:c => 1, :d => 2}]
266
+ expectation = new_expectation.with(*options)
267
+ exception = assert_raise(ExpectationError) {
268
+ expectation.verify
269
+ }
270
+ assert exception.message.include?(expectation.method_signature)
271
+ end
272
+
273
+ def test_should_combine_method_name_and_pretty_parameters
274
+ arguments = 1, 2, {'a' => true, :b => false}, [1, 2, 3]
275
+ expectation = new_expectation.with(*arguments)
276
+ assert_equal "expected_method(#{PrettyParameters.new(arguments).pretty})", expectation.method_signature
277
+ end
278
+
279
+ def test_should_not_include_parameters_in_message
280
+ assert_equal "expected_method", new_expectation.method_signature
281
+ end
282
+
283
+ def test_should_always_verify_successfully
284
+ stub = Stub.new(nil, :expected_method)
285
+ assert stub.verify
286
+ stub.invoke
287
+ assert stub.verify
288
+ end
289
+
290
+ def test_should_raise_error_with_message_indicating_which_method_was_expected_to_be_called_on_which_mock_object
291
+ mock = Class.new { def mocha_inspect; 'mock'; end }.new
292
+ expectation = Expectation.new(mock, :expected_method)
293
+ e = assert_raise(ExpectationError) { expectation.verify }
294
+ assert_match "mock.expected_method", e.message
295
+ end
296
+
297
+ end
298
+
299
+ class ExpectationSimilarExpectationsTest < Test::Unit::TestCase
300
+
301
+ include Mocha
302
+
303
+ def new_expectation
304
+ Expectation.new(nil, :expected_method)
305
+ end
306
+
307
+ def test_should_find_expectations_to_the_same_method
308
+ expectation = new_expectation.with(1)
309
+ mock = Object.new
310
+ mock.define_instance_method(:expectations) { [expectation] }
311
+ failed_expectation = MissingExpectation.new(mock, :expected_method).with(2)
312
+ assert_equal [expectation], failed_expectation.similar_expectations
313
+ end
314
+
315
+ def test_should_report_similar_expectations
316
+ mock = Object.new
317
+ expectation_1 = new_expectation.with(1)
318
+ expectation_2 = new_expectation.with(2)
319
+ mock = Object.new
320
+ mock.define_instance_method(:expectations) { [expectation_1, expectation_2] }
321
+ missing_expectation = MissingExpectation.new(mock, :expected_method).with(3)
322
+ exception = assert_raise(ExpectationError) { missing_expectation.verify }
323
+ assert_equal "#{mock.mocha_inspect}.expected_method(3) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)\nexpected_method(2)", exception.message
324
+ end
325
+
326
+ def test_should_ignore_expectations_to_different_methods
327
+ expectation = new_expectation.with(1)
328
+ mock = Object.new
329
+ mock.define_instance_method(:expectations) { [expectation] }
330
+ failed_expectation = MissingExpectation.new(mock, :other_method).with(1)
331
+ assert failed_expectation.similar_expectations.empty?
332
+ end
333
+
334
+ def test_should_not_report_similar_expectations
335
+ expectation = new_expectation.with(1)
336
+ mock = Object.new
337
+ mock.define_instance_method(:expectations) { [expectation] }
338
+ mock.define_instance_method(:mocha_inspect) { 'mocha_inspect' }
339
+ missing_expectation = MissingExpectation.new(mock, :unexpected_method).with(1)
340
+ exception = assert_raise(ExpectationError) { missing_expectation.verify }
341
+ assert_equal "mocha_inspect.unexpected_method(1) - expected calls: 0, actual calls: 1", exception.message
342
+ end
343
+
344
+ def test_should_exclude_mocha_locations_from_backtrace
345
+ mocha_lib = "/username/workspace/mocha_wibble/lib/"
346
+ backtrace = [ mocha_lib + 'exclude/me/1', mocha_lib + 'exclude/me/2', '/keep/me', mocha_lib + 'exclude/me/3']
347
+ expectation = Expectation.new(nil, :expected_method, backtrace)
348
+ expectation.define_instance_method(:mocha_lib_directory) { mocha_lib }
349
+ assert_equal ['/keep/me'], expectation.filtered_backtrace
350
+ end
351
+
352
+ def test_should_determine_path_for_mocha_lib_directory
353
+ expectation = new_expectation()
354
+ assert_match Regexp.new("/lib/$"), expectation.mocha_lib_directory
355
+ end
356
+
357
+ end