mocha 1.4.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +58 -0
  3. data/.rubocop_todo.yml +50 -0
  4. data/README.md +12 -71
  5. data/RELEASE.md +19 -0
  6. data/Rakefile +35 -28
  7. data/bin/build-matrix +17 -18
  8. data/lib/mocha/any_instance_method.rb +19 -25
  9. data/lib/mocha/api.rb +1 -5
  10. data/lib/mocha/argument_iterator.rb +4 -8
  11. data/lib/mocha/backtrace_filter.rb +1 -5
  12. data/lib/mocha/cardinality.rb +20 -27
  13. data/lib/mocha/central.rb +21 -12
  14. data/lib/mocha/change_state_side_effect.rb +0 -4
  15. data/lib/mocha/class_method.rb +31 -37
  16. data/lib/mocha/class_methods.rb +11 -15
  17. data/lib/mocha/configuration.rb +69 -10
  18. data/lib/mocha/debug.rb +3 -2
  19. data/lib/mocha/deprecation.rb +4 -11
  20. data/lib/mocha/detection/mini_test.rb +0 -2
  21. data/lib/mocha/detection/test_unit.rb +4 -4
  22. data/lib/mocha/error_with_filtered_backtrace.rb +13 -0
  23. data/lib/mocha/exception_raiser.rb +2 -5
  24. data/lib/mocha/expectation.rb +15 -18
  25. data/lib/mocha/expectation_error.rb +2 -0
  26. data/lib/mocha/expectation_error_factory.rb +0 -1
  27. data/lib/mocha/expectation_list.rb +2 -6
  28. data/lib/mocha/hooks.rb +1 -3
  29. data/lib/mocha/in_state_ordering_constraint.rb +0 -4
  30. data/lib/mocha/inspect.rb +3 -5
  31. data/lib/mocha/instance_method.rb +0 -2
  32. data/lib/mocha/integration/mini_test/adapter.rb +2 -4
  33. data/lib/mocha/integration/mini_test/exception_translation.rb +1 -1
  34. data/lib/mocha/integration/mini_test/nothing.rb +4 -4
  35. data/lib/mocha/integration/mini_test/version_13.rb +4 -1
  36. data/lib/mocha/integration/mini_test/version_140.rb +4 -1
  37. data/lib/mocha/integration/mini_test/version_141.rb +4 -1
  38. data/lib/mocha/integration/mini_test/version_142_to_172.rb +4 -1
  39. data/lib/mocha/integration/mini_test/version_200.rb +4 -1
  40. data/lib/mocha/integration/mini_test/version_201_to_222.rb +4 -1
  41. data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +4 -1
  42. data/lib/mocha/integration/mini_test/version_2112_to_320.rb +4 -1
  43. data/lib/mocha/integration/mini_test/version_230_to_2101.rb +4 -1
  44. data/lib/mocha/integration/monkey_patcher.rb +5 -7
  45. data/lib/mocha/integration/test_unit/adapter.rb +5 -6
  46. data/lib/mocha/integration/test_unit/gem_version_200.rb +5 -2
  47. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +5 -2
  48. data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +5 -2
  49. data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +5 -2
  50. data/lib/mocha/integration/test_unit/nothing.rb +4 -4
  51. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +4 -1
  52. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +4 -1
  53. data/lib/mocha/integration.rb +3 -4
  54. data/lib/mocha/is_a.rb +0 -2
  55. data/lib/mocha/logger.rb +0 -4
  56. data/lib/mocha/method_matcher.rb +1 -5
  57. data/lib/mocha/minitest.rb +1 -1
  58. data/lib/mocha/mock.rb +25 -30
  59. data/lib/mocha/mockery.rb +62 -43
  60. data/lib/mocha/module_method.rb +0 -10
  61. data/lib/mocha/module_methods.rb +0 -4
  62. data/lib/mocha/multiple_yields.rb +0 -5
  63. data/lib/mocha/names.rb +1 -11
  64. data/lib/mocha/no_yields.rb +1 -7
  65. data/lib/mocha/not_initialized_error.rb +7 -0
  66. data/lib/mocha/object_methods.rb +16 -16
  67. data/lib/mocha/parameter_matchers/all_of.rb +1 -7
  68. data/lib/mocha/parameter_matchers/any_of.rb +1 -7
  69. data/lib/mocha/parameter_matchers/any_parameters.rb +3 -9
  70. data/lib/mocha/parameter_matchers/anything.rb +2 -8
  71. data/lib/mocha/parameter_matchers/base.rb +6 -12
  72. data/lib/mocha/parameter_matchers/equals.rb +0 -6
  73. data/lib/mocha/parameter_matchers/equivalent_uri.rb +6 -6
  74. data/lib/mocha/parameter_matchers/has_entries.rb +2 -6
  75. data/lib/mocha/parameter_matchers/has_entry.rb +8 -11
  76. data/lib/mocha/parameter_matchers/has_key.rb +2 -6
  77. data/lib/mocha/parameter_matchers/has_value.rb +2 -6
  78. data/lib/mocha/parameter_matchers/includes.rb +2 -6
  79. data/lib/mocha/parameter_matchers/instance_of.rb +0 -6
  80. data/lib/mocha/parameter_matchers/is_a.rb +2 -6
  81. data/lib/mocha/parameter_matchers/kind_of.rb +2 -6
  82. data/lib/mocha/parameter_matchers/not.rb +2 -6
  83. data/lib/mocha/parameter_matchers/object.rb +0 -2
  84. data/lib/mocha/parameter_matchers/optionally.rb +4 -10
  85. data/lib/mocha/parameter_matchers/regexp_matches.rb +0 -6
  86. data/lib/mocha/parameter_matchers/responds_with.rb +3 -8
  87. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +2 -6
  88. data/lib/mocha/parameter_matchers.rb +0 -2
  89. data/lib/mocha/parameters_matcher.rb +6 -9
  90. data/lib/mocha/pretty_parameters.rb +0 -4
  91. data/lib/mocha/receivers.rb +10 -14
  92. data/lib/mocha/return_values.rb +3 -7
  93. data/lib/mocha/sequence.rb +4 -9
  94. data/lib/mocha/single_return_value.rb +0 -4
  95. data/lib/mocha/single_yield.rb +0 -5
  96. data/lib/mocha/state_machine.rb +6 -10
  97. data/lib/mocha/stubbing_error.rb +2 -13
  98. data/lib/mocha/test_unit.rb +1 -1
  99. data/lib/mocha/thrower.rb +2 -5
  100. data/lib/mocha/unexpected_invocation.rb +3 -5
  101. data/lib/mocha/version.rb +1 -1
  102. data/lib/mocha/yield_parameters.rb +3 -7
  103. data/mocha.gemspec +34 -58
  104. data/test/acceptance/acceptance_test_helper.rb +0 -6
  105. data/test/acceptance/bug_18914_test.rb +7 -12
  106. data/test/acceptance/bug_21465_test.rb +0 -2
  107. data/test/acceptance/bug_21563_test.rb +0 -2
  108. data/test/acceptance/exception_rescue_test.rb +7 -8
  109. data/test/acceptance/expectations_on_multiple_methods_test.rb +2 -1
  110. data/test/acceptance/expected_invocation_count_test.rb +27 -29
  111. data/test/acceptance/failure_messages_test.rb +3 -5
  112. data/test/acceptance/issue_272_test.rb +0 -1
  113. data/test/acceptance/issue_65_test.rb +15 -13
  114. data/test/acceptance/issue_70_test.rb +0 -1
  115. data/test/acceptance/mocha_example_test.rb +5 -6
  116. data/test/acceptance/mocha_test_result_test.rb +7 -7
  117. data/test/acceptance/mock_test.rb +2 -4
  118. data/test/acceptance/mock_with_initializer_block_test.rb +0 -2
  119. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -2
  120. data/test/acceptance/multiple_expectations_failure_message_test.rb +16 -17
  121. data/test/acceptance/optional_parameters_test.rb +0 -2
  122. data/test/acceptance/parameter_matcher_test.rb +2 -3
  123. data/test/acceptance/partial_mocks_test.rb +4 -6
  124. data/test/acceptance/prepend_test.rb +14 -16
  125. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +77 -0
  126. data/test/acceptance/raise_exception_test.rb +2 -4
  127. data/test/acceptance/return_value_test.rb +0 -2
  128. data/test/acceptance/sequence_test.rb +9 -11
  129. data/test/acceptance/states_test.rb +6 -4
  130. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +33 -2
  131. data/test/acceptance/stub_any_instance_method_test.rb +19 -17
  132. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +8 -8
  133. data/test/acceptance/stub_class_method_defined_on_class_test.rb +2 -1
  134. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -1
  135. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +34 -1
  136. data/test/acceptance/stub_everything_test.rb +1 -3
  137. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +6 -7
  138. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -1
  139. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -1
  140. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +63 -1
  141. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -1
  142. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +2 -1
  143. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -2
  144. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
  145. data/test/acceptance/stub_module_method_test.rb +13 -13
  146. data/test/acceptance/stub_test.rb +1 -3
  147. data/test/acceptance/stubba_example_test.rb +6 -14
  148. data/test/acceptance/stubba_test_result_test.rb +5 -8
  149. data/test/acceptance/stubbing_error_backtrace_test.rb +2 -2
  150. data/test/acceptance/stubbing_frozen_object_test.rb +2 -1
  151. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +12 -7
  152. data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -2
  153. data/test/acceptance/stubbing_nil_test.rb +4 -5
  154. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +1 -3
  155. data/test/acceptance/stubbing_non_existent_class_method_test.rb +3 -3
  156. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +1 -3
  157. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -2
  158. data/test/acceptance/stubbing_non_public_class_method_test.rb +3 -3
  159. data/test/acceptance/stubbing_non_public_instance_method_test.rb +1 -3
  160. data/test/acceptance/stubbing_on_non_mock_object_test.rb +4 -10
  161. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +3 -4
  162. data/test/acceptance/throw_test.rb +0 -2
  163. data/test/acceptance/unexpected_invocation_test.rb +2 -3
  164. data/test/acceptance/unstubbing_test.rb +41 -14
  165. data/test/deprecation_disabler.rb +0 -1
  166. data/test/execution_point.rb +2 -4
  167. data/test/integration/mini_test_test.rb +2 -2
  168. data/test/integration/shared_tests.rb +24 -22
  169. data/test/integration/test_unit_test.rb +2 -2
  170. data/test/method_definer.rb +1 -3
  171. data/test/mini_test_result.rb +17 -11
  172. data/test/minitest_result.rb +0 -1
  173. data/test/simple_counter.rb +0 -2
  174. data/test/test_helper.rb +13 -5
  175. data/test/test_runner.rb +2 -4
  176. data/test/test_unit_result.rb +4 -2
  177. data/test/unit/any_instance_method_test.rb +31 -17
  178. data/test/unit/array_inspect_test.rb +2 -4
  179. data/test/unit/backtrace_filter_test.rb +3 -5
  180. data/test/unit/cardinality_test.rb +0 -2
  181. data/test/unit/central_test.rb +26 -27
  182. data/test/unit/change_state_side_effect_test.rb +0 -4
  183. data/test/unit/class_method_test.rb +59 -34
  184. data/test/unit/class_methods_test.rb +34 -5
  185. data/test/unit/configuration_test.rb +1 -2
  186. data/test/unit/date_time_inspect_test.rb +1 -3
  187. data/test/unit/exception_raiser_test.rb +0 -2
  188. data/test/unit/expectation_list_test.rb +0 -2
  189. data/test/unit/expectation_test.rb +41 -46
  190. data/test/unit/hash_inspect_test.rb +3 -5
  191. data/test/unit/hooks_test.rb +14 -8
  192. data/test/unit/in_state_ordering_constraint_test.rb +0 -4
  193. data/test/unit/method_matcher_test.rb +1 -3
  194. data/test/unit/mock_test.rb +35 -22
  195. data/test/unit/mockery_test.rb +45 -28
  196. data/test/unit/module_methods_test.rb +0 -2
  197. data/test/unit/multiple_yields_test.rb +0 -2
  198. data/test/unit/no_yields_test.rb +0 -2
  199. data/test/unit/object_inspect_test.rb +16 -7
  200. data/test/unit/object_methods_test.rb +22 -6
  201. data/test/unit/parameter_matchers/all_of_test.rb +0 -2
  202. data/test/unit/parameter_matchers/any_of_test.rb +0 -2
  203. data/test/unit/parameter_matchers/anything_test.rb +2 -4
  204. data/test/unit/parameter_matchers/equals_test.rb +1 -3
  205. data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -1
  206. data/test/unit/parameter_matchers/has_entries_test.rb +2 -2
  207. data/test/unit/parameter_matchers/has_entry_test.rb +13 -14
  208. data/test/unit/parameter_matchers/has_key_test.rb +0 -1
  209. data/test/unit/parameter_matchers/has_value_test.rb +0 -2
  210. data/test/unit/parameter_matchers/includes_test.rb +8 -9
  211. data/test/unit/parameter_matchers/instance_of_test.rb +1 -3
  212. data/test/unit/parameter_matchers/is_a_test.rb +1 -3
  213. data/test/unit/parameter_matchers/kind_of_test.rb +1 -3
  214. data/test/unit/parameter_matchers/not_test.rb +0 -2
  215. data/test/unit/parameter_matchers/regexp_matches_test.rb +1 -2
  216. data/test/unit/parameter_matchers/responds_with_test.rb +9 -3
  217. data/test/unit/parameter_matchers/stub_matcher.rb +0 -4
  218. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +1 -3
  219. data/test/unit/parameters_matcher_test.rb +2 -4
  220. data/test/unit/receivers_test.rb +35 -5
  221. data/test/unit/return_values_test.rb +3 -5
  222. data/test/unit/sequence_test.rb +1 -5
  223. data/test/unit/single_return_value_test.rb +0 -2
  224. data/test/unit/single_yield_test.rb +0 -2
  225. data/test/unit/state_machine_test.rb +1 -3
  226. data/test/unit/string_inspect_test.rb +2 -4
  227. data/test/unit/thrower_test.rb +0 -2
  228. data/test/unit/yield_parameters_test.rb +0 -2
  229. data/yard-templates/default/layout/html/setup.rb +2 -3
  230. metadata +25 -6
