| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require 'objspace' |
| 4 |
|
| 5 |
RSpec.describe RE2::MatchData do |
| 6 |
it "reports a larger consuming memory size when it has more matches" do |
| 7 |
matches1 = RE2::Regexp.new('w(o)').match('woo') |
| 8 |
matches2 = RE2::Regexp.new('w(o)(o)').match('woo') |
| 9 |
|
| 10 |
expect(ObjectSpace.memsize_of(matches1)).to be < ObjectSpace.memsize_of(matches2) |
| 11 |
end |
| 12 |
|
| 13 |
describe "#dup" do |
| 14 |
it "returns a copy of the match data" do |
| 15 |
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456") |
| 16 |
copy = md.dup |
| 17 |
|
| 18 |
expect(copy.to_a).to eq(["123 456", "123", "456"]) |
| 19 |
end |
| 20 |
|
| 21 |
it "returns a different object" do |
| 22 |
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456") |
| 23 |
copy = md.dup |
| 24 |
|
| 25 |
expect(copy).to_not equal(md) |
| 26 |
end |
| 27 |
|
| 28 |
it "preserves the original string" do |
| 29 |
str = "bob 123" |
| 30 |
md = RE2::Regexp.new('(\d+)').match(str) |
| 31 |
copy = md.dup |
| 32 |
|
| 33 |
expect(copy.string).to equal(str) |
| 34 |
end |
| 35 |
|
| 36 |
it "preserves the original regexp" do |
| 37 |
re = RE2::Regexp.new('(\d+)') |
| 38 |
md = re.match("123") |
| 39 |
copy = md.dup |
| 40 |
|
| 41 |
expect(copy.regexp).to equal(re) |
| 42 |
end |
| 43 |
|
| 44 |
it "raises an error when called on an uninitialized object" do |
| 45 |
expect { described_class.allocate.dup }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 46 |
end |
| 47 |
end |
| 48 |
|
| 49 |
describe "#clone" do |
| 50 |
it "returns a copy of the match data" do |
| 51 |
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456") |
| 52 |
copy = md.clone |
| 53 |
|
| 54 |
expect(copy.to_a).to eq(["123 456", "123", "456"]) |
| 55 |
end |
| 56 |
|
| 57 |
it "raises an error when called on an uninitialized object" do |
| 58 |
expect { described_class.allocate.clone }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
describe "#to_a" do |
| 63 |
it "is populated with the match and capturing groups" do |
| 64 |
a = RE2::Regexp.new('w(o)(o)').match('woo').to_a |
| 65 |
|
| 66 |
expect(a).to eq(["woo", "o", "o"]) |
| 67 |
end |
| 68 |
|
| 69 |
it "populates optional capturing groups with empty strings if they match zero characters" do |
| 70 |
a = RE2::Regexp.new('(\d?)(a)(b)').match('ab').to_a |
| 71 |
|
| 72 |
expect(a).to eq(["ab", "", "a", "b"]) |
| 73 |
end |
| 74 |
|
| 75 |
it "distinguishes between zero-length matches and unmatched groups" do |
| 76 |
a = RE2::Regexp.new('()(a)?').match('b').to_a |
| 77 |
|
| 78 |
expect(a).to eq(["", "", nil]) |
| 79 |
end |
| 80 |
|
| 81 |
it "returns UTF-8 strings if the pattern is UTF-8" do |
| 82 |
a = RE2::Regexp.new('w(o)(o)').match('woo').to_a |
| 83 |
|
| 84 |
expect(a.map(&:encoding)).to all eq(Encoding::UTF_8) |
| 85 |
end |
| 86 |
|
| 87 |
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do |
| 88 |
a = RE2::Regexp.new('w(o)(o)', utf8: false).match('woo').to_a |
| 89 |
|
| 90 |
expect(a.map(&:encoding)).to all eq(Encoding::ISO_8859_1) |
| 91 |
end |
| 92 |
|
| 93 |
it "raises an error when called on an uninitialized object" do |
| 94 |
expect { described_class.allocate.to_a }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 95 |
end |
| 96 |
end |
| 97 |
|
| 98 |
describe "#[]" do |
| 99 |
it "accesses capturing groups by numerical index", :aggregate_failures do |
| 100 |
md = RE2::Regexp.new('(\d)(\d{2})').match("123") |
| 101 |
|
| 102 |
expect(md[1]).to eq("1") |
| 103 |
expect(md[2]).to eq("23") |
| 104 |
end |
| 105 |
|
| 106 |
it "returns a UTF-8 string by numerical index if the pattern is UTF-8" do |
| 107 |
md = RE2::Regexp.new('(\d)(\d{2})').match("123") |
| 108 |
|
| 109 |
expect(md[1].encoding).to eq(Encoding::UTF_8) |
| 110 |
end |
| 111 |
|
| 112 |
it "returns a ISO-8859-1 string by numerical index if the pattern is not UTF-8" do |
| 113 |
md = RE2::Regexp.new('(\d)(\d{2})', utf8: false).match("123") |
| 114 |
|
| 115 |
expect(md[1].encoding).to eq(Encoding::ISO_8859_1) |
| 116 |
end |
| 117 |
|
| 118 |
it "has the whole match as the 0th item" do |
| 119 |
md = RE2::Regexp.new('(\d)(\d{2})').match("123") |
| 120 |
|
| 121 |
expect(md[0]).to eq("123") |
| 122 |
end |
| 123 |
|
| 124 |
it "supports access by numerical ranges", :aggregate_failures do |
| 125 |
md = RE2::Regexp.new('(\d+) (\d+) (\d+)').match("123 456 789") |
| 126 |
|
| 127 |
expect(md[1..3]).to eq(["123", "456", "789"]) |
| 128 |
expect(md[1...3]).to eq(["123", "456"]) |
| 129 |
end |
| 130 |
|
| 131 |
it "supports slicing", :aggregate_failures do |
| 132 |
md = RE2::Regexp.new('(\d+) (\d+) (\d+)').match("123 456 789") |
| 133 |
|
| 134 |
expect(md[1, 3]).to eq(["123", "456", "789"]) |
| 135 |
expect(md[1, 2]).to eq(["123", "456"]) |
| 136 |
end |
| 137 |
|
| 138 |
it "returns nil if attempting to access non-existent capturing groups by index", :aggregate_failures do |
| 139 |
md = RE2::Regexp.new('(\d+)').match('bob 123') |
| 140 |
|
| 141 |
expect(md[2]).to be_nil |
| 142 |
expect(md[3]).to be_nil |
| 143 |
end |
| 144 |
|
| 145 |
it "allows access by string names when there are named groups" do |
| 146 |
md = RE2::Regexp.new('(?P<numbers>\d+)').match('bob 123') |
| 147 |
|
| 148 |
expect(md["numbers"]).to eq("123") |
| 149 |
end |
| 150 |
|
| 151 |
it "allows access by symbol names when there are named groups" do |
| 152 |
md = RE2::Regexp.new('(?P<numbers>\d+)').match('bob 123') |
| 153 |
|
| 154 |
expect(md[:numbers]).to eq("123") |
| 155 |
end |
| 156 |
|
| 157 |
it "allows access by names and indices with mixed groups", :aggregate_failures do |
| 158 |
md = RE2::Regexp.new('(?P<name>\w+)(\s*)(?P<numbers>\d+)').match("bob 123") |
| 159 |
|
| 160 |
expect(md["name"]).to eq("bob") |
| 161 |
expect(md[:name]).to eq("bob") |
| 162 |
expect(md[2]).to eq(" ") |
| 163 |
expect(md["numbers"]).to eq("123") |
| 164 |
expect(md[:numbers]).to eq("123") |
| 165 |
end |
| 166 |
|
| 167 |
it "returns an empty string for a zero-length capturing group" do |
| 168 |
md = RE2::Regexp.new('()').match("bob") |
| 169 |
|
| 170 |
expect(md[1]).to eq("") |
| 171 |
end |
| 172 |
|
| 173 |
it "returns nil if no such named group exists", :aggregate_failures do |
| 174 |
md = RE2::Regexp.new('(\d+)').match("bob 123") |
| 175 |
|
| 176 |
expect(md["missing"]).to be_nil |
| 177 |
expect(md[:missing]).to be_nil |
| 178 |
end |
| 179 |
|
| 180 |
it "raises an error if given an inappropriate index" do |
| 181 |
md = RE2::Regexp.new('(\d+)').match("bob 123") |
| 182 |
|
| 183 |
expect { md[nil] }.to raise_error(TypeError) |
| 184 |
end |
| 185 |
|
| 186 |
it "returns UTF-8 encoded strings by default", :aggregate_failures do |
| 187 |
md = RE2::Regexp.new('(?P<name>\S+)').match("bob") |
| 188 |
|
| 189 |
expect(md[0].encoding.name).to eq("UTF-8") |
| 190 |
expect(md["name"].encoding.name).to eq("UTF-8") |
| 191 |
expect(md[:name].encoding.name).to eq("UTF-8") |
| 192 |
end |
| 193 |
|
| 194 |
it "returns Latin 1 strings encoding when utf-8 is false", :aggregate_failures do |
| 195 |
md = RE2::Regexp.new('(?P<name>\S+)', utf8: false).match('bob') |
| 196 |
|
| 197 |
expect(md[0].encoding.name).to eq("ISO-8859-1") |
| 198 |
expect(md["name"].encoding.name).to eq("ISO-8859-1") |
| 199 |
expect(md[:name].encoding.name).to eq("ISO-8859-1") |
| 200 |
end |
| 201 |
|
| 202 |
it "supports GC compaction" do |
| 203 |
md = RE2::Regexp.new('(wo{2})').match('woohoo' * 5) |
| 204 |
GC.compact |
| 205 |
|
| 206 |
expect(md[1]).to eq("woo") |
| 207 |
end |
| 208 |
|
| 209 |
it "raises an error when called on an uninitialized object" do |
| 210 |
expect { described_class.allocate[0] }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 211 |
end |
| 212 |
end |
| 213 |
|
| 214 |
describe "#string" do |
| 215 |
it "returns the original string to match against" do |
| 216 |
re = RE2::Regexp.new('(\D+)').match("bob") |
| 217 |
|
| 218 |
expect(re.string).to eq("bob") |
| 219 |
end |
| 220 |
|
| 221 |
it "returns a copy, not the actual original" do |
| 222 |
string = +"bob" |
| 223 |
re = RE2::Regexp.new('(\D+)').match(string) |
| 224 |
|
| 225 |
expect(re.string).to_not equal(string) |
| 226 |
end |
| 227 |
|
| 228 |
it "returns a frozen string" do |
| 229 |
re = RE2::Regexp.new('(\D+)').match("bob") |
| 230 |
|
| 231 |
expect(re.string).to be_frozen |
| 232 |
end |
| 233 |
|
| 234 |
it "does not copy the string if it was already frozen" do |
| 235 |
string = "bob" |
| 236 |
re = RE2::Regexp.new('(\D+)').match(string) |
| 237 |
|
| 238 |
expect(re.string).to equal(string) |
| 239 |
end |
| 240 |
|
| 241 |
it "raises an error when called on an uninitialized object" do |
| 242 |
expect { described_class.allocate.string }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 243 |
end |
| 244 |
end |
| 245 |
|
| 246 |
describe "#size" do |
| 247 |
it "returns the number of capturing groups plus the matching string" do |
| 248 |
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56") |
| 249 |
|
| 250 |
expect(md.size).to eq(3) |
| 251 |
end |
| 252 |
|
| 253 |
it "raises an error when called on an uninitialized object" do |
| 254 |
expect { described_class.allocate.size }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 255 |
end |
| 256 |
end |
| 257 |
|
| 258 |
describe "#length" do |
| 259 |
it "returns the number of capturing groups plus the matching string" do |
| 260 |
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56") |
| 261 |
|
| 262 |
expect(md.length).to eq(3) |
| 263 |
end |
| 264 |
|
| 265 |
it "raises an error when called on an uninitialized object" do |
| 266 |
expect { described_class.allocate.length }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 267 |
end |
| 268 |
end |
| 269 |
|
| 270 |
describe "#regexp" do |
| 271 |
it "returns the original RE2::Regexp used" do |
| 272 |
re = RE2::Regexp.new('(\d+)') |
| 273 |
md = re.match("123") |
| 274 |
|
| 275 |
expect(md.regexp).to equal(re) |
| 276 |
end |
| 277 |
|
| 278 |
it "raises an error when called on an uninitialized object" do |
| 279 |
expect { described_class.allocate.regexp }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 280 |
end |
| 281 |
end |
| 282 |
|
| 283 |
describe "#inspect" do |
| 284 |
it "returns a text representation of the object and indices" do |
| 285 |
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56") |
| 286 |
|
| 287 |
expect(md.inspect).to eq('#<RE2::MatchData "1234 56" 1:"1234" 2:"56">') |
| 288 |
end |
| 289 |
|
| 290 |
it "represents missing matches as nil" do |
| 291 |
md = RE2::Regexp.new('(\d+) (\d+)?').match("1234 ") |
| 292 |
|
| 293 |
expect(md.inspect).to eq('#<RE2::MatchData "1234 " 1:"1234" 2:nil>') |
| 294 |
end |
| 295 |
|
| 296 |
it "represents zero-length capturing groups as empty strings" do |
| 297 |
md = RE2::Regexp.new('()').match("bob") |
| 298 |
|
| 299 |
expect(md.inspect).to eq('#<RE2::MatchData "" 1:"">') |
| 300 |
end |
| 301 |
|
| 302 |
it "supports matches with null bytes" do |
| 303 |
md = RE2::Regexp.new("(\\w\0\\w) (\\w\0\\w)").match("a\0b c\0d") |
| 304 |
|
| 305 |
expect(md.inspect).to eq("#<RE2::MatchData \"a\0b c\0d\" 1:\"a\0b\" 2:\"c\0d\">") |
| 306 |
end |
| 307 |
|
| 308 |
it "raises an error when called on an uninitialized object" do |
| 309 |
expect { described_class.allocate.inspect }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 310 |
end |
| 311 |
end |
| 312 |
|
| 313 |
describe "#to_s" do |
| 314 |
it "returns the matching part of the original string" do |
| 315 |
md = RE2::Regexp.new('(\d{2,5})').match("one two 23456") |
| 316 |
|
| 317 |
expect(md.to_s).to eq("23456") |
| 318 |
end |
| 319 |
|
| 320 |
it "returns an empty string for a zero-length match" do |
| 321 |
md = RE2::Regexp.new('()').match("bob") |
| 322 |
|
| 323 |
expect(md.to_s).to eq("") |
| 324 |
end |
| 325 |
|
| 326 |
it "raises an error when called on an uninitialized object" do |
| 327 |
expect { described_class.allocate.to_s }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 328 |
end |
| 329 |
end |
| 330 |
|
| 331 |
describe "#to_ary" do |
| 332 |
it "allows the object to be expanded with an asterisk", :aggregate_failures do |
| 333 |
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56") |
| 334 |
m1, m2, m3 = *md |
| 335 |
|
| 336 |
expect(m1).to eq("1234 56") |
| 337 |
expect(m2).to eq("1234") |
| 338 |
expect(m3).to eq("56") |
| 339 |
end |
| 340 |
end |
| 341 |
|
| 342 |
describe "#begin" do |
| 343 |
it "returns the offset of the start of a match by index" do |
| 344 |
md = RE2::Regexp.new('(wo{2})').match('a woohoo') |
| 345 |
|
| 346 |
expect(md.string[md.begin(0)..-1]).to eq('woohoo') |
| 347 |
end |
| 348 |
|
| 349 |
it "returns the offset of the start of a match by string name" do |
| 350 |
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar') |
| 351 |
|
| 352 |
expect(md.string[md.begin('foo')..-1]).to eq('foobar') |
| 353 |
end |
| 354 |
|
| 355 |
it "returns the offset of the start of a match by symbol name" do |
| 356 |
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar') |
| 357 |
|
| 358 |
expect(md.string[md.begin(:foo)..-1]).to eq('foobar') |
| 359 |
end |
| 360 |
|
| 361 |
it "returns the offset of the start of a match by something that can be coerced to a String" do |
| 362 |
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar') |
| 363 |
|
| 364 |
expect(md.string[md.begin(StringLike.new("foo"))..-1]).to eq('foobar') |
| 365 |
end |
| 366 |
|
| 367 |
it "returns the offset despite multibyte characters" do |
| 368 |
md = RE2::Regexp.new('(Ruby)').match('I ♥ Ruby') |
| 369 |
|
| 370 |
expect(md.string[md.begin(0)..-1]).to eq('Ruby') |
| 371 |
end |
| 372 |
|
| 373 |
it "returns the offset for a zero-length capturing group" do |
| 374 |
md = RE2::Regexp.new('()').match("bob") |
| 375 |
|
| 376 |
expect(md.begin(1)).to eq(0) |
| 377 |
end |
| 378 |
|
| 379 |
it "returns nil for non-existent numerical matches" do |
| 380 |
md = RE2::Regexp.new('(\d)').match('123') |
| 381 |
|
| 382 |
expect(md.begin(10)).to be_nil |
| 383 |
end |
| 384 |
|
| 385 |
it "returns nil for negative numerical matches" do |
| 386 |
md = RE2::Regexp.new('(\d)').match('123') |
| 387 |
|
| 388 |
expect(md.begin(-4)).to be_nil |
| 389 |
end |
| 390 |
|
| 391 |
it "returns nil for non-existent named matches" do |
| 392 |
md = RE2::Regexp.new('(\d)').match('123') |
| 393 |
|
| 394 |
expect(md.begin('foo')).to be_nil |
| 395 |
end |
| 396 |
|
| 397 |
it "returns nil for non-existent symbol named matches" do |
| 398 |
md = RE2::Regexp.new('(\d)').match('123') |
| 399 |
|
| 400 |
expect(md.begin(:foo)).to be_nil |
| 401 |
end |
| 402 |
|
| 403 |
it "raises a type error if given an invalid name or number" do |
| 404 |
md = RE2::Regexp.new('(\d)').match('123') |
| 405 |
|
| 406 |
expect { md.begin(nil) }.to raise_error(TypeError) |
| 407 |
end |
| 408 |
|
| 409 |
it "supports GC compaction" do |
| 410 |
md = RE2::Regexp.new('(wo{2})').match('woohoo' * 5) |
| 411 |
GC.compact |
| 412 |
|
| 413 |
expect(md.string[md.begin(0)..-1]).to eq('woohoo' * 5) |
| 414 |
end |
| 415 |
|
| 416 |
it "raises an error when called on an uninitialized object" do |
| 417 |
expect { described_class.allocate.begin(0) }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 418 |
end |
| 419 |
end |
| 420 |
|
| 421 |
describe "#end" do |
| 422 |
it "returns the offset of the character following the end of a match" do |
| 423 |
md = RE2::Regexp.new('(wo{2})').match('a woohoo') |
| 424 |
|
| 425 |
expect(md.string[0...md.end(0)]).to eq('a woo') |
| 426 |
end |
| 427 |
|
| 428 |
it "returns the offset of a match by string name" do |
| 429 |
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar') |
| 430 |
|
| 431 |
expect(md.string[0...md.end('foo')]).to eq('a foo') |
| 432 |
end |
| 433 |
|
| 434 |
it "returns the offset of a match by symbol name" do |
| 435 |
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar') |
| 436 |
|
| 437 |
expect(md.string[0...md.end(:foo)]).to eq('a foo') |
| 438 |
end |
| 439 |
|
| 440 |
it "returns the offset of a match by something that can be coerced to a String" do |
| 441 |
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar') |
| 442 |
|
| 443 |
expect(md.string[0...md.end(StringLike.new("foo"))]).to eq('a foo') |
| 444 |
end |
| 445 |
|
| 446 |
it "returns the offset despite multibyte characters" do |
| 447 |
md = RE2::Regexp.new('(Ruby)').match('I ♥ Ruby') |
| 448 |
|
| 449 |
expect(md.string[0...md.end(0)]).to eq('I ♥ Ruby') |
| 450 |
end |
| 451 |
|
| 452 |
it "returns the offset for a zero-length capturing group" do |
| 453 |
md = RE2::Regexp.new('()').match("bob") |
| 454 |
|
| 455 |
expect(md.end(1)).to eq(0) |
| 456 |
end |
| 457 |
|
| 458 |
it "returns nil for non-existent numerical matches" do |
| 459 |
md = RE2::Regexp.new('(\d)').match('123') |
| 460 |
|
| 461 |
expect(md.end(10)).to be_nil |
| 462 |
end |
| 463 |
|
| 464 |
it "returns nil for negative numerical matches" do |
| 465 |
md = RE2::Regexp.new('(\d)').match('123') |
| 466 |
|
| 467 |
expect(md.end(-4)).to be_nil |
| 468 |
end |
| 469 |
|
| 470 |
it "returns nil for non-existent named matches" do |
| 471 |
md = RE2::Regexp.new('(\d)').match('123') |
| 472 |
|
| 473 |
expect(md.end('foo')).to be_nil |
| 474 |
end |
| 475 |
|
| 476 |
it "returns nil for non-existent symbol named matches" do |
| 477 |
md = RE2::Regexp.new('(\d)').match('123') |
| 478 |
|
| 479 |
expect(md.end(:foo)).to be_nil |
| 480 |
end |
| 481 |
|
| 482 |
it "raises a type error if given an invalid name or number" do |
| 483 |
md = RE2::Regexp.new('(\d)').match('123') |
| 484 |
|
| 485 |
expect { md.end(nil) }.to raise_error(TypeError) |
| 486 |
end |
| 487 |
|
| 488 |
it "supports GC compaction" do |
| 489 |
md = RE2::Regexp.new('(wo{2})').match('woohoo' * 5) |
| 490 |
GC.compact |
| 491 |
|
| 492 |
expect(md.string[0...md.end(0)]).to eq('woo') |
| 493 |
end |
| 494 |
|
| 495 |
it "raises an error when called on an uninitialized object" do |
| 496 |
expect { described_class.allocate.end(0) }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 497 |
end |
| 498 |
end |
| 499 |
|
| 500 |
describe "#pre_match" do |
| 501 |
it "returns the portion of the string before the match" do |
| 502 |
md = RE2::Regexp.new('(\d+)').match("bob 123 456") |
| 503 |
|
| 504 |
expect(md.pre_match).to eq("bob ") |
| 505 |
end |
| 506 |
|
| 507 |
it "returns an empty string when the match starts at the beginning" do |
| 508 |
md = RE2::Regexp.new('(\w+)').match("bob 123") |
| 509 |
|
| 510 |
expect(md.pre_match).to eq("") |
| 511 |
end |
| 512 |
|
| 513 |
it "supports multibyte characters" do |
| 514 |
md = RE2::Regexp.new('(\d+)').match("I ♥ 123") |
| 515 |
|
| 516 |
expect(md.pre_match).to eq("I ♥ ") |
| 517 |
end |
| 518 |
|
| 519 |
it "returns UTF-8 strings by default" do |
| 520 |
md = RE2::Regexp.new('(\d+)').match("abc 123") |
| 521 |
|
| 522 |
expect(md.pre_match.encoding).to eq(Encoding::UTF_8) |
| 523 |
end |
| 524 |
|
| 525 |
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do |
| 526 |
md = RE2::Regexp.new('(\d+)', utf8: false).match("abc 123") |
| 527 |
|
| 528 |
expect(md.pre_match.encoding).to eq(Encoding::ISO_8859_1) |
| 529 |
end |
| 530 |
|
| 531 |
it "returns the text before a zero-length match" do |
| 532 |
md = RE2::Regexp.new('()').match("bob") |
| 533 |
|
| 534 |
expect(md.pre_match).to eq("") |
| 535 |
end |
| 536 |
|
| 537 |
it "raises an error when called on an uninitialized object" do |
| 538 |
expect { described_class.allocate.pre_match }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 539 |
end |
| 540 |
end |
| 541 |
|
| 542 |
describe "#post_match" do |
| 543 |
it "returns the portion of the string after the match" do |
| 544 |
md = RE2::Regexp.new('(\d+)').match("bob 123 456") |
| 545 |
|
| 546 |
expect(md.post_match).to eq(" 456") |
| 547 |
end |
| 548 |
|
| 549 |
it "returns an empty string when the match ends at the end" do |
| 550 |
md = RE2::Regexp.new('(\d+)$').match("bob 123") |
| 551 |
|
| 552 |
expect(md.post_match).to eq("") |
| 553 |
end |
| 554 |
|
| 555 |
it "supports multibyte characters" do |
| 556 |
md = RE2::Regexp.new('(\d+)').match("123 ♥ world") |
| 557 |
|
| 558 |
expect(md.post_match).to eq(" ♥ world") |
| 559 |
end |
| 560 |
|
| 561 |
it "returns UTF-8 strings by default" do |
| 562 |
md = RE2::Regexp.new('(\d+)').match("abc 123 def") |
| 563 |
|
| 564 |
expect(md.post_match.encoding).to eq(Encoding::UTF_8) |
| 565 |
end |
| 566 |
|
| 567 |
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do |
| 568 |
md = RE2::Regexp.new('(\d+)', utf8: false).match("abc 123 def") |
| 569 |
|
| 570 |
expect(md.post_match.encoding).to eq(Encoding::ISO_8859_1) |
| 571 |
end |
| 572 |
|
| 573 |
it "returns the text after a zero-length match" do |
| 574 |
md = RE2::Regexp.new('()').match("bob") |
| 575 |
|
| 576 |
expect(md.post_match).to eq("bob") |
| 577 |
end |
| 578 |
|
| 579 |
it "raises an error when called on an uninitialized object" do |
| 580 |
expect { described_class.allocate.post_match }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 581 |
end |
| 582 |
end |
| 583 |
|
| 584 |
describe "#offset" do |
| 585 |
it "returns the offset of a match by index" do |
| 586 |
md = RE2::Regexp.new('ob (\d+)').match("bob 123") |
| 587 |
|
| 588 |
expect(md.offset(0)).to eq([1, 7]) |
| 589 |
end |
| 590 |
|
| 591 |
it "returns the offset of a submatch by index" do |
| 592 |
md = RE2::Regexp.new('ob (\d+)').match("bob 123") |
| 593 |
|
| 594 |
expect(md.offset(1)).to eq([4, 7]) |
| 595 |
end |
| 596 |
|
| 597 |
it "returns the offset of a match by string name" do |
| 598 |
md = RE2::Regexp.new('(?P<number>\d+)').match("bob 123") |
| 599 |
|
| 600 |
expect(md.offset("number")).to eq([4, 7]) |
| 601 |
end |
| 602 |
|
| 603 |
it "returns the offset of a match by symbol name" do |
| 604 |
md = RE2::Regexp.new('(?P<number>\d+)').match("bob 123") |
| 605 |
|
| 606 |
expect(md.offset(:number)).to eq([4, 7]) |
| 607 |
end |
| 608 |
|
| 609 |
it "returns character offsets despite multibyte characters" do |
| 610 |
md = RE2::Regexp.new('(Ruby)').match("I ♥ Ruby") |
| 611 |
|
| 612 |
expect(md.offset(0)).to eq([4, 8]) |
| 613 |
end |
| 614 |
|
| 615 |
it "returns identical offsets for a zero-length capturing group" do |
| 616 |
md = RE2::Regexp.new('()').match("bob") |
| 617 |
|
| 618 |
expect(md.offset(1)).to eq([0, 0]) |
| 619 |
end |
| 620 |
|
| 621 |
it "returns nil for non-existent numerical matches" do |
| 622 |
md = RE2::Regexp.new('(\d)').match("123") |
| 623 |
|
| 624 |
expect(md.offset(10)).to be_nil |
| 625 |
end |
| 626 |
|
| 627 |
it "returns nil for non-existent named matches" do |
| 628 |
md = RE2::Regexp.new('(\d)').match("123") |
| 629 |
|
| 630 |
expect(md.offset("foo")).to be_nil |
| 631 |
end |
| 632 |
|
| 633 |
it "raises a type error if given an invalid name or number" do |
| 634 |
md = RE2::Regexp.new('(\d)').match("123") |
| 635 |
|
| 636 |
expect { md.offset(nil) }.to raise_error(TypeError) |
| 637 |
end |
| 638 |
|
| 639 |
it "raises an error when called on an uninitialized object" do |
| 640 |
expect { described_class.allocate.offset(0) }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 641 |
end |
| 642 |
end |
| 643 |
|
| 644 |
describe "#match_length" do |
| 645 |
it "returns the length of the overall match" do |
| 646 |
md = RE2::Regexp.new('ob (\d+)').match("bob 123") |
| 647 |
|
| 648 |
expect(md.match_length(0)).to eq(6) |
| 649 |
end |
| 650 |
|
| 651 |
it "returns the length of a submatch by index" do |
| 652 |
md = RE2::Regexp.new('ob (\d+)').match("bob 123") |
| 653 |
|
| 654 |
expect(md.match_length(1)).to eq(3) |
| 655 |
end |
| 656 |
|
| 657 |
it "returns the length of a match by string name" do |
| 658 |
md = RE2::Regexp.new('(?P<number>\d+)').match("bob 123") |
| 659 |
|
| 660 |
expect(md.match_length("number")).to eq(3) |
| 661 |
end |
| 662 |
|
| 663 |
it "returns the length of a match by symbol name" do |
| 664 |
md = RE2::Regexp.new('(?P<number>\d+)').match("bob 123") |
| 665 |
|
| 666 |
expect(md.match_length(:number)).to eq(3) |
| 667 |
end |
| 668 |
|
| 669 |
it "returns character length despite multibyte characters" do |
| 670 |
md = RE2::Regexp.new('(♥ Ruby)').match("I ♥ Ruby!") |
| 671 |
|
| 672 |
expect(md.match_length(0)).to eq(6) |
| 673 |
end |
| 674 |
|
| 675 |
it "returns zero for a zero-length capturing group" do |
| 676 |
md = RE2::Regexp.new('()').match("bob") |
| 677 |
|
| 678 |
expect(md.match_length(1)).to eq(0) |
| 679 |
end |
| 680 |
|
| 681 |
it "returns nil for non-existent numerical matches" do |
| 682 |
md = RE2::Regexp.new('(\d)').match("123") |
| 683 |
|
| 684 |
expect(md.match_length(10)).to be_nil |
| 685 |
end |
| 686 |
|
| 687 |
it "returns nil for non-existent named matches" do |
| 688 |
md = RE2::Regexp.new('(\d)').match("123") |
| 689 |
|
| 690 |
expect(md.match_length("foo")).to be_nil |
| 691 |
end |
| 692 |
|
| 693 |
it "raises a type error if given an invalid name or number" do |
| 694 |
md = RE2::Regexp.new('(\d)').match("123") |
| 695 |
|
| 696 |
expect { md.match_length(nil) }.to raise_error(TypeError) |
| 697 |
end |
| 698 |
|
| 699 |
it "raises an error when called on an uninitialized object" do |
| 700 |
expect { described_class.allocate.match_length(0) }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 701 |
end |
| 702 |
end |
| 703 |
|
| 704 |
describe "#values_at" do |
| 705 |
it "returns match values at the given indices" do |
| 706 |
md = RE2::Regexp.new('(\d+) (\d+) (\d+)').match("123 456 789") |
| 707 |
|
| 708 |
expect(md.values_at(1, 3)).to eq(["123", "789"]) |
| 709 |
end |
| 710 |
|
| 711 |
it "returns match values by named groups" do |
| 712 |
md = RE2::Regexp.new('(?P<a>\d+) (?P<b>\d+)').match("123 456") |
| 713 |
|
| 714 |
expect(md.values_at(:a, :b)).to eq(["123", "456"]) |
| 715 |
end |
| 716 |
|
| 717 |
it "supports a mix of indices and names" do |
| 718 |
md = RE2::Regexp.new('(?P<a>\d+) (\d+)').match("123 456") |
| 719 |
|
| 720 |
expect(md.values_at(2, :a)).to eq(["456", "123"]) |
| 721 |
end |
| 722 |
|
| 723 |
it "returns nil for non-existent indices" do |
| 724 |
md = RE2::Regexp.new('(\d+)').match("123") |
| 725 |
|
| 726 |
expect(md.values_at(1, 5)).to eq(["123", nil]) |
| 727 |
end |
| 728 |
|
| 729 |
it "returns nil for non-existent names" do |
| 730 |
md = RE2::Regexp.new('(?P<a>\d+)').match("123") |
| 731 |
|
| 732 |
expect(md.values_at(:a, :z)).to eq(["123", nil]) |
| 733 |
end |
| 734 |
|
| 735 |
it "returns an empty string for a zero-length capturing group" do |
| 736 |
md = RE2::Regexp.new('()(b)').match("bob") |
| 737 |
|
| 738 |
expect(md.values_at(1, 2)).to eq(["", "b"]) |
| 739 |
end |
| 740 |
|
| 741 |
it "returns the full match when given index 0" do |
| 742 |
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456") |
| 743 |
|
| 744 |
expect(md.values_at(0, 1)).to eq(["123 456", "123"]) |
| 745 |
end |
| 746 |
|
| 747 |
it "returns an empty array if given no arguments" do |
| 748 |
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456") |
| 749 |
|
| 750 |
expect(md.values_at).to be_empty |
| 751 |
end |
| 752 |
|
| 753 |
it "raises an error when called on an uninitialized object" do |
| 754 |
expect { described_class.allocate.values_at(1) }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 755 |
end |
| 756 |
end |
| 757 |
|
| 758 |
describe "#deconstruct" do |
| 759 |
it "returns all capturing groups" do |
| 760 |
md = RE2::Regexp.new('w(o)(o)').match('woo') |
| 761 |
|
| 762 |
expect(md.deconstruct).to eq(['o', 'o']) |
| 763 |
end |
| 764 |
|
| 765 |
it "includes optional capturing groups as nil" do |
| 766 |
md = RE2::Regexp.new('w(.)(.)(.)?').match('woo') |
| 767 |
|
| 768 |
expect(md.deconstruct).to eq(['o', 'o', nil]) |
| 769 |
end |
| 770 |
|
| 771 |
it "includes zero-length capturing groups as empty strings" do |
| 772 |
md = RE2::Regexp.new('()').match("bob") |
| 773 |
|
| 774 |
expect(md.deconstruct).to eq([""]) |
| 775 |
end |
| 776 |
|
| 777 |
it "raises an error when called on an uninitialized object" do |
| 778 |
expect { described_class.allocate.deconstruct }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 779 |
end |
| 780 |
end |
| 781 |
|
| 782 |
describe "#captures" do |
| 783 |
it "returns all capturing groups" do |
| 784 |
md = RE2::Regexp.new('w(o)(o)').match('woo') |
| 785 |
|
| 786 |
expect(md.captures).to eq(['o', 'o']) |
| 787 |
end |
| 788 |
|
| 789 |
it "includes optional capturing groups as nil" do |
| 790 |
md = RE2::Regexp.new('w(.)(.)(.)?').match('woo') |
| 791 |
|
| 792 |
expect(md.captures).to eq(['o', 'o', nil]) |
| 793 |
end |
| 794 |
|
| 795 |
it "raises an error when called on an uninitialized object" do |
| 796 |
expect { described_class.allocate.captures }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 797 |
end |
| 798 |
end |
| 799 |
|
| 800 |
describe "#named_captures" do |
| 801 |
it "returns a hash of capturing group names to matched strings" do |
| 802 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 803 |
|
| 804 |
expect(md.named_captures).to eq("numbers" => "123", "letters" => "abc") |
| 805 |
end |
| 806 |
|
| 807 |
it "returns an empty hash if there are no named capturing groups" do |
| 808 |
md = RE2::Regexp.new('(\d+)').match('123') |
| 809 |
|
| 810 |
expect(md.named_captures).to be_empty |
| 811 |
end |
| 812 |
|
| 813 |
it "returns unmatched optional groups as nil" do |
| 814 |
md = RE2::Regexp.new('(?P<a>\d+) (?P<b>\w+)?').match('123 ') |
| 815 |
|
| 816 |
expect(md.named_captures).to eq("a" => "123", "b" => nil) |
| 817 |
end |
| 818 |
|
| 819 |
it "returns an empty string for a zero-length named capturing group" do |
| 820 |
md = RE2::Regexp.new('(?P<empty>)(?P<word>\w+)').match("bob") |
| 821 |
|
| 822 |
expect(md.named_captures).to eq("empty" => "", "word" => "bob") |
| 823 |
end |
| 824 |
|
| 825 |
it "returns symbol keys when symbolize_names: true" do |
| 826 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 827 |
|
| 828 |
expect(md.named_captures(symbolize_names: true)).to eq(numbers: "123", letters: "abc") |
| 829 |
end |
| 830 |
|
| 831 |
it "returns string keys when symbolize_names: false" do |
| 832 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 833 |
|
| 834 |
expect(md.named_captures(symbolize_names: false)).to eq("numbers" => "123", "letters" => "abc") |
| 835 |
end |
| 836 |
|
| 837 |
it "raises an error when called on an uninitialized object" do |
| 838 |
expect { described_class.allocate.named_captures }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 839 |
end |
| 840 |
end |
| 841 |
|
| 842 |
describe "#names" do |
| 843 |
it "returns an array of names of named capturing groups" do |
| 844 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 845 |
|
| 846 |
expect(md.names).to eq(["letters", "numbers"]) |
| 847 |
end |
| 848 |
|
| 849 |
it "returns an empty array if there are no named capturing groups" do |
| 850 |
md = RE2::Regexp.new('(\d+)').match('123') |
| 851 |
|
| 852 |
expect(md.names).to be_empty |
| 853 |
end |
| 854 |
|
| 855 |
it "returns UTF-8 strings if the pattern is UTF-8" do |
| 856 |
md = RE2::Regexp.new('(?P<numbers>\d+)').match('123') |
| 857 |
|
| 858 |
expect(md.names.first.encoding).to eq(Encoding::UTF_8) |
| 859 |
end |
| 860 |
|
| 861 |
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do |
| 862 |
md = RE2::Regexp.new('(?P<numbers>\d+)', utf8: false).match('123') |
| 863 |
|
| 864 |
expect(md.names.first.encoding).to eq(Encoding::ISO_8859_1) |
| 865 |
end |
| 866 |
|
| 867 |
it "raises an error when called on an uninitialized object" do |
| 868 |
expect { described_class.allocate.names }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 869 |
end |
| 870 |
end |
| 871 |
|
| 872 |
describe "#deconstruct_keys" do |
| 873 |
it "returns all named captures if given nil" do |
| 874 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 875 |
|
| 876 |
expect(md.deconstruct_keys(nil)).to eq(numbers: '123', letters: 'abc') |
| 877 |
end |
| 878 |
|
| 879 |
it "returns only named captures if given names" do |
| 880 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 881 |
|
| 882 |
expect(md.deconstruct_keys([:numbers])).to eq(numbers: '123') |
| 883 |
end |
| 884 |
|
| 885 |
it "returns named captures up until an invalid name is given" do |
| 886 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 887 |
|
| 888 |
expect(md.deconstruct_keys([:numbers, :punctuation])).to eq(numbers: '123') |
| 889 |
end |
| 890 |
|
| 891 |
it "returns an empty hash if given more capture names than exist" do |
| 892 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 893 |
|
| 894 |
expect(md.deconstruct_keys([:numbers, :letters, :punctuation])).to eq({}) |
| 895 |
end |
| 896 |
|
| 897 |
it "returns an empty hash if there are no named capturing groups" do |
| 898 |
md = RE2::Regexp.new('(\d+) ([a-zA-Z]+)').match('123 abc') |
| 899 |
|
| 900 |
expect(md.deconstruct_keys(nil)).to eq({}) |
| 901 |
end |
| 902 |
|
| 903 |
it "returns an empty string for a zero-length named capturing group" do |
| 904 |
md = RE2::Regexp.new('(?P<empty>)(?P<word>\w+)').match("bob") |
| 905 |
|
| 906 |
expect(md.deconstruct_keys(nil)).to eq(empty: "", word: "bob") |
| 907 |
end |
| 908 |
|
| 909 |
it "raises an error if given a non-array of keys" do |
| 910 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 911 |
|
| 912 |
expect { md.deconstruct_keys(0) }.to raise_error(TypeError) |
| 913 |
end |
| 914 |
|
| 915 |
it "raises an error if given keys as non-symbols" do |
| 916 |
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc') |
| 917 |
|
| 918 |
expect { md.deconstruct_keys([0]) }.to raise_error(TypeError) |
| 919 |
end |
| 920 |
|
| 921 |
it "raises an error when called on an uninitialized object" do |
| 922 |
expect { described_class.allocate.deconstruct_keys(nil) }.to raise_error(TypeError, /uninitialized RE2::MatchData/) |
| 923 |
end |
| 924 |
end |
| 925 |
end |