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