@@ -2,16 +2,15 @@ require 'stringio'
2
2
  require 'minitest/unit'
3
3
 
4
4
  class MiniTestResult
5
-
6
5
  minitest_version = Gem::Version.new(::MiniTest::Unit::VERSION)
7
6
  if Gem::Requirement.new('<= 4.6.1').satisfied_by?(minitest_version)
8
- FAILURE_PATTERN = %r{(Failure)\:\n([^\(]+)\(([^\)]+)\) \[([^\]]+)\]\:\n(.*)\n}m
9
- ERROR_PATTERN = %r{(Error)\:\n([^\(]+)\(([^\)]+)\)\:\n(.+?)\n}m
10
- PATTERN_INDICES = { :method => 2, :testcase => 3 }
7
+ FAILURE_PATTERN = /(Failure)\:\n([^\(]+)\(([^\)]+)\) \[([^\]]+)\]\:\n(.*)\n/m
8
+ ERROR_PATTERN = /(Error)\:\n([^\(]+)\(([^\)]+)\)\:\n(.+?)\n/m
9
+ PATTERN_INDICES = { :method => 2, :testcase => 3 }.freeze
11
10
  else
12
- FAILURE_PATTERN = %r{(Failure)\:\n.([^#]+)\#([^ ]+) \[([^\]]+)\]\:\n(.*)\n}m
13
- ERROR_PATTERN = %r{(Error)\:\n.([^#]+)\#([^ ]+)\:\n(.+?)\n}m
14
- PATTERN_INDICES = { :method => 3, :testcase => 2 }
11
+ FAILURE_PATTERN = /(Failure)\:\n.([^#]+)\#([^ ]+) \[([^\]]+)\]\:\n(.*)\n/m
12
+ ERROR_PATTERN = /(Error)\:\n.([^#]+)\#([^ ]+)\:\n(.+?)\n/m
13
+ PATTERN_INDICES = { :method => 3, :testcase => 2 }.freeze
15
14
  end
16
15
 
17
16
  def self.parse_failure(raw)
@@ -30,7 +29,10 @@ class MiniTestResult
30
29
  class Failure
31
30
  attr_reader :method, :test_case, :location, :message
32
31
  def initialize(method, test_case, location, message)
33
- @method, @test_case, @location, @message = method, test_case, location, message
32
+ @method = method
33
+ @test_case = test_case
34
+ @location = location
35
+ @message = message
34
36
  end
35
37
  end
36
38
 
@@ -38,18 +40,22 @@ class MiniTestResult
38
40
  class Exception
39
41
  attr_reader :message, :backtrace
40
42
  def initialize(message, location)
41
- @message, @backtrace = message, location
43
+ @message = message
44
+ @backtrace = location
42
45
  end
43
46
  end
44
47
 
45
48
  attr_reader :method, :test_case, :exception
46
49
  def initialize(method, test_case, message, backtrace)
47
- @method, @test_case, @exception = method, test_case, Exception.new(message, backtrace)
50
+ @method = method
51
+ @test_case = test_case
52
+ @exception = Exception.new(message, backtrace)
48
53
  end
49
54
  end
50
55
 
51
56
  def initialize(runner, tests)
52
- @runner, @tests = runner, tests
57
+ @runner = runner
58
+ @tests = tests
53
59
  end
54
60
 
55
61
  def failure_count
@@ -1,7 +1,6 @@
1
1
  require 'forwardable'
2
2
 
3
3
  class MinitestResult
4
-
5
4
  class Failure
6
5
  extend Forwardable
7
6
  def_delegators :@failure, :message, :backtrace
@@ -1,5 +1,4 @@
1
1
  class SimpleCounter
2
-
3
2
  attr_reader :count
4
3
 
5
4
  def initialize
@@ -9,5 +8,4 @@ class SimpleCounter
9
8
  def increment
10
9
  @count += 1
11
10
  end
12
-
13
11
  end
data/test/test_helper.rb CHANGED
@@ -2,22 +2,26 @@ unless defined?(STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS)
2
2
  STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS = Object.instance_methods + Object.private_instance_methods
3
3
  end
4
4
 
5
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
6
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__)))
7
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'unit'))
8
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'unit', 'parameter_matchers'))
9
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'acceptance'))
5
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__)))
7
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), 'unit'))
8
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), 'unit', 'parameter_matchers'))
9
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), 'acceptance'))
10
10
 
