mocha 0.5.6 → 3.1.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 (191) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/.github/FUNDING.yml +1 -0
  4. data/.rubocop.yml +92 -0
  5. data/.rubocop_todo.yml +39 -0
  6. data/.yardopts +25 -0
  7. data/CONTRIBUTING.md +7 -0
  8. data/COPYING.md +3 -0
  9. data/Gemfile +17 -0
  10. data/{MIT-LICENSE → MIT-LICENSE.md} +2 -2
  11. data/README.md +361 -0
  12. data/RELEASE.md +1247 -0
  13. data/Rakefile +165 -123
  14. data/gemfiles/Gemfile.minitest.latest +8 -0
  15. data/gemfiles/Gemfile.rubocop +9 -0
  16. data/gemfiles/Gemfile.test-unit.latest +8 -0
  17. data/lib/mocha/any_instance_method.rb +12 -26
  18. data/lib/mocha/any_instance_receiver.rb +20 -0
  19. data/lib/mocha/api.rb +213 -0
  20. data/lib/mocha/argument_iterator.rb +17 -0
  21. data/lib/mocha/backtrace_filter.rb +25 -0
  22. data/lib/mocha/block_matchers.rb +33 -0
  23. data/lib/mocha/cardinality.rb +110 -0
  24. data/lib/mocha/central.rb +33 -22
  25. data/lib/mocha/change_state_side_effect.rb +17 -0
  26. data/lib/mocha/class_methods.rb +67 -0
  27. data/lib/mocha/configuration.rb +338 -0
  28. data/lib/mocha/default_name.rb +15 -0
  29. data/lib/mocha/default_receiver.rb +13 -0
  30. data/lib/mocha/deprecation.rb +6 -15
  31. data/lib/mocha/detection/minitest.rb +25 -0
  32. data/lib/mocha/detection/test_unit.rb +30 -0
  33. data/lib/mocha/error_with_filtered_backtrace.rb +15 -0
  34. data/lib/mocha/exception_raiser.rb +11 -10
  35. data/lib/mocha/expectation.rb +562 -171
  36. data/lib/mocha/expectation_error.rb +9 -14
  37. data/lib/mocha/expectation_error_factory.rb +37 -0
  38. data/lib/mocha/expectation_list.rb +30 -14
  39. data/lib/mocha/hooks.rb +55 -0
  40. data/lib/mocha/ignoring_warning.rb +20 -0
  41. data/lib/mocha/impersonating_any_instance_name.rb +13 -0
  42. data/lib/mocha/impersonating_name.rb +13 -0
  43. data/lib/mocha/in_state_ordering_constraint.rb +17 -0
  44. data/lib/mocha/inspect.rb +54 -30
  45. data/lib/mocha/instance_method.rb +17 -4
  46. data/lib/mocha/integration/assertion_counter.rb +15 -0
  47. data/lib/mocha/integration/minitest/adapter.rb +71 -0
  48. data/lib/mocha/integration/minitest.rb +29 -0
  49. data/lib/mocha/integration/monkey_patcher.rb +26 -0
  50. data/lib/mocha/integration/test_unit/adapter.rb +61 -0
  51. data/lib/mocha/integration/test_unit.rb +29 -0
  52. data/lib/mocha/integration.rb +5 -0
  53. data/lib/mocha/invocation.rb +76 -0
  54. data/lib/mocha/logger.rb +26 -0
  55. data/lib/mocha/macos_version.rb +7 -0
  56. data/lib/mocha/method_matcher.rb +8 -10
  57. data/lib/mocha/minitest.rb +7 -0
  58. data/lib/mocha/mock.rb +333 -108
  59. data/lib/mocha/mockery.rb +192 -0
  60. data/lib/mocha/name.rb +13 -0
  61. data/lib/mocha/not_initialized_error.rb +9 -0
  62. data/lib/mocha/object_methods.rb +183 -0
  63. data/lib/mocha/object_receiver.rb +20 -0
  64. data/lib/mocha/parameter_matchers/all_of.rb +38 -28
  65. data/lib/mocha/parameter_matchers/any_of.rb +44 -33
  66. data/lib/mocha/parameter_matchers/any_parameters.rb +33 -26
  67. data/lib/mocha/parameter_matchers/anything.rb +31 -22
  68. data/lib/mocha/parameter_matchers/base_methods.rb +64 -0
  69. data/lib/mocha/parameter_matchers/equals.rb +36 -25
  70. data/lib/mocha/parameter_matchers/equivalent_uri.rb +65 -0
  71. data/lib/mocha/parameter_matchers/has_entries.rb +48 -29
  72. data/lib/mocha/parameter_matchers/has_entry.rb +90 -42
  73. data/lib/mocha/parameter_matchers/has_key.rb +39 -26
  74. data/lib/mocha/parameter_matchers/has_keys.rb +59 -0
  75. data/lib/mocha/parameter_matchers/has_value.rb +39 -26
  76. data/lib/mocha/parameter_matchers/includes.rb +88 -23
  77. data/lib/mocha/parameter_matchers/instance_methods.rb +28 -0
  78. data/lib/mocha/parameter_matchers/instance_of.rb +37 -26
  79. data/lib/mocha/parameter_matchers/is_a.rb +38 -26
  80. data/lib/mocha/parameter_matchers/kind_of.rb +39 -26
  81. data/lib/mocha/parameter_matchers/not.rb +37 -26
  82. data/lib/mocha/parameter_matchers/optionally.rb +52 -17
  83. data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +91 -0
  84. data/lib/mocha/parameter_matchers/regexp_matches.rb +37 -25
  85. data/lib/mocha/parameter_matchers/responds_with.rb +82 -0
  86. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +55 -0
  87. data/lib/mocha/parameter_matchers.rb +12 -5
  88. data/lib/mocha/parameters_matcher.rb +28 -19
  89. data/lib/mocha/raised_exception.rb +13 -0
  90. data/lib/mocha/return_values.rb +13 -18
  91. data/lib/mocha/ruby_version.rb +7 -0
  92. data/lib/mocha/sequence.rb +23 -17
  93. data/lib/mocha/single_return_value.rb +8 -18
  94. data/lib/mocha/state_machine.rb +95 -0
  95. data/lib/mocha/stubbed_method.rb +96 -0
  96. data/lib/mocha/stubbing_error.rb +10 -0
  97. data/lib/mocha/test_unit.rb +7 -0
  98. data/lib/mocha/thrower.rb +15 -0
  99. data/lib/mocha/thrown_object.rb +14 -0
  100. data/lib/mocha/version.rb +5 -0
  101. data/lib/mocha/yield_parameters.rb +12 -20
  102. data/lib/mocha.rb +19 -17
  103. data/mocha.gemspec +40 -0
  104. metadata +129 -145
  105. data/COPYING +0 -3
  106. data/README +0 -35
  107. data/RELEASE +0 -188
  108. data/examples/misc.rb +0 -44
  109. data/examples/mocha.rb +0 -26
  110. data/examples/stubba.rb +0 -65
  111. data/lib/mocha/auto_verify.rb +0 -118
  112. data/lib/mocha/class_method.rb +0 -66
  113. data/lib/mocha/infinite_range.rb +0 -25
  114. data/lib/mocha/is_a.rb +0 -9
  115. data/lib/mocha/metaclass.rb +0 -7
  116. data/lib/mocha/missing_expectation.rb +0 -17
  117. data/lib/mocha/multiple_yields.rb +0 -20
  118. data/lib/mocha/no_yields.rb +0 -11
  119. data/lib/mocha/object.rb +0 -110
  120. data/lib/mocha/parameter_matchers/base.rb +0 -15
  121. data/lib/mocha/parameter_matchers/object.rb +0 -9
  122. data/lib/mocha/pretty_parameters.rb +0 -28
  123. data/lib/mocha/setup_and_teardown.rb +0 -23
  124. data/lib/mocha/single_yield.rb +0 -18
  125. data/lib/mocha/standalone.rb +0 -32
  126. data/lib/mocha/stub.rb +0 -18
  127. data/lib/mocha/test_case_adapter.rb +0 -49
  128. data/lib/mocha_standalone.rb +0 -2
  129. data/lib/stubba.rb +0 -2
  130. data/test/acceptance/expected_invocation_count_acceptance_test.rb +0 -187
  131. data/test/acceptance/mocha_acceptance_test.rb +0 -98
  132. data/test/acceptance/mock_with_initializer_block_acceptance_test.rb +0 -44
  133. data/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +0 -71
  134. data/test/acceptance/optional_parameters_acceptance_test.rb +0 -63
  135. data/test/acceptance/parameter_matcher_acceptance_test.rb +0 -117
  136. data/test/acceptance/partial_mocks_acceptance_test.rb +0 -40
  137. data/test/acceptance/sequence_acceptance_test.rb +0 -179
  138. data/test/acceptance/standalone_acceptance_test.rb +0 -131
  139. data/test/acceptance/stubba_acceptance_test.rb +0 -102
  140. data/test/active_record_test_case.rb +0 -36
  141. data/test/deprecation_disabler.rb +0 -15
  142. data/test/execution_point.rb +0 -34
  143. data/test/integration/mocha_test_result_integration_test.rb +0 -105
  144. data/test/integration/stubba_integration_test.rb +0 -89
  145. data/test/integration/stubba_test_result_integration_test.rb +0 -85
  146. data/test/method_definer.rb +0 -18
  147. data/test/test_helper.rb +0 -12
  148. data/test/test_runner.rb +0 -31
  149. data/test/unit/any_instance_method_test.rb +0 -126
  150. data/test/unit/array_inspect_test.rb +0 -16
  151. data/test/unit/auto_verify_test.rb +0 -129
  152. data/test/unit/central_test.rb +0 -124
  153. data/test/unit/class_method_test.rb +0 -200
  154. data/test/unit/date_time_inspect_test.rb +0 -21
  155. data/test/unit/expectation_error_test.rb +0 -24
  156. data/test/unit/expectation_list_test.rb +0 -75
  157. data/test/unit/expectation_raiser_test.rb +0 -28
  158. data/test/unit/expectation_test.rb +0 -483
  159. data/test/unit/hash_inspect_test.rb +0 -16
  160. data/test/unit/infinite_range_test.rb +0 -53
  161. data/test/unit/metaclass_test.rb +0 -22
  162. data/test/unit/method_matcher_test.rb +0 -23
  163. data/test/unit/missing_expectation_test.rb +0 -42
  164. data/test/unit/mock_test.rb +0 -323
  165. data/test/unit/multiple_yields_test.rb +0 -18
  166. data/test/unit/no_yield_test.rb +0 -18
  167. data/test/unit/object_inspect_test.rb +0 -37
  168. data/test/unit/object_test.rb +0 -165
  169. data/test/unit/parameter_matchers/all_of_test.rb +0 -26
  170. data/test/unit/parameter_matchers/any_of_test.rb +0 -26
  171. data/test/unit/parameter_matchers/anything_test.rb +0 -21
  172. data/test/unit/parameter_matchers/has_entries_test.rb +0 -30
  173. data/test/unit/parameter_matchers/has_entry_test.rb +0 -40
  174. data/test/unit/parameter_matchers/has_key_test.rb +0 -25
  175. data/test/unit/parameter_matchers/has_value_test.rb +0 -25
  176. data/test/unit/parameter_matchers/includes_test.rb +0 -25
  177. data/test/unit/parameter_matchers/instance_of_test.rb +0 -25
  178. data/test/unit/parameter_matchers/is_a_test.rb +0 -25
  179. data/test/unit/parameter_matchers/kind_of_test.rb +0 -25
  180. data/test/unit/parameter_matchers/not_test.rb +0 -26
  181. data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -25
  182. data/test/unit/parameter_matchers/stub_matcher.rb +0 -23
  183. data/test/unit/parameters_matcher_test.rb +0 -121
  184. data/test/unit/return_values_test.rb +0 -63
  185. data/test/unit/sequence_test.rb +0 -104
  186. data/test/unit/setup_and_teardown_test.rb +0 -76
  187. data/test/unit/single_return_value_test.rb +0 -33
  188. data/test/unit/single_yield_test.rb +0 -18
  189. data/test/unit/string_inspect_test.rb +0 -11
  190. data/test/unit/stub_test.rb +0 -24
  191. data/test/unit/yield_parameters_test.rb +0 -93
