mocha 0.4.0 → 0.5.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 (78) hide show
  1. data/Rakefile +28 -10
  2. data/examples/stubba.rb +1 -1
  3. data/lib/mocha/auto_verify.rb +6 -6
  4. data/lib/mocha/deprecation.rb +22 -0
  5. data/lib/mocha/exception_raiser.rb +17 -0
  6. data/lib/mocha/expectation.rb +167 -84
  7. data/lib/mocha/infinite_range.rb +4 -6
  8. data/lib/mocha/inspect.rb +3 -1
  9. data/lib/mocha/is_a.rb +9 -0
  10. data/lib/mocha/missing_expectation.rb +27 -0
  11. data/lib/mocha/mock.rb +191 -5
  12. data/lib/mocha/multiple_yields.rb +20 -0
  13. data/lib/mocha/no_yields.rb +11 -0
  14. data/lib/mocha/object.rb +11 -1
  15. data/lib/mocha/parameter_matchers.rb +9 -0
  16. data/lib/mocha/parameter_matchers/all_of.rb +39 -0
  17. data/lib/mocha/parameter_matchers/any_of.rb +44 -0
  18. data/lib/mocha/parameter_matchers/anything.rb +30 -0
  19. data/lib/mocha/parameter_matchers/has_entry.rb +39 -0
  20. data/lib/mocha/parameter_matchers/has_key.rb +39 -0
  21. data/lib/mocha/parameter_matchers/has_value.rb +39 -0
  22. data/lib/mocha/parameter_matchers/includes.rb +37 -0
  23. data/lib/mocha/return_values.rb +31 -0
  24. data/lib/mocha/single_return_value.rb +24 -0
  25. data/lib/mocha/single_yield.rb +18 -0
  26. data/lib/mocha/standalone.rb +2 -0
  27. data/lib/mocha/stub.rb +18 -0
  28. data/lib/mocha/yield_parameters.rb +31 -0
  29. data/test/{mocha_acceptance_test.rb → acceptance/mocha_acceptance_test.rb} +1 -1
  30. data/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +72 -0
  31. data/test/acceptance/parameter_matcher_acceptance_test.rb +63 -0
  32. data/test/{standalone_acceptance_test.rb → acceptance/standalone_acceptance_test.rb} +22 -1
  33. data/test/{stubba_acceptance_test.rb → acceptance/stubba_acceptance_test.rb} +1 -1
  34. data/test/deprecation_disabler.rb +15 -0
  35. data/test/{mocha_test_result_integration_test.rb → integration/mocha_test_result_integration_test.rb} +1 -1
  36. data/test/{stubba_integration_test.rb → integration/stubba_integration_test.rb} +1 -1
  37. data/test/{stubba_test_result_integration_test.rb → integration/stubba_test_result_integration_test.rb} +1 -1
  38. data/test/test_helper.rb +8 -0
  39. data/test/test_runner.rb +31 -0
  40. data/test/{mocha → unit}/any_instance_method_test.rb +0 -0
  41. data/test/unit/array_inspect_test.rb +16 -0
  42. data/test/{mocha → unit}/auto_verify_test.rb +0 -0
  43. data/test/{mocha → unit}/central_test.rb +0 -0
  44. data/test/{mocha → unit}/class_method_test.rb +0 -0
  45. data/test/unit/date_time_inspect_test.rb +21 -0
  46. data/test/unit/expectation_raiser_test.rb +28 -0
  47. data/test/{mocha → unit}/expectation_test.rb +105 -63
  48. data/test/unit/hash_inspect_test.rb +16 -0
  49. data/test/{mocha → unit}/infinite_range_test.rb +8 -5
  50. data/test/{mocha → unit}/metaclass_test.rb +0 -0
  51. data/test/unit/missing_expectation_test.rb +51 -0
  52. data/test/unit/mock_test.rb +351 -0
  53. data/test/unit/multiple_yields_test.rb +18 -0
  54. data/test/unit/no_yield_test.rb +18 -0
  55. data/test/unit/object_inspect_test.rb +35 -0
  56. data/test/{mocha → unit}/object_test.rb +0 -0
  57. data/test/unit/parameter_matchers/all_of_test.rb +26 -0
  58. data/test/unit/parameter_matchers/any_of_test.rb +26 -0
  59. data/test/unit/parameter_matchers/anything_test.rb +21 -0
  60. data/test/unit/parameter_matchers/has_entry_test.rb +25 -0
  61. data/test/unit/parameter_matchers/has_key_test.rb +25 -0
  62. data/test/unit/parameter_matchers/has_value_test.rb +25 -0
  63. data/test/unit/parameter_matchers/includes_test.rb +25 -0
  64. data/test/unit/parameter_matchers/stub_matcher.rb +22 -0
  65. data/test/{mocha → unit}/pretty_parameters_test.rb +0 -0
  66. data/test/unit/return_values_test.rb +63 -0
  67. data/test/{mocha → unit}/setup_and_teardown_test.rb +0 -0
  68. data/test/unit/single_return_value_test.rb +33 -0
  69. data/test/unit/single_yield_test.rb +18 -0
  70. data/test/unit/string_inspect_test.rb +11 -0
  71. data/test/unit/stub_test.rb +24 -0
  72. data/test/unit/yield_parameters_test.rb +93 -0
  73. metadata +117 -73
  74. data/lib/mocha/mock_methods.rb +0 -122
  75. data/test/all_tests.rb +0 -75
  76. data/test/mocha/inspect_test.rb +0 -90
  77. data/test/mocha/mock_methods_test.rb +0 -235
  78. data/test/mocha/mock_test.rb +0 -84
