From 6ba13eb33c5fcd36eee8888346a33592d7b665e0 Mon Sep 17 00:00:00 2001 From: Paul Mucur Date: Sun, 24 Sep 2023 12:51:12 +0100 Subject: [PATCH] Add test coverage for more edge cases --- spec/kernel_spec.rb | 16 ++++++++++++++++ spec/re2/regexp_spec.rb | 22 +++++++++++++++++++--- spec/re2_spec.rb | 10 ++++++---- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/spec/kernel_spec.rb b/spec/kernel_spec.rb index 3ae0246..206a756 100644 --- a/spec/kernel_spec.rb +++ b/spec/kernel_spec.rb @@ -9,5 +9,21 @@ expect(re).not_to be_case_sensitive end + + it "raises an error if given an inappropriate type" do + expect { RE2(nil) }.to raise_error(TypeError) + end + + it "allows invalid patterns to be created" do + re = RE2('???', :log_errors => false) + + expect(re).to be_a(RE2::Regexp) + end + + it "supports passing something that can be coerced to a String as input" do + re = RE2(StringLike.new('w(o)(o)')) + + expect(re).to be_a(RE2::Regexp) + end end end diff --git a/spec/re2/regexp_spec.rb b/spec/re2/regexp_spec.rb index 56dc952..8f226f2 100644 --- a/spec/re2/regexp_spec.rb +++ b/spec/re2/regexp_spec.rb @@ -18,9 +18,15 @@ re = RE2::Regexp.new('???', :log_errors => false) expect(re).to be_a(RE2::Regexp) end + + it "supports passing something that can be coerced to a String as input" do + re = RE2::Regexp.new(StringLike.new('w(o)(o)')) + + expect(re).to be_a(RE2::Regexp) + end end - describe "#compile" do + describe ".compile" do it "returns an instance given only a pattern" do re = RE2::Regexp.compile('woo') expect(re).to be_a(RE2::Regexp) @@ -31,10 +37,20 @@ expect(re).to be_a(RE2::Regexp) end + it "raises an error if given an inappropriate type" do + expect { RE2::Regexp.compile(nil) }.to raise_error(TypeError) + end + it "allows invalid patterns to be created" do re = RE2::Regexp.compile('???', :log_errors => false) expect(re).to be_a(RE2::Regexp) end + + it "supports passing something that can be coerced to a String as input" do + re = RE2::Regexp.compile(StringLike.new('w(o)(o)')) + + expect(re).to be_a(RE2::Regexp) + end end describe "#options" do @@ -59,7 +75,7 @@ it "is populated with overridden options when specified" do options = RE2::Regexp.new('woo', :case_sensitive => false).options - expect(options[:case_sensitive]).to eq(false) + expect(options).to include(:case_sensitive => false) end end @@ -82,7 +98,7 @@ expect(error_arg).to be_nil end - it "returns the offending portin of the regexp if there is an error" do + it "returns the offending portion of the regexp if there is an error" do error_arg = RE2::Regexp.new('wo(o', :log_errors => false).error_arg expect(error_arg).to eq("wo(o") end diff --git a/spec/re2_spec.rb b/spec/re2_spec.rb index 90b6305..e1be015 100644 --- a/spec/re2_spec.rb +++ b/spec/re2_spec.rb @@ -12,11 +12,12 @@ expect(RE2.Replace("Good morning", "(?i)gOOD MORNING", "hi")).to eq("hi") end - it "does not perform replacements in-place" do + it "does not perform replacements in-place", :aggregate_failures do name = "Robert" replacement = RE2.Replace(name, "R", "Cr") - expect(name).not_to equal(replacement) + expect(name).to eq("Robert") + expect(replacement).to eq("Crobert") end it "supports passing an RE2::Regexp as the pattern" do @@ -89,11 +90,12 @@ expect(RE2.GlobalReplace("Robert", "(?i)r", "w")).to eq("wobewt") end - it "does not perform replacement in-place" do + it "does not perform replacement in-place", :aggregate_failures do name = "Robert" replacement = RE2.GlobalReplace(name, "(?i)R", "w") - expect(name).not_to equal(replacement) + expect(name).to eq("Robert") + expect(replacement).to eq("wobewt") end it "supports passing an RE2::Regexp as the pattern" do