@@ -1,42 +1,55 @@
1
- require 'mocha/parameter_matchers/base'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
2
4
 
3
5
  module Mocha
4
-
5
6
  module ParameterMatchers
6
-
7
- # :call-seq: has_value(value) -> parameter_matcher
8
- #
9
- # Matches +Hash+ containing +value+.
10
- # object = mock()
11
- # object.expects(:method_1).with(has_value(1))
12
- # object.method_1('key_1' => 1, 'key_2' => 2)
13
- # # no error raised
14
- #
15
- # object = mock()
16
- # object.expects(:method_1).with(has_value(1))
17
- # object.method_1('key_2' => 2)
18
- # # error raised, because method_1 was not called with Hash containing value: 1
19
- def has_value(value)
20
- HasValue.new(value)
7
+ module Methods
8
+ # Matches +Hash+ containing +value+.
9
+ #
10
+ # @param [Object] value expected value.
11
+ # @return [HasValue] parameter matcher.
12
+ #
13
+ # @see Expectation#with
14
+ #
15
+ # @example Actual parameter contains entry with expected value.
16
+ # object = mock()
17
+ # object.expects(:method_1).with(has_value(1))
18
+ # object.method_1('key_1' => 1, 'key_2' => 2)
19
+ # # no error raised
20
+ #
21
+ # @example Actual parameter does not contain entry with expected value.
22
+ # object = mock()
23
+ # object.expects(:method_1).with(has_value(1))
24
+ # object.method_1('key_2' => 2)
25
+ # # error raised, because method_1 was not called with Hash containing value: 1
26
+ #
27
+ def has_value(value) # rubocop:disable Naming/PredicatePrefix
28
+ HasValue.new(value)
29
+ end
21
30
  end
