summaryrefslogtreecommitdiff
path: root/test/filtercontainer.cpp
blob: 780100b1fba6f3f1f265043322ffff8c56d69514 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "filtercontainer.h"

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

using namespace newsboat;

TEST_CASE("FilterContainer::handle_action handles `define-filter`",
	"[FilterContainer]")
{
	FilterContainer filters;

	const auto action = "define-filter";

	SECTION("Throws ConfigHandlerException if less than 2 parameters") {
		REQUIRE_THROWS_AS(filters.handle_action(action, {}), ConfigHandlerException);
		REQUIRE_THROWS_AS(filters.handle_action(action, {"param1"}),
			ConfigHandlerException);
	}

	SECTION("Throws ConfigHandlerException if second param can't be parsed as filter expression") {
		REQUIRE_THROWS_AS(filters.handle_action(action, {"filter1", "!?"}),
			ConfigHandlerException);
	}

	SECTION("Doesn't throw and increases size after correct `define-filter`") {
		REQUIRE(filters.size() == 0);
		REQUIRE_NOTHROW(filters.handle_action(action, {"filter1", "title = \"Updates\""}));
		REQUIRE(filters.size() == 1);
		REQUIRE_NOTHROW(filters.handle_action(action, {"filter2", "author =~ \"\\s*Doe$\""}));
		REQUIRE(filters.size() == 2);
	}
}

TEST_CASE("FilterContainer allows two filters to have the same name",
	"[FilterContainer]")
{
	FilterContainer filters;

	const auto action = "define-filter";
	const auto filter_name = "arbitrary name";

	REQUIRE_NOTHROW(filters.handle_action(action, {filter_name, "title = \"Updates\""}));
	REQUIRE_NOTHROW(filters.handle_action(action, {filter_name, "author != \"Jon Doe\""}));

	REQUIRE(filters.size() == 2);
}

TEST_CASE("FilterContainer allows the same filter to have two different names",
	"[FilterContainer]")
{
	FilterContainer filters;

	const auto action = "define-filter";
	const auto filter = "title = \"Whatever, really\"";

	REQUIRE_NOTHROW(filters.handle_action(action, {"first filter", filter}));
	REQUIRE_NOTHROW(filters.handle_action(action, {"second filter", filter}));

	REQUIRE(filters.size() == 2);
}

TEST_CASE("FilterContainer::handle_action throws ConfigHandlerException on unknown command",
	"[FilterContainer]")
{
	FilterContainer filters;

	REQUIRE_THROWS_AS(filters.handle_action("bind-key", {"ESC", "quit"}),
		ConfigHandlerException);
	REQUIRE_THROWS_AS(filters.handle_action("reset-unread-on-update", {"url1", "https://example.com/feed.xml"}),
		ConfigHandlerException);
}

TEST_CASE("FilterContainer::get_filters() gives direct access to stored filters",
	"[FilterContainer]")
{
	FilterContainer filters;

	const auto action = "define-filter";
	const auto name1 = "filter1";
	const auto filter1 =  "title == \"Best\"";
	const auto name2 = "other";
	const auto filter2 =  "author # \"Ted\"";

	filters.handle_action(action, {name1, filter1});
	filters.handle_action(action, {name2, filter2});

	REQUIRE(filters.get_filters().size() == 2);
	REQUIRE(filters.get_filters()[0].name == name1);
	REQUIRE(filters.get_filters()[0].expr == filter1);
	REQUIRE(filters.get_filters()[1].name == name2);
	REQUIRE(filters.get_filters()[1].expr == filter2);

	filters.get_filters().clear();
	REQUIRE(filters.size() == 0);
}

TEST_CASE("FilterContainer::dump_config() writes out all configured settings to the provided vector",
	"[FilterContainer]")
{
	FilterContainer filters;

	const auto action = "define-filter";

	filters.handle_action(action, {"first", "title = \"x\""});
	filters.handle_action(action, {"second", "author = \"Me\""});
	filters.handle_action(action, {"third", "content =~ \"Linux\""});

	std::vector<std::string> config;

	const auto comment =
		"# Comment to check that RssIgnores::dump_config() doesn't clear the vector";
	config.push_back(comment);

	filters.dump_config(config);

	REQUIRE(config.size() == 4); // three `define-filter`s plus one comment
	REQUIRE(config[0] == comment);
	REQUIRE(config[1] == R"#(define-filter "first" "title = \"x\"")#");
	REQUIRE(config[2] == R"#(define-filter "second" "author = \"Me\"")#");
	REQUIRE(config[3] == R"#(define-filter "third" "content =~ \"Linux\"")#");
}