1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
# rubocop:todo all
require 'spec_helper'
describe Regexp::Raw do
let(:pattern) { '\W+' }
let(:options) { '' }
let(:bson) { "#{pattern}#{BSON::NULL_BYTE}#{options}#{BSON::NULL_BYTE}" }
describe "#as_json" do
let(:object) do
described_class.new(pattern, 'im')
end
it "returns the legacy serialization including regex pattern and options" do
expect(object.as_json).to eq({ "$regex" => "\\W+", "$options" => "im" })
end
it_behaves_like "a JSON serializable object"
end
describe '#as_extended_json' do
let(:object) do
described_class.new(pattern, 'im')
end
context 'legacy mode' do
it "returns the legacy serialization including regex pattern and options" do
expect(object.as_extended_json(mode: :legacy)).to eq({ "$regex" => "\\W+", "$options" => "im" })
end
end
context 'canonical/relaxed mode' do
it "returns the extended json 2.0 serialization" do
expect(object.as_extended_json).to eq(
"$regularExpression" => {'pattern' => "\\W+", "options" => "im"}
)
end
end
end
describe "#to_bson/#from_bson" do
let(:options) { 'ilmsux' }
let(:obj) { described_class.new(pattern, options) }
let(:type) { 11.chr }
let(:bson) { "#{pattern}#{BSON::NULL_BYTE}#{options}#{BSON::NULL_BYTE}" }
let(:klass) { ::Regexp }
it_behaves_like "a bson element"
it_behaves_like "a serializable bson element"
it_behaves_like "a deserializable bson element"
end
describe "#initialize" do
let(:object) do
described_class.new(pattern, options)
end
context "when options are not passed" do
it "sets the options on the raw regex" do
expect(object.options). to eq(options)
end
context "When the raw regexp is compiled" do
let(:regexp) do
object.compile
end
it "sets the options on the compiled regexp object" do
expect(regexp.options).to eq(0)
end
end
end
context "when options are passed" do
context "when options are an Integer" do
let(:options) { ::Regexp::EXTENDED }
it "raises an error" do
expect do
object
end.to raise_error(ArgumentError, /Regexp options must be a String or Symbol/)
end
end
context "when options are a String" do
let(:options) { 'x' }
it "sets the options on the raw regex" do
expect(object.options). to eq(options)
end
context "When the raw regexp is compiled" do
let(:regexp) do
object.compile
end
it "sets the options on the compiled regexp object" do
expect(regexp.options).to eq(::Regexp::EXTENDED)
end
end
end
end
context 'when options are not passed' do
let(:object) do
described_class.new(pattern)
end
it "sets no options on the raw regex" do
expect(object.options). to eq('')
end
context "When the raw regexp is compiled" do
let(:regexp) do
object.compile
end
it "sets the options on the compiled regexp object" do
expect(regexp.options).to eq(0)
end
end
end
end
describe "#from_bson" do
let(:obj) { ::Regexp.from_bson(io) }
let(:io) { BSON::ByteBuffer.new(bson) }
it "deserializes to a Regexp::Raw object" do
expect(obj).to be_a(Regexp::Raw)
end
it "deserializes the pattern" do
expect(obj.pattern).to eq(pattern)
end
context "when there are no options" do
it "does not set any options on the raw regexp object" do
expect(obj.options).to eq(options)
end
end
context "when there are options" do
context "when there is the i ignorecase option" do
let(:options) { 'i' }
it "deserializes the pattern" do
expect(obj.pattern).to eq(pattern)
end
it "sets the i option on the raw regexp object" do
expect(obj.options).to eq(options)
end
end
context "when there is the l locale dependent option" do
let(:options) { 'l' }
it "deserializes the pattern" do
expect(obj.pattern).to eq(pattern)
end
it "sets the l option on the raw regexp object" do
expect(obj.options).to eq(options)
end
end
context "when there is the m multiline option" do
let(:options) { 'm' }
it "deserializes the pattern" do
expect(obj.pattern).to eq(pattern)
end
it "sets the m option on the raw regexp object" do
expect(obj.options).to eq(options)
end
end
context "when there is the s dotall option" do
let(:options) { 's' }
it "deserializes the pattern" do
expect(obj.pattern).to eq(pattern)
end
it "sets the s option on the raw regexp object" do
expect(obj.options).to eq(options)
end
end
context "when there is the u match unicode option" do
let(:options) { 'u' }
it "deserializes the pattern" do
expect(obj.pattern).to eq(pattern)
end
it "sets the u option on the raw regexp object" do
expect(obj.options).to eq(options)
end
end
context "when there is the x verbose option" do
let(:options) { 'x' }
it "deserializes the pattern" do
expect(obj.pattern).to eq(pattern)
end
it "sets the x option on the raw regexp object" do
expect(obj.options).to eq(options)
end
end
context "when all options are set" do
let(:options) { 'ilmsux' }
it "deserializes the pattern" do
expect(obj.pattern).to eq(pattern)
end
it "sets all options on the raw regexp object" do
expect(obj.options).to eq(options)
end
end
end
end
context "when a method is called on a Raw regexp object" do
let(:obj) { ::Regexp.from_bson(io) }
let(:io) { BSON::ByteBuffer.new(bson) }
it "forwards the method call on to the compiled Ruby Regexp object" do
expect(obj.source).to eq(pattern)
end
end
context "when respond_to? is called on the Raw Regexp object" do
let(:obj) { Regexp::Raw.new(pattern, options) }
context "when include_private is false" do
it "does not consider private methods" do
expect(obj.respond_to?(:initialize_copy)).to eq(false)
end
end
context "when include private is true" do
it "considers private methods" do
expect(obj.respond_to?(:initialize_copy, true)).to eq(true)
end
end
context "when include_private is not specified" do
it "does not consider private methods" do
expect(obj.respond_to?(:initialize_copy)).to eq(false)
end
end
end
context "#to_bson" do
let(:obj) { Regexp::Raw.new(pattern, options) }
let(:options) { '' }
let(:bson) { "#{pattern}#{BSON::NULL_BYTE}#{options}#{BSON::NULL_BYTE}" }
let(:serialized) { obj.to_bson.to_s }
it "serializes the pattern" do
expect(serialized).to eq(bson)
end
context "where there are no options" do
it "does not set any options on the bson regex object" do
expect(serialized).to eq(bson)
end
end
context "when there are options" do
context "when options are specified as an Integer" do
let(:options) { ::Regexp::EXTENDED }
let(:bson) { "#{pattern}#{BSON::NULL_BYTE}mx#{BSON::NULL_BYTE}" }
it "raises an error" do
expect do
serialized
end.to raise_error(ArgumentError, /Regexp options must be a String or Symbol/)
end
end
context "when there is the i ignorecase option" do
let(:options) { 'i' }
it "sets the option on the serialized bson object" do
expect(serialized).to eq(bson)
end
end
context "when there is the l locale dependent option" do
let(:options) { 'l' }
it "sets the option on the serialized bson object" do
expect(serialized).to eq(bson)
end
end
context "when there is the m multiline option" do
let(:options) { 'm' }
it "sets the option on the serialized bson object" do
expect(serialized).to eq(bson)
end
end
context "when there is the s dotall option" do
let(:options) { 's' }
it "sets the option on the serialized bson object" do
expect(serialized).to eq(bson)
end
end
context "when there is the u match unicode option" do
let(:options) { 'u' }
it "sets the option on the serialized bson object" do
expect(serialized).to eq(bson)
end
end
context "when there is the x verbose option" do
let(:options) { 'x' }
it "sets the option on the serialized bson object" do
expect(serialized).to eq(bson)
end
end
context "when all options are set" do
let(:options) { 'ilmsux' }
it "sets all options on the serialized bson object" do
expect(serialized).to eq(bson)
end
context "when the options are not provided in alphabetical order" do
let(:options) { 'mislxu' }
let(:bson) { "#{pattern}#{BSON::NULL_BYTE}ilmsux#{BSON::NULL_BYTE}" }
it "serializes the options in alphabetical order" do
expect(serialized).to eq(bson)
end
end
end
end
end
describe "#compile" do
let(:obj) { Regexp.from_bson(io) }
let(:io) { BSON::ByteBuffer.new(bson) }
let(:ruby_regexp) { obj.compile }
it "sets the pattern on the Ruby Regexp object" do
expect(obj.pattern).to eq(ruby_regexp.source)
end
context "when there are no options set" do
it "does not set any options on the Ruby Regexp object" do
expect(ruby_regexp.options).to eq(0)
end
end
context "when there are options set" do
context "when there is the i ignorecase option" do
let(:options) { 'i' }
it "sets the i option on the Ruby Regexp object" do
expect(ruby_regexp.options).to eq(::Regexp::IGNORECASE)
end
end
context "when there is the l locale dependent option" do
let(:options) { 'l' }
it "does not set an option on the Ruby Regexp object" do
expect(ruby_regexp.options).to eq(0)
end
end
context "when there is the m multiline option" do
let(:options) { 'm' }
it "does not set an option on the Ruby Regexp object" do
expect(ruby_regexp.options).to eq(0)
end
end
context "when there is the s dotall option" do
let(:options) { 's' }
# s in a bson regex maps to a Ruby Multiline Regexp option
it "sets the m option on the Ruby Regexp object" do
expect(ruby_regexp.options).to eq(::Regexp::MULTILINE)
end
end
context "when there is the u match unicode option" do
let(:options) { 'u' }
it "does not set an option on the Ruby Regexp object" do
expect(ruby_regexp.options).to eq(0)
end
end
context "when there is the x verbose option" do
let(:options) { 'x' }
it "sets the x option on the Ruby Regexp object" do
expect(ruby_regexp.options).to eq(::Regexp::EXTENDED)
end
end
context "when all options are set" do
let(:options) { 'ilmsux' }
# s in a bson regex maps to a Ruby Multiline Regexp option
it "sets the i, m, and x options on the Ruby Regexp object" do
expect(ruby_regexp.options).to eq(::Regexp::IGNORECASE | ::Regexp::MULTILINE | ::Regexp::EXTENDED)
end
end
end
end
context "when a Regexp::Raw object is roundtripped" do
let(:obj) { Regexp::Raw.new(pattern, options) }
let(:serialized) { obj.to_bson.to_s }
let(:roundtripped) { Regexp.from_bson(BSON::ByteBuffer.new(serialized)) }
it "roundtrips the pattern" do
expect(roundtripped.pattern).to eq(pattern)
end
context "when there are no options" do
let(:options) { '' }
it "does not set any options on the roundtripped Regexp::Raw object" do
expect(roundtripped.options).to eq(options)
end
end
context "when there are options set" do
context "when there is the i ignorecase option" do
let(:options) { 'i' }
it "sets the i option on the roundtripped Regexp::Raw object" do
expect(roundtripped.options).to eq(options)
end
end
context "when there is the l locale dependent option" do
let(:options) { 'l' }
it "sets the l option on the roundtripped Regexp::Raw object" do
expect(roundtripped.options).to eq(options)
end
end
context "when there is the m multiline option" do
let(:options) { 'm' }
it "sets the m option on the roundtripped Regexp::Raw object" do
expect(roundtripped.options).to eq(options)
end
end
context "when there is the s dotall option" do
let(:options) { 's' }
it "sets the s option on the roundtripped Regexp::Raw object" do
expect(roundtripped.options).to eq(options)
end
end
context "when there is the u match unicode option" do
let(:options) { 'u' }
it "sets the u option on the roundtripped Regexp::Raw object" do
expect(roundtripped.options).to eq(options)
end
end
context "when there is the x verbose option" do
let(:options) { 'x' }
it "sets the x option on the roundtripped Regexp::Raw object" do
expect(roundtripped.options).to eq(options)
end
end
context "when all options are set" do
let(:options) { 'ilmsux' }
it "sets all the options on the roundtripped Regexp::Raw object" do
expect(roundtripped.options).to eq(options)
end
context "when the options are passed in not in alphabetical order" do
let(:options) { 'sumlxi' }
it "sets all the options on the roundtripped Regexp::Raw object in order" do
expect(roundtripped.options).to eq(options.chars.sort.join)
end
end
end
end
end
describe 'yaml loading' do
let(:regexp) { described_class.new('hello.world', 's') }
it 'round-trips' do
actual = if YAML.respond_to?(:unsafe_load)
# In psych >= 4.0.0 `load` is basically an alias to `safe_load`,
# which will fail here.
YAML.unsafe_load(regexp.to_yaml)
else
YAML.load(regexp.to_yaml)
end
actual.pattern.should == 'hello.world'
actual.options.should == 's'
actual.compile.should =~ "hello\nworld"
end
end
end
|