22
31
 
23
- class HasValue < Base # :nodoc:
24
-
32
+ # Parameter matcher which matches when actual parameter contains +Hash+ entry with expected value.
33
+ class HasValue
34
+ include BaseMethods
35
+
36
+ # @private
25
37
  def initialize(value)
26
38
  @value = value
27
39
  end
28
-
40
+
41
+ # @private
29
42
  def matches?(available_parameters)
30
43
  parameter = available_parameters.shift
31
- parameter.values.include?(@value)
44
+ return false unless parameter.respond_to?(:values)
45
+
46
+ parameter.values.any? { |value| @value.to_matcher.matches?([value]) }
32
47
  end
33
-
48
+
49
+ # @private
34
50
  def mocha_inspect
35
51
  "has_value(#{@value.mocha_inspect})"
36
52
  end
37
-
38
53
  end
39
-
40
54
  end
41
-
42
- end
55
+ end
@@ -1,40 +1,105 @@
1
- require 'mocha/parameter_matchers/base'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/all_of'
4
+ require 'mocha/parameter_matchers/base_methods'
2
5
 
3
6
  module Mocha
4
-
5
7
  module ParameterMatchers
6
-
7
- # :call-seq: includes(item) -> parameter_matcher
8
- #
9
- # Matches any object that responds true to include?(item)
10
- # object = mock()
11
- # object.expects(:method_1).with(includes('foo'))
12
- # object.method_1(['foo', 'bar'])
13
- # # no error raised
14
- #
15
- # object.method_1(['baz'])
16
- # # error raised, because ['baz'] does not include 'foo'.
17
- def includes(item)
18
- Includes.new(item)
8
+ module Methods
9
+ # Matches any object that responds with +true+ to +include?(item)+
10
+ # for all items.
11
+ #
12
+ # @param [*Array] items expected items.
13
+ # @return [Includes] parameter matcher.
14
+ #
15
+ # @see Expectation#with
16
+ #
17
+ # @example Actual parameter includes all items.
18
+ # object = mock()
19
+ # object.expects(:method_1).with(includes('foo', 'bar'))
20
+ # object.method_1(['foo', 'bar', 'baz'])
21
+ # # no error raised
22
+ #
23
+ # @example Actual parameter does not include all items.
24
+ # object.method_1(['foo', 'baz'])
25
+ # # error raised, because ['foo', 'baz'] does not include 'bar'.
26
+ #
27
+ # @example Actual parameter includes item which matches nested matcher.
28
+ # object = mock()
29
+ # object.expects(:method_1).with(includes(has_key(:key)))
30
+ # object.method_1(['foo', 'bar', {key: 'baz'}])
31
+ # # no error raised
32
+ #
33
+ # @example Actual parameter does not include item matching nested matcher.
34
+ # object.method_1(['foo', 'bar', {:other_key => 'baz'}])
35
+ # # error raised, because no element matches `has_key(:key)` matcher
36
+ #
37
+ # @example Actual parameter is a String including substring.
38
+ # object = mock()
39
+ # object.expects(:method_1).with(includes('bar'))
40
+ # object.method_1('foobarbaz')
41
+ # # no error raised
42
+ #
43
+ # @example Actual parameter is a String not including substring.
44
+ # object.method_1('foobaz')
45
+ # # error raised, because 'foobaz' does not include 'bar'
46
+ #
47
+ # @example Actual parameter is a Hash including the given key.
48
+ # object = mock()
49
+ # object.expects(:method_1).with(includes(:bar))
50
+ # object.method_1({foo: 1, bar: 2})
51
+ # # no error raised
52
+ #
53
+ # @example Actual parameter is a Hash without the given key.
54
+ # object.method_1({foo: 1, baz: 2})
55
+ # # error raised, because hash does not include key 'bar'
56
+ #
57
+ # @example Actual parameter is a Hash with a key matching the given matcher.
58
+ # object = mock()
59
+ # object.expects(:method_1).with(includes(regexp_matches(/ar/)))
60
+ # object.method_1({'foo' => 1, 'bar' => 2})
61
+ # # no error raised
62
+ #
63
+ # @example Actual parameter is a Hash no key matching the given matcher.
64
+ # object.method_1({'foo' => 1, 'baz' => 3})
65
+ # # error raised, because hash does not include a key matching /ar/
66
+ def includes(*items)
67
+ Includes.new(*items)
68
+ end
19
69
  end