11
11
  require 'mocha/detection/mini_test'
12
12
 
13
13
  begin
14
14
  require 'minitest'
15
+ # rubocop:disable Lint/HandleExceptions
15
16
  rescue LoadError
16
17
  end
18
+ # rubocop:enable Lint/HandleExceptions
17
19
  begin
18
20
  require 'minitest/unit'
21
+ # rubocop:disable Lint/HandleExceptions
19
22
  rescue LoadError
20
23
  end
24
+ # rubocop:enable Lint/HandleExceptions
21
25
 
22
26
  module Mocha; end
23
27
 
@@ -27,6 +31,7 @@ if (minitest_testcase = Mocha::Detection::MiniTest.testcase) && (ENV['MOCHA_RUN_
27
31
  rescue LoadError
28
32
  MiniTest::Unit.autorun
29
33
  end
34
+ # rubocop:disable Style/ClassAndModuleChildren
30
35
  class Mocha::TestCase < minitest_testcase
31
36
  def assert_nothing_raised(exception = StandardError)
32
37
  yield
@@ -39,12 +44,15 @@ if (minitest_testcase = Mocha::Detection::MiniTest.testcase) && (ENV['MOCHA_RUN_
39
44
  alias_method :assert_not_same, :refute_same
40
45
  alias_method :assert_no_match, :refute_match
41
46
  end
47
+ # rubocop:enable Style/ClassAndModuleChildren
42
48
  else
43
49
  require 'test/unit'
50
+ # rubocop:disable Style/ClassAndModuleChildren
44
51
  class Mocha::TestCase < Test::Unit::TestCase
45
52
  def test_dummy
46
53
  # Some versions (?) of Test::Unit try to run this base class as a test case
47
54
  # and it fails because it has no test methods, so I'm adding a dummy test.
48
55
  end
49
56
  end
57
+ # rubocop:enable Style/ClassAndModuleChildren
50
58
  end
data/test/test_runner.rb CHANGED
@@ -23,9 +23,7 @@ module TestRunner
23
23
  minitest_version = Gem::Version.new(Mocha::Detection::MiniTest.version)
24
24
  if Gem::Requirement.new('>= 5.0.0').satisfied_by?(minitest_version)
25
25
  require File.expand_path('../minitest_result', __FILE__)
26
- tests.each do |test|
27
- test.run
28
- end
26
+ tests.each(&:run)
29
27
  Minitest::Runnable.runnables.delete(test_class)
30
28
  test_result = MinitestResult.new(tests)
31
29
  elsif Gem::Requirement.new('> 0.0.0', '< 5.0.0').satisfied_by?(minitest_version)
@@ -53,6 +51,6 @@ module TestRunner
53
51
  end
54
52
 
55
53
  def assert_failed(test_result)
56
- flunk "Test passed unexpectedly" unless test_result.failure_count + test_result.error_count > 0
54
+ flunk 'Test passed unexpectedly' unless test_result.failure_count + test_result.error_count > 0
57
55
  end
58
56
  end
@@ -6,13 +6,15 @@ class TestUnitResult
6
6
  class << test_result
7
7
  attr_reader :failures, :errors
8
8
  def failure_messages
9
- failures.map { |failure| failure.message }
9
+ failures.map(&:message)
10
10
  end
11
+
11
12
  def failure_message_lines
12
13
  failure_messages.map { |message| message.split("\n") }.flatten
13
14
  end
15
+
14
16
  def error_messages
15
- errors.map { |error| error.message }
17
+ errors.map(&:message)
16
18
  end
17
19
  end
18
20
  test_result
@@ -4,19 +4,18 @@ require 'mocha/mock'
4
4
  require 'mocha/any_instance_method'
5
5
 
6
6
  class AnyInstanceMethodTest < Mocha::TestCase
7
-
8
7
  include Mocha
9
8
 
10
- unless RUBY_V2_PLUS
11
- def test_should_hide_original_method
12
- klass = Class.new { def method_x; end }
13
- method = AnyInstanceMethod.new(klass, :method_x)
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)
14
13
 
15
- method.hide_original_method
14
+ method.hide_original_method
16
15
 
17
- assert_equal false, klass.method_defined?(:method_x)
16
+ assert_equal false, klass.method_defined?(:method_x)
17
+ end
18
18
  end
19
- end
20
19
 
21
20
  def test_should_not_raise_error_hiding_method_that_isnt_defined
22
21
  klass = Class.new
@@ -45,7 +44,11 @@ end
45
44
  end
46
45
 
47
46
  def test_should_restore_original_method
48
- klass = Class.new { def method_x; :original_result; end }
47
+ klass = Class.new do
48
+ def method_x
49
+ :original_result
50
+ end
51
+ end
49
52
  method = AnyInstanceMethod.new(klass, :method_x)
50
53
 
51
54
  method.hide_original_method
@@ -59,7 +62,11 @@ end
59
62
  end
60
63
 
61
64
  def test_should_not_restore_original_method_if_none_was_defined_in_first_place
62
- klass = Class.new { def method_x; :new_result; end }
65
+ klass = Class.new do
66
+ def method_x
67
+ :new_result
68
+ end
69
+ end
63
70
  method = AnyInstanceMethod.new(klass, :method_x)
64
71
 
65
72
  method.restore_original_method
@@ -75,8 +82,8 @@ end
75
82
  any_instance.stubs(:mocha).returns(any_instance_mocha)
76
83
  klass.define_instance_method(:any_instance) { any_instance }
77
84
  method = AnyInstanceMethod.new(klass, :method_x)
78
- method.replace_instance_method(:restore_original_method) { }
79
- method.replace_instance_method(:reset_mocha) { }
85
+ method.replace_instance_method(:restore_original_method) {}
86
+ method.replace_instance_method(:reset_mocha) {}
80
87
  method.define_instance_accessor(:remove_called)
81
88
  method.replace_instance_method(:remove_new_method) { self.remove_called = true }
82
89
 
@@ -92,8 +99,8 @@ end
92
99
  any_instance.stubs(:mocha).returns(any_instance_mocha)
93
100
  klass.define_instance_method(:any_instance) { any_instance }
94
101
  method = AnyInstanceMethod.new(klass, :method_x)
95
- method.replace_instance_method(:remove_new_method) { }
96
- method.replace_instance_method(:reset_mocha) { }
102
+ method.replace_instance_method(:remove_new_method) {}
103
+ method.replace_instance_method(:reset_mocha) {}
97
104
  method.define_instance_accessor(:restore_called)
98
105
  method.replace_instance_method(:restore_original_method) { self.restore_called = true }
99
106
 
@@ -107,9 +114,16 @@ end
107
114
 
108
115
  method = AnyInstanceMethod.new(klass, :method_x)
109
116
 
110
- method.replace_instance_method(:remove_new_method) { }
111
- method.replace_instance_method(:restore_original_method) { }
112
- mocha = Class.new { class << self; attr_accessor :unstub_method; end; def self.unstub(method); self.unstub_method = method; end; }
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
113
127
  mocha.define_instance_method(:any_expectations?) { true }
114
128
  method.replace_instance_method(:mock) { mocha }
115
129
 
@@ -2,15 +2,13 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/inspect'
3
3
 
4
4
  class ArrayInspectTest < Mocha::TestCase
5
-
6
5
  def test_should_use_inspect
7
6
  array = [1, 2]
8
7
  assert_equal array.inspect, array.mocha_inspect
9
8
  end
10
9
 
11
10
  def test_should_use_mocha_inspect_on_each_item
12
- array = [1, 2, "chris"]
13
- assert_equal %{[1, 2, "chris"]}, array.mocha_inspect
11
+ array = [1, 2, 'chris']
12
+ assert_equal %([1, 2, "chris"]), array.mocha_inspect
14
13
  end
15
-
16
14
  end
@@ -2,18 +2,16 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/backtrace_filter'
3
3
 
4
4
  class BacktraceFilterTest < Mocha::TestCase
5
-
6
5
  include Mocha
7
6
 
8
7
  def test_should_exclude_mocha_locations_from_backtrace
9
- mocha_lib = "/username/workspace/mocha_wibble/lib/"
10
- backtrace = [ mocha_lib + 'exclude/me/1', mocha_lib + 'exclude/me/2', '/keep/me', mocha_lib + 'exclude/me/3']
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']
11
10
  filter = BacktraceFilter.new(mocha_lib)
12
11
  assert_equal ['/keep/me'], filter.filtered(backtrace)
13
12
  end
14
13
 
15
14
  def test_should_determine_path_for_mocha_lib_directory
16
- assert_match Regexp.new("/lib/$"), BacktraceFilter::LIB_DIRECTORY
15
+ assert_match Regexp.new('/lib/$'), BacktraceFilter::LIB_DIRECTORY
17
16
  end
18
-
19
17
  end
@@ -2,7 +2,6 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/cardinality'
3
3
 
4
4
  class CardinalityTest < Mocha::TestCase
5
-
6
5
  include Mocha
7
6
 
8
7
  def test_should_allow_invocations_if_invocation_count_has_not_yet_reached_maximum
@@ -52,5 +51,4 @@ class CardinalityTest < Mocha::TestCase
52
51
  def test_should_not_need_verifying
53
52
  assert_equal false, Cardinality.at_least(0).needs_verifying?
54
53
  end
55
-
56
54
  end
@@ -5,7 +5,6 @@ require 'mocha/mock'
5
5
  require 'method_definer'
6
6
 
7
7
  class CentralTest < Mocha::TestCase
8
-
9
8
  include Mocha
10
9
 
11
10
  def test_should_start_with_empty_stubba_methods
@@ -48,48 +47,48 @@ class CentralTest < Mocha::TestCase
48
47
 
49
48
  def test_should_unstub_specified_method
50
49
  stubba = Central.new
51
- method_1 = build_mock
52
- method_1.stubs(:matches?).returns(false)
53
- method_2 = build_mock
54
- method_2.stubs(:matches?).returns(true)
55
- method_2.expects(:unstub)
56
- stubba.stubba_methods = [method_1, method_2]
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]
57
56
 
58
- stubba.unstub(method_2)
57
+ stubba.unstub(method2)
59
58
 
60
- assert_equal [method_1], stubba.stubba_methods
61
- assert method_2.__verified__?
59
+ assert_equal [method1], stubba.stubba_methods
60
+ assert method2.__verified__?
62
61
  end
63
62
 
64
63
  def test_should_not_unstub_specified_method_if_not_already_stubbed
65
64
  stubba = Central.new
66
- method_1 = build_mock
67
- method_1.stubs(:matches?).returns(false)
68
- method_2 = build_mock
69
- method_2.expects(:unstub).never
70
- stubba.stubba_methods = [method_1]
65
+ method1 = build_mock
66
+ method1.stubs(:matches?).returns(false)
67
+ method2 = build_mock
68
+ method2.expects(:unstub).never
69
+ stubba.stubba_methods = [method1]
71
70
 
72
- stubba.unstub(method_2)
71
+ stubba.unstub(method2)
73
72
 
74
- assert_equal [method_1], stubba.stubba_methods
75
- assert method_2.__verified__?
73
+ assert_equal [method1], stubba.stubba_methods
74
+ assert method2.__verified__?
76
75
  end
77
76
 
78
77
  def test_should_unstub_all_methods
79
78
  stubba = Central.new
80
- method_1 = build_mock
81
- method_1.stubs(:matches?).returns(true)
82
- method_1.expects(:unstub)
83
- method_2 = build_mock
84
- method_2.stubs(:matches?).returns(true)
85
- method_2.expects(:unstub)
86
- stubba.stubba_methods = [method_1, method_2]
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]
87
86
 
88
87
  stubba.unstub_all
89
88
 
90
89
  assert_equal [], stubba.stubba_methods
91
- assert method_1.__verified__?
92
- assert method_2.__verified__?
90
+ assert method1.__verified__?
91
+ assert method2.__verified__?
93
92
  end
94
93
 
95
94
  private
@@ -3,11 +3,9 @@ require File.expand_path('../../test_helper', __FILE__)
3
3
  require 'mocha/change_state_side_effect'
4
4
 
5
5
  class ChangeStateSideEffectTest < Mocha::TestCase
6
-
7
6
  include Mocha
8
7
 
9
8
  class FakeState
10
-
11
9
  attr_reader :active
12
10
  attr_writer :description
13
11
 
@@ -18,7 +16,6 @@ class ChangeStateSideEffectTest < Mocha::TestCase
18
16
  def mocha_inspect
19
17
  @description
20
18
  end
21
-
22
19
  end
23
20
 
24
21
  def test_should_activate_the_given_state
@@ -37,5 +34,4 @@ class ChangeStateSideEffectTest < Mocha::TestCase
37
34
 
38
35
  assert_equal 'then the-new-state', side_effect.mocha_inspect
39
36
  end
40
-
41
37
  end
@@ -5,20 +5,19 @@ require 'mocha/mock'
5
5
  require 'mocha/class_method'
6
6
 
7
7
  class ClassMethodTest < Mocha::TestCase
8
-
9
8
  include Mocha
10
9
 
11
- unless RUBY_V2_PLUS
12
- def test_should_hide_original_method
13
- klass = Class.new { def self.method_x; end }
14
- klass.__metaclass__.send(:alias_method, :_method, :method)
15
- method = ClassMethod.new(klass, :method_x)
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)
16
15
 
