rspec-expectations 3.8.6
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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +5 -0
- data/.document +5 -0
- data/.yardopts +6 -0
- data/Changelog.md +1156 -0
- data/LICENSE.md +25 -0
- data/README.md +305 -0
- data/lib/rspec/expectations.rb +82 -0
- data/lib/rspec/expectations/block_snippet_extractor.rb +253 -0
- data/lib/rspec/expectations/configuration.rb +215 -0
- data/lib/rspec/expectations/expectation_target.rb +127 -0
- data/lib/rspec/expectations/fail_with.rb +39 -0
- data/lib/rspec/expectations/failure_aggregator.rb +194 -0
- data/lib/rspec/expectations/handler.rb +170 -0
- data/lib/rspec/expectations/minitest_integration.rb +58 -0
- data/lib/rspec/expectations/syntax.rb +132 -0
- data/lib/rspec/expectations/version.rb +8 -0
- data/lib/rspec/matchers.rb +1034 -0
- data/lib/rspec/matchers/aliased_matcher.rb +116 -0
- data/lib/rspec/matchers/built_in.rb +52 -0
- data/lib/rspec/matchers/built_in/all.rb +86 -0
- data/lib/rspec/matchers/built_in/base_matcher.rb +193 -0
- data/lib/rspec/matchers/built_in/be.rb +288 -0
- data/lib/rspec/matchers/built_in/be_between.rb +77 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +26 -0
- data/lib/rspec/matchers/built_in/be_kind_of.rb +20 -0
- data/lib/rspec/matchers/built_in/be_within.rb +72 -0
- data/lib/rspec/matchers/built_in/change.rb +428 -0
- data/lib/rspec/matchers/built_in/compound.rb +271 -0
- data/lib/rspec/matchers/built_in/contain_exactly.rb +302 -0
- data/lib/rspec/matchers/built_in/cover.rb +24 -0
- data/lib/rspec/matchers/built_in/eq.rb +40 -0
- data/lib/rspec/matchers/built_in/eql.rb +34 -0
- data/lib/rspec/matchers/built_in/equal.rb +81 -0
- data/lib/rspec/matchers/built_in/exist.rb +90 -0
- data/lib/rspec/matchers/built_in/has.rb +103 -0
- data/lib/rspec/matchers/built_in/have_attributes.rb +114 -0
- data/lib/rspec/matchers/built_in/include.rb +149 -0
- data/lib/rspec/matchers/built_in/match.rb +106 -0
- data/lib/rspec/matchers/built_in/operators.rb +128 -0
- data/lib/rspec/matchers/built_in/output.rb +200 -0
- data/lib/rspec/matchers/built_in/raise_error.rb +230 -0
- data/lib/rspec/matchers/built_in/respond_to.rb +165 -0
- data/lib/rspec/matchers/built_in/satisfy.rb +60 -0
- data/lib/rspec/matchers/built_in/start_or_end_with.rb +94 -0
- data/lib/rspec/matchers/built_in/throw_symbol.rb +132 -0
- data/lib/rspec/matchers/built_in/yield.rb +432 -0
- data/lib/rspec/matchers/composable.rb +171 -0
- data/lib/rspec/matchers/dsl.rb +527 -0
- data/lib/rspec/matchers/english_phrasing.rb +58 -0
- data/lib/rspec/matchers/expecteds_for_multiple_diffs.rb +73 -0
- data/lib/rspec/matchers/fail_matchers.rb +42 -0
- data/lib/rspec/matchers/generated_descriptions.rb +41 -0
- data/lib/rspec/matchers/matcher_delegator.rb +35 -0
- data/lib/rspec/matchers/matcher_protocol.rb +99 -0
- metadata +215 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,90 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
# @api private
|
5
|
+
# Provides the implementation for `exist`.
|
6
|
+
# Not intended to be instantiated directly.
|
7
|
+
class Exist < BaseMatcher
|
8
|
+
def initialize(*expected)
|
9
|
+
@expected = expected
|
10
|
+
end
|
11
|
+
|
12
|
+
# @api private
|
13
|
+
# @return [Boolean]
|
14
|
+
def matches?(actual)
|
15
|
+
@actual = actual
|
16
|
+
@test = ExistenceTest.new @actual, @expected
|
17
|
+
@test.valid_test? && @test.actual_exists?
|
18
|
+
end
|
19
|
+
|
20
|
+
# @api private
|
21
|
+
# @return [Boolean]
|
22
|
+
def does_not_match?(actual)
|
23
|
+
@actual = actual
|
24
|
+
@test = ExistenceTest.new @actual, @expected
|
25
|
+
@test.valid_test? && !@test.actual_exists?
|
26
|
+
end
|
27
|
+
|
28
|
+
# @api private
|
29
|
+
# @return [String]
|
30
|
+
def failure_message
|
31
|
+
"expected #{actual_formatted} to exist#{@test.validity_message}"
|
32
|
+
end
|
33
|
+
|
34
|
+
# @api private
|
35
|
+
# @return [String]
|
36
|
+
def failure_message_when_negated
|
37
|
+
"expected #{actual_formatted} not to exist#{@test.validity_message}"
|
38
|
+
end
|
39
|
+
|
40
|
+
# @api private
|
41
|
+
# Simple class for memoizing actual/expected for this matcher
|
42
|
+
# and examining the match
|
43
|
+
class ExistenceTest < Struct.new(:actual, :expected)
|
44
|
+
# @api private
|
45
|
+
# @return [Boolean]
|
46
|
+
def valid_test?
|
47
|
+
uniq_truthy_values.size == 1
|
48
|
+
end
|
49
|
+
|
50
|
+
# @api private
|
51
|
+
# @return [Boolean]
|
52
|
+
def actual_exists?
|
53
|
+
existence_values.first
|
54
|
+
end
|
55
|
+
|
56
|
+
# @api private
|
57
|
+
# @return [String]
|
58
|
+
def validity_message
|
59
|
+
case uniq_truthy_values.size
|
60
|
+
when 0
|
61
|
+
" but it does not respond to either `exist?` or `exists?`"
|
62
|
+
when 2
|
63
|
+
" but `exist?` and `exists?` returned different values:\n\n"\
|
64
|
+
" exist?: #{existence_values.first}\n"\
|
65
|
+
"exists?: #{existence_values.last}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def uniq_truthy_values
|
72
|
+
@uniq_truthy_values ||= existence_values.map { |v| !!v }.uniq
|
73
|
+
end
|
74
|
+
|
75
|
+
def existence_values
|
76
|
+
@existence_values ||= predicates.map { |p| actual.__send__(p, *expected) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def predicates
|
80
|
+
@predicates ||= [:exist?, :exists?].select { |p| actual.respond_to?(p) && !deprecated(p, actual) }
|
81
|
+
end
|
82
|
+
|
83
|
+
def deprecated(predicate, actual)
|
84
|
+
predicate == :exists? && File == actual
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
# @api private
|
5
|
+
# Provides the implementation for `has_<predicate>`.
|
6
|
+
# Not intended to be instantiated directly.
|
7
|
+
class Has < BaseMatcher
|
8
|
+
def initialize(method_name, *args, &block)
|
9
|
+
@method_name, @args, @block = method_name, args, block
|
10
|
+
end
|
11
|
+
|
12
|
+
# @private
|
13
|
+
def matches?(actual, &block)
|
14
|
+
@actual = actual
|
15
|
+
@block ||= block
|
16
|
+
predicate_accessible? && predicate_matches?
|
17
|
+
end
|
18
|
+
|
19
|
+
# @private
|
20
|
+
def does_not_match?(actual, &block)
|
21
|
+
@actual = actual
|
22
|
+
@block ||= block
|
23
|
+
predicate_accessible? && !predicate_matches?
|
24
|
+
end
|
25
|
+
|
26
|
+
# @api private
|
27
|
+
# @return [String]
|
28
|
+
def failure_message
|
29
|
+
validity_message || "expected ##{predicate}#{failure_message_args_description} to return true, got false"
|
30
|
+
end
|
31
|
+
|
32
|
+
# @api private
|
33
|
+
# @return [String]
|
34
|
+
def failure_message_when_negated
|
35
|
+
validity_message || "expected ##{predicate}#{failure_message_args_description} to return false, got true"
|
36
|
+
end
|
37
|
+
|
38
|
+
# @api private
|
39
|
+
# @return [String]
|
40
|
+
def description
|
41
|
+
[method_description, args_description].compact.join(' ')
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def predicate_accessible?
|
47
|
+
!private_predicate? && predicate_exists?
|
48
|
+
end
|
49
|
+
|
50
|
+
# support 1.8.7, evaluate once at load time for performance
|
51
|
+
if String === methods.first
|
52
|
+
# :nocov:
|
53
|
+
def private_predicate?
|
54
|
+
@actual.private_methods.include? predicate.to_s
|
55
|
+
end
|
56
|
+
# :nocov:
|
57
|
+
else
|
58
|
+
def private_predicate?
|
59
|
+
@actual.private_methods.include? predicate
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def predicate_exists?
|
64
|
+
@actual.respond_to? predicate
|
65
|
+
end
|
66
|
+
|
67
|
+
def predicate_matches?
|
68
|
+
@actual.__send__(predicate, *@args, &@block)
|
69
|
+
end
|
70
|
+
|
71
|
+
def predicate
|
72
|
+
# On 1.9, there appears to be a bug where String#match can return `false`
|
73
|
+
# rather than the match data object. Changing to Regex#match appears to
|
74
|
+
# work around this bug. For an example of this bug, see:
|
75
|
+
# https://travis-ci.org/rspec/rspec-expectations/jobs/27549635
|
76
|
+
@predicate ||= :"has_#{Matchers::HAS_REGEX.match(@method_name.to_s).captures.first}?"
|
77
|
+
end
|
78
|
+
|
79
|
+
def method_description
|
80
|
+
@method_name.to_s.tr('_', ' ')
|
81
|
+
end
|
82
|
+
|
83
|
+
def args_description
|
84
|
+
return nil if @args.empty?
|
85
|
+
@args.map { |arg| RSpec::Support::ObjectFormatter.format(arg) }.join(', ')
|
86
|
+
end
|
87
|
+
|
88
|
+
def failure_message_args_description
|
89
|
+
desc = args_description
|
90
|
+
"(#{desc})" if desc
|
91
|
+
end
|
92
|
+
|
93
|
+
def validity_message
|
94
|
+
if private_predicate?
|
95
|
+
"expected #{@actual} to respond to `#{predicate}` but `#{predicate}` is a private method"
|
96
|
+
elsif !predicate_exists?
|
97
|
+
"expected #{@actual} to respond to `#{predicate}`"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
# @api private
|
5
|
+
# Provides the implementation for `have_attributes`.
|
6
|
+
# Not intended to be instantiated directly.
|
7
|
+
class HaveAttributes < BaseMatcher
|
8
|
+
# @private
|
9
|
+
attr_reader :respond_to_failed
|
10
|
+
|
11
|
+
def initialize(expected)
|
12
|
+
@expected = expected
|
13
|
+
@values = {}
|
14
|
+
@respond_to_failed = false
|
15
|
+
@negated = false
|
16
|
+
end
|
17
|
+
|
18
|
+
# @private
|
19
|
+
def actual
|
20
|
+
@values
|
21
|
+
end
|
22
|
+
|
23
|
+
# @api private
|
24
|
+
# @return [Boolean]
|
25
|
+
def matches?(actual)
|
26
|
+
@actual = actual
|
27
|
+
@negated = false
|
28
|
+
return false unless respond_to_attributes?
|
29
|
+
perform_match(:all?)
|
30
|
+
end
|
31
|
+
|
32
|
+
# @api private
|
33
|
+
# @return [Boolean]
|
34
|
+
def does_not_match?(actual)
|
35
|
+
@actual = actual
|
36
|
+
@negated = true
|
37
|
+
return false unless respond_to_attributes?
|
38
|
+
perform_match(:none?)
|
39
|
+
end
|
40
|
+
|
41
|
+
# @api private
|
42
|
+
# @return [String]
|
43
|
+
def description
|
44
|
+
described_items = surface_descriptions_in(expected)
|
45
|
+
improve_hash_formatting "have attributes #{RSpec::Support::ObjectFormatter.format(described_items)}"
|
46
|
+
end
|
47
|
+
|
48
|
+
# @api private
|
49
|
+
# @return [Boolean]
|
50
|
+
def diffable?
|
51
|
+
!@respond_to_failed && !@negated
|
52
|
+
end
|
53
|
+
|
54
|
+
# @api private
|
55
|
+
# @return [String]
|
56
|
+
def failure_message
|
57
|
+
respond_to_failure_message_or do
|
58
|
+
"expected #{actual_formatted} to #{description} but had attributes #{ formatted_values }"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# @api private
|
63
|
+
# @return [String]
|
64
|
+
def failure_message_when_negated
|
65
|
+
respond_to_failure_message_or { "expected #{actual_formatted} not to #{description}" }
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def cache_all_values
|
71
|
+
@values = {}
|
72
|
+
expected.each do |attribute_key, _attribute_value|
|
73
|
+
actual_value = @actual.__send__(attribute_key)
|
74
|
+
@values[attribute_key] = actual_value
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def perform_match(predicate)
|
79
|
+
cache_all_values
|
80
|
+
expected.__send__(predicate) do |attribute_key, attribute_value|
|
81
|
+
actual_has_attribute?(attribute_key, attribute_value)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def actual_has_attribute?(attribute_key, attribute_value)
|
86
|
+
values_match?(attribute_value, @values.fetch(attribute_key))
|
87
|
+
end
|
88
|
+
|
89
|
+
def respond_to_attributes?
|
90
|
+
matches = respond_to_matcher.matches?(@actual)
|
91
|
+
@respond_to_failed = !matches
|
92
|
+
matches
|
93
|
+
end
|
94
|
+
|
95
|
+
def respond_to_matcher
|
96
|
+
@respond_to_matcher ||= RespondTo.new(*expected.keys).with(0).arguments
|
97
|
+
end
|
98
|
+
|
99
|
+
def respond_to_failure_message_or
|
100
|
+
if respond_to_failed
|
101
|
+
respond_to_matcher.failure_message
|
102
|
+
else
|
103
|
+
improve_hash_formatting(yield)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def formatted_values
|
108
|
+
values = RSpec::Support::ObjectFormatter.format(@values)
|
109
|
+
improve_hash_formatting(values)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
# @api private
|
5
|
+
# Provides the implementation for `include`.
|
6
|
+
# Not intended to be instantiated directly.
|
7
|
+
class Include < BaseMatcher
|
8
|
+
# @private
|
9
|
+
attr_reader :expecteds
|
10
|
+
|
11
|
+
def initialize(*expecteds)
|
12
|
+
@expecteds = expecteds
|
13
|
+
end
|
14
|
+
|
15
|
+
# @api private
|
16
|
+
# @return [Boolean]
|
17
|
+
def matches?(actual)
|
18
|
+
actual = actual.to_hash if convert_to_hash?(actual)
|
19
|
+
perform_match(actual) { |v| v }
|
20
|
+
end
|
21
|
+
|
22
|
+
# @api private
|
23
|
+
# @return [Boolean]
|
24
|
+
def does_not_match?(actual)
|
25
|
+
actual = actual.to_hash if convert_to_hash?(actual)
|
26
|
+
perform_match(actual) { |v| !v }
|
27
|
+
end
|
28
|
+
|
29
|
+
# @api private
|
30
|
+
# @return [String]
|
31
|
+
def description
|
32
|
+
improve_hash_formatting("include#{readable_list_of(expecteds)}")
|
33
|
+
end
|
34
|
+
|
35
|
+
# @api private
|
36
|
+
# @return [String]
|
37
|
+
def failure_message
|
38
|
+
format_failure_message("to") { super }
|
39
|
+
end
|
40
|
+
|
41
|
+
# @api private
|
42
|
+
# @return [String]
|
43
|
+
def failure_message_when_negated
|
44
|
+
format_failure_message("not to") { super }
|
45
|
+
end
|
46
|
+
|
47
|
+
# @api private
|
48
|
+
# @return [Boolean]
|
49
|
+
def diffable?
|
50
|
+
!diff_would_wrongly_highlight_matched_item?
|
51
|
+
end
|
52
|
+
|
53
|
+
# @api private
|
54
|
+
# @return [Array, Hash]
|
55
|
+
def expected
|
56
|
+
if expecteds.one? && Hash === expecteds.first
|
57
|
+
expecteds.first
|
58
|
+
else
|
59
|
+
expecteds
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def format_failure_message(preposition)
|
66
|
+
if actual.respond_to?(:include?)
|
67
|
+
improve_hash_formatting("expected #{description_of @actual} #{preposition} include#{readable_list_of @divergent_items}")
|
68
|
+
else
|
69
|
+
improve_hash_formatting(yield) + ", but it does not respond to `include?`"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def readable_list_of(items)
|
74
|
+
described_items = surface_descriptions_in(items)
|
75
|
+
if described_items.all? { |item| item.is_a?(Hash) }
|
76
|
+
" #{described_items.inject(:merge).inspect}"
|
77
|
+
else
|
78
|
+
EnglishPhrasing.list(described_items)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def perform_match(actual, &block)
|
83
|
+
@actual = actual
|
84
|
+
@divergent_items = excluded_from_actual(&block)
|
85
|
+
actual.respond_to?(:include?) && @divergent_items.empty?
|
86
|
+
end
|
87
|
+
|
88
|
+
def excluded_from_actual
|
89
|
+
return [] unless @actual.respond_to?(:include?)
|
90
|
+
|
91
|
+
expecteds.inject([]) do |memo, expected_item|
|
92
|
+
if comparing_hash_to_a_subset?(expected_item)
|
93
|
+
expected_item.each do |(key, value)|
|
94
|
+
memo << { key => value } unless yield actual_hash_includes?(key, value)
|
95
|
+
end
|
96
|
+
elsif comparing_hash_keys?(expected_item)
|
97
|
+
memo << expected_item unless yield actual_hash_has_key?(expected_item)
|
98
|
+
else
|
99
|
+
memo << expected_item unless yield actual_collection_includes?(expected_item)
|
100
|
+
end
|
101
|
+
memo
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def comparing_hash_to_a_subset?(expected_item)
|
106
|
+
actual.is_a?(Hash) && expected_item.is_a?(Hash)
|
107
|
+
end
|
108
|
+
|
109
|
+
def actual_hash_includes?(expected_key, expected_value)
|
110
|
+
actual_value = actual.fetch(expected_key) { return false }
|
111
|
+
values_match?(expected_value, actual_value)
|
112
|
+
end
|
113
|
+
|
114
|
+
def comparing_hash_keys?(expected_item)
|
115
|
+
actual.is_a?(Hash) && !expected_item.is_a?(Hash)
|
116
|
+
end
|
117
|
+
|
118
|
+
def actual_hash_has_key?(expected_key)
|
119
|
+
# We check `key?` first for perf:
|
120
|
+
# `key?` is O(1), but `any?` is O(N).
|
121
|
+
actual.key?(expected_key) ||
|
122
|
+
actual.keys.any? { |key| values_match?(expected_key, key) }
|
123
|
+
end
|
124
|
+
|
125
|
+
def actual_collection_includes?(expected_item)
|
126
|
+
return true if actual.include?(expected_item)
|
127
|
+
|
128
|
+
# String lacks an `any?` method...
|
129
|
+
return false unless actual.respond_to?(:any?)
|
130
|
+
|
131
|
+
actual.any? { |value| values_match?(expected_item, value) }
|
132
|
+
end
|
133
|
+
|
134
|
+
def diff_would_wrongly_highlight_matched_item?
|
135
|
+
return false unless actual.is_a?(String) && expected.is_a?(Array)
|
136
|
+
|
137
|
+
lines = actual.split("\n")
|
138
|
+
expected.any? do |str|
|
139
|
+
actual.include?(str) && lines.none? { |line| line == str }
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def convert_to_hash?(obj)
|
144
|
+
!obj.respond_to?(:include?) && obj.respond_to?(:to_hash)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|