20
70
 
21
- class Includes < Base # :nodoc:
71
+ # Parameter matcher which matches when actual parameter includes expected values.
72
+ class Includes
73
+ include BaseMethods
22
74
 
23
- def initialize(item)
24
- @item = item
75
+ # @private
76
+ def initialize(*items)
77
+ @items = items
25
78
  end
26
79
 
80
+ # @private
27
81
  def matches?(available_parameters)
28
82
  parameter = available_parameters.shift
29
- return parameter.include?(@item)
83
+ return false unless parameter.respond_to?(:include?)
84
+
85
+ if @items.size == 1
86
+ if parameter.respond_to?(:any?) && !parameter.is_a?(String)
87
+ parameter = parameter.keys if parameter.is_a?(Hash)
88
+ parameter.any? { |p| @items.first.to_matcher.matches?([p]) }
89
+ else
90
+ parameter.include?(@items.first)
91
+ end
92
+ else
93
+ includes_matchers = @items.map { |item| Includes.new(item) }
94
+ AllOf.new(*includes_matchers).matches?([parameter])
95
+ end
30
96
  end
31
97
 
98
+ # @private
32
99
  def mocha_inspect
33
- "includes(#{@item.mocha_inspect})"
100
+ item_descriptions = @items.map(&:mocha_inspect)
101
+ "includes(#{item_descriptions.join(', ')})"
34
102
  end
