comparison tests/src/plugins/history/main.cpp @ 611:9fbd1700435b

Tests: reoganize hierarchy
author David Demelier <markand@malikania.fr>
date Fri, 15 Dec 2017 15:37:58 +0100
parents tests/src/plugin-history/main.cpp@9d4da384f5d6
children 27587ff92a64
comparison
equal deleted inserted replaced
610:22b3cd6f991f 611:9fbd1700435b
1 /*
2 * main.cpp -- test history plugin
3 *
4 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <regex>
20
21 #define BOOST_TEST_MODULE "History plugin"
22 #include <boost/test/unit_test.hpp>
23
24 #include <irccd/daemon/irccd.hpp>
25 #include <irccd/daemon/server.hpp>
26
27 #include <irccd/test/plugin_test.hpp>
28
29 namespace irccd {
30
31 class history_test : public plugin_test {
32 public:
33 history_test()
34 : plugin_test(PLUGIN_NAME, PLUGIN_PATH)
35 {
36 plugin_->set_formats({
37 { "error", "error=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}" },
38 { "seen", "seen=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}:%H:%M" },
39 { "said", "said=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}:#{message}:%H:%M" },
40 { "unknown", "unknown=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}" },
41 });
42 }
43
44 void load(plugin_config config = {})
45 {
46 // Add file if not there.
47 if (config.count("file") == 0)
48 config.emplace("file", CMAKE_CURRENT_SOURCE_DIR "/words.conf");
49
50 plugin_->set_config(config);
51 plugin_->on_load(irccd_);
52 }
53 };
54
55 BOOST_FIXTURE_TEST_SUITE(history_test_suite, history_test)
56
57 BOOST_AUTO_TEST_CASE(format_error)
58 {
59 load({{"file", CMAKE_CURRENT_SOURCE_DIR "/broken-conf.json"}});
60
61 plugin_->on_command(irccd_, {server_, "jean!jean@localhost", "#history", "seen francis"});
62
63 auto cmd = server_->cqueue().front();
64
65 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
66 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
67 BOOST_REQUIRE_EQUAL(cmd["message"].get<std::string>(), "error=history:!history:test:#history:jean!jean@localhost:jean");
68 }
69
70 BOOST_AUTO_TEST_CASE(format_seen)
71 {
72 const std::regex rule("seen=history:!history:test:#history:destructor!dst@localhost:destructor:jean:\\d{2}:\\d{2}");
73
74 remove(CMAKE_CURRENT_BINARY_DIR "/seen.json");
75 load({{ "file", CMAKE_CURRENT_BINARY_DIR "/seen.json" }});
76
77 plugin_->on_message(irccd_, {server_, "jean!jean@localhost", "#history", "hello"});
78 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#history", "seen jean"});
79
80 auto cmd = server_->cqueue().front();
81
82 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
83 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
84 BOOST_REQUIRE(std::regex_match(cmd["message"].get<std::string>(), rule));
85 }
86
87 BOOST_AUTO_TEST_CASE(format_said)
88 {
89 std::regex rule("said=history:!history:test:#history:destructor!dst@localhost:destructor:jean:hello:\\d{2}:\\d{2}");
90
91 remove(CMAKE_CURRENT_BINARY_DIR "/said.json");
92 load({{ "file", CMAKE_CURRENT_BINARY_DIR "/said.json" }});
93
94 plugin_->on_message(irccd_, {server_, "jean!jean@localhost", "#history", "hello"});
95 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#history", "said jean"});
96
97 auto cmd = server_->cqueue().front();
98
99 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
100 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
101 BOOST_REQUIRE(std::regex_match(cmd["message"].get<std::string>(), rule));
102 }
103
104 BOOST_AUTO_TEST_CASE(format_unknown)
105 {
106 remove(CMAKE_CURRENT_BINARY_DIR "/unknown.json");
107 load({{ "file", CMAKE_CURRENT_BINARY_DIR "/unknown.json" }});
108
109 plugin_->on_message(irccd_, {server_, "jean!jean@localhost", "#history", "hello"});
110 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#history", "seen nobody"});
111
112 auto cmd = server_->cqueue().front();
113
114 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
115 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
116 BOOST_REQUIRE_EQUAL(cmd["message"].get<std::string>(), "unknown=history:!history:test:#history:destructor!dst@localhost:destructor:nobody");
117 }
118
119 BOOST_AUTO_TEST_CASE(fix_642)
120 {
121 const std::regex rule("said=history:!history:test:#history:destructor!dst@localhost:destructor:jean:hello:\\d{2}:\\d{2}");
122
123 remove(CMAKE_CURRENT_BINARY_DIR "/case.json");
124 load({{"file", CMAKE_CURRENT_BINARY_DIR "/case.json"}});
125
126 plugin_->on_message(irccd_, {server_, "JeaN!JeaN@localhost", "#history", "hello"});
127
128 // Full caps.
129 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#HISTORY", "said JEAN"});
130
131 auto cmd = server_->cqueue().front();
132
133 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
134 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
135 BOOST_REQUIRE(std::regex_match(cmd["message"].get<std::string>(), rule));
136
137 // Random caps.
138 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#HiSToRy", "said JeaN"});
139
140 cmd = server_->cqueue().back();
141
142 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
143 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
144 BOOST_REQUIRE(std::regex_match(cmd["message"].get<std::string>(), rule));
145 }
146
147 BOOST_AUTO_TEST_SUITE_END()
148
149 } // !irccd