Debian Codegraph @

rspec-expectations/spec/rspec/matchers/built_in/match_spec.rb

raw symbols SCIP
1 RSpec.describe "expect(...).to match(expected)" do
2 include RSpec::Support::Spec::DiffHelpers
3
4 it_behaves_like "an RSpec value matcher", :valid_value => 'ab', :invalid_value => 'bc' do
5 let(:matcher) { match(/a/) }
6 end
7
8 it "passes when target (String) matches expected (Regexp)" do
9 expect("string").to match(/tri/)
10 end
11
12 it "passes when target (Regexp) matches expected (String)" do
13 expect(/tri/).to match("string")
14 end
15
16 it "passes when target (Regexp) matches expected (Regexp)" do
17 expect(/tri/).to match(/tri/)
18 end
19
20 it "passes when target (String) matches expected (a matcher)" do
21 expect("string").to match(a_string_including("str"))
22 end
23
24 it "passes when target (Regexp) matches expected (a matcher)" do
25 expect(/foo/).to match(be_a Regexp)
26 end
27
28 it "passes when target (String) matches expected (String)" do
29 expect("string").to match("tri")
30 end
31
32 it "fails when target (String) does not match expected (Regexp)" do
33 expect {
34 expect("string").to match(/rings/)
35 }.to fail_with a_string_starting_with 'expected "string" to match /rings/'
36 end
37
38 it "fails when target (Regexp) does not match expected (String)" do
39 expect {
40 expect(/rings/).to match("string")
41 }.to fail_with a_string_starting_with 'expected /rings/ to match "string"'
42 end
43
44 it "fails when target (String) does not match expected (a matcher)" do
45 expect {
46 expect("string").to match(a_string_including("foo"))
47 }.to fail_with(a_string_starting_with 'expected "string" to match (a string including "foo")')
48 end
49
50 it "fails when target (Regexp) does not match expected (a matcher)" do
51 expect {
52 expect(/foo/).to match(be_a_kind_of String)
53 }.to fail_with(a_string_starting_with 'expected /foo/ to match (be a kind of String)')
54 end
55
56 it "fails when target (String) does not match expected (String)" do
57 expect {
58 expect("string").to match("rings")
59 }.to fail
60 end
61
62 it "provides message, expected and actual on failure" do
63 matcher = match(/rings/)
64 matcher.matches?("string")
65 expect(matcher.failure_message).to eq "expected \"string\" to match /rings/"
66 end
67
68 it "provides a diff on failure" do
69 allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false)
70
71 failure_message_that_includes_diff = %r|
72 \s*Diff:
73 \s*@@ #{Regexp.escape one_line_header} @@
74 \s*-/bar/
75 \s*\+"foo"|
76
77 expect { expect("foo").to match(/bar/) }.to fail_with(failure_message_that_includes_diff)
78 end
79
80 context "when passed a data structure with matchers" do
81 it 'passes when the matchers match' do
82 expect(["food", 1.1]).to match([a_string_matching(/foo/), a_value_within(0.2).of(1)])
83 end
84
85 it 'fails when the matchers do not match' do
86 expect {
87 expect(["fod", 1.1]).to match([a_string_matching(/foo/), a_value_within(0.2).of(1)])
88 }.to fail_with('expected ["fod", 1.1] to match [(a string matching /foo/), (a value within 0.2 of 1)]')
89 end
90
91 it 'provides a description' do
92 description = match([a_string_matching(/foo/), a_value_within(0.2).of(1)]).description
93 expect(description).to eq("match [(a string matching /foo/), (a value within 0.2 of 1)]")
94 end
95 end
96
97 it "fails when target type (String) does not match expected (Array)" do
98 if RUBY_VERSION == "1.8.7"
99 expect {
100 expect("string").to match(["c", "a", "b"])
101 }.to fail
102 else
103 expect {
104 expect("string").to match(["c", "a", "b"])
105 }.to fail_with('expected a collection that can be converted to an array with `#to_ary` or `#to_a`, but got "string"')
106 end
107 end
108 end
109
110 RSpec.describe "expect(...).not_to match(expected)" do
111 it "passes when target (String) matches does not match (Regexp)" do
112 expect("string").not_to match(/rings/)
113 end
114
115 it "passes when target (String) matches does not match (String)" do
116 expect("string").not_to match("rings")
117 end
118
119 it "fails when target (String) matches expected (Regexp)" do
120 expect {
121 expect("string").not_to match(/tri/)
122 }.to fail_with a_string_starting_with 'expected "string" not to match /tri/'
123 end
124
125 it "fails when target (String) matches expected (String)" do
126 expect {
127 expect("string").not_to match("tri")
128 }.to fail_with a_string_starting_with 'expected "string" not to match "tri"'
129 end
130
131 it "provides message, expected and actual on failure" do
132 matcher = match(/tri/)
133 matcher.matches?("string")
134 expect(matcher.failure_message_when_negated).to eq "expected \"string\" not to match /tri/"
135 end
136
137 context "when passed a data structure with matchers" do
138 it 'passes when the matchers match' do
139 expect(["food", 1.1]).not_to match([a_string_matching(/fod/), a_value_within(0.2).of(1)])
140 end
141
142 it 'fails when the matchers do not match' do
143 expect {
144 expect(["fod", 1.1]).not_to match([a_string_matching(/fod/), a_value_within(0.2).of(1)])
145 }.to fail_with('expected ["fod", 1.1] not to match [(a string matching /fod/), (a value within 0.2 of 1)]')
146 end
147 end
148
149 it "passes when target type (String) does not match expected (Array)" do
150 expect("string").not_to match(["c", "a", "b"])
151 end
152 end