| 1 |
|
| 2 |
|
| 3 |
RSpec.describe RE2::Set do |
| 4 |
describe "#initialize" do |
| 5 |
it "returns an instance given no args" do |
| 6 |
set = RE2::Set.new |
| 7 |
|
| 8 |
expect(set).to be_a(RE2::Set) |
| 9 |
end |
| 10 |
|
| 11 |
it "returns an instance given only an anchor of :unanchored" do |
| 12 |
set = RE2::Set.new(:unanchored) |
| 13 |
|
| 14 |
expect(set).to be_a(RE2::Set) |
| 15 |
end |
| 16 |
|
| 17 |
it "returns an instance given only an anchor of :anchor_start" do |
| 18 |
set = RE2::Set.new(:anchor_start) |
| 19 |
|
| 20 |
expect(set).to be_a(RE2::Set) |
| 21 |
end |
| 22 |
|
| 23 |
it "returns an instance given only an anchor of :anchor_both" do |
| 24 |
set = RE2::Set.new(:anchor_both) |
| 25 |
|
| 26 |
expect(set).to be_a(RE2::Set) |
| 27 |
end |
| 28 |
|
| 29 |
it "returns an instance given an anchor and options" do |
| 30 |
set = RE2::Set.new(:unanchored, case_sensitive: false) |
| 31 |
|
| 32 |
expect(set).to be_a(RE2::Set) |
| 33 |
end |
| 34 |
|
| 35 |
it "raises an error if given an inappropriate type" do |
| 36 |
expect { RE2::Set.new(0) }.to raise_error(TypeError) |
| 37 |
end |
| 38 |
|
| 39 |
it "raises an error if given an invalid anchor" do |
| 40 |
expect { RE2::Set.new(:not_a_valid_anchor) }.to raise_error( |
| 41 |
ArgumentError, |
| 42 |
"anchor should be one of: :unanchored, :anchor_start, :anchor_both" |
| 43 |
) |
| 44 |
end |
| 45 |
|
| 46 |
it "raises an error if given an invalid anchor and options" do |
| 47 |
expect { RE2::Set.new(:not_a_valid_anchor, case_sensitive: false) }.to raise_error( |
| 48 |
ArgumentError, |
| 49 |
"anchor should be one of: :unanchored, :anchor_start, :anchor_both" |
| 50 |
) |
| 51 |
end |
| 52 |
end |
| 53 |
|
| 54 |
describe "#dup" do |
| 55 |
it "raises a TypeError" do |
| 56 |
expect { described_class.new.dup }.to raise_error(TypeError, /cannot copy RE2::Set/) |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
describe "#clone" do |
| 61 |
it "raises a TypeError" do |
| 62 |
expect { described_class.new.clone }.to raise_error(TypeError, /cannot copy RE2::Set/) |
| 63 |
end |
| 64 |
end |
| 65 |
|
| 66 |
describe "#add" do |
| 67 |
it "allows multiple patterns to be added", :aggregate_failures do |
| 68 |
set = RE2::Set.new |
| 69 |
|
| 70 |
expect(set.add("abc")).to eq(0) |
| 71 |
expect(set.add("def")).to eq(1) |
| 72 |
expect(set.add("ghi")).to eq(2) |
| 73 |
end |
| 74 |
|
| 75 |
it "rejects invalid patterns when added" do |
| 76 |
set = RE2::Set.new(:unanchored, log_errors: false) |
| 77 |
|
| 78 |
expect { set.add("???") }.to raise_error(ArgumentError, /str rejected by RE2::Set->Add\(\)/) |
| 79 |
end |
| 80 |
|
| 81 |
it "includes the full error message" do |
| 82 |
set = RE2::Set.new(:unanchored, log_errors: false) |
| 83 |
|
| 84 |
expect { set.add("(?P<#{'o' * 200}") }.to raise_error(ArgumentError, "str rejected by RE2::Set->Add(): invalid named capture group: (?P<oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo") |
| 85 |
end |
| 86 |
|
| 87 |
it "raises a FrozenError if called after #compile" do |
| 88 |
set = RE2::Set.new(:unanchored, log_errors: false) |
| 89 |
set.add("abc") |
| 90 |
set.compile |
| 91 |
|
| 92 |
expect { set.add("def") }.to raise_error(FrozenError) |
| 93 |
end |
| 94 |
|
| 95 |
it "raises an error if given a pattern that can't be coerced to a String" do |
| 96 |
set = RE2::Set.new(:unanchored, log_errors: false) |
| 97 |
|
| 98 |
expect { set.add(0) }.to raise_error(TypeError) |
| 99 |
end |
| 100 |
|
| 101 |
it "accepts a pattern that can be coerced to a String" do |
| 102 |
set = RE2::Set.new |
| 103 |
|
| 104 |
expect(set.add(StringLike.new("abc"))).to eq(0) |
| 105 |
end |
| 106 |
|
| 107 |
it "raises an error when called on an uninitialized object" do |
| 108 |
expect { described_class.allocate.add("foo") }.to raise_error(TypeError, /uninitialized RE2::Set/) |
| 109 |
end |
| 110 |
end |
| 111 |
|
| 112 |
describe "#compile" do |
| 113 |
it "compiles the set without error" do |
| 114 |
set = RE2::Set.new |
| 115 |
set.add("abc") |
| 116 |
set.add("def") |
| 117 |
set.add("ghi") |
| 118 |
|
| 119 |
expect(set.compile).to be_truthy |
| 120 |
end |
| 121 |
|
| 122 |
it "freezes the set on successful compilation" do |
| 123 |
set = RE2::Set.new |
| 124 |
set.add("abc") |
| 125 |
set.compile |
| 126 |
|
| 127 |
expect(set).to be_frozen |
| 128 |
end |
| 129 |
|
| 130 |
it "is not frozen before compilation" do |
| 131 |
set = RE2::Set.new |
| 132 |
set.add("abc") |
| 133 |
|
| 134 |
expect(set).to_not be_frozen |
| 135 |
end |
| 136 |
|
| 137 |
it "cannot be re-initialized after compilation" do |
| 138 |
set = RE2::Set.new |
| 139 |
set.add("abc") |
| 140 |
set.compile |
| 141 |
|
| 142 |
expect { set.send(:initialize) }.to raise_error(FrozenError) |
| 143 |
end |
| 144 |
|
| 145 |
it "raises an error when called on an uninitialized object" do |
| 146 |
expect { described_class.allocate.compile }.to raise_error(TypeError, /uninitialized RE2::Set/) |
| 147 |
end |
| 148 |
end |
| 149 |
|
| 150 |
describe "#match" do |
| 151 |
it "matches against multiple patterns" do |
| 152 |
set = RE2::Set.new |
| 153 |
set.add("abc") |
| 154 |
set.add("def") |
| 155 |
set.add("ghi") |
| 156 |
set.compile |
| 157 |
|
| 158 |
expect(set.match("abcdefghi", exception: false)).to eq([0, 1, 2]) |
| 159 |
end |
| 160 |
|
| 161 |
it "returns an empty array if there is no match" do |
| 162 |
set = RE2::Set.new |
| 163 |
set.add("abc") |
| 164 |
set.compile |
| 165 |
|
| 166 |
expect(set.match("def", exception: false)).to be_empty |
| 167 |
end |
| 168 |
|
| 169 |
it "supports matching null bytes", :aggregate_failures do |
| 170 |
set = RE2::Set.new |
| 171 |
set.add("a\0b") |
| 172 |
set.compile |
| 173 |
|
| 174 |
expect(set.match("a\0b", exception: false)).to eq([0]) |
| 175 |
end |
| 176 |
|
| 177 |
it "returns an empty array if there is no match when :exception is true" do |
| 178 |
skip "Underlying RE2::Set::Match does not output error information" unless RE2::Set.match_raises_errors? |
| 179 |
|
| 180 |
set = RE2::Set.new |
| 181 |
set.add("abc") |
| 182 |
set.compile |
| 183 |
|
| 184 |
expect(set.match("def")).to be_empty |
| 185 |
end |
| 186 |
|
| 187 |
it "raises an error if called before #compile by default" do |
| 188 |
skip "Underlying RE2::Set::Match does not output error information" unless RE2::Set.match_raises_errors? |
| 189 |
|
| 190 |
set = RE2::Set.new(:unanchored, log_errors: false) |
| 191 |
|
| 192 |
silence_stderr do |
| 193 |
expect { set.match("") }.to raise_error(RE2::Set::MatchError) |
| 194 |
end |
| 195 |
end |
| 196 |
|
| 197 |
it "raises an error if called before #compile when :exception is true" do |
| 198 |
skip "Underlying RE2::Set::Match does not output error information" unless RE2::Set.match_raises_errors? |
| 199 |
|
| 200 |
set = RE2::Set.new(:unanchored, log_errors: false) |
| 201 |
|
| 202 |
silence_stderr do |
| 203 |
expect { set.match("", exception: true) }.to raise_error(RE2::Set::MatchError) |
| 204 |
end |
| 205 |
end |
| 206 |
|
| 207 |
it "returns an empty array if called before #compile when :exception is false" do |
| 208 |
set = RE2::Set.new(:unanchored, log_errors: false) |
| 209 |
|
| 210 |
silence_stderr do |
| 211 |
expect(set.match("", exception: false)).to be_empty |
| 212 |
end |
| 213 |
end |
| 214 |
|
| 215 |
it "raises an error if :exception is true and RE2 does not support it" do |
| 216 |
skip "Underlying RE2::Set::Match outputs error information" if RE2::Set.match_raises_errors? |
| 217 |
|
| 218 |
set = RE2::Set.new(:unanchored, log_errors: false) |
| 219 |
|
| 220 |
silence_stderr do |
| 221 |
expect { set.match("", exception: true) }.to raise_error(RE2::Set::UnsupportedError) |
| 222 |
end |
| 223 |
end |
| 224 |
|
| 225 |
it "raises an error if given non-hash options" do |
| 226 |
set = RE2::Set.new |
| 227 |
|
| 228 |
expect { set.match("", 0) }.to raise_error(TypeError) |
| 229 |
end |
| 230 |
|
| 231 |
it "raises a Type Error if given input that can't be coerced to a String" do |
| 232 |
set = RE2::Set.new |
| 233 |
set.add("abc") |
| 234 |
set.compile |
| 235 |
|
| 236 |
expect { set.match(0, exception: false) }.to raise_error(TypeError) |
| 237 |
end |
| 238 |
|
| 239 |
it "accepts input if it can be coerced to a String" do |
| 240 |
set = RE2::Set.new |
| 241 |
set.add("abc") |
| 242 |
set.compile |
| 243 |
|
| 244 |
expect(set.match(StringLike.new("abcdef"), exception: false)).to contain_exactly(0) |
| 245 |
end |
| 246 |
|
| 247 |
it "raises an error when called on an uninitialized object" do |
| 248 |
expect { described_class.allocate.match("foo") }.to raise_error(TypeError, /uninitialized RE2::Set/) |
| 249 |
end |
| 250 |
|
| 251 |
it "can be run concurrently" do |
| 252 |
set = RE2::Set.new |
| 253 |
set.add("abc") |
| 254 |
set.add("def") |
| 255 |
set.add("ghi") |
| 256 |
set.compile |
| 257 |
|
| 258 |
threads = 10.times.map do |
| 259 |
Thread.new { set.match("abcdefghi", exception: false) } |
| 260 |
end |
| 261 |
|
| 262 |
expect(threads.map(&:value)).to all(eq([0, 1, 2])) |
| 263 |
end |
| 264 |
end |
| 265 |
|
| 266 |
describe "#size" do |
| 267 |
it "returns the number of patterns added to the set", :aggregate_failures do |
| 268 |
skip "Underlying RE2::Set has no Size method" unless RE2::Set.size? |
| 269 |
|
| 270 |
set = RE2::Set.new |
| 271 |
|
| 272 |
expect(set.size).to eq(0) |
| 273 |
|
| 274 |
set.add("abc") |
| 275 |
|
| 276 |
expect(set.size).to eq(1) |
| 277 |
|
| 278 |
set.add("def") |
| 279 |
|
| 280 |
expect(set.size).to eq(2) |
| 281 |
end |
| 282 |
|
| 283 |
it "raises an error if RE2 does not support Set::Size" do |
| 284 |
skip "Underlying RE2::Set has a Size method" if RE2::Set.size? |
| 285 |
|
| 286 |
set = RE2::Set.new |
| 287 |
|
| 288 |
expect { set.size }.to raise_error(RE2::Set::UnsupportedError) |
| 289 |
end |
| 290 |
|
| 291 |
it "raises an error when called on an uninitialized object" do |
| 292 |
skip "Underlying RE2::Set has no Size method" unless RE2::Set.size? |
| 293 |
|
| 294 |
expect { described_class.allocate.size }.to raise_error(TypeError, /uninitialized RE2::Set/) |
| 295 |
end |
| 296 |
end |
| 297 |
|
| 298 |
describe "#length" do |
| 299 |
it "is an alias for size" do |
| 300 |
skip "Underlying RE2::Set has no Size method" unless RE2::Set.size? |
| 301 |
|
| 302 |
set = RE2::Set.new |
| 303 |
|
| 304 |
expect(set.length).to eq(0) |
| 305 |
|
| 306 |
set.add("abc") |
| 307 |
|
| 308 |
expect(set.length).to eq(1) |
| 309 |
|
| 310 |
set.add("def") |
| 311 |
|
| 312 |
expect(set.length).to eq(2) |
| 313 |
end |
| 314 |
|
| 315 |
it "raises an error when called on an uninitialized object" do |
| 316 |
skip "Underlying RE2::Set has no Size method" unless RE2::Set.size? |
| 317 |
|
| 318 |
expect { described_class.allocate.length }.to raise_error(TypeError, /uninitialized RE2::Set/) |
| 319 |
end |
| 320 |
end |
| 321 |
|
| 322 |
def silence_stderr |
| 323 |
original_stream = STDERR |
| 324 |
|
| 325 |
if File.const_defined?(:NULL) |
| 326 |
STDERR.reopen(File::NULL) |
| 327 |
else |
| 328 |
platform = RUBY_PLATFORM == 'java' ? RbConfig::CONFIG['host_os'] : RUBY_PLATFORM |
| 329 |
|
| 330 |
case platform |
| 331 |
when /mswin|mingw/i |
| 332 |
STDERR.reopen('NUL') |
| 333 |
when /amiga/i |
| 334 |
STDERR.reopen('NIL') |
| 335 |
when /openvms/i |
| 336 |
STDERR.reopen('NL:') |
| 337 |
else |
| 338 |
STDERR.reopen('/dev/null') |
| 339 |
end |
| 340 |
end |
| 341 |
|
| 342 |
yield |
| 343 |
ensure |
| 344 |
STDERR.reopen(original_stream) |
| 345 |
end |
| 346 |
end |