rspec-expectations 3.0.0.beta1 → 3.0.0.beta2
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.
- data.tar.gz.sig +2 -2
- data/.yardopts +1 -0
- data/Changelog.md +138 -0
- data/README.md +75 -8
- data/features/README.md +2 -2
- data/features/built_in_matchers/README.md +12 -9
- data/features/built_in_matchers/comparisons.feature +2 -2
- data/features/built_in_matchers/contain_exactly.feature +46 -0
- data/features/built_in_matchers/expect_change.feature +2 -2
- data/features/built_in_matchers/include.feature +0 -48
- data/features/built_in_matchers/output.feature +70 -0
- data/features/composing_matchers.feature +250 -0
- data/features/compound_expectations.feature +45 -0
- data/features/custom_matchers/access_running_example.feature +1 -1
- data/features/custom_matchers/define_matcher.feature +6 -6
- data/features/custom_matchers/define_matcher_outside_rspec.feature +4 -8
- data/features/test_frameworks/{test_unit.feature → minitest.feature} +11 -11
- data/lib/rspec/expectations.rb +31 -42
- data/lib/rspec/expectations/diff_presenter.rb +141 -0
- data/lib/rspec/expectations/differ.rb +22 -132
- data/lib/rspec/expectations/encoded_string.rb +56 -0
- data/lib/rspec/expectations/expectation_target.rb +0 -30
- data/lib/rspec/expectations/fail_with.rb +2 -2
- data/lib/rspec/expectations/handler.rb +128 -31
- data/lib/rspec/expectations/minitest_integration.rb +16 -0
- data/lib/rspec/expectations/syntax.rb +4 -58
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +298 -60
- data/lib/rspec/matchers/aliased_matcher.rb +35 -0
- data/lib/rspec/matchers/built_in.rb +37 -33
- data/lib/rspec/matchers/built_in/base_matcher.rb +25 -15
- data/lib/rspec/matchers/built_in/be.rb +23 -31
- data/lib/rspec/matchers/built_in/be_between.rb +55 -0
- data/lib/rspec/matchers/built_in/be_within.rb +15 -11
- data/lib/rspec/matchers/built_in/change.rb +198 -81
- data/lib/rspec/matchers/built_in/compound.rb +106 -0
- data/lib/rspec/matchers/built_in/contain_exactly.rb +245 -0
- data/lib/rspec/matchers/built_in/eq.rb +43 -4
- data/lib/rspec/matchers/built_in/eql.rb +2 -2
- data/lib/rspec/matchers/built_in/equal.rb +35 -18
- data/lib/rspec/matchers/built_in/has.rb +16 -15
- data/lib/rspec/matchers/built_in/include.rb +45 -23
- data/lib/rspec/matchers/built_in/match.rb +6 -3
- data/lib/rspec/matchers/built_in/operators.rb +103 -0
- data/lib/rspec/matchers/built_in/output.rb +108 -0
- data/lib/rspec/matchers/built_in/raise_error.rb +9 -15
- data/lib/rspec/matchers/built_in/respond_to.rb +5 -4
- data/lib/rspec/matchers/built_in/satisfy.rb +4 -3
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +37 -16
- data/lib/rspec/matchers/built_in/throw_symbol.rb +6 -5
- data/lib/rspec/matchers/built_in/yield.rb +31 -29
- data/lib/rspec/matchers/composable.rb +138 -0
- data/lib/rspec/matchers/dsl.rb +330 -0
- data/lib/rspec/matchers/generated_descriptions.rb +6 -6
- data/lib/rspec/matchers/matcher_delegator.rb +33 -0
- data/lib/rspec/matchers/pretty.rb +13 -2
- data/spec/rspec/expectations/{differ_spec.rb → diff_presenter_spec.rb} +56 -36
- data/spec/rspec/expectations/encoded_string_spec.rb +74 -0
- data/spec/rspec/expectations/extensions/kernel_spec.rb +11 -11
- data/spec/rspec/expectations/fail_with_spec.rb +8 -8
- data/spec/rspec/expectations/handler_spec.rb +27 -49
- data/spec/rspec/expectations/minitest_integration_spec.rb +27 -0
- data/spec/rspec/expectations/syntax_spec.rb +17 -67
- data/spec/rspec/expectations_spec.rb +7 -52
- data/spec/rspec/matchers/aliased_matcher_spec.rb +48 -0
- data/spec/rspec/matchers/aliases_spec.rb +449 -0
- data/spec/rspec/matchers/{base_matcher_spec.rb → built_in/base_matcher_spec.rb} +24 -3
- data/spec/rspec/matchers/built_in/be_between_spec.rb +159 -0
- data/spec/rspec/matchers/{be_instance_of_spec.rb → built_in/be_instance_of_spec.rb} +0 -0
- data/spec/rspec/matchers/{be_kind_of_spec.rb → built_in/be_kind_of_spec.rb} +0 -0
- data/spec/rspec/matchers/{be_spec.rb → built_in/be_spec.rb} +76 -32
- data/spec/rspec/matchers/{be_within_spec.rb → built_in/be_within_spec.rb} +6 -2
- data/spec/rspec/matchers/{change_spec.rb → built_in/change_spec.rb} +310 -69
- data/spec/rspec/matchers/built_in/compound_spec.rb +292 -0
- data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +441 -0
- data/spec/rspec/matchers/{cover_spec.rb → built_in/cover_spec.rb} +0 -0
- data/spec/rspec/matchers/built_in/eq_spec.rb +156 -0
- data/spec/rspec/matchers/{eql_spec.rb → built_in/eql_spec.rb} +2 -2
- data/spec/rspec/matchers/built_in/equal_spec.rb +106 -0
- data/spec/rspec/matchers/{exist_spec.rb → built_in/exist_spec.rb} +1 -1
- data/spec/rspec/matchers/{has_spec.rb → built_in/has_spec.rb} +39 -0
- data/spec/rspec/matchers/{include_spec.rb → built_in/include_spec.rb} +118 -109
- data/spec/rspec/matchers/{match_spec.rb → built_in/match_spec.rb} +30 -2
- data/spec/rspec/matchers/{operator_matcher_spec.rb → built_in/operators_spec.rb} +26 -26
- data/spec/rspec/matchers/built_in/output_spec.rb +165 -0
- data/spec/rspec/matchers/{raise_error_spec.rb → built_in/raise_error_spec.rb} +81 -11
- data/spec/rspec/matchers/{respond_to_spec.rb → built_in/respond_to_spec.rb} +0 -0
- data/spec/rspec/matchers/{satisfy_spec.rb → built_in/satisfy_spec.rb} +0 -0
- data/spec/rspec/matchers/{start_with_end_with_spec.rb → built_in/start_and_end_with_spec.rb} +82 -15
- data/spec/rspec/matchers/{throw_symbol_spec.rb → built_in/throw_symbol_spec.rb} +29 -10
- data/spec/rspec/matchers/{yield_spec.rb → built_in/yield_spec.rb} +90 -0
- data/spec/rspec/matchers/configuration_spec.rb +7 -39
- data/spec/rspec/matchers/description_generation_spec.rb +22 -6
- data/spec/rspec/matchers/dsl_spec.rb +838 -0
- data/spec/rspec/matchers/legacy_spec.rb +101 -0
- data/spec/rspec/matchers_spec.rb +74 -0
- data/spec/spec_helper.rb +35 -21
- data/spec/support/shared_examples.rb +26 -4
- metadata +172 -116
- metadata.gz.sig +3 -4
- checksums.yaml +0 -15
- checksums.yaml.gz.sig +0 -0
- data/features/built_in_matchers/match_array.feature +0 -37
- data/lib/rspec/expectations/errors.rb +0 -9
- data/lib/rspec/expectations/extensions.rb +0 -1
- data/lib/rspec/expectations/extensions/object.rb +0 -29
- data/lib/rspec/matchers/built_in/match_array.rb +0 -51
- data/lib/rspec/matchers/compatibility.rb +0 -14
- data/lib/rspec/matchers/matcher.rb +0 -301
- data/lib/rspec/matchers/method_missing.rb +0 -12
- data/lib/rspec/matchers/operator_matcher.rb +0 -99
- data/lib/rspec/matchers/test_unit_integration.rb +0 -11
- data/spec/rspec/matchers/eq_spec.rb +0 -60
- data/spec/rspec/matchers/equal_spec.rb +0 -78
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
- data/spec/rspec/matchers/match_array_spec.rb +0 -194
- data/spec/rspec/matchers/matcher_spec.rb +0 -706
- data/spec/rspec/matchers/matchers_spec.rb +0 -36
- data/spec/rspec/matchers/method_missing_spec.rb +0 -28
- data/spec/support/classes.rb +0 -56
- data/spec/support/in_sub_process.rb +0 -37
- data/spec/support/ruby_version.rb +0 -10
metadata.gz.sig
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
|
-
p
|
3
|
-
�
|
4
|
-
i�������Ĩ�ik9�gu��@�5?a��Ejdڳ*:�)�WL�B�i�g�2q �GF��D?�UN|*�����/�|�>)�gMkĺ��<؛tZ
|
1
|
+
��5>u��-c�rEJ�
|
2
|
+
��'���{���J���Ncc�K� j��5��'���Մ�8z���2br��f��G�.R�p�H���ա�P(����^�1GNw�'W 2~�~�U�ӖE��ò\W VF�G�
|
3
|
+
���"�-���g��3�
|
checksums.yaml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
!binary "U0hBMQ==":
|
3
|
-
metadata.gz: !binary |-
|
4
|
-
ZTQwOGIzZDdiMmUzMGI5OWQ2NmQzOTBjYmJiZTUzODkyYTAxOGM2YQ==
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
YzI4OTM1NjI1ZDRiZTRkYjY3NTJkMTMzMThkMWY3OTY2OWE2MDFiNQ==
|
7
|
-
!binary "U0hBNTEy":
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
ODFmYzI4Mjk1MTVmMjYyMjc0NzBiNTE4YTc2MjE2ZjZiMDhhODBlN2Q3ZmZm
|
10
|
-
MWFmMzU4NTkxOWZiMTczYzgzMWI5NzU5MTM5M2IyYjBkYmUxNGQ5ZDM0YjVm
|
11
|
-
MGJjMWY1ODY4MjU5ZDZmODdmOTY4MzZkMDEzNmI5Y2I5YjljNzM=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MjA3YWZlNjdmOTA4YzRhYmVjODQ5MDUzMzIxNTJhMTFmMmQwZTk1MTQ3OGM2
|
14
|
-
OTNmYjI1NjhlZDMxNDU0NTlhNjljNWQ2ZWE4MTI3YWQzYjA3N2JmZmViNmY1
|
15
|
-
MThhZWNkNzZmOGJmY2M3MDBjNDQzMjJmOTkwZDAyZTlhZDJkMjc=
|
checksums.yaml.gz.sig
DELETED
Binary file
|
@@ -1,37 +0,0 @@
|
|
1
|
-
Feature: match_array matcher
|
2
|
-
|
3
|
-
The `match_array` matcher provides a way to test arrays against each other
|
4
|
-
in a way that disregards differences in the ordering between the actual
|
5
|
-
and expected array. For example:
|
6
|
-
|
7
|
-
```ruby
|
8
|
-
expect([1, 2, 3]).to match_array [2, 3, 1] # pass
|
9
|
-
expect([:a, :c, :b]).to match_array [:a, :c] # fail
|
10
|
-
```
|
11
|
-
|
12
|
-
Scenario: array operator matchers
|
13
|
-
Given a file named "array_operator_matchers_spec.rb" with:
|
14
|
-
"""ruby
|
15
|
-
describe do
|
16
|
-
example { expect([1, 2, 3]).to match_array [1, 2, 3] }
|
17
|
-
example { expect([1, 2, 3]).to match_array [1, 3, 2] }
|
18
|
-
example { expect([1, 2, 3]).to match_array [2, 1, 3] }
|
19
|
-
example { expect([1, 2, 3]).to match_array [2, 3, 1] }
|
20
|
-
example { expect([1, 2, 3]).to match_array [3, 1, 2] }
|
21
|
-
example { expect([1, 2, 3]).to match_array [3, 2, 1] }
|
22
|
-
|
23
|
-
# deliberate failures
|
24
|
-
example { expect([1, 2, 3]).to match_array [1, 2, 1] }
|
25
|
-
end
|
26
|
-
"""
|
27
|
-
When I run `rspec array_operator_matchers_spec.rb`
|
28
|
-
Then the output should contain "7 examples, 1 failure"
|
29
|
-
And the output should contain:
|
30
|
-
"""
|
31
|
-
Failure/Error: example { expect([1, 2, 3]).to match_array [1, 2, 1] }
|
32
|
-
expected collection contained: [1, 1, 2]
|
33
|
-
actual collection contained: [1, 2, 3]
|
34
|
-
the missing elements were: [1]
|
35
|
-
the extra elements were: [3]
|
36
|
-
"""
|
37
|
-
|
@@ -1 +0,0 @@
|
|
1
|
-
require 'rspec/expectations/extensions/object'
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module RSpec
|
2
|
-
module Expectations
|
3
|
-
module DeprecatedConstants
|
4
|
-
# Displays deprecation warning when it captures Rspec and Spec. Otherwise
|
5
|
-
# delegates to super.
|
6
|
-
def const_missing(name)
|
7
|
-
case name
|
8
|
-
when :Rspec, :Spec
|
9
|
-
RSpec.deprecate(name.to_s, :replacement => "RSpec")
|
10
|
-
RSpec
|
11
|
-
else
|
12
|
-
begin
|
13
|
-
super
|
14
|
-
rescue Exception => e
|
15
|
-
e.backtrace.reject! {|l| l =~ Regexp.compile(__FILE__) }
|
16
|
-
raise e
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# @deprecated (no replacement)
|
23
|
-
def differ=(ignore)
|
24
|
-
RSpec.deprecate("RSpec::Expectations.differ=(differ)")
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
extend RSpec::Expectations::DeprecatedConstants
|
@@ -1,51 +0,0 @@
|
|
1
|
-
module RSpec
|
2
|
-
module Matchers
|
3
|
-
module BuiltIn
|
4
|
-
class MatchArray < BaseMatcher
|
5
|
-
def match(expected, actual)
|
6
|
-
return false unless actual.respond_to? :to_ary
|
7
|
-
@extra_items = difference_between_arrays(actual, expected)
|
8
|
-
@missing_items = difference_between_arrays(expected, actual)
|
9
|
-
@extra_items.empty? & @missing_items.empty?
|
10
|
-
end
|
11
|
-
|
12
|
-
def failure_message_for_should
|
13
|
-
if actual.respond_to? :to_ary
|
14
|
-
message = "expected collection contained: #{safe_sort(expected).inspect}\n"
|
15
|
-
message += "actual collection contained: #{safe_sort(actual).inspect}\n"
|
16
|
-
message += "the missing elements were: #{safe_sort(@missing_items).inspect}\n" unless @missing_items.empty?
|
17
|
-
message += "the extra elements were: #{safe_sort(@extra_items).inspect}\n" unless @extra_items.empty?
|
18
|
-
else
|
19
|
-
message = "expected an array, actual collection was #{actual.inspect}"
|
20
|
-
end
|
21
|
-
|
22
|
-
message
|
23
|
-
end
|
24
|
-
|
25
|
-
def failure_message_for_should_not
|
26
|
-
"Matcher does not support should_not"
|
27
|
-
end
|
28
|
-
|
29
|
-
def description
|
30
|
-
"contain exactly #{_pretty_print(expected)}"
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def safe_sort(array)
|
36
|
-
array.sort rescue array
|
37
|
-
end
|
38
|
-
|
39
|
-
def difference_between_arrays(array_1, array_2)
|
40
|
-
difference = array_1.to_ary.dup
|
41
|
-
array_2.to_ary.each do |element|
|
42
|
-
if index = difference.index(element)
|
43
|
-
difference.delete_at(index)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
difference
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
RSpec::Matchers.constants.each do |c|
|
2
|
-
if Class === (klass = RSpec::Matchers.const_get(c))
|
3
|
-
if klass.public_instance_methods.any? {|m| ['failure_message_for_should',:failure_message_for_should].include?(m)}
|
4
|
-
klass.class_exec do
|
5
|
-
alias_method :failure_message, :failure_message_for_should
|
6
|
-
end
|
7
|
-
end
|
8
|
-
if klass.public_instance_methods.any? {|m| ['failure_message_for_should_not',:failure_message_for_should_not].include?(m)}
|
9
|
-
klass.class_exec do
|
10
|
-
alias_method :negative_failure_message, :failure_message_for_should_not
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,301 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Matchers
|
5
|
-
module DSL
|
6
|
-
# Contains the methods that are available from within the
|
7
|
-
# `RSpec::Matchers.define` DSL for creating custom matchers.
|
8
|
-
module Macros
|
9
|
-
# Stores the block that is used to determine whether this matcher passes
|
10
|
-
# or fails. The block should return a boolean value. When the matcher is
|
11
|
-
# passed to `expect(...).to` and the block returns `true`, then the expectation
|
12
|
-
# passes. Similarly, when the matcher is passed to `expect(...).not_to` and the
|
13
|
-
# block returns `false`, then the expectation passes.
|
14
|
-
#
|
15
|
-
# Use `match_for_should` when used in conjunction with
|
16
|
-
# `match_for_should_not`.
|
17
|
-
#
|
18
|
-
# @example
|
19
|
-
#
|
20
|
-
# RSpec::Matchers.define :be_even do
|
21
|
-
# match do |actual|
|
22
|
-
# actual.even?
|
23
|
-
# end
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# expect(4).to be_even # passes
|
27
|
-
# expect(3).not_to be_even # passes
|
28
|
-
# expect(3).to be_even # fails
|
29
|
-
# expect(4).not_to be_even # fails
|
30
|
-
#
|
31
|
-
# @yield [Object] actual the actual value (i.e. the value wrapped by `expect`)
|
32
|
-
def match(&match_block)
|
33
|
-
define_user_override(:matches?, match_block) do |actual|
|
34
|
-
begin
|
35
|
-
@actual = actual
|
36
|
-
super(*actual_arg_for(match_block))
|
37
|
-
rescue RSpec::Expectations::ExpectationNotMetError
|
38
|
-
false
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
alias match_for_should match
|
43
|
-
|
44
|
-
# Use this to define the block for a negative expectation (`expect(...).not_to`)
|
45
|
-
# when the positive and negative forms require different handling. This
|
46
|
-
# is rarely necessary, but can be helpful, for example, when specifying
|
47
|
-
# asynchronous processes that require different timeouts.
|
48
|
-
#
|
49
|
-
# @yield [Object] actual the actual value (i.e. the value wrapped by `expect`)
|
50
|
-
def match_for_should_not(&match_block)
|
51
|
-
define_user_override(:does_not_match?, match_block) do |actual|
|
52
|
-
@actual = actual
|
53
|
-
super(*actual_arg_for(match_block))
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# Use this instead of `match` when the block will raise an exception
|
58
|
-
# rather than returning false to indicate a failure.
|
59
|
-
#
|
60
|
-
# @example
|
61
|
-
#
|
62
|
-
# RSpec::Matchers.define :accept_as_valid do |candidate_address|
|
63
|
-
# match_unless_raises ValidationException do |validator|
|
64
|
-
# validator.validate(candidate_address)
|
65
|
-
# end
|
66
|
-
# end
|
67
|
-
#
|
68
|
-
# expect(email_validator).to accept_as_valid("person@company.com")
|
69
|
-
#
|
70
|
-
# @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
|
71
|
-
def match_unless_raises(expected_exception=Exception, &match_block)
|
72
|
-
define_user_override(:matches?, match_block) do |actual|
|
73
|
-
@actual = actual
|
74
|
-
begin
|
75
|
-
super(*actual_arg_for(match_block))
|
76
|
-
rescue expected_exception => @rescued_exception
|
77
|
-
false
|
78
|
-
else
|
79
|
-
true
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
# Customizes the failure messsage to use when this matcher is
|
85
|
-
# asked to positively match. Only use this when the message
|
86
|
-
# generated by default doesn't suit your needs.
|
87
|
-
#
|
88
|
-
# @example
|
89
|
-
#
|
90
|
-
# RSpec::Matchers.define :have_strength do |expected|
|
91
|
-
# match { your_match_logic }
|
92
|
-
#
|
93
|
-
# failure_message_for_should do |actual|
|
94
|
-
# "Expected strength of #{expected}, but had #{actual.strength}"
|
95
|
-
# end
|
96
|
-
# end
|
97
|
-
#
|
98
|
-
# @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
|
99
|
-
def failure_message_for_should(&definition)
|
100
|
-
define_user_override(__method__, definition)
|
101
|
-
end
|
102
|
-
|
103
|
-
# Customize the failure messsage to use when this matcher is asked
|
104
|
-
# to negatively match. Only use this when the message generated by
|
105
|
-
# default doesn't suit your needs.
|
106
|
-
#
|
107
|
-
# @example
|
108
|
-
#
|
109
|
-
# RSpec::Matchers.define :have_strength do |expected|
|
110
|
-
# match { your_match_logic }
|
111
|
-
#
|
112
|
-
# failure_message_for_should_not do |actual|
|
113
|
-
# "Expected not to have strength of #{expected}, but did"
|
114
|
-
# end
|
115
|
-
# end
|
116
|
-
#
|
117
|
-
# @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
|
118
|
-
def failure_message_for_should_not(&definition)
|
119
|
-
define_user_override(__method__, definition)
|
120
|
-
end
|
121
|
-
|
122
|
-
# Customize the description to use for one-liners. Only use this when
|
123
|
-
# the description generated by default doesn't suit your needs.
|
124
|
-
#
|
125
|
-
# @example
|
126
|
-
#
|
127
|
-
# RSpec::Matchers.define :qualify_for do |expected|
|
128
|
-
# match { your_match_logic }
|
129
|
-
#
|
130
|
-
# description do
|
131
|
-
# "qualify for #{expected}"
|
132
|
-
# end
|
133
|
-
# end
|
134
|
-
#
|
135
|
-
# @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
|
136
|
-
def description(&definition)
|
137
|
-
define_user_override(__method__, definition)
|
138
|
-
end
|
139
|
-
|
140
|
-
# Tells the matcher to diff the actual and expected values in the failure
|
141
|
-
# message.
|
142
|
-
def diffable
|
143
|
-
define_method(:diffable?) { true }
|
144
|
-
end
|
145
|
-
|
146
|
-
# Convenience for defining methods on this matcher to create a fluent
|
147
|
-
# interface. The trick about fluent interfaces is that each method must
|
148
|
-
# return self in order to chain methods together. `chain` handles that
|
149
|
-
# for you.
|
150
|
-
#
|
151
|
-
# @example
|
152
|
-
#
|
153
|
-
# RSpec::Matchers.define :have_errors_on do |key|
|
154
|
-
# chain :with do |message|
|
155
|
-
# @message = message
|
156
|
-
# end
|
157
|
-
#
|
158
|
-
# match do |actual|
|
159
|
-
# actual.errors[key] == @message
|
160
|
-
# end
|
161
|
-
# end
|
162
|
-
#
|
163
|
-
# expect(minor).to have_errors_on(:age).with("Not old enough to participate")
|
164
|
-
def chain(name, &definition)
|
165
|
-
define_user_override(name, definition) do |*args, &block|
|
166
|
-
super(*args, &block)
|
167
|
-
self
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
private
|
172
|
-
|
173
|
-
# Does the following:
|
174
|
-
#
|
175
|
-
# - Defines the named method usign a user-provided block
|
176
|
-
# in @user_method_defs, which is included as an ancestor
|
177
|
-
# in the singleton class in which we eval the `define` block.
|
178
|
-
# - Defines an overriden definition for the same method
|
179
|
-
# usign the provided `our_def` block.
|
180
|
-
# - Provides a default `our_def` block for the common case
|
181
|
-
# of needing to call the user's definition with `@actual`
|
182
|
-
# as an arg, but only if their block's arity can handle it.
|
183
|
-
#
|
184
|
-
# This compiles the user block into an actual method, allowing
|
185
|
-
# them to use normal method constructs like `return`
|
186
|
-
# (e.g. for a early guard statement), while allowing us to define
|
187
|
-
# an override that can provide the wrapped handling
|
188
|
-
# (e.g. assigning `@actual`, rescueing errors, etc) and
|
189
|
-
# can `super` to the user's definition.
|
190
|
-
def define_user_override(method_name, user_def, &our_def)
|
191
|
-
@user_method_defs.__send__(:define_method, method_name, &user_def)
|
192
|
-
our_def ||= lambda { super(*actual_arg_for(user_def)) }
|
193
|
-
define_method(method_name, &our_def)
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
# Defines default implementations of the matcher
|
198
|
-
# protocol methods for custom matchers. You can
|
199
|
-
# override any of these using the {RSpec::Matchers::DSL::Macros Macros} methods
|
200
|
-
# from within an `RSpec::Matchers.define` block.
|
201
|
-
module DefaultImplementations
|
202
|
-
# @api private
|
203
|
-
# Used internally by objects returns by `should` and `should_not`.
|
204
|
-
def diffable?
|
205
|
-
false
|
206
|
-
end
|
207
|
-
|
208
|
-
# The default description.
|
209
|
-
def description
|
210
|
-
"#{name_to_sentence}#{expected_to_sentence}"
|
211
|
-
end
|
212
|
-
|
213
|
-
# The default failure message for positive expectations.
|
214
|
-
def failure_message_for_should
|
215
|
-
"expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"
|
216
|
-
end
|
217
|
-
|
218
|
-
# The default failure message for negative expectations.
|
219
|
-
def failure_message_for_should_not
|
220
|
-
"expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
# The class used for custom matchers. The block passed to
|
225
|
-
# `RSpec::Matchers.define` will be evaluated in the context
|
226
|
-
# of the singleton class of an instance, and will have the
|
227
|
-
# {RSpec::Matchers::DSL::Macros Macros} methods available.
|
228
|
-
class Matcher
|
229
|
-
# Provides default implementations for the matcher protocol methods.
|
230
|
-
include DefaultImplementations
|
231
|
-
|
232
|
-
# Allows expectation expressions to be used in the match block.
|
233
|
-
include RSpec::Matchers
|
234
|
-
|
235
|
-
# Converts matcher name and expected args to an English expresion.
|
236
|
-
include RSpec::Matchers::Pretty
|
237
|
-
|
238
|
-
# Makes the macro methods available to an `RSpec::Matchers.define` block.
|
239
|
-
extend Macros
|
240
|
-
|
241
|
-
attr_reader :expected, :actual, :rescued_exception
|
242
|
-
attr_accessor :matcher_execution_context
|
243
|
-
|
244
|
-
# @api private
|
245
|
-
def initialize(name, declarations, *expected)
|
246
|
-
@name = name
|
247
|
-
@actual = nil
|
248
|
-
@expected = expected
|
249
|
-
|
250
|
-
class << self
|
251
|
-
# See `Macros#define_user_override` above, for an explanation.
|
252
|
-
include(@user_method_defs = Module.new)
|
253
|
-
self
|
254
|
-
end.class_exec(*expected, &declarations)
|
255
|
-
end
|
256
|
-
|
257
|
-
# Adds the name (rather than a cryptic hex number)
|
258
|
-
# so we can identify an instance of
|
259
|
-
# the matcher in error messages (e.g. for `NoMethodError`)
|
260
|
-
def inspect
|
261
|
-
"#<#{self.class.name} #{name}>"
|
262
|
-
end
|
263
|
-
|
264
|
-
if RUBY_VERSION.to_f >= 1.9
|
265
|
-
# Indicates that this matcher responds to messages
|
266
|
-
# from the `matcher_execution_context` as well.
|
267
|
-
# Also, supports getting a method object for such methods.
|
268
|
-
def respond_to_missing?(method, include_private=false)
|
269
|
-
super || matcher_execution_context.respond_to?(method, include_private)
|
270
|
-
end
|
271
|
-
else # for 1.8.7
|
272
|
-
# Indicates that this matcher responds to messages
|
273
|
-
# from the `matcher_execution_context` as well.
|
274
|
-
def respond_to?(method, include_private=false)
|
275
|
-
super || matcher_execution_context.respond_to?(method, include_private)
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
private
|
280
|
-
|
281
|
-
def actual_arg_for(block)
|
282
|
-
block.arity.zero? ? [] : [@actual]
|
283
|
-
end
|
284
|
-
|
285
|
-
# Takes care of forwarding unhandled messages to the
|
286
|
-
# `matcher_execution_context` (typically the current
|
287
|
-
# running `RSpec::Core::Example`). This is needed by
|
288
|
-
# rspec-rails so that it can define matchers that wrap
|
289
|
-
# Rails' test helper methods, but it's also a useful
|
290
|
-
# feature in its own right.
|
291
|
-
def method_missing(method, *args, &block)
|
292
|
-
if matcher_execution_context.respond_to?(method)
|
293
|
-
matcher_execution_context.__send__ method, *args, &block
|
294
|
-
else
|
295
|
-
super(method, *args, &block)
|
296
|
-
end
|
297
|
-
end
|
298
|
-
end
|
299
|
-
end
|
300
|
-
end
|
301
|
-
end
|