@@ -1,90 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/inspect'
3
- require 'method_definer'
4
-
5
- class InspectTest
6
- def self.suite
7
- suite = Test::Unit::TestSuite.new('MochaInspectTests')
8
- suite << ObjectInspectTest.suite
9
- suite << TimeDateInspectTest.suite
10
- suite << StringInspectTest.suite
11
- suite << ArrayInstanceTest.suite
12
- suite << HashInspectTest.suite
13
- suite
14
- end
15
- end
16
-
17
- class ObjectInspectTest < Test::Unit::TestCase
18
-
19
- def test_should_provide_custom_representation_of_object
20
- object = Object.new
21
- assert_equal "#<#{object.class}:#{"0x%x" % object.__id__}>", object.mocha_inspect
22
- end
23
-
24
- def test_should_use_underscored_id_instead_of_object_id_or_id_so_that_they_can_be_stubbed
25
- object = Object.new
26
- object.define_instance_accessor(:called)
27
- object.called = false
28
- object.replace_instance_method(:object_id) { self.called = true; 1 }
29
- object.replace_instance_method(:id) { self.called = true; 1 }
30
- object.mocha_inspect
31
- assert_equal false, object.called
32
- end
33
-
34
- end
35
-
36
- class TimeDateInspectTest < Test::Unit::TestCase
37
-
38
- def test_should_use_include_date_in_seconds
39
- time = Time.now
40
- assert_equal "#{time.inspect} (#{time.to_f} secs)", time.mocha_inspect
41
- end
42
-
43
- def test_should_use_to_s_for_date
44
- date = Date.new(2006, 1, 1)
45
- assert_equal date.to_s, date.mocha_inspect
46
- end
47
-
48
- def test_should_use_to_s_for_datetime
49
- datetime = DateTime.new(2006, 1, 1)
50
- assert_equal datetime.to_s, datetime.mocha_inspect
51
- end
52
-
53
- end
54
-
55
- class StringInspectTest < Test::Unit::TestCase
56
-
57
- def test_should_replace_escaped_quotes_with_single_quote
58
- string = "my_string"
59
- assert_equal "'my_string'", string.mocha_inspect
60
- end
61
-
62
- end
63
-
64
- class ArrayInstanceTest < Test::Unit::TestCase
65
-
66
- def test_should_use_inspect
67
- array = [1, 2]
68
- assert_equal array.inspect, array.mocha_inspect
69
- end
70
-
71
- def test_should_use_mocha_inspect_on_each_item
72
- array = [1, 2, "chris"]
73
- assert_equal "[1, 2, 'chris']", array.mocha_inspect
74
- end
75
-
76
- end
77
-
78
- class HashInspectTest < Test::Unit::TestCase
79
-
80
- def test_should_keep_spacing_between_key_value
81
- hash = {:a => true}
82
- assert_equal '{:a => true}', hash.mocha_inspect
83
- end
84
-
85
- def test_should_use_mocha_inspect_on_each_item
86
- hash = {:a => 'mocha'}
87
- assert_equal "{:a => 'mocha'}", hash.mocha_inspect
88
- end
89
-
90
- end
@@ -1,235 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/mock_methods'
3
- require 'set'
4
-
5
- class MockMethodsTest < Test::Unit::TestCase
6
-
7
- include Mocha
8
-
9
- def test_should_create_and_add_expectations
10
- mock = Object.new
11
- mock.extend(MockMethods)
12
-
13
- expectation1 = mock.expects(:method1)
14
- expectation2 = mock.expects(:method2)
15
-
16
- assert_equal [expectation1, expectation2].to_set, mock.expectations.to_set
17
- end
18
-
19
- def test_should_pass_backtrace_into_expectation
20
- mock = Object.new
21
- mock.extend(MockMethods)
22
- backtrace = Object.new
23
- expectation = mock.expects(:method1, backtrace)
24
- assert_equal backtrace, expectation.backtrace
25
- end
26
-
27
- def test_should_pass_backtrace_into_stub
28
- mock = Object.new
29
- mock.extend(MockMethods)
30
- backtrace = Object.new
31
- stub = mock.stubs(:method1, backtrace)
32
- assert_equal backtrace, stub.backtrace
33
- end
34
-
35
- def test_should_create_and_add_stubs
36
- mock = Object.new
37
- mock.extend(MockMethods)
38
-
39
- stub1 = mock.stubs(:method1)
40
- stub2 = mock.stubs(:method2)
41
-
42
- assert_equal [stub1, stub2].to_set, mock.expectations.to_set
43
- end
44
-
45
- def test_should_find_matching_expectation
46
- mock = Object.new
47
- mock.extend(MockMethods)
48
-
49
- expectation1 = mock.expects(:my_method).with(:argument1, :argument2)
50
- expectation2 = mock.expects(:my_method).with(:argument3, :argument4)
51
-
52
- assert_same expectation2, mock.matching_expectation(:my_method, :argument3, :argument4)
53
- end
54
-
55
- def test_should_find_most_recent_matching_expectation
56
- mock = Object.new
57
- mock.extend(MockMethods)
58
-
59
- expectation1 = mock.expects(:my_method).with(:argument1, :argument2)
60
- expectation2 = mock.expects(:my_method).with(:argument1, :argument2)
61
-
62
- assert_same expectation2, mock.matching_expectation(:my_method, :argument1, :argument2)
63
- end
64
-
65
- def test_should_invoke_expectation_and_return_result
66
- mock = Object.new
67
- mock.extend(MockMethods)
68
- mock.expects(:my_method).returns(:result)
69
-
70
- result = mock.my_method
71
-
72
- assert_equal :result, result
73
- end
74
-
75
- def test_should_not_raise_error_if_stubbing_everything
76
- mock = Class.new { def initialize; @stub_everything = true; end }.new
77
- mock.extend(MockMethods)
78
-
79
- result = nil
80
- assert_nothing_raised(ExpectationError) do
81
- result = mock.unexpected_method
82
- end
83
- assert_nil result
84
- end
85
-
86
- def test_should_raise_no_method_error
87
- mock = Object.new
88
- mock.extend(MockMethods)
89
- assert_raise(NoMethodError) do
90
- mock.super_method_missing(nil)
91
- end
92
- end
93
-
94
- def test_should_raise_assertion_error_for_unexpected_method_call
95
- mock = Object.new
96
- mock.extend(MockMethods)
97
- error = assert_raise(ExpectationError) do
98
- mock.unexpected_method_called(:my_method, :argument1, :argument2)
99
- end
100
- assert_match /my_method/, error.message
101
- assert_match /argument1/, error.message
102
- assert_match /argument2/, error.message
103
- end
104
-
105
- def test_should_indicate_unexpected_method_called
106
- mock = Object.new
107
- mock.extend(MockMethods)
108
- class << mock
109
- attr_accessor :symbol, :arguments
110
- def unexpected_method_called(symbol, *arguments)
111
- self.symbol, self.arguments = symbol, arguments
112
- end
113
- end
114
- mock.my_method(:argument1, :argument2)
115
-
116
- assert_equal :my_method, mock.symbol
117
- assert_equal [:argument1, :argument2], mock.arguments
118
- end
119
-
120
- def test_should_call_method_missing_for_parent
121
- mock = Object.new
122
- mock.extend(MockMethods)
123
- class << mock
124
- attr_accessor :symbol, :arguments
125
- def super_method_missing(symbol, *arguments, &block)
126
- self.symbol, self.arguments = symbol, arguments
127
- end
128
- end
129
-
130
- mock.my_method(:argument1, :argument2)
131
-
132
- assert_equal :my_method, mock.symbol
133
- assert_equal [:argument1, :argument2], mock.arguments
134
- end
135
-
136
- def test_should_verify_that_all_expectations_have_been_fulfilled
137
- mock = Object.new
138
- mock.extend(MockMethods)
139
- mock.expects(:method1)
140
- mock.expects(:method2)
141
- mock.method1
142
- assert_raise(ExpectationError) do
143
- mock.verify
144
- end
145
- end
146
-
147
- def test_should_report_possible_expectations
148
- mock = Object.new.extend(MockMethods)
149
- mock.expects(:expected_method).with(1)
150
- exception = assert_raise(ExpectationError) { mock.expected_method(2) }
151
- assert_equal "#{mock.mocha_inspect}.expected_method(2) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)", exception.message
152
- end
153
-
154
- def test_should_pass_block_through_to_expectations_verify_method
155
- mock = Object.new
156
- mock.extend(MockMethods)
157
- expected_expectation = mock.expects(:method1)
158
- mock.method1
159
- expectations = []
160
- mock.verify() { |expectation| expectations << expectation }
161
- assert_equal [expected_expectation], expectations
162
- end
163
-
164
- def test_should_yield_supplied_parameters_to_block
165
- mock = Object.new
166
- mock.extend(MockMethods)
167
- parameters_for_yield = [1, 2, 3]
168
- mock.expects(:method1).yields(*parameters_for_yield)
169
- yielded_parameters = nil
170
- mock.method1() { |*parameters| yielded_parameters = parameters }
171
- assert_equal parameters_for_yield, yielded_parameters
172
- end
173
-
174
- def test_should_respond_to_expected_method
175
- mock = Object.new
176
- mock.extend(MockMethods)
177
- mock.expects(:method1)
178
- assert_equal true, mock.respond_to?(:method1)
179
- end
180
-
181
- def test_should_not_respond_to_unexpected_method
182
- mock = Object.new
183
- mock.extend(MockMethods)
184
- assert_equal false, mock.respond_to?(:method1)
185
- end
186
-
187
- def test_should_set_up_multiple_expectations_with_return_values
188
- mock = Object.new
189
- mock.extend(MockMethods)
190
- mock.expects(:method1 => :result1, :method2 => :result2)
191
- assert_equal :result1, mock.method1
192
- assert_equal :result2, mock.method2
193
- end
194
-
195
- def test_should_set_up_multiple_stubs_with_return_values
196
- mock = Object.new
197
- mock.extend(MockMethods)
198
- mock.stubs(:method1 => :result1, :method2 => :result2)
199
- assert_equal :result1, mock.method1
200
- assert_equal :result2, mock.method2
201
- end
202
-
203
- def test_should_match_most_recent_call_to_expects
204
- mock = Object.new
205
- mock.extend(MockMethods)
206
- mock.expects(:method1).returns(0)
207
- mock.expects(:method1).returns(1)
208
- assert_equal 1, mock.method1
209
- end
210
-
211
- def test_should_match_most_recent_call_to_stubs
212
- mock = Object.new
213
- mock.extend(MockMethods)
214
- mock.stubs(:method1).returns(0)
215
- mock.stubs(:method1).returns(1)
216
- assert_equal 1, mock.method1
217
- end
218
-
219
- def test_should_match_call_to_expects_with_previous_call_to_stubs
220
- mock = Object.new
221
- mock.extend(MockMethods)
222
- mock.stubs(:method1).returns(0)
223
- mock.expects(:method1).returns(1)
224
- assert_equal 1, mock.method1
225
- end
226
-
227
- def test_should_match_call_to_stubs_with_previous_call_to_expects
228
- mock = Object.new
229
- mock.extend(MockMethods)
230
- mock.expects(:method1).returns(0)
231
- mock.stubs(:method1).returns(1)
232
- assert_equal 1, mock.method1
233
- end
234
-
235
- end
@@ -1,84 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/mock'
3
- require 'mocha/expectation_error'
4
-
5
- class MockTest < Test::Unit::TestCase
6
-
7
- include Mocha
8
-
9
- def test_should_include_mocha_methods
10
- assert Mock.included_modules.include?(MockMethods)
11
- end
12
-
13
- def test_should_set_single_expectation
14
- mock = Mock.new
15
- mock.expects(:method1).returns(1)
16
- assert_nothing_raised(ExpectationError) do
17
- assert_equal 1, mock.method1
18
- end
19
- end
20
-
21
- def test_should_build_and_store_expectations
22
- mock = Mock.new
23
- expectation = mock.expects(:method1)
24
- assert_not_nil expectation
25
- assert_equal [expectation], mock.expectations
26
- end
27
-
28
- def test_should_not_stub_everything_by_default
29
- mock = Mock.new
30
- assert_equal false, mock.stub_everything
31
- end
32
-
33
- def test_should_stub_everything
34
- mock = Mock.new(stub_everything = true)
35
- assert_equal true, mock.stub_everything
36
- end
37
-
38
- def test_should_display_object_id_for_inspect_if_mock_has_no_name
39
- mock = Mock.new
40
- assert_match Regexp.new("#<Mock:0x[0-9A-Fa-f]{6}>"), mock.mocha_inspect
41
- end
42
-
43
- def test_should_display_name_for_inspect_if_mock_has_name
44
- mock = Mock.new(false, 'named_mock')
45
- assert_equal "#<Mock:named_mock>", mock.mocha_inspect
46
- end
47
-
48
- def test_should_give_name_in_inspect_message
49
- mock = Mock.new(false, 'named_mock')
50
- assert_equal "#<Mock:named_mock>", mock.mocha_inspect
51
- end
52
-
53
- def test_should_be_able_to_extend_mock_object_with_module
54
- mock = Mock.new
55
- assert_nothing_raised(ExpectationError) { mock.extend(Module.new) }
56
- end
57
-
58
- def test_should_be_equal
59
- mock = Mock.new
60
- assert_equal true, mock.eql?(mock)
61
- end
62
-
63
- def test_should_be_able_to_mock_standard_object_methods
64
- mock = Mock.new
65
- object_methods = Object.public_instance_methods.reject { |m| m =~ /^__.*__$/ }.sort
66
- object_methods.each { |method| mock.__expects__(method.to_sym).returns(method) }
67
- object_methods.each { |method| assert_equal method, mock.__send__(method.to_sym) }
68
- assert_nothing_raised(ExpectationError) { mock.verify }
69
- end
70
-
71
- def test_should_be_able_to_stub_standard_object_methods
72
- mock = Mock.new
73
- object_methods = Object.public_instance_methods.reject { |m| m =~ /^__.*__$/ }.sort
74
- object_methods.each { |method| mock.__stubs__(method.to_sym).returns(method) }
75
- object_methods.each { |method| assert_equal method, mock.__send__(method.to_sym) }
76
- end
77
-
78
- def test_should_respond_to_expected_methods
79
- mock = Mock.new
80
- mock.expects(:method1)
81
- assert_equal true, mock.respond_to?(:method1)
82
- end
83
-
84
- end