35
-
36
103
  end
37
-
38
104
  end
39
-
40
105
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
4
+ require 'mocha/parameter_matchers/equals'
5
+ require 'mocha/parameter_matchers/positional_or_keyword_hash'
6
+
7
+ module Mocha
8
+ module ParameterMatchers
9
+ # @private
10
+ module InstanceMethods
11
+ # @private
12
+ def to_matcher(expectation: nil, top_level: false, last: false)
13
+ if is_a?(BaseMethods)
14
+ self
15
+ elsif Hash === self && top_level
16
+ Mocha::ParameterMatchers::PositionalOrKeywordHash.new(self, expectation, last)
17
+ else
18
+ Mocha::ParameterMatchers::Equals.new(self)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ # @private
26
+ class Object # rubocop:disable Style/OneClassPerFile
27
+ include Mocha::ParameterMatchers::InstanceMethods
28
+ end
@@ -1,42 +1,53 @@
1
- require 'mocha/parameter_matchers/base'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
2
4
 
3
5
  module Mocha
4
-
5
6
  module ParameterMatchers
6
-
7
- # :call-seq: instance_of(klass) -> parameter_matcher
8
- #
9
- # Matches any object that is an instance of +klass+
10
- # object = mock()
11
- # object.expects(:method_1).with(instance_of(String))
12
- # object.method_1('string')
13
- # # no error raised
14
- #
15
- # object = mock()
16
- # object.expects(:method_1).with(instance_of(String))
17
- # object.method_1(99)
18
- # # error raised, because method_1 was not called with an instance of String
19
- def instance_of(klass)
20
- InstanceOf.new(klass)
7
+ module Methods
8
+ # Matches any object that is an instance of +klass+
9
+ #
10
+ # @param [Class] klass expected class.
11
+ # @return [InstanceOf] parameter matcher.
12
+ #
13
+ # @see Expectation#with
14
+ # @see Kernel#instance_of?
15
+ #
16
+ # @example Actual parameter is an instance of +String+.
17
+ # object = mock()
18
+ # object.expects(:method_1).with(instance_of(String))
19
+ # object.method_1('string')
20
+ # # no error raised
21
+ #
22
+ # @example Actual parameter is not an instance of +String+.
23
+ # object = mock()
24
+ # object.expects(:method_1).with(instance_of(String))
25
+ # object.method_1(99)
26
+ # # error raised, because method_1 was not called with an instance of String
27
+ def instance_of(klass)
28
+ InstanceOf.new(klass)
29
+ end
21
30
  end
