mocha 2.7.0 → 2.8.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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -0
  3. data/RELEASE.md +23 -0
  4. data/lib/mocha/api.rb +1 -1
  5. data/lib/mocha/configuration.rb +8 -1
  6. data/lib/mocha/expectation.rb +26 -5
  7. data/lib/mocha/parameter_matchers/all_of.rb +30 -21
  8. data/lib/mocha/parameter_matchers/any_of.rb +36 -27
  9. data/lib/mocha/parameter_matchers/any_parameters.rb +28 -19
  10. data/lib/mocha/parameter_matchers/anything.rb +25 -16
  11. data/lib/mocha/parameter_matchers/base.rb +22 -4
  12. data/lib/mocha/parameter_matchers/deprecations.rb +46 -0
  13. data/lib/mocha/parameter_matchers/equals.rb +31 -22
  14. data/lib/mocha/parameter_matchers/equivalent_uri.rb +30 -21
  15. data/lib/mocha/parameter_matchers/has_entries.rb +31 -22
  16. data/lib/mocha/parameter_matchers/has_entry.rb +72 -65
  17. data/lib/mocha/parameter_matchers/has_key.rb +31 -22
  18. data/lib/mocha/parameter_matchers/has_keys.rb +31 -22
  19. data/lib/mocha/parameter_matchers/has_value.rb +31 -22
  20. data/lib/mocha/parameter_matchers/includes.rb +69 -60
  21. data/lib/mocha/parameter_matchers/instance_methods.rb +1 -1
  22. data/lib/mocha/parameter_matchers/instance_of.rb +31 -22
  23. data/lib/mocha/parameter_matchers/is_a.rb +32 -23
  24. data/lib/mocha/parameter_matchers/kind_of.rb +31 -22
  25. data/lib/mocha/parameter_matchers/not.rb +31 -22
  26. data/lib/mocha/parameter_matchers/optionally.rb +43 -33
  27. data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +6 -1
  28. data/lib/mocha/parameter_matchers/regexp_matches.rb +31 -22
  29. data/lib/mocha/parameter_matchers/responds_with.rb +55 -46
  30. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +30 -21
  31. data/lib/mocha/parameter_matchers.rb +6 -2
  32. data/lib/mocha/parameters_matcher.rb +7 -3
  33. data/lib/mocha/version.rb +1 -1
  34. metadata +4 -3
@@ -1,33 +1,40 @@
1
1
  require 'mocha/parameter_matchers/base'
2
+ require 'mocha/parameter_matchers/deprecations'
2
3
  require 'uri'
3
4
  require 'cgi'
4
5
 
5
6
  module Mocha
6
7
  module ParameterMatchers
7
- # Matches a URI without regard to the ordering of parameters in the query string.
8
- #
9
- # @param [String] uri URI to match.
10
- # @return [EquivalentUri] parameter matcher.
11
- #
12
- # @see Expectation#with
13
- #
14
- # @example Actual URI is equivalent.
15
- # object = mock()
16
- # object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
17
- # object.method_1('http://example.com/foo?b=2&a=1')
18
- # # no error raised
19
- #
20
- # @example Actual URI is not equivalent.
21
- # object = mock()
22
- # object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
23
- # object.method_1('http://example.com/foo?a=1&b=3')
24
- # # error raised, because the query parameters were different
25
- def equivalent_uri(uri)
26
- EquivalentUri.new(uri)
8
+ module Methods
9
+ # Matches a URI without regard to the ordering of parameters in the query string.
10
+ #
11
+ # @param [String] uri URI to match.
12
+ # @return [EquivalentUri] parameter matcher.
13
+ #
14
+ # @see Expectation#with
15
+ #
16
+ # @example Actual URI is equivalent.
17
+ # object = mock()
18
+ # object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
19
+ # object.method_1('http://example.com/foo?b=2&a=1')
20
+ # # no error raised
21
+ #
22
+ # @example Actual URI is not equivalent.
23
+ # object = mock()
24
+ # object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
25
+ # object.method_1('http://example.com/foo?a=1&b=3')
26
+ # # error raised, because the query parameters were different
27
+ def equivalent_uri(uri)
28
+ EquivalentUri.new(uri)
29
+ end
27
30
  end
28
31
 
32
+ define_deprecated_matcher_method(:equivalent_uri)
33
+
29
34
  # Parameter matcher which matches URIs with equivalent query strings.
30
- class EquivalentUri < Base
35
+ class EquivalentUri
36
+ include BaseMethods
37
+
31
38
  # @private
