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 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
|
#include "regexmanager.h"
#include "3rd-party/catch.hpp"
#include "confighandlerexception.h"
#include "matchable.h"
#include "stflrichtext.h"
using namespace newsboat;
TEST_CASE("RegexManager throws on invalid command", "[RegexManager]")
{
RegexManager rxman;
SECTION("on invalid command") {
std::vector<std::string> params;
REQUIRE_THROWS_AS(
rxman.handle_action("an-invalid-command", params),
ConfigHandlerException);
}
}
TEST_CASE("RegexManager throws on invalid `highlight' definition",
"[RegexManager]")
{
RegexManager rxman;
std::vector<std::string> params;
SECTION("on `highlight' without parameters") {
REQUIRE_THROWS_AS(rxman.handle_action("highlight", params),
ConfigHandlerException);
}
SECTION("on invalid location") {
params = {"invalidloc", "foo", "blue", "red"};
REQUIRE_THROWS_AS(rxman.handle_action("highlight", params),
ConfigHandlerException);
}
SECTION("on invalid regex") {
params = {"feedlist", "*", "blue", "red"};
REQUIRE_THROWS_AS(rxman.handle_action("highlight", params),
ConfigHandlerException);
}
}
TEST_CASE("RegexManager doesn't throw on valid `highlight' definition",
"[RegexManager]")
{
RegexManager rxman;
std::vector<std::string> params;
params = {"articlelist", "foo", "blue", "red"};
REQUIRE_NOTHROW(rxman.handle_action("highlight", params));
params = {"feedlist", "foo", "blue", "red"};
REQUIRE_NOTHROW(rxman.handle_action("highlight", params));
params = {"feedlist", "fbo", "blue", "red", "bold", "underline"};
REQUIRE_NOTHROW(rxman.handle_action("highlight", params));
params = {"all", "fba", "blue", "red", "bold", "underline"};
REQUIRE_NOTHROW(rxman.handle_action("highlight", params));
}
TEST_CASE("RegexManager highlights according to definition", "[RegexManager]")
{
RegexManager rxman;
SECTION("In articlelist") {
rxman.handle_action("highlight", {"articlelist", "foo", "blue", "red"});
auto input = StflRichText::from_plaintext("xfoox");
rxman.quote_and_highlight(input, "articlelist");
REQUIRE(input.stfl_quoted() == "x<0>foo</>x");
}
SECTION("In feedlist") {
rxman.handle_action("highlight", {"feedlist", "foo", "blue", "red"});
auto input = StflRichText::from_plaintext("yfooy");
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == "y<0>foo</>y");
}
}
TEST_CASE("quote_and_highlight() only matches `^` at the start of the line",
"[RegexManager]")
{
RegexManager rxman;
auto input = StflRichText::from_quoted("This is a test");
SECTION("^.") {
rxman.handle_action("highlight", {"article", "^.", "blue", "red"});
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == "<0>T</>his is a test");
}
SECTION("(^Th|^is)") {
rxman.handle_action("highlight", {"article", "(^Th|^is)", "blue", "red"});
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == "<0>Th</>is is a test");
}
}
// This test might seem totally out of the blue, but we do need it: as of this
// writing, `highlight-article` add a nullptr for a regex to "articlelist"
// context, which would've crashed the program if it were to be dereferenced.
// This test checks that those nullptrs are skipped.
TEST_CASE("RegexManager::quote_and_highlight works fine even if there were "
"`highlight-article` commands",
"[RegexManager]")
{
RegexManager rxman;
rxman.handle_action("highlight", {"articlelist", "foo", "blue", "red"});
rxman.handle_action("highlight-article", {"title==\"\"", "blue", "red"});
auto input = StflRichText::from_plaintext("xfoox");
rxman.quote_and_highlight(input, "articlelist");
REQUIRE(input.stfl_quoted() == "x<0>foo</>x");
}
TEST_CASE("RegexManager preserves text when there's nothing to highlight",
"[RegexManager]")
{
RegexManager rxman;
auto input = StflRichText::from_plaintext("xbarx");
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == "xbarx");
input = StflRichText::from_quoted("a<b>");
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == "a<b>");
SECTION("encode `<` as `<>` for stfl") {
input = StflRichText::from_plaintext("<");
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == "<>");
}
}
TEST_CASE("`highlight all` adds rules for all locations", "[RegexManager]")
{
RegexManager rxman;
std::vector<std::string> params = {"all", "foo", "red"};
REQUIRE_NOTHROW(rxman.handle_action("highlight", params));
auto input = StflRichText::from_plaintext("xxfooyy");
for (auto location : {
"article", "articlelist", "feedlist"
}) {
SECTION(location) {
rxman.quote_and_highlight(input, location);
REQUIRE(input.stfl_quoted() == "xx<0>foo</>yy");
}
}
}
TEST_CASE("RegexManager does not hang on regexes that can match empty strings",
"[RegexManager]")
{
RegexManager rxman;
auto input =
StflRichText::from_plaintext("The quick brown fox jumps over the lazy dog");
rxman.handle_action("highlight", {"feedlist", "w*", "blue", "red"});
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == "The quick bro<0>w</>n fox jumps over the lazy dog");
}
TEST_CASE("RegexManager does not hang on regexes that match empty strings",
"[RegexManager]")
{
RegexManager rxman;
auto input =
StflRichText::from_plaintext("The quick brown fox jumps over the lazy dog");
const std::string compare = input.plaintext();
SECTION("testing end of line empty.") {
rxman.handle_action("highlight", {"feedlist", "$", "blue", "red"});
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == compare);
}
SECTION("testing beginning of line empty") {
rxman.handle_action("highlight", {"feedlist", "^", "blue", "red"});
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == compare);
}
SECTION("testing empty line") {
rxman.handle_action("highlight", {"feedlist", "^$", "blue", "red"});
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == compare);
}
}
TEST_CASE("quote_and_highlight wraps highlighted text in numbered tags",
"[RegexManager]")
{
RegexManager rxman;
auto input =
StflRichText::from_plaintext("The quick brown fox jumps over the lazy dog");
SECTION("Beginning of line match first") {
const std::string output =
"<0>The</> quick <1>brown</> fox jumps over <0>the</> lazy dog";
rxman.handle_action("highlight", {"article", "the", "red"});
rxman.handle_action("highlight", {"article", "brown", "blue"});
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
SECTION("Beginning of line match second") {
const std::string output =
"<1>The</> quick <0>brown</> fox jumps over <1>the</> lazy dog";
rxman.handle_action("highlight", {"article", "brown", "blue"});
rxman.handle_action("highlight", {"article", "the", "red"});
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
SECTION("2 non-overlapping highlights") {
const std::string output =
"The <0>quick</> <1>brown</> fox jumps over the lazy dog";
rxman.handle_action("highlight", {"article", "quick", "red"});
rxman.handle_action("highlight", {"article", "brown", "blue"});
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
TEST_CASE("RegexManager::dump_config turns each `highlight`, "
"`highlight-article` and `highlight-feed` rule into a string, and appends "
"them onto a vector",
"[RegexManager]")
{
RegexManager rxman;
std::vector<std::string> result;
SECTION("Empty object returns empty vector") {
REQUIRE_NOTHROW(rxman.dump_config(result));
REQUIRE(result.empty());
}
SECTION("One rule") {
rxman.handle_action("highlight", {"article", "this test is", "green"});
REQUIRE_NOTHROW(rxman.dump_config(result));
REQUIRE(result.size() == 1);
REQUIRE(result[0] == R"#(highlight "article" "this test is" "green")#");
}
SECTION("Two rules, one of them a `highlight-article`") {
rxman.handle_action("highlight", {"all", "keywords", "red", "blue"});
rxman.handle_action(
"highlight-article",
{"title==\"\"", "green", "black"});
REQUIRE_NOTHROW(rxman.dump_config(result));
REQUIRE(result.size() == 2);
REQUIRE(result[0] == R"#(highlight "all" "keywords" "red" "blue")#");
REQUIRE(result[1] == R"#(highlight-article "title==\"\"" "green" "black")#");
}
SECTION("Three rules") {
rxman.handle_action("highlight", {"all", "keywords", "red", "blue"});
rxman.handle_action(
"highlight-article",
{"title==\"\"", "green", "black"});
rxman.handle_action(
"highlight-feed",
{"title==\"\"", "red", "black"});
REQUIRE_NOTHROW(rxman.dump_config(result));
REQUIRE(result.size() == 3);
REQUIRE(result[0] == R"#(highlight "all" "keywords" "red" "blue")#");
REQUIRE(result[1] == R"#(highlight-article "title==\"\"" "green" "black")#");
REQUIRE(result[2] == R"#(highlight-feed "title==\"\"" "red" "black")#");
}
}
TEST_CASE("RegexManager::dump_config appends to given vector", "[RegexManager]")
{
RegexManager rxman;
std::vector<std::string> result;
result.emplace_back("sentinel");
rxman.handle_action("highlight", {"all", "this", "black"});
REQUIRE_NOTHROW(rxman.dump_config(result));
REQUIRE(result.size() == 2);
REQUIRE(result[0] == "sentinel");
REQUIRE(result[1] == R"#(highlight "all" "this" "black")#");
}
TEST_CASE("RegexManager::handle_action throws ConfigHandlerException "
"on invalid foreground color",
"[RegexManager]")
{
RegexManager rxman;
REQUIRE_THROWS_AS(
rxman.handle_action("highlight", {"all", "keyword", "whatever"}),
ConfigHandlerException);
REQUIRE_THROWS_AS(
rxman.handle_action("highlight", {"feedlist", "keyword", ""}),
ConfigHandlerException);
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight-article",
{"author == \"\"", "whatever", "white"}),
ConfigHandlerException);
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight-article",
{"title =~ \"k\"", "", "white"}),
ConfigHandlerException);
}
TEST_CASE("RegexManager::handle_action throws ConfigHandlerException "
"on invalid background color",
"[RegexManager]")
{
RegexManager rxman;
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight",
{"all", "keyword", "red", "whatever"}),
ConfigHandlerException);
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight",
{"feedlist", "keyword", "green", ""}),
ConfigHandlerException);
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight-article",
{"title == \"keyword\"", "red", "whatever"}),
ConfigHandlerException);
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight-article",
{"content == \"\"", "green", ""}),
ConfigHandlerException);
}
TEST_CASE("RegexManager::handle_action throws ConfigHandlerException "
"on invalid attribute",
"[RegexManager]")
{
RegexManager rxman;
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight",
{"all", "keyword", "red", "green", "sparkles"}),
ConfigHandlerException);
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight",
{"feedlist", "keyword", "green", "red", ""}),
ConfigHandlerException);
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight-article",
{"title==\"\"", "red", "green", "sparkles"}),
ConfigHandlerException);
REQUIRE_THROWS_AS(
rxman.handle_action(
"highlight-article",
{"title==\"\"", "green", "red", ""}),
ConfigHandlerException);
}
TEST_CASE("RegexManager throws on invalid `highlight-article' definition",
"[RegexManager]")
{
RegexManager rxman;
std::vector<std::string> params;
SECTION("on `highlight-article' without parameters") {
REQUIRE_THROWS_AS(rxman.handle_action("highlight-article", params),
ConfigHandlerException);
}
SECTION("on invalid filter expression") {
params = {"a = b", "red", "green"};
REQUIRE_THROWS_AS(rxman.handle_action("highlight-article", params),
ConfigHandlerException);
}
SECTION("on missing colors") {
params = {"title==\"\""};
REQUIRE_THROWS_AS(rxman.handle_action("highlight-article", params),
ConfigHandlerException);
}
SECTION("on missing background color") {
params = {"title==\"\"", "white"};
REQUIRE_THROWS_AS(rxman.handle_action("highlight-article", params),
ConfigHandlerException);
}
}
TEST_CASE("RegexManager doesn't throw on valid `highlight-article' definition",
"[RegexManager]")
{
RegexManager rxman;
std::vector<std::string> params;
params = {"title == \"\"", "blue", "red"};
REQUIRE_NOTHROW(rxman.handle_action("highlight-article", params));
params = {"content =~ \"keyword\"", "blue", "red"};
REQUIRE_NOTHROW(rxman.handle_action("highlight-article", params));
params = {"unread == \"yes\"", "blue", "red", "bold", "underline"};
REQUIRE_NOTHROW(rxman.handle_action("highlight-article", params));
params = {"age > 3", "blue", "red", "bold", "underline"};
REQUIRE_NOTHROW(rxman.handle_action("highlight-article", params));
}
TEST_CASE("RegexManager throws on invalid `highlight-feed' definition",
"[RegexManager]")
{
RegexManager rxman;
std::vector<std::string> params;
SECTION("on `highlight-feed' without parameters") {
REQUIRE_THROWS_AS(rxman.handle_action("highlight-feed", params),
ConfigHandlerException);
}
SECTION("on invalid filter expression") {
params = {"a = b", "red", "green"};
REQUIRE_THROWS_AS(rxman.handle_action("highlight-feed", params),
ConfigHandlerException);
}
SECTION("on missing colors") {
params = {"title==\"\""};
REQUIRE_THROWS_AS(rxman.handle_action("highlight-feed", params),
ConfigHandlerException);
}
SECTION("on missing background color") {
params = {"title==\"\"", "white"};
REQUIRE_THROWS_AS(rxman.handle_action("highlight-feed", params),
ConfigHandlerException);
}
}
TEST_CASE("RegexManager doesn't throw on valid `highlight-feed' definition",
"[RegexManager]")
{
RegexManager rxman;
std::vector<std::string> params;
params = {"title == \"\"", "blue", "red"};
REQUIRE_NOTHROW(rxman.handle_action("highlight-feed", params));
params = {"content =~ \"keyword\"", "blue", "red"};
REQUIRE_NOTHROW(rxman.handle_action("highlight-feed", params));
params = {"unread == \"yes\"", "blue", "red", "bold", "underline"};
REQUIRE_NOTHROW(rxman.handle_action("highlight-feed", params));
params = {"age > 3", "blue", "red", "bold", "underline"};
REQUIRE_NOTHROW(rxman.handle_action("highlight-feed", params));
}
struct RegexManagerMockMatchable : public Matchable {
public:
nonstd::optional<std::string> attribute_value(const std::string& attribname)
const override
{
if (attribname == "attr") {
return "val";
}
return nonstd::nullopt;
}
};
TEST_CASE("RegexManager::article_matches returns position of the Matcher "
"that matches a given Matchable",
"[RegexManager]")
{
RegexManager rxman;
RegexManagerMockMatchable mock;
const auto cmd = std::string("highlight-article");
SECTION("Just one rule") {
rxman.handle_action(cmd, {"attr != \"hello\"", "red", "green"});
REQUIRE(rxman.article_matches(&mock) == 0);
}
SECTION("Couple rules") {
rxman.handle_action(cmd, {"attr != \"val\"", "green", "white"});
rxman.handle_action(cmd, {"attr # \"entry\"", "green", "white"});
rxman.handle_action(cmd, {"attr == \"val\"", "green", "white"});
rxman.handle_action(cmd, {"attr =~ \"hello\"", "green", "white"});
REQUIRE(rxman.article_matches(&mock) == 2);
}
}
TEST_CASE("RegexManager::article_matches returns -1 if there are no Matcher "
"to match a given Matchable",
"[RegexManager]")
{
RegexManager rxman;
RegexManagerMockMatchable mock;
const auto cmd = std::string("highlight-article");
SECTION("No rules") {
REQUIRE(rxman.article_matches(&mock) == -1);
}
SECTION("Just one rule") {
rxman.handle_action(cmd, {"attr == \"hello\"", "red", "green"});
REQUIRE(rxman.article_matches(&mock) == -1);
}
SECTION("Couple rules") {
rxman.handle_action(cmd, {"attr != \"val\"", "green", "white"});
rxman.handle_action(cmd, {"attr # \"entry\"", "green", "white"});
rxman.handle_action(cmd, {"attr =~ \"hello\"", "green", "white"});
REQUIRE(rxman.article_matches(&mock) == -1);
}
}
TEST_CASE("RegexManager::feed_matches returns position of the Matcher "
"that matches a given Matchable",
"[RegexManager]")
{
RegexManager rxman;
RegexManagerMockMatchable mock;
const auto cmd = std::string("highlight-feed");
SECTION("Just one rule") {
rxman.handle_action(cmd, {"attr != \"hello\"", "red", "green"});
REQUIRE(rxman.feed_matches(&mock) == 0);
}
SECTION("Couple rules") {
rxman.handle_action(cmd, {"attr != \"val\"", "green", "white"});
rxman.handle_action(cmd, {"attr # \"entry\"", "green", "white"});
rxman.handle_action(cmd, {"attr == \"val\"", "green", "white"});
rxman.handle_action(cmd, {"attr =~ \"hello\"", "green", "white"});
REQUIRE(rxman.feed_matches(&mock) == 2);
}
}
TEST_CASE("RegexManager::feed_matches returns -1 if there are no Matcher "
"to match a given Matchable",
"[RegexManager]")
{
RegexManager rxman;
RegexManagerMockMatchable mock;
const auto cmd = std::string("highlight-feed");
SECTION("No rules") {
REQUIRE(rxman.feed_matches(&mock) == -1);
}
SECTION("Just one rule") {
rxman.handle_action(cmd, {"attr == \"hello\"", "red", "green"});
REQUIRE(rxman.feed_matches(&mock) == -1);
}
SECTION("Couple rules") {
rxman.handle_action(cmd, {"attr != \"val\"", "green", "white"});
rxman.handle_action(cmd, {"attr # \"entry\"", "green", "white"});
rxman.handle_action(cmd, {"attr =~ \"hello\"", "green", "white"});
REQUIRE(rxman.feed_matches(&mock) == -1);
}
}
TEST_CASE("RegexManager::remove_last_regex removes last added `highlight` rule",
"[RegexManager]")
{
RegexManager rxman;
rxman.handle_action("highlight", {"articlelist", "foo", "blue", "red"});
rxman.handle_action("highlight", {"articlelist", "bar", "blue", "red"});
const auto INPUT = std::string("xfoobarx");
auto input = StflRichText::from_plaintext(INPUT);
rxman.quote_and_highlight(input, "articlelist");
REQUIRE(input.stfl_quoted() == "x<0>foo<1>bar</>x");
input = StflRichText::from_plaintext(INPUT);
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == INPUT);
REQUIRE_NOTHROW(rxman.remove_last_regex("articlelist"));
input = StflRichText::from_plaintext(INPUT);
rxman.quote_and_highlight(input, "articlelist");
REQUIRE(input.stfl_quoted() == "x<0>foo</>barx");
input = StflRichText::from_plaintext(INPUT);
rxman.quote_and_highlight(input, "feedlist");
REQUIRE(input.stfl_quoted() == INPUT);
}
TEST_CASE("RegexManager::remove_last_regex does not crash if there are "
"no regexes to remove",
"[RegexManager]")
{
RegexManager rxman;
SECTION("No rules existed") {
rxman.remove_last_regex("articlelist");
SECTION("Repeated calls don't crash either") {
rxman.remove_last_regex("articlelist");
rxman.remove_last_regex("articlelist");
rxman.remove_last_regex("articlelist");
rxman.remove_last_regex("feedlist");
}
}
SECTION("A few rules were added and then deleted") {
rxman.handle_action("highlight", {"articlelist", "test test", "red"});
rxman.handle_action("highlight", {"articlelist", "another test", "red"});
rxman.handle_action("highlight", {"articlelist", "more", "green", "blue"});
rxman.remove_last_regex("articlelist");
rxman.remove_last_regex("articlelist");
rxman.remove_last_regex("articlelist");
// At this point, all the rules are removed
rxman.remove_last_regex("articlelist");
SECTION("Repeated calls don't crash either") {
rxman.remove_last_regex("articlelist");
rxman.remove_last_regex("articlelist");
rxman.remove_last_regex("articlelist");
rxman.remove_last_regex("feedlist");
}
}
}
TEST_CASE("RegexManager uses POSIX extended regex syntax",
"[RegexManager]")
{
// This syntax is documented in The Open Group Base Specifications Issue 7,
// IEEE Std 1003.1-2008 Section 9, "Regular Expressions":
// https://pubs.opengroup.org/onlinepubs/9699919799.2008edition/basedefs/V1_chap09.html
// Since POSIX extended regular expressions are pretty basic, it's hard to
// find stuff that they support but other engines don't. So in order to
// ensure that we're using EREs, these tests try stuff that's *not*
// supported by EREs.
//
// FreeBSD 13 returns an error from regexec() instead of failing the match
// like other OSes do. That's why we wrap our tests in a try-catch.
//
// Ideas gleaned from https://www.regular-expressions.info/refcharacters.html
RegexManager rxman;
// Supported by Perl, PCRE, PHP and others
SECTION("No support for escape sequence") {
try {
rxman.handle_action("highlight", {"articlelist", R"#(\Q*]+\E)#", "red"});
} catch (const ConfigHandlerException& e) {
const std::string expected(
R"#(`\Q*]+\E' is not a valid regular expression: trailing backslash (\))#");
REQUIRE(e.what() == expected);
}
auto input = StflRichText::from_plaintext("*]+");
rxman.quote_and_highlight(input, "articlelist");
REQUIRE(input.stfl_quoted() == "*]+");
}
SECTION("No support for hexadecimal escape") {
try {
rxman.handle_action("highlight", {"articlelist", R"#(^va\x6Cue)#", "red"});
} catch (const ConfigHandlerException& e) {
const std::string expected(
R"#(`^va\x6Cue' is not a valid regular expression: trailing backslash (\))#");
REQUIRE(e.what() == expected);
}
auto input = StflRichText::from_plaintext("value");
rxman.quote_and_highlight(input, "articlelist");
REQUIRE(input.stfl_quoted() == "value");
}
SECTION("No support for \\a as alert/bell control character") {
try {
rxman.handle_action("highlight", {"articlelist", R"#(\a)#", "red"});
} catch (const ConfigHandlerException& e) {
const std::string expected(
R"#(`\a' is not a valid regular expression: trailing backslash (\))#");
REQUIRE(e.what() == expected);
}
auto input = StflRichText::from_plaintext("\x07");
rxman.quote_and_highlight(input, "articlelist");
REQUIRE(input.stfl_quoted() == "\x07");
}
SECTION("No support for \\b as backspace control character") {
try {
rxman.handle_action("highlight", {"articlelist", R"#(\b)#", "red"});
} catch (const ConfigHandlerException& e) {
const std::string expected(
R"#(`\b' is not a valid regular expression: trailing backslash (\))#");
REQUIRE(e.what() == expected);
}
auto input = StflRichText::from_plaintext("\x08");
rxman.quote_and_highlight(input, "articlelist");
REQUIRE(input.stfl_quoted() == "\x08");
}
// If you add more checks to this test, consider adding the same to Matcher tests
}
TEST_CASE("quote_and_highlight() does not break existing tags like <unread>",
"[RegexManager]")
{
RegexManager rxman;
auto input = StflRichText::from_quoted("<unread>This entry is unread</>");
WHEN("matching `read`") {
const std::string output = "<unread>This entry is un<0>read</>";
rxman.handle_action("highlight", {"article", "read", "red"});
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
WHEN("matching `unread`") {
const std::string output = "<unread>This entry is <0>unread</>";
rxman.handle_action("highlight", {"article", "unread", "red"});
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
WHEN("matching the full line") {
rxman.handle_action("highlight", {"article", "^.*$", "red"});
THEN("the <unread> tag is overwritten") {
const std::string output = "<0>This entry is unread</>";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
}
TEST_CASE("quote_and_highlight() ignores tags when matching the regular expressions",
"[RegexManager]")
{
RegexManager rxman;
auto input = StflRichText::from_quoted("<unread>This entry is unread</>");
WHEN("matching text at the start of the line") {
rxman.handle_action("highlight", {"article", "^This", "red"});
THEN("the <unread> tag should be ignored") {
const std::string output = "<0>This<unread> entry is unread</>";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
WHEN("matching text at the end of the line") {
rxman.handle_action("highlight", {"article", "unread$", "red"});
THEN("the closing tag `</>` (related to <unread>) should be ignored") {
const std::string output = "<unread>This entry is <0>unread</>";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
WHEN("matching text which contains `<u>...</>` markers (added by HTML renderer)") {
input = StflRichText::from_quoted("Some<u>thing</> is underlined");
rxman.handle_action("highlight", {"article", "Something", "red"});
THEN("the tags should be ignored when matching text with a regular expression") {
const std::string output = "<0>Something</> is underlined";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
}
TEST_CASE("quote_and_highlight() generates a sensible output when multiple matches overlap",
"[RegexManager]")
{
RegexManager rxman;
auto input =
StflRichText::from_plaintext("The quick brown fox jumps over the lazy dog");
WHEN("a second match is completely inside of the first match") {
rxman.handle_action("highlight", {"article", "The quick brown", "red"});
rxman.handle_action("highlight", {"article", "quick", "red"});
THEN("the first style should be restored at the end of the second match") {
const std::string output =
"<0>The <1>quick<0> brown</> fox jumps over the lazy dog";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
WHEN("a second match completely encloses the first match") {
rxman.handle_action("highlight", {"article", "quick", "red"});
rxman.handle_action("highlight", {"article", "The quick brown", "red"});
THEN("the first style should not be present anymore") {
const std::string output =
"<1>The quick brown</> fox jumps over the lazy dog";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
WHEN("a second match starts somewhere in the first match") {
rxman.handle_action("highlight", {"article", "quick brown", "red"});
rxman.handle_action("highlight", {"article", "brown fox", "red"});
THEN("the first style should only be overwritten for the part where the matches intersect") {
const std::string output =
"The <0>quick <1>brown fox</> jumps over the lazy dog";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
WHEN("a second match ends somewhere in the first match") {
rxman.handle_action("highlight", {"article", "brown fox", "red"});
rxman.handle_action("highlight", {"article", "quick brown", "red"});
THEN("the first style should only be overwritten for the part where the matches intersect") {
const std::string output =
"The <1>quick brown<0> fox</> jumps over the lazy dog";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
WHEN("there are three overlapping matches") {
rxman.handle_action("highlight", {"article", "quick.*dog", "red"});
rxman.handle_action("highlight", {"article", "fox jumps over", "red"});
rxman.handle_action("highlight", {"article", "brown fox", "red"});
THEN("newer matches overwrite older matches") {
const std::string output =
"The <0>quick <2>brown fox<1> jumps over<0> the lazy dog</>";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
}
TEST_CASE("quote_and_highlight() keeps stfl-encoded angle brackets and allows matching them directly",
"[RegexManager]")
{
RegexManager rxman;
auto input = StflRichText::from_quoted("<unread>title with <>literal> angle brackets</>");
SECTION("stfl-encoded angle brackets are kept/restored") {
const std::string output = "<unread>title with <>literal> angle brackets</>";
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
SECTION("angle brackets can be matched directly") {
const std::string output =
"<unread>title with <0><>literal><unread> angle brackets</>";
rxman.handle_action("highlight", {"article", "<literal>", "red"});
rxman.quote_and_highlight(input, "article");
REQUIRE(input.stfl_quoted() == output);
}
}
|