22
-
23
- class InstanceOf < Base # :nodoc:
24
-
31
+
32
+ # Parameter matcher which matches when actual parameter is an instance of the specified class.
33
+ class InstanceOf
34
+ include BaseMethods
35
+
36
+ # @private
25
37
  def initialize(klass)
26
38
  @klass = klass
27
39
  end
28
-
40
+
41
+ # @private
29
42
  def matches?(available_parameters)
30
43
  parameter = available_parameters.shift
31
44
  parameter.instance_of?(@klass)
32
45
  end
33
-
46
+
47
+ # @private
34
48
  def mocha_inspect
35
49
  "instance_of(#{@klass.mocha_inspect})"
36
50
  end
37
-
38
51
  end
39
-
40
52
  end
41
-
42
- end
53
+ end
@@ -1,42 +1,54 @@
1
- require 'mocha/parameter_matchers/base'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
2
4
 
3
5
  module Mocha
4
-
5
6
  module ParameterMatchers
6
-
7
- # :call-seq: is_a(klass) -> parameter_matcher
8
- #
9
- # Matches any object that is a +klass+
10
- # object = mock()
11
- # object.expects(:method_1).with(is_a(Integer))
12
- # object.method_1(99)
13
- # # no error raised
14
- #
15
- # object = mock()
16
- # object.expects(:method_1).with(is_a(Integer))
17
- # object.method_1('string')
18
- # # error raised, because method_1 was not called with an Integer
19
- def is_a(klass)
20
- IsA.new(klass)
7
+ module Methods
8
+ # Matches any object that is a +klass+.
9
+ #
10
+ # @param [Class] klass expected class.
11
+ # @return [IsA] parameter matcher.
12
+ #
13
+ # @see Expectation#with
14
+ # @see Kernel#is_a?
15
+ #
16
+ # @example Actual parameter is a +Integer+.
17
+ # object = mock()
18
+ # object.expects(:method_1).with(is_a(Integer))
19
+ # object.method_1(99)
20
+ # # no error raised
21
+ #
22
+ # @example Actual parameter is not a +Integer+.
23
+ # object = mock()
24
+ # object.expects(:method_1).with(is_a(Integer))
25
+ # object.method_1('string')
26
+ # # error raised, because method_1 was not called with an Integer
27
+ #
28
+ def is_a(klass) # rubocop:disable Naming/PredicatePrefix
29
+ IsA.new(klass)
30
+ end
21
31
  end
22
-
23
- class IsA < Base # :nodoc:
24
-
32
+
33
+ # Parameter matcher which matches when actual parameter is a specific class.
34
+ class IsA
35
+ include BaseMethods
36
+
37
+ # @private
25
38
  def initialize(klass)
26
39
  @klass = klass
27
40
  end
