# frozen_string_literal: true RSpec.describe RE2::Set do describe "#initialize" do it "returns an instance given no args" do set = RE2::Set.new expect(set).to be_a(RE2::Set) end it "returns an instance given only an anchor of :unanchored" do set = RE2::Set.new(:unanchored) expect(set).to be_a(RE2::Set) end it "returns an instance given only an anchor of :anchor_start" do set = RE2::Set.new(:anchor_start) expect(set).to be_a(RE2::Set) end it "returns an instance given only an anchor of :anchor_both" do set = RE2::Set.new(:anchor_both) expect(set).to be_a(RE2::Set) end it "returns an instance given an anchor and options" do set = RE2::Set.new(:unanchored, case_sensitive: false) expect(set).to be_a(RE2::Set) end it "raises an error if given an inappropriate type" do expect { RE2::Set.new(0) }.to raise_error(TypeError) end it "raises an error if given an invalid anchor" do expect { RE2::Set.new(:not_a_valid_anchor) }.to raise_error( ArgumentError, "anchor should be one of: :unanchored, :anchor_start, :anchor_both" ) end it "raises an error if given an invalid anchor and options" do expect { RE2::Set.new(:not_a_valid_anchor, case_sensitive: false) }.to raise_error( ArgumentError, "anchor should be one of: :unanchored, :anchor_start, :anchor_both" ) end end describe "#dup" do it "raises a TypeError" do expect { described_class.new.dup }.to raise_error(TypeError, /cannot copy RE2::Set/) end end describe "#clone" do it "raises a TypeError" do expect { described_class.new.clone }.to raise_error(TypeError, /cannot copy RE2::Set/) end end describe "#add" do it "allows multiple patterns to be added", :aggregate_failures do set = RE2::Set.new expect(set.add("abc")).to eq(0) expect(set.add("def")).to eq(1) expect(set.add("ghi")).to eq(2) end it "rejects invalid patterns when added" do set = RE2::Set.new(:unanchored, log_errors: false) expect { set.add("???") }.to raise_error(ArgumentError, /str rejected by RE2::Set->Add\(\)/) end it "includes the full error message" do set = RE2::Set.new(:unanchored, log_errors: false) expect { set.add("(?P<#{'o' * 200}") }.to raise_error(ArgumentError, "str rejected by RE2::Set->Add(): invalid named capture group: (?P