17
- method.hide_original_method
16
+ method.hide_original_method
18
17
 
19
- assert_equal false, klass.respond_to?(:method_x)
18
+ assert_equal false, klass.respond_to?(:method_x)
19
+ end
20
20
  end
21
- end
22
21
 
23
22
  def test_should_not_raise_error_hiding_method_that_isnt_defined
24
23
  klass = Class.new
@@ -59,7 +58,11 @@ end
59
58
  end
60
59
 
61
60
  def test_should_restore_original_method
62
- klass = Class.new { def self.method_x; :original_result; end }
61
+ klass = Class.new do
62
+ def self.method_x
63
+ :original_result
64
+ end
65
+ end
63
66
  klass.__metaclass__.send(:alias_method, :_method, :method)
64
67
  method = ClassMethod.new(klass, :method_x)
65
68
 
@@ -73,7 +76,11 @@ end
73
76
  end
74
77
 
75
78
  def test_should_restore_original_method_accepting_a_block_parameter
76
- klass = Class.new { def self.method_x(&block); block.call if block_given? ; end }
79
+ klass = Class.new do
80
+ def self.method_x(&block)
81
+ block.call if block_given?
82
+ end
83
+ end
77
84
  klass.__metaclass__.send(:alias_method, :_method, :method)
78
85
  method = ClassMethod.new(klass, :method_x)