28
-
41
+
42
+ # @private
29
43
  def matches?(available_parameters)
30
44
  parameter = available_parameters.shift
31
45
  parameter.is_a?(@klass)
32
46
  end
33
-
47
+
48
+ # @private
34
49
  def mocha_inspect
35
50
  "is_a(#{@klass.mocha_inspect})"
36
51
  end
37
-
38
52
  end
39
-
40
53
  end
41
-
42
- end
54
+ end
@@ -1,42 +1,55 @@
1
- require 'mocha/parameter_matchers/base'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
2
4
 
3
5
  module Mocha
4
-
5
6
  module ParameterMatchers
6
-
7
- # :call-seq: kind_of(klass) -> parameter_matcher
8
- #
9
- # Matches any object that is a kind of +klass+
10
- # object = mock()
11
- # object.expects(:method_1).with(kind_of(Integer))
12
- # object.method_1(99)
13
- # # no error raised
14
- #
15
- # object = mock()
16
- # object.expects(:method_1).with(kind_of(Integer))
17
- # object.method_1('string')
18
- # # error raised, because method_1 was not called with a kind of Integer
19
- def kind_of(klass)
20
- KindOf.new(klass)
7
+ module Methods
8
+ # Matches any +Object+ that is a kind of +klass+.
9
+ #
10
+ # @param [Class] klass expected class.
11
+ # @return [KindOf] parameter matcher.
12
+ #
13
+ # @see Expectation#with
14
+ # @see Kernel#kind_of?
15
+ #
16
+ # @example Actual parameter is a kind of +Integer+.
17
+ # object = mock()
18
+ # object.expects(:method_1).with(kind_of(Integer))
19
+ # object.method_1(99)
20
+ # # no error raised
21
+ #
22
+ # @example Actual parameter is not a kind of +Integer+.
23
+ # object = mock()
24
+ # object.expects(:method_1).with(kind_of(Integer))
25
+ # object.method_1('string')
26
+ # # error raised, because method_1 was not called with a kind of Integer
27
+ def kind_of(klass)
28
+ KindOf.new(klass)
29
+ end
21
30
  end
22
-
23
- class KindOf < Base # :nodoc:
24
-
31
+
32
+ # Parameter matcher which matches when actual parameter is a kind of specified class.
33
+ class KindOf
34
+ include BaseMethods
35
+
36
+ # @private
25
37
  def initialize(klass)
26
38
  @klass = klass
27
39
  end
28
-
40
+
41
+ # @private
29
42
  def matches?(available_parameters)
30
43
  parameter = available_parameters.shift
44
+ # rubocop:disable Style/ClassCheck
31
45
  parameter.kind_of?(@klass)
46
+ # rubocop:enable Style/ClassCheck
32
47
  end
33
-
48
+
49
+ # @private
34
50
  def mocha_inspect
35
51
  "kind_of(#{@klass.mocha_inspect})"
36
52
  end
37
-
38
53
  end
39
-
40
54
  end
41
-
42
- end
55
+ end
@@ -1,42 +1,53 @@
1
- require 'mocha/parameter_matchers/base'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
2
4
 
3
5
  module Mocha
4
-
5
6
  module ParameterMatchers
6
-
7
- # :call-seq: Not(matcher) -> parameter_matcher
8
- #
9
- # Matches if +matcher+ does not match.
10
- # object = mock()
11
- # object.expects(:method_1).with(Not(includes(1)))
12
- # object.method_1([0, 2, 3])
13
- # # no error raised
14
- #
15
- # object = mock()
16
- # object.expects(:method_1).with(Not(includes(1)))
17
- # object.method_1([0, 1, 2, 3])
18
- # # error raised, because method_1 was not called with object not including 1
19
- def Not(matcher)
20
- Not.new(matcher)
7
+ module Methods
8
+ # Matches if +matcher+ does *not* match.
9
+ #
10
+ # @param [BaseMethods] matcher matcher whose logic to invert.
11
+ # @return [Not] parameter matcher.
12
+ #
13
+ # @see Expectation#with
14
+ #
15
+ # @example Actual parameter does not include the value +1+.
16
+ # object = mock()
17
+ # object.expects(:method_1).with(Not(includes(1)))
18
+ # object.method_1([0, 2, 3])
19
+ # # no error raised
20
+ #
21
+ # @example Actual parameter does include the value +1+.
22
+ # object = mock()
23
+ # object.expects(:method_1).with(Not(includes(1)))
24
+ # object.method_1([0, 1, 2, 3])
25
+ # # error raised, because method_1 was not called with object not including 1
26
+ #
27
+ def Not(matcher) # rubocop:disable Naming/MethodName
28
+ Not.new(matcher)
29
+ end
21
30
  end
