mocha 1.8.0 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (274) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.rubocop.yml +3 -0
  4. data/.rubocop_todo.yml +7 -30
  5. data/.yardopts +1 -0
  6. data/CONTRIBUTING.md +1 -1
  7. data/README.md +56 -31
  8. data/RELEASE.md +166 -0
  9. data/Rakefile +15 -7
  10. data/init.rb +1 -3
  11. data/lib/mocha/any_instance_method.rb +11 -53
  12. data/lib/mocha/api.rb +119 -56
  13. data/lib/mocha/block_matcher.rb +31 -0
  14. data/lib/mocha/cardinality.rb +52 -34
  15. data/lib/mocha/class_methods.rb +17 -15
  16. data/lib/mocha/configuration.rb +351 -67
  17. data/lib/mocha/deprecation.rb +2 -1
  18. data/lib/mocha/detection/test_unit.rb +1 -3
  19. data/lib/mocha/exception_raiser.rb +2 -1
  20. data/lib/mocha/expectation.rb +120 -81
  21. data/lib/mocha/expectation_error.rb +1 -3
  22. data/lib/mocha/expectation_list.rb +6 -6
  23. data/lib/mocha/inspect.rb +30 -26
  24. data/lib/mocha/instance_method.rb +19 -2
  25. data/lib/mocha/integration/mini_test.rb +7 -0
  26. data/lib/mocha/integration/test_unit.rb +7 -0
  27. data/lib/mocha/integration.rb +1 -3
  28. data/lib/mocha/invocation.rb +77 -0
  29. data/lib/mocha/macos_version.rb +5 -0
  30. data/lib/mocha/minitest.rb +6 -1
  31. data/lib/mocha/mock.rb +80 -39
  32. data/lib/mocha/mockery.rb +34 -80
  33. data/lib/mocha/names.rb +1 -1
  34. data/lib/mocha/object_methods.rb +13 -19
  35. data/lib/mocha/parameter_matchers/all_of.rb +1 -1
  36. data/lib/mocha/parameter_matchers/any_of.rb +1 -1
  37. data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -9
  38. data/lib/mocha/parameter_matchers/has_entries.rb +2 -3
  39. data/lib/mocha/parameter_matchers/has_entry.rb +24 -16
  40. data/lib/mocha/parameter_matchers/has_key.rb +2 -3
  41. data/lib/mocha/parameter_matchers/has_keys.rb +53 -0
  42. data/lib/mocha/parameter_matchers/has_value.rb +2 -3
  43. data/lib/mocha/parameter_matchers/includes.rb +2 -0
  44. data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
  45. data/lib/mocha/parameter_matchers/is_a.rb +2 -3
  46. data/lib/mocha/parameter_matchers/not.rb +2 -3
  47. data/lib/mocha/parameter_matchers.rb +2 -1
  48. data/lib/mocha/raised_exception.rb +11 -0
  49. data/lib/mocha/return_values.rb +3 -3
  50. data/lib/mocha/setup.rb +5 -0
  51. data/lib/mocha/single_return_value.rb +2 -1
  52. data/lib/mocha/singleton_class.rb +9 -0
  53. data/lib/mocha/state_machine.rb +31 -40
  54. data/lib/mocha/stubbed_method.rb +125 -0
  55. data/lib/mocha/test_unit.rb +6 -1
  56. data/lib/mocha/thrower.rb +2 -1
  57. data/lib/mocha/thrown_object.rb +12 -0
  58. data/lib/mocha/version.rb +1 -1
  59. data/lib/mocha/yield_parameters.rb +5 -11
  60. data/lib/mocha.rb +8 -0
  61. data/mocha.gemspec +14 -10
  62. metadata +16 -233
  63. data/bin/build-matrix +0 -83
  64. data/docs/CNAME +0 -1
  65. data/docs/Mocha/API.html +0 -1056
  66. data/docs/Mocha/ClassMethods.html +0 -267
  67. data/docs/Mocha/Configuration.html +0 -626
  68. data/docs/Mocha/Expectation.html +0 -2709
  69. data/docs/Mocha/ExpectationError.html +0 -157
  70. data/docs/Mocha/ExpectationErrorFactory.html +0 -269
  71. data/docs/Mocha/Hooks.html +0 -385
  72. data/docs/Mocha/Integration/MiniTest/Adapter.html +0 -165
  73. data/docs/Mocha/Integration/MiniTest.html +0 -123
  74. data/docs/Mocha/Integration/TestUnit/Adapter.html +0 -165
  75. data/docs/Mocha/Integration/TestUnit.html +0 -123
  76. data/docs/Mocha/Integration.html +0 -125
  77. data/docs/Mocha/Mock.html +0 -1197
  78. data/docs/Mocha/ObjectMethods.html +0 -712
  79. data/docs/Mocha/ParameterMatchers/AllOf.html +0 -154
  80. data/docs/Mocha/ParameterMatchers/AnyOf.html +0 -154
  81. data/docs/Mocha/ParameterMatchers/AnyParameters.html +0 -153
  82. data/docs/Mocha/ParameterMatchers/Anything.html +0 -153
  83. data/docs/Mocha/ParameterMatchers/Base.html +0 -448
  84. data/docs/Mocha/ParameterMatchers/Equals.html +0 -154
  85. data/docs/Mocha/ParameterMatchers/EquivalentUri.html +0 -153
  86. data/docs/Mocha/ParameterMatchers/HasEntries.html +0 -154
  87. data/docs/Mocha/ParameterMatchers/HasEntry.html +0 -154
  88. data/docs/Mocha/ParameterMatchers/HasKey.html +0 -154
  89. data/docs/Mocha/ParameterMatchers/HasValue.html +0 -154
  90. data/docs/Mocha/ParameterMatchers/Includes.html +0 -154
  91. data/docs/Mocha/ParameterMatchers/InstanceOf.html +0 -154
  92. data/docs/Mocha/ParameterMatchers/IsA.html +0 -153
  93. data/docs/Mocha/ParameterMatchers/KindOf.html +0 -154
  94. data/docs/Mocha/ParameterMatchers/Not.html +0 -154
  95. data/docs/Mocha/ParameterMatchers/Optionally.html +0 -153
  96. data/docs/Mocha/ParameterMatchers/RegexpMatches.html +0 -154
  97. data/docs/Mocha/ParameterMatchers/RespondsWith.html +0 -154
  98. data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +0 -154
  99. data/docs/Mocha/ParameterMatchers.html +0 -3049
  100. data/docs/Mocha/Sequence.html +0 -149
  101. data/docs/Mocha/StateMachine/State.html +0 -141
  102. data/docs/Mocha/StateMachine/StatePredicate.html +0 -141
  103. data/docs/Mocha/StateMachine.html +0 -539
  104. data/docs/Mocha/StubbingError.html +0 -150
  105. data/docs/Mocha/UnexpectedInvocation.html +0 -140
  106. data/docs/Mocha.html +0 -127
  107. data/docs/_index.html +0 -537
  108. data/docs/class_list.html +0 -51
  109. data/docs/css/common.css +0 -1
  110. data/docs/css/full_list.css +0 -58
  111. data/docs/css/style.css +0 -496
  112. data/docs/file.COPYING.html +0 -81
  113. data/docs/file.MIT-LICENSE.html +0 -85
  114. data/docs/file.README.html +0 -418
  115. data/docs/file.RELEASE.html +0 -875
  116. data/docs/file_list.html +0 -71
  117. data/docs/frames.html +0 -17
  118. data/docs/index.html +0 -418
  119. data/docs/js/app.js +0 -292
  120. data/docs/js/full_list.js +0 -216
  121. data/docs/js/jquery.js +0 -4
  122. data/docs/method_list.html +0 -571
  123. data/docs/top-level-namespace.html +0 -118
  124. data/gemfiles/Gemfile.minitest.1.3.0 +0 -7
  125. data/gemfiles/Gemfile.minitest.1.4.0 +0 -7
  126. data/gemfiles/Gemfile.minitest.1.4.1 +0 -7
  127. data/gemfiles/Gemfile.minitest.1.4.2 +0 -7
  128. data/gemfiles/Gemfile.minitest.2.0.0 +0 -7
  129. data/gemfiles/Gemfile.minitest.2.0.1 +0 -7
  130. data/gemfiles/Gemfile.minitest.2.11.0 +0 -7
  131. data/gemfiles/Gemfile.minitest.2.11.2 +0 -7
  132. data/gemfiles/Gemfile.minitest.2.3.0 +0 -7
  133. data/gemfiles/Gemfile.test-unit.2.0.0 +0 -7
  134. data/gemfiles/Gemfile.test-unit.2.0.1 +0 -7
  135. data/gemfiles/Gemfile.test-unit.2.0.3 +0 -7
  136. data/lib/mocha/class_method.rb +0 -113
  137. data/lib/mocha/mini_test.rb +0 -5
  138. data/lib/mocha/module_method.rb +0 -6
  139. data/lib/mocha/module_methods.rb +0 -10
  140. data/lib/mocha/multiple_yields.rb +0 -15
  141. data/lib/mocha/no_yields.rb +0 -5
  142. data/lib/mocha/parameter_matchers/object.rb +0 -15
  143. data/lib/mocha/pretty_parameters.rb +0 -24
  144. data/lib/mocha/single_yield.rb +0 -13
  145. data/lib/mocha/standalone.rb +0 -4
  146. data/lib/mocha/unexpected_invocation.rb +0 -24
  147. data/lib/mocha_standalone.rb +0 -4
  148. data/test/acceptance/acceptance_test_helper.rb +0 -35
  149. data/test/acceptance/bug_18914_test.rb +0 -38
  150. data/test/acceptance/bug_21465_test.rb +0 -32
  151. data/test/acceptance/bug_21563_test.rb +0 -23
  152. data/test/acceptance/exception_rescue_test.rb +0 -54
  153. data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -56
  154. data/test/acceptance/expected_invocation_count_test.rb +0 -230
  155. data/test/acceptance/failure_messages_test.rb +0 -62
  156. data/test/acceptance/issue_272_test.rb +0 -51
  157. data/test/acceptance/issue_65_test.rb +0 -65
  158. data/test/acceptance/issue_70_test.rb +0 -54
  159. data/test/acceptance/mocha_example_test.rb +0 -97
  160. data/test/acceptance/mocha_test_result_test.rb +0 -84
  161. data/test/acceptance/mock_test.rb +0 -98
  162. data/test/acceptance/mock_with_initializer_block_test.rb +0 -56
  163. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -76
  164. data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -67
  165. data/test/acceptance/optional_parameters_test.rb +0 -68
  166. data/test/acceptance/parameter_matcher_test.rb +0 -300
  167. data/test/acceptance/partial_mocks_test.rb +0 -45
  168. data/test/acceptance/prepend_test.rb +0 -87
  169. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -77
  170. data/test/acceptance/raise_exception_test.rb +0 -37
  171. data/test/acceptance/return_value_test.rb +0 -50
  172. data/test/acceptance/sequence_test.rb +0 -190
  173. data/test/acceptance/states_test.rb +0 -72
  174. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +0 -65
  175. data/test/acceptance/stub_any_instance_method_test.rb +0 -282
  176. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -106
  177. data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -79
  178. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -74
  179. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +0 -145
  180. data/test/acceptance/stub_everything_test.rb +0 -54
  181. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -92
  182. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -68
  183. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -68
  184. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -137
  185. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -77
  186. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -76
  187. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -68
  188. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -71
  189. data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -39
  190. data/test/acceptance/stub_module_method_test.rb +0 -201
  191. data/test/acceptance/stub_test.rb +0 -50
  192. data/test/acceptance/stubba_example_test.rb +0 -94
  193. data/test/acceptance/stubba_test_result_test.rb +0 -72
  194. data/test/acceptance/stubbing_error_backtrace_test.rb +0 -64
  195. data/test/acceptance/stubbing_frozen_object_test.rb +0 -89
  196. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -53
  197. data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -63
  198. data/test/acceptance/stubbing_nil_test.rb +0 -60
  199. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +0 -141
  200. data/test/acceptance/stubbing_non_existent_class_method_test.rb +0 -157
  201. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +0 -145
  202. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -128
  203. data/test/acceptance/stubbing_non_public_class_method_test.rb +0 -163
  204. data/test/acceptance/stubbing_non_public_instance_method_test.rb +0 -141
  205. data/test/acceptance/stubbing_on_non_mock_object_test.rb +0 -70
  206. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -34
  207. data/test/acceptance/throw_test.rb +0 -43
  208. data/test/acceptance/unexpected_invocation_test.rb +0 -24
  209. data/test/acceptance/unstubbing_test.rb +0 -195
  210. data/test/assertions.rb +0 -8
  211. data/test/deprecation_disabler.rb +0 -15
  212. data/test/execution_point.rb +0 -36
  213. data/test/integration/mini_test_test.rb +0 -8
  214. data/test/integration/shared_tests.rb +0 -176
  215. data/test/integration/test_unit_test.rb +0 -8
  216. data/test/method_definer.rb +0 -22
  217. data/test/mini_test_result.rb +0 -96
  218. data/test/minitest_result.rb +0 -48
  219. data/test/simple_counter.rb +0 -11
  220. data/test/test_helper.rb +0 -58
  221. data/test/test_runner.rb +0 -56
  222. data/test/test_unit_result.rb +0 -22
  223. data/test/unit/any_instance_method_test.rb +0 -150
  224. data/test/unit/array_inspect_test.rb +0 -14
  225. data/test/unit/backtrace_filter_test.rb +0 -17
  226. data/test/unit/cardinality_test.rb +0 -54
  227. data/test/unit/central_test.rb +0 -99
  228. data/test/unit/change_state_side_effect_test.rb +0 -37
  229. data/test/unit/class_method_test.rb +0 -254
  230. data/test/unit/class_methods_test.rb +0 -69
  231. data/test/unit/configuration_test.rb +0 -37
  232. data/test/unit/date_time_inspect_test.rb +0 -19
  233. data/test/unit/exception_raiser_test.rb +0 -40
  234. data/test/unit/expectation_list_test.rb +0 -80
  235. data/test/unit/expectation_test.rb +0 -492
  236. data/test/unit/hash_inspect_test.rb +0 -14
  237. data/test/unit/hooks_test.rb +0 -35
  238. data/test/unit/in_state_ordering_constraint_test.rb +0 -39
  239. data/test/unit/method_matcher_test.rb +0 -26
  240. data/test/unit/mock_test.rb +0 -363
  241. data/test/unit/mockery_test.rb +0 -174
  242. data/test/unit/module_methods_test.rb +0 -17
  243. data/test/unit/multiple_yields_test.rb +0 -16
  244. data/test/unit/no_yields_test.rb +0 -16
  245. data/test/unit/object_inspect_test.rb +0 -58
  246. data/test/unit/object_methods_test.rb +0 -62
  247. data/test/unit/parameter_matchers/all_of_test.rb +0 -24
  248. data/test/unit/parameter_matchers/any_of_test.rb +0 -24
  249. data/test/unit/parameter_matchers/anything_test.rb +0 -19
  250. data/test/unit/parameter_matchers/equals_test.rb +0 -23
  251. data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -50
  252. data/test/unit/parameter_matchers/has_entries_test.rb +0 -51
  253. data/test/unit/parameter_matchers/has_entry_test.rb +0 -128
  254. data/test/unit/parameter_matchers/has_key_test.rb +0 -54
  255. data/test/unit/parameter_matchers/has_value_test.rb +0 -55
  256. data/test/unit/parameter_matchers/includes_test.rb +0 -106
  257. data/test/unit/parameter_matchers/instance_of_test.rb +0 -23
  258. data/test/unit/parameter_matchers/is_a_test.rb +0 -23
  259. data/test/unit/parameter_matchers/kind_of_test.rb +0 -23
  260. data/test/unit/parameter_matchers/not_test.rb +0 -24
  261. data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -45
  262. data/test/unit/parameter_matchers/responds_with_test.rb +0 -38
  263. data/test/unit/parameter_matchers/stub_matcher.rb +0 -23
  264. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +0 -23
  265. data/test/unit/parameters_matcher_test.rb +0 -119
  266. data/test/unit/receivers_test.rb +0 -96
  267. data/test/unit/return_values_test.rb +0 -61
  268. data/test/unit/sequence_test.rb +0 -100
  269. data/test/unit/single_return_value_test.rb +0 -12
  270. data/test/unit/single_yield_test.rb +0 -16
  271. data/test/unit/state_machine_test.rb +0 -96
  272. data/test/unit/string_inspect_test.rb +0 -9
  273. data/test/unit/thrower_test.rb +0 -18
  274. data/test/unit/yield_parameters_test.rb +0 -91
