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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file or at https://opensource.org/licenses/MIT.
#include "third_party/liburlpattern/pattern.h"
#include <algorithm>
#include <iterator>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/types/expected.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/status/status.h"
#include "third_party/liburlpattern/options.h"
#include "third_party/liburlpattern/parse.h"
namespace {
base::expected<std::string, absl::Status> PassThrough(std::string_view input) {
return std::string(input);
}
base::expected<std::string, absl::Status> ToUpper(std::string_view input) {
std::string output;
std::ranges::transform(input, std::back_inserter(output),
[](unsigned char c) {
return static_cast<unsigned char>(std::toupper(c));
});
return base::ok(std::move(output));
}
} // namespace
namespace liburlpattern {
void RunRegexTest(std::string_view input,
std::string_view expected_regex,
std::vector<std::string> expected_name_list,
Options options = Options()) {
auto result = Parse(input, PassThrough, options);
ASSERT_TRUE(result.has_value());
auto& pattern = result.value();
std::vector<std::string> name_list;
std::string regex = pattern.GenerateRegexString(&name_list);
EXPECT_EQ(regex, expected_regex);
EXPECT_EQ(name_list, expected_name_list);
}
// The following expected test case values were generated using path-to-regexp
// 6.2.0.
TEST(PatternRegexTest, Fixed) {
RunRegexTest("/foo/bar", R"(^\/foo\/bar[\/#\?]?$)", {});
}
TEST(PatternRegexTest, FixedWithModifier) {
RunRegexTest("{/foo/bar}?", R"(^(?:\/foo\/bar)?[\/#\?]?$)", {});
}
TEST(PatternRegexTest, Name) {
RunRegexTest(":foo", R"(^([^\/#\?]+?)[\/#\?]?$)", {"foo"});
}
TEST(PatternRegexTest, NameWithUnicode) {
RunRegexTest(":fooßar", R"(^([^\/#\?]+?)[\/#\?]?$)", {"fooßar"});
}
TEST(PatternRegexTest, NameWithOptionalModifier) {
RunRegexTest(":foo?", R"(^([^\/#\?]+?)?[\/#\?]?$)", {"foo"});
}
TEST(PatternRegexTest, NameWithZeroOrMoreModifier) {
RunRegexTest(":foo*", R"(^((?:[^\/#\?]+?)*)[\/#\?]?$)", {"foo"});
}
TEST(PatternRegexTest, NameWithOneOrMoreModifier) {
RunRegexTest(":foo+", R"(^((?:[^\/#\?]+?)+)[\/#\?]?$)", {"foo"});
}
TEST(PatternRegexTest, NameWithPrefix) {
RunRegexTest("/foo/:bar", R"(^\/foo(?:\/([^\/#\?]+?))[\/#\?]?$)", {"bar"});
}
TEST(PatternRegexTest, NameWithPrefixAndOptionalModifier) {
RunRegexTest("/foo/:bar?", R"(^\/foo(?:\/([^\/#\?]+?))?[\/#\?]?$)", {"bar"});
}
TEST(PatternRegexTest, NameWithPrefixAndOneOrMoreModifier) {
RunRegexTest("/foo/:bar+",
R"(^\/foo(?:\/((?:[^\/#\?]+?)(?:\/(?:[^\/#\?]+?))*))[\/#\?]?$)",
{"bar"});
}
TEST(PatternRegexTest, NameWithPrefixAndZeroOrMoreModifier) {
RunRegexTest("/foo/:bar*",
R"(^\/foo(?:\/((?:[^\/#\?]+?)(?:\/(?:[^\/#\?]+?))*))?[\/#\?]?$)",
{"bar"});
}
TEST(PatternRegexTest, Regex) {
RunRegexTest("([a-z]+)", R"(^([a-z]+)[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, RegexFullWildcard) {
RunRegexTest("(.*)", R"(^(.*)[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, Wildcard) {
RunRegexTest("*", R"(^(.*)[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, RegexWithOptionalModifier) {
RunRegexTest("([a-z]+)?", R"(^([a-z]+)?[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, WildcardWithOptionalModifier) {
RunRegexTest("*?", R"(^(.*)?[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, RegexWithPrefix) {
RunRegexTest("/foo/([a-z]+)", R"(^\/foo(?:\/([a-z]+))[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, WildcardWithPrefix) {
RunRegexTest("/foo/*", R"(^\/foo(?:\/(.*))[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, RegexWithPrefixAndOptionalModifier) {
RunRegexTest("/foo/([a-z]+)?", R"(^\/foo(?:\/([a-z]+))?[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, WildcardWithPrefixAndOptionalModifier) {
RunRegexTest("/foo/*?", R"(^\/foo(?:\/(.*))?[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, RegexWithPrefixAndOneOrMoreModifier) {
RunRegexTest("/foo/([a-z]+)+",
R"(^\/foo(?:\/((?:[a-z]+)(?:\/(?:[a-z]+))*))[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, WildcardWithPrefixAndOneOrMoreModifier) {
RunRegexTest("/foo/*+", R"(^\/foo(?:\/((?:.*)(?:\/(?:.*))*))[\/#\?]?$)",
{"0"});
}
TEST(PatternRegexTest, RegexWithPrefixAndZeroOrMoreModifier) {
RunRegexTest("/foo/([a-z]+)*",
R"(^\/foo(?:\/((?:[a-z]+)(?:\/(?:[a-z]+))*))?[\/#\?]?$)", {"0"});
}
TEST(PatternRegexTest, WildcardWithPrefixAndZeroOrMoreModifier) {
RunRegexTest("/foo/**", R"(^\/foo(?:\/((?:.*)(?:\/(?:.*))*))?[\/#\?]?$)",
{"0"});
}
TEST(PatternRegexTest, NameWithCustomRegex) {
RunRegexTest(":foo([a-z]+)", R"(^([a-z]+)[\/#\?]?$)", {"foo"});
}
TEST(PatternRegexTest, ManyGroups) {
RunRegexTest("/([a-z])+/:foo/([a-z])+/:bar+",
R"(^(?:\/((?:[a-z])(?:\/(?:[a-z]))*))(?:\/([^\/#\?]+?))(?:\/)"
R"(((?:[a-z])(?:\/(?:[a-z]))*))(?:\/((?:[^\/#\?]+?)(?:\/)"
R"((?:[^\/#\?]+?))*))[\/#\?]?$)",
{"0", "foo", "1", "bar"});
}
TEST(PatternRegexTest, StrictNameWithPrefixAndOneOrMoreModifier) {
RunRegexTest("/foo/:bar+",
R"(^\/foo(?:\/((?:[^\/#\?]+?)(?:\/(?:[^\/#\?]+?))*))$)", {"bar"},
{.strict = true});
}
TEST(PatternRegexTest, NoEndNameWithPrefixAndOneOrMoreModifier) {
RunRegexTest("/foo/:bar+",
R"(^\/foo(?:\/((?:[^\/#\?]+?)(?:\/(?:[^\/#\?]+?))*))(?:[\/)"
R"(#\?](?=[]|$))?(?=[\/#\?]|[]|$))",
{"bar"}, {.end = false});
}
TEST(PatternRegexTest, NoEndFixedWithTrailingDelimiter) {
RunRegexTest("/foo/bar/", R"(^\/foo\/bar\/(?:[\/#\?](?=[]|$))?)", {},
{.end = false});
}
TEST(PatternRegexTest, StrictNoEndFixedWithTrailingDelimiter) {
RunRegexTest("/foo/bar/", R"(^\/foo\/bar\/)", {},
{.strict = true, .end = false});
}
TEST(PatternRegexTest, StrictNoEndFixedWithoutTrailingDelimiter) {
RunRegexTest("/foo/bar", R"(^\/foo\/bar(?=[\/#\?]|[]|$))", {},
{.strict = true, .end = false});
}
TEST(PatternRegexTest, NoStartNameWithPrefixAndOneOrMoreModifier) {
RunRegexTest("/foo/:bar+",
R"(\/foo(?:\/((?:[^\/#\?]+?)(?:\/(?:[^\/#\?]+?))*))[\/#\?]?$)",
{"bar"}, {.start = false});
}
TEST(PatternRegexTest, EndsWithNameWithPrefixAndOneOrMoreModifier) {
RunRegexTest("/foo/:bar+",
R"(^\/foo(?:\/((?:[^\/#\?]+?)(?:\/(?:[^\/#\?]+?))*))[\/)"
R"(#\?]?(?=[#]|$))",
{"bar"}, {.ends_with = "#"});
}
TEST(PatternRegexTest, EndsWithNoEndNameWithPrefixAndOneOrMoreModifier) {
RunRegexTest("/foo/:bar+",
R"(^\/foo(?:\/((?:[^\/#\?]+?)(?:\/(?:[^\/#\?]+?))*))(?:[\/)"
R"(#\?](?=[#]|$))?(?=[\/#\?]|[#]|$))",
{"bar"}, {.end = false, .ends_with = "#"});
}
void RunPatternStringTest(std::string_view input,
std::string_view expected_pattern_string) {
auto result = Parse(input, PassThrough);
ASSERT_TRUE(result.has_value());
auto& pattern = result.value();
std::string pattern_string = pattern.GeneratePatternString();
EXPECT_EQ(pattern_string, expected_pattern_string);
// The computed pattern string should be valid and parse correctly.
auto result2 = Parse(pattern_string, PassThrough);
EXPECT_TRUE(result.has_value());
// The second Pattern object may or may not be identical to the first
// due to normalization. For example, stripping the unnecessary grouping
// from a `{foo}` term.
// Computing a second pattern string should result in an identical
// value, however.
std::string pattern_string2 = result2.value().GeneratePatternString();
EXPECT_EQ(pattern_string2, pattern_string);
}
TEST(PatternStringTest, Fixed) {
RunPatternStringTest("/foo/bar", "/foo/bar");
}
TEST(PatternStringTest, Group) {
RunPatternStringTest("/foo/{bar}", "/foo/bar");
}
TEST(PatternStringTest, GroupWithRegexp) {
RunPatternStringTest("/foo/{(bar)}", "/foo/{(bar)}");
}
TEST(PatternStringTest, GroupWithPrefixAndRegexp) {
RunPatternStringTest("/foo/{b(ar)}", "/foo/{b(ar)}");
}
TEST(PatternStringTest, GroupWithDefaultPrefixAndRegexp) {
RunPatternStringTest("/foo{/(bar)}", "/foo/(bar)");
}
TEST(PatternStringTest, GroupWithRegexpAndSuffix) {
RunPatternStringTest("/foo/{(ba)r}", "/foo/{(ba)r}");
}
TEST(PatternStringTest, GroupWithDefaultPrefixRegexpAndSuffix) {
RunPatternStringTest("/foo{/(ba)r}", "/foo{/(ba)r}");
}
TEST(PatternStringTest, GroupWithQuestionModifier) {
RunPatternStringTest("/foo/{bar}?", "/foo/{bar}?");
}
TEST(PatternStringTest, GroupWithStarModifier) {
RunPatternStringTest("/foo/{bar}*", "/foo/{bar}*");
}
TEST(PatternStringTest, GroupWithPlusModifier) {
RunPatternStringTest("/foo/{bar}+", "/foo/{bar}+");
}
TEST(PatternStringTest, NamedGroup) {
RunPatternStringTest("/foo/:bar", "/foo/:bar");
}
TEST(PatternStringTest, SegmentWildcardWithoutName) {
RunPatternStringTest("/foo/([^\\/#\\?]+?)", "/foo/([^\\/#\\?]+?)");
}
TEST(PatternStringTest, NamedGroupWithRegexp) {
RunPatternStringTest("/foo/:bar(baz)", "/foo/:bar(baz)");
}
TEST(PatternStringTest, NamedGroupWithEquivalentRegexp) {
RunPatternStringTest("/foo/:bar([^\\/#\\?]+?)", "/foo/:bar");
}
TEST(PatternStringTest, NamedGroupWithWildcardEquivalentRegexp) {
RunPatternStringTest("/foo/:bar(.*)", "/foo/:bar(.*)");
}
TEST(PatternStringTest, NamedGroupWithQuestionModifier) {
RunPatternStringTest("/foo/:bar?", "/foo/:bar?");
}
TEST(PatternStringTest, NamedGroupWithStarModifier) {
RunPatternStringTest("/foo/:bar*", "/foo/:bar*");
}
TEST(PatternStringTest, NamedGroupWithPlusModifier) {
RunPatternStringTest("/foo/:bar+", "/foo/:bar+");
}
TEST(PatternStringTest, Regexp) {
RunPatternStringTest("/foo/(bar)", "/foo/(bar)");
}
TEST(PatternStringTest, RegexpWithQuestionModifier) {
RunPatternStringTest("/foo/(bar)?", "/foo/(bar)?");
}
TEST(PatternStringTest, RegexpWithStarModifier) {
RunPatternStringTest("/foo/(bar)*", "/foo/(bar)*");
}
TEST(PatternStringTest, RegexpWithPlusModifier) {
RunPatternStringTest("/foo/(bar)+", "/foo/(bar)+");
}
TEST(PatternStringTest, Wildcard) {
RunPatternStringTest("/foo/*", "/foo/*");
}
TEST(PatternStringTest, RegexpWildcardEquivalent) {
RunPatternStringTest("/foo/(.*)", "/foo/*");
}
TEST(PatternStringTest, RegexpEscapedNonPatternChar) {
RunPatternStringTest("/foo/\\bar", "/foo/bar");
}
TEST(PatternStringTest, RegexpEscapedPatternChar) {
RunPatternStringTest("/foo/\\:bar", "/foo/\\:bar");
}
TEST(PatternStringTest, RegexpEscapedPatternCharInPrefix) {
RunPatternStringTest("/foo/{\\:bar(foo)}", "/foo/{\\:bar(foo)}");
}
TEST(PatternStringTest, RegexpEscapedPatternCharInSuffix) {
RunPatternStringTest("/foo/{(foo)\\:bar}", "/foo/{(foo)\\:bar}");
}
TEST(PatternStringTest, RegexpFollowedByWildcard) {
RunPatternStringTest("(foo)(.*)", "(foo)(.*)");
}
TEST(PatternStringTest, RegexpWithOptionalModifierFollowedByWildcard) {
RunPatternStringTest("(foo)?(.*)", "(foo)?*");
}
TEST(PatternStringTest, RegexpWithSuffixModifierFollowedByWildcard) {
RunPatternStringTest("{(foo)a}(.*)", "{(foo)a}(.*)");
}
TEST(PatternStringTest, NamedGroupInGroupingFollowedByWildcard) {
RunPatternStringTest("{:foo}(.*)", "{:foo}(.*)");
}
TEST(PatternStringTest, NamedGroupInGroupingFollowedByRegexp) {
RunPatternStringTest("{:foo}(bar)", "{:foo}(bar)");
}
TEST(PatternStringTest, NamedGroupInGroupingFollowedByWildcardInGrouping) {
RunPatternStringTest("{:foo}{(.*)}", "{:foo}(.*)");
}
TEST(PatternStringTest, NamedGroupInGroupingFollowedByWildcardWithSuffix) {
RunPatternStringTest("{:foo}{(.*)bar}", ":foo{*bar}");
}
TEST(PatternStringTest, NamedGroupInGroupingFollowedByWildcardWithPrefix) {
RunPatternStringTest("{:foo}{bar(.*)}", ":foo{bar*}");
}
TEST(PatternStringTest, NamedGroupInGroupingFollowedByWildcardWithCustomName) {
RunPatternStringTest("{:foo}:bar(.*)", ":foo:bar(.*)");
}
TEST(PatternStringTest,
NamedGroupInGroupingWithOptionalModifierFollowedByWildcard) {
RunPatternStringTest("{:foo}?(.*)", ":foo?*");
}
TEST(PatternStringTest, NamedGroupWithEscapedValidNameSuffix) {
RunPatternStringTest("{:foo\\bar}", "{:foo\\bar}");
}
TEST(PatternStringTest, NamedGroupWithEscapedInvalidNameSuffix) {
RunPatternStringTest("{:foo\\.bar}", "{:foo.bar}");
}
TEST(PatternStringTest, NamedGroupInGroupingFollowedByValidNameText) {
RunPatternStringTest("{:foo}bar", "{:foo}bar");
}
TEST(PatternStringTest, NamedGroupFollowedByEscapedValidNameText) {
RunPatternStringTest(":foo\\bar", "{:foo}bar");
}
TEST(PatternStringTest, NamedGroupWithRegexpFollowedByValidNameText) {
RunPatternStringTest(":foo(baz)bar", ":foo(baz)bar");
}
TEST(PatternStringTest, NamedGroupFollowedByEmptyGroupAndWildcard) {
RunPatternStringTest(":foo{}(.*)", "{:foo}(.*)");
}
TEST(PatternStringTest, NamedGroupFollowedByEmptyGroupAndValidNameText) {
RunPatternStringTest(":foo{}bar", "{:foo}bar");
}
TEST(PatternStringTest,
NamedGroupFollowedByEmptyGroupWithOptionalModifierAndValidNameText) {
RunPatternStringTest(":foo{}?bar", "{:foo}bar");
}
TEST(PatternStringTest, NamedGroupWithRegexpFollowedByWildcard) {
RunPatternStringTest(":foo(bar)(.*)", ":foo(bar)(.*)");
}
TEST(PatternStringTest, NamedGroupWithRegexpAndValidNameSuffix) {
RunPatternStringTest("{:foo(baz)bar}", "{:foo(baz)bar}");
}
TEST(PatternStringTest, WildcardSlashAndWildcard) {
RunPatternStringTest("*/*", "*/*");
}
TEST(PatternStringTest, WildcardEscapedSlashAndWildcard) {
// The backslash in the original input forces the `/` to not be an
// implicit prefix for the second `*`. The generated pattern string
// must similarly prevent the implicit prefix from occuring. This
// is done using the `{}` grouping instead, however, as its a bit more
// readable than escape backslashes.
RunPatternStringTest("*\\/*", "*/{*}");
}
TEST(PatternStringTest, WildcardSlashAndWildcardInGrouping) {
RunPatternStringTest("*/{*}", "*/{*}");
}
TEST(PatternStringTest, WildcardSlashSlashAndWildcard) {
RunPatternStringTest("*//*", "*//*");
}
TEST(
PatternStringTest,
WildcardFollowedByEmptyGroupWithZeroOrMoreModifierAndWildcardWithOptionalModifier) {
RunPatternStringTest("*{}**?", "*(.*)?");
}
TEST(PatternStringTest, CaseFromFuzzer) {
RunPatternStringTest(".:bax\\a*{}**", "{.:bax}a*(.*)");
}
struct DirectMatchCase {
std::string_view input;
bool expected_match = true;
std::vector<std::pair<std::string_view, std::optional<std::string_view>>>
expected_groups;
};
void RunDirectMatchTest(std::string_view input,
std::vector<DirectMatchCase> case_list) {
auto result =
Parse(input, PassThrough,
{.sensitive = true, .strict = true, .end = true, .start = true});
ASSERT_TRUE(result.has_value());
auto& pattern = result.value();
EXPECT_TRUE(pattern.CanDirectMatch());
for (const auto& c : case_list) {
std::vector<std::pair<std::string_view, std::optional<std::string_view>>>
matched_groups;
EXPECT_EQ(c.expected_match, pattern.DirectMatch(c.input, &matched_groups));
ASSERT_EQ(c.expected_groups.size(), matched_groups.size());
for (size_t i = 0; i < matched_groups.size(); ++i) {
EXPECT_EQ(c.expected_groups[i], matched_groups[i]);
}
}
}
void RunDirectMatchUnsupportedTest(std::string_view input,
Options options = {.sensitive = true,
.strict = true,
.end = true,
.start = true}) {
auto result = Parse(input, PassThrough, options);
ASSERT_TRUE(result.has_value());
auto& pattern = result.value();
EXPECT_FALSE(pattern.CanDirectMatch());
}
TEST(PatternDirectMatch, FullWildcardSupported) {
RunDirectMatchTest("*",
{
{.input = "/foo", .expected_groups = {{"0", "/foo"}}},
{.input = "", .expected_groups = {{"0", ""}}},
});
}
TEST(PatternDirectMatch, FullWildcardInGroupSupported) {
RunDirectMatchTest("{*}",
{
{.input = "/foo", .expected_groups = {{"0", "/foo"}}},
{.input = "", .expected_groups = {{"0", ""}}},
});
}
TEST(PatternDirectMatch, FullWildcardUsingRegexSupported) {
RunDirectMatchTest("(.*)",
{
{.input = "/foo", .expected_groups = {{"0", "/foo"}}},
{.input = "", .expected_groups = {{"0", ""}}},
});
}
TEST(PatternDirectMatch, FullWildcardAndOptionalModifierSupported) {
RunDirectMatchTest("*?",
{
{.input = "/foo", .expected_groups = {{"0", "/foo"}}},
{.input = "", .expected_groups = {{"0", ""}}},
});
}
TEST(PatternDirectMatch, FullWildcardAndOneOrMoreModifierSupported) {
RunDirectMatchTest("*+",
{
{.input = "/foo", .expected_groups = {{"0", "/foo"}}},
{.input = "", .expected_groups = {{"0", ""}}},
});
}
TEST(PatternDirectMatch, FullWildcardAndZeroOrMoreModifierSupported) {
RunDirectMatchTest("**",
{
{.input = "/foo", .expected_groups = {{"0", "/foo"}}},
{.input = "", .expected_groups = {{"0", ""}}},
});
}
TEST(PatternDirectMatch, FullWildcardWithNameUsingRegexSupported) {
RunDirectMatchTest(
":name(.*)", {
{.input = "/foo", .expected_groups = {{"name", "/foo"}}},
{.input = "", .expected_groups = {{"name", ""}}},
});
}
TEST(PatternDirectMatch, OptionsFalseStartUnsupported) {
RunDirectMatchUnsupportedTest(
"*", {.sensitive = true, .strict = true, .end = true, .start = false});
}
TEST(PatternDirectMatch, OptionsFalseEndUnsupported) {
RunDirectMatchUnsupportedTest(
"*", {.sensitive = true, .strict = true, .end = false, .start = true});
}
TEST(PatternDirectMatch, OptionsFalseStrictUnsupported) {
RunDirectMatchUnsupportedTest(
"*", {.sensitive = true, .strict = false, .end = true, .start = true});
}
TEST(PatternDirectMatch, OptionsFalseSensitiveUnsupported) {
RunDirectMatchUnsupportedTest(
"*", {.sensitive = false, .strict = true, .end = true, .start = true});
}
TEST(PatternDirectMatch, FullWildcardWithPrefixUnsupported) {
RunDirectMatchUnsupportedTest("/*");
}
TEST(PatternDirectMatch, FullWildcardInGroupWithPrefixUnsupported) {
RunDirectMatchUnsupportedTest("{foo*}");
}
TEST(PatternDirectMatch, FullWildcardInGroupWithSuffixUnsupported) {
RunDirectMatchUnsupportedTest("{*foo}");
}
TEST(PatternDirectMatch, EmptyPatternSupported) {
RunDirectMatchTest("", {
{.input = "", .expected_groups = {}},
{.input = "/", .expected_match = false},
});
}
TEST(PatternDirectMatch, FixedTextSupported) {
RunDirectMatchTest("foo", {
{.input = "foo", .expected_groups = {}},
{.input = "fo", .expected_match = false},
{.input = "foobar", .expected_match = false},
});
}
TEST(PatternDirectMatch, FixedTextInGroupSupported) {
RunDirectMatchTest("{foo}", {
{.input = "foo", .expected_groups = {}},
{.input = "fo", .expected_match = false},
{.input = "foobar", .expected_match = false},
});
}
TEST(PatternDirectMatch, FixedTextInGroupWithOptionalModifierUnsupported) {
RunDirectMatchUnsupportedTest("{foo}?");
}
TEST(PatternDirectMatch, FixedTextInGroupWithZeroOrMoreModifierUnsupported) {
RunDirectMatchUnsupportedTest("{foo}*");
}
TEST(PatternDirectMatch, FixedTextInGroupWithOneOrMoreModifierUnsupported) {
RunDirectMatchUnsupportedTest("{foo}+");
}
TEST(PatternDirectMatch, FixedTextAndFullWildcardUnsupported) {
RunDirectMatchUnsupportedTest("/foo*");
}
TEST(PatternDirectMatch, NamedGroupUnsupported) {
RunDirectMatchUnsupportedTest(":name");
}
TEST(PatternDirectMatch, RegexUnsupported) {
RunDirectMatchUnsupportedTest("(foo)");
}
void ExpectGenerateSuccess(
std::string_view input_pattern,
const std::unordered_map<std::string, std::string>& input_groups,
std::string_view expected_output,
Options options = Options(),
EncodeCallback callback = PassThrough) {
auto parsed = Parse(input_pattern, callback, options);
ASSERT_TRUE(parsed.has_value());
Pattern& pattern = parsed.value();
auto result = pattern.Generate(input_groups, callback);
EXPECT_TRUE(result.has_value());
if (result.has_value()) {
EXPECT_EQ(result.value(), expected_output);
}
}
void ExpectGenerateFailure(
std::string_view input_pattern,
const std::unordered_map<std::string, std::string>& input_groups,
absl::StatusCode expected_error_code,
Options options = Options(),
EncodeCallback callback = PassThrough) {
auto parsed = Parse(input_pattern, callback, options);
ASSERT_TRUE(parsed.has_value());
Pattern& pattern = parsed.value();
auto result = pattern.Generate(input_groups, std::move(callback));
EXPECT_FALSE(result.has_value());
if (!result.has_value()) {
// Tests only error codes, not looking into error messages.
EXPECT_EQ(result.error().code(), expected_error_code);
}
}
TEST(PatternGenerateTest, EmptyPatternSupported) {
ExpectGenerateSuccess("", {}, "");
}
TEST(PatternGenerateTest, FixedTextSupported) {
ExpectGenerateSuccess("foo", {}, "foo");
}
TEST(PatternGenerateTest, FixedTextInGroupSupported) {
ExpectGenerateSuccess("{foo}", {}, "foo");
}
TEST(PatternGenerateTest, ModifiersUnimplemented) {
ExpectGenerateFailure("{foo}?", {}, absl::StatusCode::kUnimplemented);
ExpectGenerateFailure("{foo}*", {}, absl::StatusCode::kUnimplemented);
ExpectGenerateFailure("{foo}+", {}, absl::StatusCode::kUnimplemented);
}
TEST(PatternGenerateTest, SingleNamedGroupSupported) {
ExpectGenerateSuccess(":foo", {{"foo", "bar"}}, "bar");
}
TEST(PatternGenerateTest, SingleNamedGroupWithPresixSupported) {
ExpectGenerateSuccess("foo:bar", {{"bar", "baz"}}, "foobaz");
}
TEST(PatternGenerateTest, EncodeCallbackUsed) {
ExpectGenerateSuccess("foo:bar", {{"bar", "baz"}}, "FOOBAZ", Options(),
ToUpper);
}
TEST(PatternGenerateTest, MultipleNamedGroupsSupported) {
ExpectGenerateSuccess(":foo/:bar", {{"foo", "baz"}, {"bar", "qux"}},
"baz/qux");
}
TEST(PatternGnerateTest, FailsIfInputNotProvided) {
ExpectGenerateFailure(":foo", {}, absl::StatusCode::kInvalidArgument);
}
TEST(PatternGenerateTest, RegexpUnimplemented) {
ExpectGenerateFailure("(foo)", {}, absl::StatusCode::kUnimplemented);
}
TEST(PatternGenerateTest, FullWildcardUnimplemented) {
ExpectGenerateFailure("*", {}, absl::StatusCode::kUnimplemented);
}
TEST(PatternGenerateTest, UnnamedSegmentWildcardUnimplemented) {
ExpectGenerateFailure("([^\\/]+?)", {}, absl::StatusCode::kUnimplemented,
{.delimiter_list = "/"});
}
TEST(PatternGenerateTest, InputWithDelimiterCharsUnimplemented) {
ExpectGenerateFailure(":foo", {{"foo", "bar/baz"}},
absl::StatusCode::kUnimplemented,
{.delimiter_list = "/"});
}
} // namespace liburlpattern
|