22
-
23
- class Not < Base # :nodoc:
24
-
31
+
32
+ # Parameter matcher which inverts the logic of the specified matcher using a logical NOT operation.
33
+ class Not
34
+ include BaseMethods
35
+
36
+ # @private
25
37
  def initialize(matcher)
26
38
  @matcher = matcher
27
39
  end
28
-
40
+
41
+ # @private
29
42
  def matches?(available_parameters)
30
43
  parameter = available_parameters.shift
31
44
  !@matcher.matches?([parameter])
32
45
  end
33
-
46
+
47
+ # @private
34
48
  def mocha_inspect
35
49
  "Not(#{@matcher.mocha_inspect})"
36
50
  end
37
-
38
51
  end
39
-
40
52
  end
41
-
42
- end
53
+ end
@@ -1,33 +1,68 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
-
3
4
  module ParameterMatchers
4
-
5
- def optionally(*matchers)
6
- Optionally.new(*matchers)
5
+ module Methods
6
+ # Matches optional parameters if available.
7
+ #
8
+ # @param [*Array<BaseMethods>] matchers matchers for optional parameters.
9
+ # @return [Optionally] parameter matcher.
10
+ #
11
+ # @see Expectation#with
12
+ #
13
+ # @example Only the two required parameters are supplied and they both match their expected value.
14
+ # object = mock()
15
+ # object.expects(:method_1).with(1, 2, optionally(3, 4))
16
+ # object.method_1(1, 2)
17
+ # # no error raised
18
+ #
19
+ # @example Both required parameters and one of the optional parameters are supplied and they all match their expected value.
20
+ # object = mock()
21
+ # object.expects(:method_1).with(1, 2, optionally(3, 4))
22
+ # object.method_1(1, 2, 3)
23
+ # # no error raised
24
+ #
25
+ # @example Both required parameters and both of the optional parameters are supplied and they all match their expected value.
26
+ # object = mock()
27
+ # object.expects(:method_1).with(1, 2, optionally(3, 4))
28
+ # object.method_1(1, 2, 3, 4)
29
+ # # no error raised
30
+ #
31
+ # @example One of the actual optional parameters does not match the expected value.
32
+ # object = mock()
33
+ # object.expects(:method_1).with(1, 2, optionally(3, 4))
34
+ # object.method_1(1, 2, 3, 5)
35
+ # # error raised, because optional parameters did not match
36
+ def optionally(*matchers)
37
+ Optionally.new(*matchers)
38
+ end
7
39
  end
8
-
9
- class Optionally < Base # :nodoc:
10
-
40
+
41
+ # Parameter matcher which allows optional parameters to be specified.
42
+ class Optionally
43
+ include BaseMethods
44
+
45
+ # @private
11
46
  def initialize(*parameters)
12
- @matchers = parameters.map { |parameter| parameter.to_matcher }
47
+ @matchers = parameters.map(&:to_matcher)
13
48
  end
14
-
49
+
50
+ # @private
15
51
  def matches?(available_parameters)
16
52
  index = 0
17
- while (available_parameters.length > 0) && (index < @matchers.length) do
53
+ while !available_parameters.empty? && (index < @matchers.length)
18
54
  matcher = @matchers[index]
19
55
  return false unless matcher.matches?(available_parameters)
56
+
20
57
  index += 1
21
58
  end
22
- return true
59
+ true
23
60
  end
24
-
61
+
62
+ # @private
25
63
  def mocha_inspect
26
- "optionally(#{@matchers.map { |matcher| matcher.mocha_inspect }.join(", ") })"
64
+ "optionally(#{@matchers.map(&:mocha_inspect).join(', ')})"
27
65
  end
28
-
29
66
  end
30
-
31
67
  end
32
-
33
- end
68
+ end