Debian Codegraph @

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

raw symbols SCIP
1 RSpec.describe "expect(regex).to match(string).with_captures" do
2 context "with a string target" do
3 it "does match a regex with a missing capture" do
4 expect("a123a").to match(/(a)(b)?/).with_captures("a", nil)
5 end
6
7 it "does not match a regex with an incorrect match" do
8 expect("a123a").not_to match(/(a)/).with_captures("b")
9 end
10
11 it "matches a regex without named captures" do
12 expect("a123a").to match(/(a)/).with_captures("a")
13 end
14
15 it "uses the match description if the regex doesn't match" do
16 expect {
17 expect(/(a)/).to match("123").with_captures
18 }.to fail_with(/expected \/\(a\)\/ to match "123"/)
19 end
20
21 if RUBY_VERSION != "1.8.7"
22 it "matches a regex with named captures" do
23 expect("a123a").to match(Regexp.new("(?<num>123)")).with_captures(:num => "123")
24 end
25
26 it "matches a regex with a nested matcher" do
27 expect("a123a").to match(Regexp.new("(?<num>123)(asdf)?")).with_captures(a_hash_including(:num => "123"))
28 end
29
30 it "does not match a regex with an incorrect named group match" do
31 expect("a123a").not_to match(Regexp.new("(?<name>a)")).with_captures(:name => "b")
32 end
33
34 it "has a sensible failure description with a hash including matcher" do
35 expect {
36 expect("a123a").not_to match(Regexp.new("(?<num>123)(asdf)?")).with_captures(a_hash_including(:num => "123"))
37 }.to fail_with(/#{hash_inspect_for_regex({ :num => "123" })}/)
38 end
39
40 it "matches named captures when not passing a hash" do
41 expect("a123a").to match(Regexp.new("(?<num>123)")).with_captures("123")
42 end
43 end
44 end
45
46 context "with a regex target" do
47 it "does match a regex with a missing capture" do
48 expect(/(a)(b)?/).to match("a123a").with_captures("a", nil)
49 end
50
51 it "does not match a regex with an incorrect match" do
52 expect(/(a)/).not_to match("a123a").with_captures("b")
53 end
54
55 it "matches a regex without named captures" do
56 expect(/(a)/).to match("a123a").with_captures("a")
57 end
58
59 it "uses the match description if the regex doesn't match" do
60 expect {
61 expect(/(a)/).to match("123").with_captures
62 }.to fail_with(/expected \/\(a\)\/ to match "123"/)
63 end
64
65 if RUBY_VERSION != "1.8.7"
66 it "matches a regex with named captures" do
67 expect(Regexp.new("(?<num>123)")).to match("a123a").with_captures(:num => "123")
68 end
69
70 it "matches a regex with a nested matcher" do
71 expect(Regexp.new("(?<num>123)(asdf)?")).to match("a123a").with_captures(a_hash_including(:num => "123"))
72 end
73
74 it "does not match a regex with an incorrect named group match" do
75 expect(Regexp.new("(?<name>a)")).not_to match("a123a").with_captures(:name => "b")
76 end
77
78 it "has a sensible failure description with a hash including matcher" do
79 expect {
80 expect(Regexp.new("(?<num>123)(asdf)?")).not_to match("a123a").with_captures(a_hash_including(:num => "123"))
81 }.to fail_with(/#{hash_inspect_for_regex({ :num => "123" })}/)
82 end
83
84 it "matches named captures when not passing a hash" do
85 expect(Regexp.new("(?<num>123)")).to match("a123a").with_captures("123")
86 end
87 end
88 end
89 end