32
39
  def initialize(uri)
33
40
  @uri = URI.parse(uri)
@@ -53,5 +60,7 @@ module Mocha
53
60
  URI::Generic::COMPONENT.inject({}) { |h, k| h.merge(k => uri.__send__(k)) }.merge(query: query_hash)
54
61
  end
55
62
  end
63
+
64
+ provide_deprecated_access_to(:EquivalentUri)
56
65
  end
57
66
  end
@@ -1,34 +1,41 @@
1
1
  require 'mocha/parameter_matchers/base'
2
2
  require 'mocha/parameter_matchers/all_of'
3
3
  require 'mocha/parameter_matchers/has_entry'
4
+ require 'mocha/parameter_matchers/deprecations'
4
5
 
5
6
  module Mocha
6
7
  module ParameterMatchers
7
- # Matches +Hash+ containing all +entries+.
8
- #
9
- # @param [Hash] entries expected +Hash+ entries.
10
- # @return [HasEntries] parameter matcher.
11
- #
12
- # @see Expectation#with
13
- #
14
- # @example Actual parameter contains all expected entries.
15
- # object = mock()
16
- # object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
17
- # object.method_1('key_1' => 1, 'key_2' => 2, 'key_3' => 3)
18
- # # no error raised
19
- #
20
- # @example Actual parameter does not contain all expected entries.
21
- # object = mock()
22
- # object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
23
- # object.method_1('key_1' => 1, 'key_2' => 99)
24
- # # error raised, because method_1 was not called with Hash containing entries: 'key_1' => 1, 'key_2' => 2
25
- #
26
- def has_entries(entries) # rubocop:disable Naming/PredicateName
27
- HasEntries.new(entries)
8
+ module Methods
9
+ # Matches +Hash+ containing all +entries+.
10
+ #
11
+ # @param [Hash] entries expected +Hash+ entries.
12
+ # @return [HasEntries] parameter matcher.
13
+ #
14
+ # @see Expectation#with
15
+ #
16
+ # @example Actual parameter contains all expected entries.
17
+ # object = mock()
18
+ # object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
19
+ # object.method_1('key_1' => 1, 'key_2' => 2, 'key_3' => 3)
20
+ # # no error raised
21
+ #
22
+ # @example Actual parameter does not contain all expected entries.
23
+ # object = mock()
24
+ # object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
25
+ # object.method_1('key_1' => 1, 'key_2' => 99)
26
+ # # error raised, because method_1 was not called with Hash containing entries: 'key_1' => 1, 'key_2' => 2
27
+ #
28
+ def has_entries(entries) # rubocop:disable Naming/PredicateName
29
+ HasEntries.new(entries)
30
+ end
28
31
  end
29
32
 
33
+ define_deprecated_matcher_method(:has_entries)
34
+
30
35
  # Parameter matcher which matches when actual parameter contains all expected +Hash+ entries.
31
- class HasEntries < Base
36
+ class HasEntries
37
+ include BaseMethods
38
+
32
39
  # @private
33
40
  def initialize(entries, exact: false)
34
41
  @entries = entries
@@ -51,5 +58,7 @@ module Mocha
51
58
  @exact ? @entries.mocha_inspect : "has_entries(#{@entries.mocha_inspect})"
52
59
  end
53
60
  end
61
+
62
+ provide_deprecated_access_to(:HasEntries)
54
63
  end
55
64
  end
@@ -1,61 +1,68 @@
1
1
  require 'mocha/parameter_matchers/base'
2
+ require 'mocha/parameter_matchers/deprecations'
2
3
 
3
4
  module Mocha
4
5
  module ParameterMatchers
