aboutsummaryrefslogtreecommitdiff
path: root/test/stflrichtext.cpp
blob: f0f9e530f14f5ae17b2dc024c71819cb8465d620 (plain) (blame)
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
#include "stflrichtext.h"

#include "3rd-party/catch.hpp"

#include <map>
#include <string>
#include <vector>

using namespace newsboat;

TEST_CASE("Constructing StflRichText from plaintext string does not make changes",
	"[StflRichText]")
{
	const std::vector<std::string> test_strings {
		"",
		"foo",
		"<tag>",
		">test",
		"<test",
		"test<",
		"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<std::string, std::string> input_vs_plaintext {
		{ "", ""},
		{ "normal text", "normal text" },
		{ "<tag>text</>", "text" },
		{ "abc <tag>text</> def", "abc text def" },
		{ "escaped <>bracket>", "escaped <bracket>" },
		{ "just end tag </> without opening tag", "just end tag  without opening tag" },
		{ "no <color> 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("text<a>abc<b>def</> and remainder");
	const std::string newTag = "<tag>";

	SECTION("complete string overrides all existing tags") {
		richtext.apply_style_tag(newTag, 0, 24);
		REQUIRE(richtext.stfl_quoted() == "<tag>textabcdef and remainder</>");
	}

	SECTION("empty range is ignored") {
		richtext.apply_style_tag(newTag, 10, 10);
		REQUIRE(richtext.stfl_quoted() == "text<a>abc<b>def</> and remainder");
	}

	SECTION("invalid range is ignored") {
		richtext.apply_style_tag(newTag, 10, 9);
		REQUIRE(richtext.stfl_quoted() == "text<a>abc<b>def</> and remainder");
	}

	SECTION("exact overlap with existing tag ranges") {
		SECTION("a") {
			richtext.apply_style_tag(newTag, 4, 7);
			REQUIRE(richtext.stfl_quoted() == "text<tag>abc<b>def</> and remainder");
		}

		SECTION("b") {
			richtext.apply_style_tag(newTag, 7, 10);
			REQUIRE(richtext.stfl_quoted() == "text<a>abc<tag>def</> and remainder");
		}

		SECTION("a+b") {
			richtext.apply_style_tag(newTag, 4, 10);
			REQUIRE(richtext.stfl_quoted() == "text<tag>abcdef</> and remainder");
		}
	}

	SECTION("overlap with start of existing range") {
		richtext.apply_style_tag(newTag, 0, 6);
		REQUIRE(richtext.stfl_quoted() == "<tag>textab<a>c<b>def</> and remainder");
	}

	SECTION("overlap with end of existing range") {
		richtext.apply_style_tag(newTag, 8, 14);
		REQUIRE(richtext.stfl_quoted() == "text<a>abc<b>d<tag>ef and</> remainder");
	}
}