File: string_spec.rb

package info (click to toggle)
ruby-re2 2.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,872 kB
  • sloc: ruby: 1,902; cpp: 1,165; makefile: 7
file content (60 lines) | stat: -rw-r--r-- 1,710 bytes parent folder | download
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
require "re2/string"

class String
  include RE2::String
end

RSpec.describe RE2::String do
  describe "#re2_sub" do
    it "delegates to RE2.Replace to perform replacement" do
      expect("My name is Robert Paulson".re2_sub('Robert', 'Crobert')).to eq("My name is Crobert Paulson")
    end

    it "doesn't perform an in-place replacement" do
      string = "My name is Robert Paulson"

      expect(string.re2_sub('Robert', 'Crobert')).not_to equal(string)
    end
  end

  describe "#re2_gsub" do
    it "delegates to RE2.GlobalReplace to perform replacement" do
      expect("My name is Robert Paulson".re2_gsub('a', 'e')).to eq("My neme is Robert Peulson")
    end

    it "doesn't perform an in-place replacement" do
      string = "My name is Robert Paulson"

      expect(string.re2_gsub('a', 'e')).not_to equal(string)
    end
  end

  describe "#re2_match" do
    it "delegates to RE2::Regexp#match to perform matches", :aggregate_failures do
      md = "My name is Robert Paulson".re2_match('My name is (\S+) (\S+)')

      expect(md).to be_a(RE2::MatchData)
      expect(md[0]).to eq("My name is Robert Paulson")
      expect(md[1]).to eq("Robert")
      expect(md[2]).to eq("Paulson")
    end

    it "supports limiting the number of matches" do
      md = "My name is Robert Paulson".re2_match('My name is (\S+) (\S+)', 0)

      expect(md).to eq(true)
    end
  end

  describe "#re2_escape" do
    it "escapes the string for use in regular expressions" do
      expect("1.5-2.0?".re2_escape).to eq('1\.5\-2\.0\?')
    end
  end

  describe "#re2_quote" do
    it "escapes the string for use in regular expressions" do
      expect("1.5-2.0?".re2_quote).to eq('1\.5\-2\.0\?')
    end
  end
end