5
- # Matches +Hash+ containing entry with +key+ and +value+.
6
- #
7
- # @overload def has_entry(key, value)
8
- # @param [Object] key key for entry.
9
- # @param [Object] value value for entry.
10
- # @overload def has_entry(single_entry_hash)
11
- # @param [Hash] single_entry_hash +Hash+ with single entry.
12
- # @raise [ArgumentError] if +single_entry_hash+ does not contain exactly one entry.
13
- #
14
- # @return [HasEntry] parameter matcher.
15
- #
16
- # @see Expectation#with
17
- #
18
- # @example Actual parameter contains expected entry supplied as key and value.
19
- # object = mock()
20
- # object.expects(:method_1).with(has_entry('key_1', 1))
21
- # object.method_1('key_1' => 1, 'key_2' => 2)
22
- # # no error raised
23
- #
24
- # @example Actual parameter contains expected entry supplied as +Hash+ entry.
25
- # object = mock()
26
- # object.expects(:method_1).with(has_entry('key_1' => 1))
27
- # object.method_1('key_1' => 1, 'key_2' => 2)
28
- # # no error raised
29
- #
30
- # @example Actual parameter does not contain expected entry supplied as key and value.
31
- # object = mock()
32
- # object.expects(:method_1).with(has_entry('key_1', 1))
33
- # object.method_1('key_1' => 2, 'key_2' => 1)
34
- # # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
35
- #
36
- # @example Actual parameter does not contain expected entry supplied as +Hash+ entry.
37
- #
38
- # object = mock()
39
- # object.expects(:method_1).with(has_entry('key_1' => 1))
40
- # object.method_1('key_1' => 2, 'key_2' => 1)
41
- # # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
42
- #
43
- def has_entry(*options) # rubocop:disable Naming/PredicateName
44
- case options.length
45
- when 0
46
- raise ArgumentError, 'No arguments. Expecting at least one.'
47
- when 1
48
- key, value = parse_option(options[0])
49
- when 2
50
- key, value = options
51
- else
52
- raise ArgumentError, 'Too many arguments; use either a single argument (must be a Hash) or two arguments (a key and a value).'
6
+ module Methods
7
+ # Matches +Hash+ containing entry with +key+ and +value+.
8
+ #
9
+ # @overload def has_entry(key, value)
10
+ # @param [Object] key key for entry.
11
+ # @param [Object] value value for entry.
12
+ # @overload def has_entry(single_entry_hash)
13
+ # @param [Hash] single_entry_hash +Hash+ with single entry.
14
+ # @raise [ArgumentError] if +single_entry_hash+ does not contain exactly one entry.
15
+ #
16
+ # @return [HasEntry] parameter matcher.
17
+ #
18
+ # @see Expectation#with
19
+ #
20
+ # @example Actual parameter contains expected entry supplied as key and value.
21
+ # object = mock()
22
+ # object.expects(:method_1).with(has_entry('key_1', 1))
23
+ # object.method_1('key_1' => 1, 'key_2' => 2)
24
+ # # no error raised
25
+ #
26
+ # @example Actual parameter contains expected entry supplied as +Hash+ entry.
27
+ # object = mock()
28
+ # object.expects(:method_1).with(has_entry('key_1' => 1))
29
+ # object.method_1('key_1' => 1, 'key_2' => 2)
30
+ # # no error raised
31
+ #
32
+ # @example Actual parameter does not contain expected entry supplied as key and value.
33
+ # object = mock()
34
+ # object.expects(:method_1).with(has_entry('key_1', 1))
35
+ # object.method_1('key_1' => 2, 'key_2' => 1)
36
+ # # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
37
+ #
38
+ # @example Actual parameter does not contain expected entry supplied as +Hash+ entry.
39
+ #
40
+ # object = mock()
41
+ # object.expects(:method_1).with(has_entry('key_1' => 1))
42
+ # object.method_1('key_1' => 2, 'key_2' => 1)
43
+ # # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
44
+ #
45
+ def has_entry(*options) # rubocop:disable Naming/PredicateName
46
+ case options.length
47
+ when 0
48
+ raise ArgumentError, 'No arguments. Expecting at least one.'
49
+ when 1
50
+ key, value = HasEntry.parse_option(options[0])
51
+ when 2
52
+ key, value = options
53
+ else
54
+ raise ArgumentError, 'Too many arguments; use either a single argument (must be a Hash) or two arguments (a key and a value).'
55
+ end
56
+ HasEntry.new(key, value)
53
57
  end
54
- HasEntry.new(key, value)
55
58
  end
56
59
 
60
+ define_deprecated_matcher_method(:has_entry)
61
+
57
62
  # Parameter matcher which matches when actual parameter contains expected +Hash+ entry.
58
- class HasEntry < Base
63
+ class HasEntry
64
+ include BaseMethods
65
+
59
66
  # @private
60
67
  def initialize(key, value)
61
68
  @key = key
@@ -74,25 +81,25 @@ module Mocha
74
81
  def mocha_inspect
75
82
  "has_entry(#{@key.mocha_inspect} => #{@value.mocha_inspect})"
76
83
  end
77
- end
78
-
79
- private
80
84
 
