File: captures_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (89 lines) | stat: -rw-r--r-- 3,221 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
RSpec.describe "expect(regex).to match(string).with_captures" do
  context "with a string target" do
    it "does match a regex with a missing capture" do
      expect("a123a").to match(/(a)(b)?/).with_captures("a", nil)
    end

    it "does not match a regex with an incorrect match" do
      expect("a123a").not_to match(/(a)/).with_captures("b")
    end

    it "matches a regex without named captures" do
      expect("a123a").to match(/(a)/).with_captures("a")
    end

    it "uses the match description if the regex doesn't match" do
      expect {
        expect(/(a)/).to match("123").with_captures
      }.to fail_with(/expected \/\(a\)\/ to match "123"/)
    end

    if RUBY_VERSION != "1.8.7"
      it "matches a regex with named captures" do
        expect("a123a").to match(Regexp.new("(?<num>123)")).with_captures(:num => "123")
      end

      it "matches a regex with a nested matcher" do
        expect("a123a").to match(Regexp.new("(?<num>123)(asdf)?")).with_captures(a_hash_including(:num => "123"))
      end

      it "does not match a regex with an incorrect named group match" do
        expect("a123a").not_to match(Regexp.new("(?<name>a)")).with_captures(:name => "b")
      end

      it "has a sensible failure description with a hash including matcher" do
        expect {
          expect("a123a").not_to match(Regexp.new("(?<num>123)(asdf)?")).with_captures(a_hash_including(:num => "123"))
        }.to fail_with(/num => "123"/)
      end

      it "matches named captures when not passing a hash" do
        expect("a123a").to match(Regexp.new("(?<num>123)")).with_captures("123")
      end
    end
  end

  context "with a regex target" do
    it "does match a regex with a missing capture" do
      expect(/(a)(b)?/).to match("a123a").with_captures("a", nil)
    end

    it "does not match a regex with an incorrect match" do
      expect(/(a)/).not_to match("a123a").with_captures("b")
    end

    it "matches a regex without named captures" do
      expect(/(a)/).to match("a123a").with_captures("a")
    end

    it "uses the match description if the regex doesn't match" do
      expect {
        expect(/(a)/).to match("123").with_captures
      }.to fail_with(/expected \/\(a\)\/ to match "123"/)
    end

    if RUBY_VERSION != "1.8.7"
      it "matches a regex with named captures" do
        expect(Regexp.new("(?<num>123)")).to match("a123a").with_captures(:num => "123")
      end

      it "matches a regex with a nested matcher" do
        expect(Regexp.new("(?<num>123)(asdf)?")).to match("a123a").with_captures(a_hash_including(:num => "123"))
      end

      it "does not match a regex with an incorrect named group match" do
        expect(Regexp.new("(?<name>a)")).not_to match("a123a").with_captures(:name => "b")
      end

      it "has a sensible failure description with a hash including matcher" do
        expect {
          expect(Regexp.new("(?<num>123)(asdf)?")).not_to match("a123a").with_captures(a_hash_including(:num => "123"))
        }.to fail_with(/num => "123"/)
      end

      it "matches named captures when not passing a hash" do
        expect(Regexp.new("(?<num>123)")).to match("a123a").with_captures("123")
      end
    end
  end
end