| 1 |
|
| 2 |
|
| 3 |
require "re2/string" |
| 4 |
|
| 5 |
class String |
| 6 |
include RE2::String |
| 7 |
end |
| 8 |
|
| 9 |
RSpec.describe RE2::String do |
| 10 |
describe "#re2_sub" do |
| 11 |
it "delegates to RE2.Replace to perform replacement" do |
| 12 |
expect("My name is Robert Paulson".re2_sub('Robert', 'Crobert')).to eq("My name is Crobert Paulson") |
| 13 |
end |
| 14 |
|
| 15 |
it "doesn't perform an in-place replacement" do |
| 16 |
string = "My name is Robert Paulson" |
| 17 |
|
| 18 |
expect(string.re2_sub('Robert', 'Crobert')).not_to equal(string) |
| 19 |
end |
| 20 |
end |
| 21 |
|
| 22 |
describe "#re2_gsub" do |
| 23 |
it "delegates to RE2.GlobalReplace to perform replacement" do |
| 24 |
expect("My name is Robert Paulson".re2_gsub('a', 'e')).to eq("My neme is Robert Peulson") |
| 25 |
end |
| 26 |
|
| 27 |
it "doesn't perform an in-place replacement" do |
| 28 |
string = "My name is Robert Paulson" |
| 29 |
|
| 30 |
expect(string.re2_gsub('a', 'e')).not_to equal(string) |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
describe "#re2_match" do |
| 35 |
it "delegates to RE2::Regexp#match to perform matches", :aggregate_failures do |
| 36 |
md = "My name is Robert Paulson".re2_match('My name is (\S+) (\S+)') |
| 37 |
|
| 38 |
expect(md).to be_a(RE2::MatchData) |
| 39 |
expect(md[0]).to eq("My name is Robert Paulson") |
| 40 |
expect(md[1]).to eq("Robert") |
| 41 |
expect(md[2]).to eq("Paulson") |
| 42 |
end |
| 43 |
|
| 44 |
it "supports limiting the number of matches" do |
| 45 |
md = "My name is Robert Paulson".re2_match('My name is (\S+) (\S+)', 0) |
| 46 |
|
| 47 |
expect(md).to eq(true) |
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
describe "#re2_escape" do |
| 52 |
it "escapes the string for use in regular expressions" do |
| 53 |
expect("1.5-2.0?".re2_escape).to eq('1\.5\-2\.0\?') |
| 54 |
end |
| 55 |
end |
| 56 |
|
| 57 |
describe "#re2_quote" do |
| 58 |
it "escapes the string for use in regular expressions" do |
| 59 |
expect("1.5-2.0?".re2_quote).to eq('1\.5\-2\.0\?') |
| 60 |
end |
| 61 |
end |
| 62 |
end |