81
- # @private
82
- def parse_option(option)
83
- case option
84
- when Hash
85
- case option.length
86
- when 0
87
- raise ArgumentError, 'Argument has no entries.'
88
- when 1
89
- option.first
85
+ # @private
86
+ def self.parse_option(option)
87
+ case option
88
+ when Hash
89
+ case option.length
90
+ when 0
91
+ raise ArgumentError, 'Argument has no entries.'
92
+ when 1
93
+ option.first
94
+ else
95
+ raise ArgumentError, 'Argument has multiple entries. Use Mocha::ParameterMatchers#has_entries instead.'
96
+ end
90
97
  else
91
- raise ArgumentError, 'Argument has multiple entries. Use Mocha::ParameterMatchers#has_entries instead.'
98
+ raise ArgumentError, 'Argument is not a Hash.'
92
99
  end
93
- else
94
- raise ArgumentError, 'Argument is not a Hash.'
95
100
  end
96
101
  end
102
+
103
+ provide_deprecated_access_to(:HasEntry)
97
104
  end
98
105
  end
@@ -1,32 +1,39 @@
1
1
  require 'mocha/parameter_matchers/base'
2
+ require 'mocha/parameter_matchers/deprecations'
2
3
 
3
4
  module Mocha
4
5
  module ParameterMatchers
5
- # Matches +Hash+ containing +key+.
6
- #
7
- # @param [Object] key expected key.
8
- # @return [HasKey] parameter matcher.
9
- #
10
- # @see Expectation#with
11
- #
12
- # @example Actual parameter contains entry with expected key.
13
- # object = mock()
14
- # object.expects(:method_1).with(has_key('key_1'))
15
- # object.method_1('key_1' => 1, 'key_2' => 2)
16
- # # no error raised
17
- #
18
- # @example Actual parameter does not contain entry with expected key.
19
- # object = mock()
20
- # object.expects(:method_1).with(has_key('key_1'))
21
- # object.method_1('key_2' => 2)
22
- # # error raised, because method_1 was not called with Hash containing key: 'key_1'
23
- #
24
- def has_key(key) # rubocop:disable Naming/PredicateName
25
- HasKey.new(key)
6
+ module Methods
7
+ # Matches +Hash+ containing +key+.
8
+ #
9
+ # @param [Object] key expected key.
10
+ # @return [HasKey] parameter matcher.
11
+ #
12
+ # @see Expectation#with
13
+ #
14
+ # @example Actual parameter contains entry with expected key.
15
+ # object = mock()
16
+ # object.expects(:method_1).with(has_key('key_1'))
17
+ # object.method_1('key_1' => 1, 'key_2' => 2)
18
+ # # no error raised
19
+ #
20
+ # @example Actual parameter does not contain entry with expected key.
21
+ # object = mock()
22
+ # object.expects(:method_1).with(has_key('key_1'))
23
+ # object.method_1('key_2' => 2)
24
+ # # error raised, because method_1 was not called with Hash containing key: 'key_1'
25
+ #
26
+ def has_key(key) # rubocop:disable Naming/PredicateName
27
+ HasKey.new(key)
28
+ end
26
29
  end
27
30
 
31
+ define_deprecated_matcher_method(:has_key)
32
+
28
33
  # Parameter matcher which matches when actual parameter contains +Hash+ entry with expected key.
29
- class HasKey < Base
34
+ class HasKey
35
+ include BaseMethods
36
+
30
37
  # @private
31
38
  def initialize(key)
32
39
  @key = key
@@ -44,5 +51,7 @@ module Mocha
44
51
  "has_key(#{@key.mocha_inspect})"
45
52
  end
46
53
  end
54
+
55
+ provide_deprecated_access_to(:HasKey)
47
56
  end
48
57
  end
@@ -1,32 +1,39 @@
1
1
  require 'mocha/parameter_matchers/base'
2
+ require 'mocha/parameter_matchers/deprecations'
2
3
 
3
4
  module Mocha
4
5
  module ParameterMatchers
