#include "stflrichtext.h" #include "3rd-party/catch.hpp" #include #include #include using namespace newsboat; TEST_CASE("Constructing StflRichText from plaintext string does not make changes", "[StflRichText]") { const std::vector test_strings { "", "foo", "", ">test", "", }; for (const auto& input : test_strings) { DYNAMIC_SECTION("plaintext: " << input) { const auto stflString = StflRichText::from_plaintext(input); REQUIRE(stflString.plaintext() == input); } } } TEST_CASE("Constructing StflRichText from quoted string splits tags from plaintext", "[StflRichText]") { const std::map input_vs_plaintext { { "", ""}, { "normal text", "normal text" }, { "text", "text" }, { "abc text def", "abc text def" }, { "escaped <>bracket>", "escaped " }, { "just end tag without opening tag", "just end tag without opening tag" }, { "no tag", "no tag" }, }; for (const auto& kv : input_vs_plaintext) { const auto& input = kv.first; const auto& expected_plaintext = kv.second; DYNAMIC_SECTION("plaintext: " << input) { const auto stflString = StflRichText::from_quoted(input); REQUIRE(stflString.plaintext() == expected_plaintext); } } } TEST_CASE("Apply overlapping style tags", "[StflRichText]") { // Indices: // start: 0 // a: 4 // b: 7 // /: 10 // end: 24 auto richtext = StflRichText::from_quoted("textabcdef and remainder"); const std::string newTag = ""; SECTION("complete string overrides all existing tags") { richtext.apply_style_tag(newTag, 0, 24); REQUIRE(richtext.stfl_quoted() == "textabcdef and remainder"); } SECTION("empty range is ignored") { richtext.apply_style_tag(newTag, 10, 10); REQUIRE(richtext.stfl_quoted() == "textabcdef and remainder"); } SECTION("invalid range is ignored") { richtext.apply_style_tag(newTag, 10, 9); REQUIRE(richtext.stfl_quoted() == "textabcdef and remainder"); } SECTION("exact overlap with existing tag ranges") { SECTION("a") { richtext.apply_style_tag(newTag, 4, 7); REQUIRE(richtext.stfl_quoted() == "textabcdef and remainder"); } SECTION("b") { richtext.apply_style_tag(newTag, 7, 10); REQUIRE(richtext.stfl_quoted() == "textabcdef and remainder"); } SECTION("a+b") { richtext.apply_style_tag(newTag, 4, 10); REQUIRE(richtext.stfl_quoted() == "textabcdef and remainder"); } } SECTION("overlap with start of existing range") { richtext.apply_style_tag(newTag, 0, 6); REQUIRE(richtext.stfl_quoted() == "textabcdef and remainder"); } SECTION("overlap with end of existing range") { richtext.apply_style_tag(newTag, 8, 14); REQUIRE(richtext.stfl_quoted() == "textabcdef and remainder"); } }