79
86
 
@@ -88,7 +95,11 @@ end
88
95
  end
89
96
 
90
97
  def test_should_not_restore_original_method_if_none_was_defined_in_first_place
91
- klass = Class.new { def self.method_x; :new_result; end }
98
+ klass = Class.new do
99
+ def self.method_x
100
+ :new_result
101
+ end
102
+ end
92
103
  method = ClassMethod.new(klass, :method_x)
93
104
 
94
105
  method.restore_original_method
@@ -124,7 +135,7 @@ end
124
135
  method = ClassMethod.new(klass, :method_x)
125
136
  mocha = build_mock
126
137
  klass.define_instance_method(:mocha) { mocha }
127
- method.replace_instance_method(:reset_mocha) { }
138
+ method.replace_instance_method(:reset_mocha) {}
128
139
  method.define_instance_accessor(:remove_called)
129
140
  method.replace_instance_method(:remove_new_method) { self.remove_called = true }
130
141
 
@@ -138,7 +149,7 @@ end
138
149
  mocha = build_mock
139
150
  klass.define_instance_method(:mocha) { mocha }
140
151
  method = ClassMethod.new(klass, :method_x)
141
- method.replace_instance_method(:reset_mocha) { }
152
+ method.replace_instance_method(:reset_mocha) {}
142
153
  method.define_instance_accessor(:restore_called)