5
- # Matches +Hash+ containing +keys+.
6
- #
7
- # @param [*Array<Object>] keys expected keys.
8
- # @return [HasKeys] parameter matcher.
9
- #
10
- # @see Expectation#with
11
- #
12
- # @example Actual parameter contains entry with expected keys.
13
- # object = mock()
14
- # object.expects(:method_1).with(has_keys(:key_1, :key_2))
15
- # object.method_1(:key_1 => 1, :key_2 => 2, :key_3 => 3)
16
- # # no error raised
17
- #
18
- # @example Actual parameter does not contain all expected keys.
19
- # object = mock()
20
- # object.expects(:method_1).with(has_keys(:key_1, :key_2))
21
- # object.method_1(:key_2 => 2)
22
- # # error raised, because method_1 was not called with Hash containing key: :key_1
23
- #
24
- def has_keys(*keys) # rubocop:disable Naming/PredicateName
25
- HasKeys.new(*keys)
6
+ module Methods
7
+ # Matches +Hash+ containing +keys+.
8
+ #
9
+ # @param [*Array<Object>] keys expected keys.
10
+ # @return [HasKeys] parameter matcher.
11
+ #
12
+ # @see Expectation#with
13
+ #
14
+ # @example Actual parameter contains entry with expected keys.
15
+ # object = mock()
16
+ # object.expects(:method_1).with(has_keys(:key_1, :key_2))
17
+ # object.method_1(:key_1 => 1, :key_2 => 2, :key_3 => 3)
18
+ # # no error raised
19
+ #
20
+ # @example Actual parameter does not contain all expected keys.
21
+ # object = mock()
22
+ # object.expects(:method_1).with(has_keys(:key_1, :key_2))
23
+ # object.method_1(:key_2 => 2)
24
+ # # error raised, because method_1 was not called with Hash containing key: :key_1
25
+ #
26
+ def has_keys(*keys) # rubocop:disable Naming/PredicateName
27
+ HasKeys.new(*keys)
28
+ end
26
29
  end
27
30
 
31
+ define_deprecated_matcher_method(:has_keys)
32
+
28
33
  # Parameter matcher which matches when actual parameter contains +Hash+ with all expected keys.
29
- class HasKeys < Base
34
+ class HasKeys
35
+ include BaseMethods
36
+
30
37
  # @private
31
38
  def initialize(*keys)
32
39
  raise ArgumentError, 'No arguments. Expecting at least one.' if keys.empty?
@@ -49,5 +56,7 @@ module Mocha
49
56
  "has_keys(#{@keys.mocha_inspect(false)})"
50
57
  end
51
58
  end
59
+
60
+ provide_deprecated_access_to(:HasKeys)
52
61
  end
53
62
  end
@@ -1,32 +1,39 @@
1
1
  require 'mocha/parameter_matchers/base'
2
+ require 'mocha/parameter_matchers/deprecations'
2
3
 
3
4
  module Mocha
4
5
  module ParameterMatchers
5
- # Matches +Hash+ containing +value+.
6
- #
7
- # @param [Object] value expected value.
8
- # @return [HasValue] parameter matcher.
9
- #
10
- # @see Expectation#with
11
- #
12
- # @example Actual parameter contains entry with expected value.
13
- # object = mock()
14
- # object.expects(:method_1).with(has_value(1))
15
- # object.method_1('key_1' => 1, 'key_2' => 2)
16
- # # no error raised
17
- #
18
- # @example Actual parameter does not contain entry with expected value.
19
- # object = mock()
20
- # object.expects(:method_1).with(has_value(1))
21
- # object.method_1('key_2' => 2)
22
- # # error raised, because method_1 was not called with Hash containing value: 1
23
- #
24
- def has_value(value) # rubocop:disable Naming/PredicateName
25
- HasValue.new(value)
6
+ module Methods
7
+ # Matches +Hash+ containing +value+.
8
+ #
9
+ # @param [Object] value expected value.
10
+ # @return [HasValue] parameter matcher.
11
+ #
12
+ # @see Expectation#with
13
+ #
14
+ # @example Actual parameter contains entry with expected value.
15
+ # object = mock()
16
+ # object.expects(:method_1).with(has_value(1))
17
+ # object.method_1('key_1' => 1, 'key_2' => 2)
18
+ # # no error raised
19
+ #
20
+ # @example Actual parameter does not contain entry with expected value.
21
+ # object = mock()
22
+ # object.expects(:method_1).with(has_value(1))
23
+ # object.method_1('key_2' => 2)
24
+ # # error raised, because method_1 was not called with Hash containing value: 1
25
+ #
26
+ def has_value(value) # rubocop:disable Naming/PredicateName
27
+ HasValue.new(value)
28
+ end
26
29
  end
27
30
 
31
+ define_deprecated_matcher_method(:has_value)
32
+
28
33
  # Parameter matcher which matches when actual parameter contains +Hash+ entry with expected value.
29
- class HasValue < Base
34
+ class HasValue
35
+ include BaseMethods
36
+
30
37
  # @private
31
38
  def initialize(value)
32
39
  @value = value
@@ -44,5 +51,7 @@ module Mocha
44
51
  "has_value(#{@value.mocha_inspect})"
45
52
  end
46
53
  end
54
+
55
+ provide_deprecated_access_to(:HasValue)
47
56
  end
48
57
  end