data/test/test_runner.rb DELETED
@@ -1,56 +0,0 @@
1
- require 'assertions'
2
-
3
- require 'mocha/detection/mini_test'
4
-
5
- module TestRunner
6
- def run_as_test(&block)
7
- run_as_tests(:test_me => block)
8
- end
9
-
10
- def run_as_tests(methods = {})
11
- base_class = Mocha::TestCase
12
- test_class = Class.new(base_class) do
13
- include Assertions
14
-
15
- methods.each do |(method_name, proc)|
16
- define_method(method_name, proc)
17
- end
18
- end
19
-
20
- tests = methods.keys.select { |m| m.to_s[/^test/] }.map { |m| test_class.new(m) }
21
-
22
- if Mocha::Detection::MiniTest.testcase && (ENV['MOCHA_RUN_INTEGRATION_TESTS'] != 'test-unit')
23
- minitest_version = Gem::Version.new(Mocha::Detection::MiniTest.version)
24
- if Gem::Requirement.new('>= 5.0.0').satisfied_by?(minitest_version)
25
- require File.expand_path('../minitest_result', __FILE__)
26
- tests.each(&:run)
27
- Minitest::Runnable.runnables.delete(test_class)
28
- test_result = MinitestResult.new(tests)
29
- elsif Gem::Requirement.new('> 0.0.0', '< 5.0.0').satisfied_by?(minitest_version)
30
- require File.expand_path('../mini_test_result', __FILE__)
31
- runner = MiniTest::Unit.new
32
- tests.each do |test|
33
- test.run(runner)
34
- end
35
- test_result = MiniTestResult.new(runner, tests)
36
- end
37
- else
38
- require File.expand_path('../test_unit_result', __FILE__)
39
- test_result = TestUnitResult.build_test_result
40
- tests.each do |test|
41
- test.run(test_result) {}
42
- end
43
- end
44
-
45
- test_result
46
- end
47
-
48
- def assert_passed(test_result)
49
- flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0
50
- flunk "Test failed unexpectedly with message: #{test_result.errors.map(&:exception)}" if test_result.error_count > 0
51
- end
52
-
53
- def assert_failed(test_result)
54
- flunk 'Test passed unexpectedly' unless test_result.failure_count + test_result.error_count > 0
55
- end
56
- end
@@ -1,22 +0,0 @@
1
- require 'test/unit/testresult'
2
-
3
- class TestUnitResult
4
- def self.build_test_result
5
- test_result = Test::Unit::TestResult.new
6
- class << test_result
7
- attr_reader :failures, :errors
8
- def failure_messages
9
- failures.map(&:message)
10
- end
11
-
12
- def failure_message_lines
13
- failure_messages.map { |message| message.split("\n") }.flatten
14
- end
15
-
16
- def error_messages
17
- errors.map(&:message)
18
- end
19
- end
20
- test_result
21
- end
22
- end
@@ -1,150 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'method_definer'
3
- require 'mocha/mock'
4
- require 'mocha/any_instance_method'
5
-
6
- class AnyInstanceMethodTest < Mocha::TestCase
7
- include Mocha
8
-
9
- unless RUBY_V2_PLUS
10
- def test_should_hide_original_method
11
- klass = Class.new { def method_x; end }
12
- method = AnyInstanceMethod.new(klass, :method_x)
13
-
14
- method.hide_original_method
15
-
16
- assert_equal false, klass.method_defined?(:method_x)
17
- end
18
- end
19
-
20
- def test_should_not_raise_error_hiding_method_that_isnt_defined
21
- klass = Class.new
22
- method = AnyInstanceMethod.new(klass, :method_x)
23
-
24
- assert_nothing_raised { method.hide_original_method }
25
- end
26
-
27
- def test_should_define_a_new_method
28
- klass = Class.new { def method_x; end }
29
- method = AnyInstanceMethod.new(klass, :method_x)
30
- mocha = build_mock
31
- mocha.expects(:method_x).with(:param1, :param2).returns(:result)
32
- any_instance = Object.new
33
- any_instance.define_instance_method(:mocha) { mocha }
34
- klass.define_instance_method(:any_instance) { any_instance }
35
-
36
- method.hide_original_method
37
- method.define_new_method
38
-
39
- instance = klass.new
40
- result = instance.method_x(:param1, :param2)
41
-
42
- assert_equal :result, result
43
- assert mocha.__verified__?
44
- end
45
-
46
- def test_should_restore_original_method
47
- klass = Class.new do
48
- def method_x
49
- :original_result
50
- end
51
- end
52
- method = AnyInstanceMethod.new(klass, :method_x)
53
-
54
- method.hide_original_method
55
- method.define_new_method
56
- method.remove_new_method
57
- method.restore_original_method
58
-
59
- instance = klass.new
60
- assert instance.respond_to?(:method_x)
61
- assert_equal :original_result, instance.method_x
62
- end
63
-
64
- def test_should_not_restore_original_method_if_none_was_defined_in_first_place
65
- klass = Class.new do
66
- def method_x
67
- :new_result
68
- end
69
- end
70
- method = AnyInstanceMethod.new(klass, :method_x)
71
-
72
- method.restore_original_method
73
-
74
- instance = klass.new
75
- assert_equal :new_result, instance.method_x
76
- end
77
-
78
- def test_should_call_remove_new_method
79
- klass = Class.new { def method_x; end }
80
- any_instance = build_mock
81
- any_instance_mocha = build_mock
82
- any_instance.stubs(:mocha).returns(any_instance_mocha)
83
- klass.define_instance_method(:any_instance) { any_instance }
84
- method = AnyInstanceMethod.new(klass, :method_x)
85
- method.replace_instance_method(:restore_original_method) {}
86
- method.replace_instance_method(:reset_mocha) {}
87
- method.define_instance_accessor(:remove_called)
88
- method.replace_instance_method(:remove_new_method) { self.remove_called = true }
89
-
90
- method.unstub
91
-
92
- assert method.remove_called
93
- end
94
-
95
- def test_should_call_restore_original_method
96
- klass = Class.new { def method_x; end }
97
- any_instance = build_mock
98
- any_instance_mocha = build_mock
99
- any_instance.stubs(:mocha).returns(any_instance_mocha)
100
- klass.define_instance_method(:any_instance) { any_instance }
101
- method = AnyInstanceMethod.new(klass, :method_x)
102
- method.replace_instance_method(:remove_new_method) {}
103
- method.replace_instance_method(:reset_mocha) {}
104
- method.define_instance_accessor(:restore_called)
105
- method.replace_instance_method(:restore_original_method) { self.restore_called = true }
106
-
107
- method.unstub
108
-
109
- assert method.restore_called
110
- end
111
-
112
- def test_should_call_mock_unstub
113
- klass = Class.new { def method_x; end }
114
-
115
- method = AnyInstanceMethod.new(klass, :method_x)
116
-
117
- method.replace_instance_method(:remove_new_method) {}
118
- method.replace_instance_method(:restore_original_method) {}
119
- mocha = Class.new do
120
- class << self
121
- attr_accessor :unstub_method
122
- end
123
- def self.unstub(method)
124
- self.unstub_method = method
125
- end
126
- end
127
- mocha.define_instance_method(:any_expectations?) { true }
128
- method.replace_instance_method(:mock) { mocha }
129
-
130
- method.unstub
131
-
132
- assert_equal mocha.unstub_method, :method_x
133
- end
134
-
135
- def test_should_return_any_instance_mocha_for_stubbee
136
- mocha = Object.new
137
- any_instance = Object.new
138
- any_instance.define_instance_method(:mocha) { mocha }
139
- stubbee = Class.new
140
- stubbee.define_instance_method(:any_instance) { any_instance }
141
- method = AnyInstanceMethod.new(stubbee, :method_name)
142
- assert_equal stubbee.any_instance.mocha, method.mock
143
- end
144
-
145
- private
146
-
147
- def build_mock
148
- Mock.new(nil)
149
- end
150
- end
@@ -1,14 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'mocha/inspect'
3
-
4
- class ArrayInspectTest < Mocha::TestCase
5
- def test_should_use_inspect
6
- array = [1, 2]
7
- assert_equal array.inspect, array.mocha_inspect
8
- end
9
-
10
- def test_should_use_mocha_inspect_on_each_item
11
- array = [1, 2, 'chris']
12
- assert_equal %([1, 2, "chris"]), array.mocha_inspect
13
- end
14
- end
@@ -1,17 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'mocha/backtrace_filter'
3
-
4
- class BacktraceFilterTest < Mocha::TestCase
5
- include Mocha
6
-
7
- def test_should_exclude_mocha_locations_from_backtrace
8
- mocha_lib = '/username/workspace/mocha_wibble/lib/'
9
- backtrace = [mocha_lib + 'exclude/me/1', mocha_lib + 'exclude/me/2', '/keep/me', mocha_lib + 'exclude/me/3']
10
- filter = BacktraceFilter.new(mocha_lib)
11
- assert_equal ['/keep/me'], filter.filtered(backtrace)
12
- end
13
-
14
- def test_should_determine_path_for_mocha_lib_directory
15
- assert_match Regexp.new('/lib/$'), BacktraceFilter::LIB_DIRECTORY
16
- end
17
- end
@@ -1,54 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'mocha/cardinality'
3
-
4
- class CardinalityTest < Mocha::TestCase
5
- include Mocha
6
-
7
- def test_should_allow_invocations_if_invocation_count_has_not_yet_reached_maximum
8
- cardinality = Cardinality.new(2, 3)
9
- assert cardinality.invocations_allowed?(0)
10
- assert cardinality.invocations_allowed?(1)
11
- assert cardinality.invocations_allowed?(2)
12
- assert !cardinality.invocations_allowed?(3)
13
- end
14
-
15
- def test_should_be_satisfied_if_invocations_so_far_have_reached_required_threshold
16
- cardinality = Cardinality.new(2, 3)
17
- assert !cardinality.satisfied?(0)
18
- assert !cardinality.satisfied?(1)
19
- assert cardinality.satisfied?(2)
20
- assert cardinality.satisfied?(3)
21
- end
22
-
23
- def test_should_describe_cardinality
24
- assert_equal 'allowed any number of times', Cardinality.at_least(0).mocha_inspect
25
-
26
- assert_equal 'expected at most once', Cardinality.at_most(1).mocha_inspect
27
- assert_equal 'expected at most twice', Cardinality.at_most(2).mocha_inspect
28
- assert_equal 'expected at most 3 times', Cardinality.at_most(3).mocha_inspect
29
-
30
- assert_equal 'expected at least once', Cardinality.at_least(1).mocha_inspect
31
- assert_equal 'expected at least twice', Cardinality.at_least(2).mocha_inspect
32
- assert_equal 'expected at least 3 times', Cardinality.at_least(3).mocha_inspect
33
-
34
- assert_equal 'expected never', Cardinality.exactly(0).mocha_inspect
35
- assert_equal 'expected exactly once', Cardinality.exactly(1).mocha_inspect
36
- assert_equal 'expected exactly twice', Cardinality.exactly(2).mocha_inspect
37
- assert_equal 'expected exactly 3 times', Cardinality.times(3).mocha_inspect
38
-
39
- assert_equal 'expected between 2 and 4 times', Cardinality.times(2..4).mocha_inspect
40
- assert_equal 'expected between 1 and 3 times', Cardinality.times(1..3).mocha_inspect
41
- end
42
-
43
- def test_should_need_verifying
44
- assert Cardinality.exactly(2).needs_verifying?
45
- assert Cardinality.at_least(3).needs_verifying?
46
- assert Cardinality.at_most(2).needs_verifying?
47
- assert Cardinality.times(4).needs_verifying?
48
- assert Cardinality.times(2..4).needs_verifying?
49
- end
50
-
51
- def test_should_not_need_verifying
52
- assert_equal false, Cardinality.at_least(0).needs_verifying?
53
- end
54
- end
@@ -1,99 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
-
3
- require 'mocha/central'
4
- require 'mocha/mock'
5
- require 'method_definer'
6
-
7
- class CentralTest < Mocha::TestCase
8
- include Mocha
9
-
10
- def test_should_start_with_empty_stubba_methods
11
- stubba = Central.new
12
-
13
- assert_equal [], stubba.stubba_methods
14
- end
15
-
16
- def test_should_stub_method_if_not_already_stubbed
17
- method = build_mock
18
- method.expects(:stub)
19
- stubba = Central.new
20
-
21
- stubba.stub(method)
22
-
23
- assert method.__verified__?
24
- end
25
-
26
- def test_should_not_stub_method_if_already_stubbed
27
- method = build_mock
28
- method.stubs(:matches?).returns(true)
29
- method.expects(:stub).times(0)
30
- stubba = Central.new
31
- stubba.stubba_methods = [method]
32
-
33
- stubba.stub(method)
34
-
35
- assert method.__verified__?
36
- end
37
-
38
- def test_should_record_method
39
- method = build_mock
40
- method.expects(:stub)
41
- stubba = Central.new
42
-
43
- stubba.stub(method)
44
-
45
- assert_equal [method], stubba.stubba_methods
46
- end
47
-
48
- def test_should_unstub_specified_method
49
- stubba = Central.new
50
- method1 = build_mock
51
- method1.stubs(:matches?).returns(false)
52
- method2 = build_mock
53
- method2.stubs(:matches?).returns(true)
54
- method2.expects(:unstub)
55
- stubba.stubba_methods = [method1, method2]
56
-
57
- stubba.unstub(method2)
58
-
59
- assert_equal [method1], stubba.stubba_methods
60
- assert method2.__verified__?
61
- end
62
-
63
- def test_should_not_unstub_specified_method_if_not_already_stubbed
64
- stubba = Central.new
65
- method1 = build_mock
66
- method1.stubs(:matches?).returns(false)
67
- method2 = build_mock
68
- method2.expects(:unstub).never
69
- stubba.stubba_methods = [method1]
70
-
71
- stubba.unstub(method2)
72
-
73
- assert_equal [method1], stubba.stubba_methods
74
- assert method2.__verified__?
75
- end
76
-
77
- def test_should_unstub_all_methods
78
- stubba = Central.new
79
- method1 = build_mock
80
- method1.stubs(:matches?).returns(true)
81
- method1.expects(:unstub)
82
- method2 = build_mock
83
- method2.stubs(:matches?).returns(true)
84
- method2.expects(:unstub)
85
- stubba.stubba_methods = [method1, method2]
86
-
87
- stubba.unstub_all
88
-
89
- assert_equal [], stubba.stubba_methods
90
- assert method1.__verified__?
91
- assert method2.__verified__?
92
- end
93
-
94
- private
95
-
96
- def build_mock
97
- Mock.new(nil)
98
- end
99
- end
@@ -1,37 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
-
3
- require 'mocha/change_state_side_effect'
4
-
5
- class ChangeStateSideEffectTest < Mocha::TestCase
6
- include Mocha
7
-
8
- class FakeState
9
- attr_reader :active
10
- attr_writer :description
11
-
12
- def activate
13
- @active = true
14
- end
15
-
16
- def mocha_inspect
17
- @description
18
- end
19
- end
20
-
21
- def test_should_activate_the_given_state
22
- state = FakeState.new
23
- side_effect = ChangeStateSideEffect.new(state)
24
-
25
- side_effect.perform
26
-
27
- assert state.active
28
- end
29
-
30
- def test_should_describe_itself_in_terms_of_the_activated_state
31
- state = FakeState.new
32
- state.description = 'the-new-state'
33
- side_effect = ChangeStateSideEffect.new(state)
34
-
35
- assert_equal 'then the-new-state', side_effect.mocha_inspect
36
- end
37
- end
@@ -1,254 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'method_definer'
3
- require 'mocha/mock'
4
-
5
- require 'mocha/class_method'
6
-
7
- class ClassMethodTest < Mocha::TestCase
8
- include Mocha
9
-
10
- unless RUBY_V2_PLUS
11
- def test_should_hide_original_method
12
- klass = Class.new { def self.method_x; end }
13
- klass.__metaclass__.send(:alias_method, :_method, :method)
14
- method = ClassMethod.new(klass, :method_x)
15
-
16
- method.hide_original_method
17
-
18
- assert_equal false, klass.respond_to?(:method_x)
19
- end
20
- end
21
-
22
- def test_should_not_raise_error_hiding_method_that_isnt_defined
23
- klass = Class.new
24
- method = ClassMethod.new(klass, :method_x)
25
-
26
- assert_nothing_raised { method.hide_original_method }
27
- end
28
-
29
- def test_should_not_raise_error_hiding_method_in_class_that_implements_method_called_method
30
- klass = Class.new { def self.method; end }
31
- method = ClassMethod.new(klass, :method)
32
-
33
- assert_nothing_raised { method.hide_original_method }
34
- end
35
-
36
- def test_should_define_a_new_method_which_should_call_mocha_method_missing
37
- klass = Class.new { def self.method_x; end }
38
- mocha = build_mock
39
- klass.define_instance_method(:mocha) { mocha }
40
- mocha.expects(:method_x).with(:param1, :param2).returns(:result)
41
- method = ClassMethod.new(klass, :method_x)
42
-
43
- method.hide_original_method
44
- method.define_new_method
45
- result = klass.method_x(:param1, :param2)
46
-
47
- assert_equal :result, result
48
- assert mocha.__verified__?
49
- end
50
-
51
- def test_should_remove_new_method
52
- klass = Class.new { def self.method_x; end }
53
- method = ClassMethod.new(klass, :method_x)
54
-
55
- method.remove_new_method
56
-
57
- assert_equal false, klass.respond_to?(:method_x)
58
- end
59
-
60
- def test_should_restore_original_method
61
- klass = Class.new do
62
- def self.method_x
63
- :original_result
64
- end
65
- end
66
- klass.__metaclass__.send(:alias_method, :_method, :method)
67
- method = ClassMethod.new(klass, :method_x)
68
-
69
- method.hide_original_method
70
- method.define_new_method
71
- method.remove_new_method
72
- method.restore_original_method
73
-
74
- assert klass.respond_to?(:method_x)
75
- assert_equal :original_result, klass.method_x
76
- end
77
-
78
- def test_should_restore_original_method_accepting_a_block_parameter
79
- klass = Class.new do
80
- def self.method_x(&block)
81
- block.call if block_given?
82
- end
83
- end
84
- klass.__metaclass__.send(:alias_method, :_method, :method)
85
- method = ClassMethod.new(klass, :method_x)
86
-
87
- method.hide_original_method
88
- method.define_new_method
89
- method.remove_new_method
90
- method.restore_original_method
91
-
92
- block_called = false
93
- klass.method_x { block_called = true }
94
- assert block_called
95
- end
96
-
97
- def test_should_not_restore_original_method_if_none_was_defined_in_first_place
98
- klass = Class.new do
99
- def self.method_x
100
- :new_result
101
- end
102
- end
103
- method = ClassMethod.new(klass, :method_x)
104
-
105
- method.restore_original_method
106
-
107
- assert_equal :new_result, klass.method_x
108
- end
109
-
110
- def test_should_call_hide_original_method
111
- klass = Class.new { def self.method_x; end }
112
- method = ClassMethod.new(klass, :method_x)
113
- method.hide_original_method
114
- method.define_instance_accessor(:hide_called)
115
- method.replace_instance_method(:hide_original_method) { self.hide_called = true }
116
-
117
- method.stub
118
-
119
- assert method.hide_called
120
- end
121
-
122
- def test_should_call_define_new_method
123
- klass = Class.new { def self.method_x; end }
124
- method = ClassMethod.new(klass, :method_x)
125
- method.define_instance_accessor(:define_called)
126
- method.replace_instance_method(:define_new_method) { self.define_called = true }
127
-
128
- method.stub
129
-
130
- assert method.define_called
131
- end
132
-
133
- def test_should_call_remove_new_method
134
- klass = Class.new { def self.method_x; end }
135
- method = ClassMethod.new(klass, :method_x)
136
- mocha = build_mock
137
- klass.define_instance_method(:mocha) { mocha }
138
- method.replace_instance_method(:reset_mocha) {}
139
- method.define_instance_accessor(:remove_called)
140
- method.replace_instance_method(:remove_new_method) { self.remove_called = true }
141
-
142
- method.unstub
143
-
144
- assert method.remove_called
145
- end
146
-
147
- def test_should_call_restore_original_method
148
- klass = Class.new { def self.method_x; end }
149
- mocha = build_mock
150
- klass.define_instance_method(:mocha) { mocha }
151
- method = ClassMethod.new(klass, :method_x)
152
- method.replace_instance_method(:reset_mocha) {}
153
- method.define_instance_accessor(:restore_called)
154
- method.replace_instance_method(:restore_original_method) { self.restore_called = true }
155
-
156
- method.unstub
157
-
158
- assert method.restore_called
159
- end
160
-
161
- def test_should_call_mocha_unstub
162
- klass = Class.new { def self.method_x; end }
163
- method = ClassMethod.new(klass, :method_x)
164
- method.replace_instance_method(:restore_original_method) {}
165
- mocha = Class.new do
166
- class << self
167
- attr_accessor :unstub_method
168
- end
169
- def self.unstub(method)
170
- self.unstub_method = method
171
- end
172
- end
173
- mocha.define_instance_method(:any_expectations?) { true }
174
- method.replace_instance_method(:mock) { mocha }
175
-
176
- method.unstub
177
- assert_equal mocha.unstub_method, :method_x
178
- end
179
-
180
- def test_should_call_stubbee_reset_mocha_if_no_expectations_remaining
181
- klass = Class.new { def self.method_x; end }
182
- method = ClassMethod.new(klass, :method_x)
183
- method.replace_instance_method(:remove_new_method) {}
184
- method.replace_instance_method(:restore_original_method) {}
185
- mocha = Class.new
186
- mocha.define_instance_method(:unstub) { |method_name| }
187
- mocha.define_instance_method(:any_expectations?) { false }
188
- method.replace_instance_method(:mock) { mocha }
189
- stubbee = Class.new do
190
- attr_accessor :reset_mocha_called
191
- def reset_mocha
192
- self.reset_mocha_called = true
193
- end
194
- end.new
195
- method.replace_instance_method(:stubbee) { stubbee }
196
-
197
- method.unstub
198
-
199
- assert stubbee.reset_mocha_called
200
- end
201
-
202
- def test_should_return_mock_for_stubbee
203
- mocha = Object.new
204
- stubbee = Object.new
205
- stubbee.define_instance_method(:mocha) { mocha }
206
- method = ClassMethod.new(stubbee, :method_name)
207
- assert_equal mocha, method.mock
208
- end
209
-
210
- def test_should_not_match_if_other_object_has_a_different_class
211
- class_method = ClassMethod.new(Object.new, :method)
212
- other_object = Object.new
213
- assert !class_method.matches?(other_object)
214
- end
215
-
216
- def test_should_not_match_if_other_class_method_has_different_stubbee
217
- stubbee1 = Object.new
218
- stubbee2 = Object.new
219
- class_method1 = ClassMethod.new(stubbee1, :method)
220
- class_method2 = ClassMethod.new(stubbee2, :method)
221
- assert !class_method1.matches?(class_method2)
222
- end
223
-
224
- def test_should_not_match_if_other_class_method_has_different_method
225
- stubbee = Object.new
226
- class_method1 = ClassMethod.new(stubbee, :method_1)
227
- class_method2 = ClassMethod.new(stubbee, :method_2)
228
- assert !class_method1.matches?(class_method2)
229
- end
230
-
231
- def test_should_match_if_other_class_method_has_same_stubbee_and_same_method_so_no_attempt_is_made_to_stub_a_method_twice
232
- stubbee = Object.new
233
- class_method1 = ClassMethod.new(stubbee, :method)
234
- class_method2 = ClassMethod.new(stubbee, :method)
235
- assert class_method1.matches?(class_method2)
236
- end
237
-
238
- def test_should_match_if_other_class_method_has_same_stubbee_and_same_method_but_stubbee_equal_method_lies_like_active_record_association_proxy
239
- stubbee = Class.new do
240
- def equal?(_other)
241
- false
242
- end
243
- end.new
244
- class_method1 = ClassMethod.new(stubbee, :method)
245
- class_method2 = ClassMethod.new(stubbee, :method)
246
- assert class_method1.matches?(class_method2)
247
- end
248
-
249
- private
250
-
251
- def build_mock
252
- Mock.new(nil)
253
- end
254
- end