143
154
  method.replace_instance_method(:restore_original_method) { self.restore_called = true }
144
155
 
@@ -150,8 +161,15 @@ end
150
161
  def test_should_call_mocha_unstub
151
162
  klass = Class.new { def self.method_x; end }
152
163
  method = ClassMethod.new(klass, :method_x)
153
- method.replace_instance_method(:restore_original_method) { }
154
- mocha = Class.new { class << self; attr_accessor :unstub_method; end; def self.unstub(method); self.unstub_method = method; end; }
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
155
173
  mocha.define_instance_method(:any_expectations?) { true }
156
174
  method.replace_instance_method(:mock) { mocha }
157
175
 
@@ -162,13 +180,18 @@ end
162
180
  def test_should_call_stubbee_reset_mocha_if_no_expectations_remaining
163
181
  klass = Class.new { def self.method_x; end }
164
182
  method = ClassMethod.new(klass, :method_x)
165
- method.replace_instance_method(:remove_new_method) { }
166
- method.replace_instance_method(:restore_original_method) { }
183
+ method.replace_instance_method(:remove_new_method) {}
184
+ method.replace_instance_method(:restore_original_method) {}
167
185
  mocha = Class.new
168
186
  mocha.define_instance_method(:unstub) { |method_name| }
169
187
  mocha.define_instance_method(:any_expectations?) { false }
170
188
  method.replace_instance_method(:mock) { mocha }
171
- stubbee = Class.new { attr_accessor :reset_mocha_called; def reset_mocha; self.reset_mocha_called = true; end; }.new
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
172
195
  method.replace_instance_method(:stubbee) { stubbee }
173
196
 
174
197
  method.unstub
@@ -191,34 +214,36 @@ end
191
214
  end
192
215
 
193
216
  def test_should_not_match_if_other_class_method_has_different_stubbee
194
- stubbee_1 = Object.new
195
- stubbee_2 = Object.new
196
- class_method_1 = ClassMethod.new(stubbee_1, :method)
197
- class_method_2 = ClassMethod.new(stubbee_2, :method)
198
- assert !class_method_1.matches?(class_method_2)
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)
199
222
  end
200
223
 
201
224
  def test_should_not_match_if_other_class_method_has_different_method
202
225
  stubbee = Object.new
203
- class_method_1 = ClassMethod.new(stubbee, :method_1)
204
- class_method_2 = ClassMethod.new(stubbee, :method_2)
205
- assert !class_method_1.matches?(class_method_2)
226
+ class_method1 = ClassMethod.new(stubbee, :method_1)
227
+ class_method2 = ClassMethod.new(stubbee, :method_2)
228
+ assert !class_method1.matches?(class_method2)
206
229
  end
207
230
 
208
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
209
232
  stubbee = Object.new
210
- class_method_1 = ClassMethod.new(stubbee, :method)
211
- class_method_2 = ClassMethod.new(stubbee, :method)
212
- assert class_method_1.matches?(class_method_2)
233
+ class_method1 = ClassMethod.new(stubbee, :method)
234
+ class_method2 = ClassMethod.new(stubbee, :method)
235
+ assert class_method1.matches?(class_method2)
213
236
  end
214
237
 
215
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
216
239
  stubbee = Class.new do
217
- def equal?(other); false; end
240
+ def equal?(_other)
241
+ false
242
+ end
218
243
  end.new
219
- class_method_1 = ClassMethod.new(stubbee, :method)
220
- class_method_2 = ClassMethod.new(stubbee, :method)
221
- assert class_method_1.matches?(class_method_2)
244
+ class_method1 = ClassMethod.new(stubbee, :method)
245
+ class_method2 = ClassMethod.new(stubbee, :method)
246
+ assert class_method1.matches?(class_method2)
222
247
  end
223
248
 
224
249
  private
@@ -1,13 +1,19 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/class_methods'
3
3
  require 'mocha/object_methods'
4
+ require 'mocha/mockery'
5
+ require 'mocha/names'
4
6
 
5
7
  class ClassMethodsTest < Mocha::TestCase
6
-
7
8
  def setup
9
+ Mocha::Mockery.setup
8
10
  @klass = Class.new.extend(Mocha::ClassMethods, Mocha::ObjectMethods)
9
11
  end
10
12
 
13
+ def teardown
14
+ Mocha::Mockery.teardown
15
+ end
16
+
11
17
  def test_should_build_any_instance_object
12
18
  any_instance = @klass.any_instance
13
19
  assert_not_nil any_instance
@@ -15,9 +21,33 @@ class ClassMethodsTest < Mocha::TestCase
15
21
  end
16
22
 
17
23
  def test_should_return_same_any_instance_object
18
- any_instance_1 = @klass.any_instance
19
- any_instance_2 = @klass.any_instance
20
- assert_equal any_instance_1, any_instance_2
24
+ any_instance1 = @klass.any_instance
25
+ any_instance2 = @klass.any_instance
26
+ assert_equal any_instance1, any_instance2
27
+ end
28
+
29
+ def test_any_instance_should_build_mocha_referring_to_klass
30
+ mocha = @klass.any_instance.mocha
31
+ assert_not_nil mocha
32
+ assert mocha.is_a?(Mocha::Mock)
33
+ expected_name = Mocha::ImpersonatingAnyInstanceName.new(@klass).mocha_inspect
34
+ assert_equal expected_name, mocha.mocha_inspect
35
+ end
36
+
37
+ def test_any_instance_should_not_build_mocha_if_instantiate_is_false
38
+ assert_nil @klass.any_instance.mocha(false)
39
+ end
40
+
41
+ def test_any_instance_should_reuse_existing_mocha
42
+ mocha1 = @klass.any_instance.mocha
43
+ mocha2 = @klass.any_instance.mocha
44
+ assert_equal mocha1, mocha2
45
+ end
46
+
47
+ def test_any_instance_should_reuse_existing_mocha_even_if_instantiate_is_false
48
+ mocha1 = @klass.any_instance.mocha
49
+ mocha2 = @klass.any_instance.mocha(false)
50
+ assert_equal mocha1, mocha2
21
51
  end
22
52
 
23
53
  def test_should_use_stubba_class_method_for_class
@@ -36,5 +66,4 @@ class ClassMethodsTest < Mocha::TestCase
36
66
  any_instance = @klass.any_instance
37
67
  assert_equal @klass, any_instance.stubba_object
38
68
  end
39
-
40
69
  end