comparison tests/plugin-history/main.cpp @ 489:349fe29d86d5

Tests: switch to Boost, closes #680
author David Demelier <markand@malikania.fr>
date Sun, 20 Aug 2017 08:16:39 +0200
parents 7e273b7f4f92
children d070d33bf4a0
comparison
equal deleted inserted replaced
488:7e273b7f4f92 489:349fe29d86d5
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <regex> 19 #include <regex>
20 20
21 #include <gtest/gtest.h> 21 #define BOOST_TEST_MODULE "History plugin"
22 #include <boost/test/unit_test.hpp>
22 23
23 #include <irccd/irccd.hpp> 24 #include <irccd/irccd.hpp>
24 #include <irccd/server.hpp> 25 #include <irccd/server.hpp>
25 #include <irccd/service.hpp> 26 #include <irccd/service.hpp>
26 27
27 #include "plugin_test.hpp" 28 #include "plugin_test.hpp"
28 29
29 using namespace irccd; 30 namespace irccd {
30
31 class server_test : public server {
32 private:
33 std::string last_;
34
35 public:
36 inline server_test()
37 : server("test")
38 {
39 }
40
41 inline const std::string& last() const noexcept
42 {
43 return last_;
44 }
45
46 void message(std::string target, std::string message) override
47 {
48 last_ = util::join({target, message});
49 }
50 };
51 31
52 class history_test : public plugin_test { 32 class history_test : public plugin_test {
53 protected:
54 std::shared_ptr<server_test> server_;
55
56 public: 33 public:
57 history_test() 34 history_test()
58 : plugin_test(PLUGIN_NAME, PLUGIN_PATH) 35 : plugin_test(PLUGIN_NAME, PLUGIN_PATH)
59 , server_(std::make_shared<server_test>())
60 { 36 {
61 plugin_->set_formats({ 37 plugin_->set_formats({
62 { "error", "error=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}" }, 38 { "error", "error=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}" },
63 { "seen", "seen=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}:%H:%M" }, 39 { "seen", "seen=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}:%H:%M" },
64 { "said", "said=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}:#{message}:%H:%M" }, 40 { "said", "said=#{plugin}:#{command}:#{server}:#{channel}:#{origin}:#{nickname}:#{target}:#{message}:%H:%M" },
75 plugin_->set_config(config); 51 plugin_->set_config(config);
76 plugin_->on_load(irccd_); 52 plugin_->on_load(irccd_);
77 } 53 }
78 }; 54 };
79 55
80 TEST_F(history_test, formatError) 56 BOOST_FIXTURE_TEST_SUITE(history_test_suite, history_test)
57
58 BOOST_AUTO_TEST_CASE(format_error)
81 { 59 {
82 load({{ "file", SOURCEDIR "/broken-conf.json" }}); 60 load({{"file", SOURCEDIR "/broken-conf.json"}});
83 61
84 plugin_->on_command(irccd_, {server_, "jean!jean@localhost", "#history", "seen francis"}); 62 plugin_->on_command(irccd_, {server_, "jean!jean@localhost", "#history", "seen francis"});
85 ASSERT_EQ("#history:error=history:!history:test:#history:jean!jean@localhost:jean", server_->last()); 63
64 auto cmd = server_->cqueue().front();
65
66 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
67 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
68 BOOST_REQUIRE_EQUAL(cmd["message"].get<std::string>(), "error=history:!history:test:#history:jean!jean@localhost:jean");
86 } 69 }
87 70
88 TEST_F(history_test, formatSeen) 71 BOOST_AUTO_TEST_CASE(format_seen)
89 { 72 {
90 std::regex rule("#history:seen=history:!history:test:#history:destructor!dst@localhost:destructor:jean:\\d{2}:\\d{2}"); 73 const std::regex rule("seen=history:!history:test:#history:destructor!dst@localhost:destructor:jean:\\d{2}:\\d{2}");
91 74
92 remove(BINARYDIR "/seen.json"); 75 remove(BINARYDIR "/seen.json");
93 load({{ "file", BINARYDIR "/seen.json" }}); 76 load({{ "file", BINARYDIR "/seen.json" }});
94 77
95 plugin_->on_message(irccd_, {server_, "jean!jean@localhost", "#history", "hello"}); 78 plugin_->on_message(irccd_, {server_, "jean!jean@localhost", "#history", "hello"});
96 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#history", "seen jean"}); 79 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#history", "seen jean"});
97 80
98 ASSERT_TRUE(std::regex_match(server_->last(), rule)); 81 auto cmd = server_->cqueue().front();
82
83 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
84 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
85 BOOST_REQUIRE(std::regex_match(cmd["message"].get<std::string>(), rule));
99 } 86 }
100 87
101 TEST_F(history_test, formatSaid) 88 BOOST_AUTO_TEST_CASE(format_said)
102 { 89 {
103 std::regex rule("#history:said=history:!history:test:#history:destructor!dst@localhost:destructor:jean:hello:\\d{2}:\\d{2}"); 90 std::regex rule("said=history:!history:test:#history:destructor!dst@localhost:destructor:jean:hello:\\d{2}:\\d{2}");
104 91
105 remove(BINARYDIR "/said.json"); 92 remove(BINARYDIR "/said.json");
106 load({{ "file", BINARYDIR "/said.json" }}); 93 load({{ "file", BINARYDIR "/said.json" }});
107 94
108 plugin_->on_message(irccd_, {server_, "jean!jean@localhost", "#history", "hello"}); 95 plugin_->on_message(irccd_, {server_, "jean!jean@localhost", "#history", "hello"});
109 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#history", "said jean"}); 96 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#history", "said jean"});
110 97
111 ASSERT_TRUE(std::regex_match(server_->last(), rule)); 98 auto cmd = server_->cqueue().front();
99
100 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
101 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
102 BOOST_REQUIRE(std::regex_match(cmd["message"].get<std::string>(), rule));
112 } 103 }
113 104
114 TEST_F(history_test, formatUnknown) 105 BOOST_AUTO_TEST_CASE(format_unknown)
115 { 106 {
116 remove(BINARYDIR "/unknown.json"); 107 remove(BINARYDIR "/unknown.json");
117 load({{ "file", BINARYDIR "/unknown.json" }}); 108 load({{ "file", BINARYDIR "/unknown.json" }});
118 109
119 plugin_->on_message(irccd_, {server_, "jean!jean@localhost", "#history", "hello"}); 110 plugin_->on_message(irccd_, {server_, "jean!jean@localhost", "#history", "hello"});
120 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#history", "seen nobody"}); 111 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#history", "seen nobody"});
121 112
122 ASSERT_EQ("#history:unknown=history:!history:test:#history:destructor!dst@localhost:destructor:nobody", server_->last()); 113 auto cmd = server_->cqueue().front();
114
115 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
116 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
117 BOOST_REQUIRE_EQUAL(cmd["message"].get<std::string>(), "unknown=history:!history:test:#history:destructor!dst@localhost:destructor:nobody");
123 } 118 }
124 119
125 TEST_F(history_test, case_fix_642) 120 BOOST_AUTO_TEST_CASE(fix_642)
126 { 121 {
127 std::regex rule("#history:said=history:!history:test:#history:destructor!dst@localhost:destructor:jean:hello:\\d{2}:\\d{2}"); 122 const std::regex rule("said=history:!history:test:#history:destructor!dst@localhost:destructor:jean:hello:\\d{2}:\\d{2}");
128 123
129 remove(BINARYDIR "/case.json"); 124 remove(BINARYDIR "/case.json");
130 load({{"file", BINARYDIR "/case.json"}}); 125 load({{"file", BINARYDIR "/case.json"}});
131 126
132 plugin_->on_message(irccd_, {server_, "JeaN!JeaN@localhost", "#history", "hello"}); 127 plugin_->on_message(irccd_, {server_, "JeaN!JeaN@localhost", "#history", "hello"});
133 128
129 // Full caps.
134 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#HISTORY", "said JEAN"}); 130 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#HISTORY", "said JEAN"});
135 ASSERT_TRUE(std::regex_match(server_->last(), rule)); 131
132 auto cmd = server_->cqueue().front();
133
134 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
135 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
136 BOOST_REQUIRE(std::regex_match(cmd["message"].get<std::string>(), rule));
137
138 // Random caps.
136 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#HiSToRy", "said JeaN"}); 139 plugin_->on_command(irccd_, {server_, "destructor!dst@localhost", "#HiSToRy", "said JeaN"});
137 ASSERT_TRUE(std::regex_match(server_->last(), rule)); 140
141 cmd = server_->cqueue().front();
142
143 BOOST_REQUIRE_EQUAL(cmd["command"].get<std::string>(), "message");
144 BOOST_REQUIRE_EQUAL(cmd["target"].get<std::string>(), "#history");
145 BOOST_REQUIRE(std::regex_match(cmd["message"].get<std::string>(), rule));
138 } 146 }
139 147
140 int main(int argc, char** argv) 148 BOOST_AUTO_TEST_SUITE_END()
141 {
142 testing::InitGoogleTest(&argc, argv);
143 149
144 return RUN_ALL_TESTS(); 150 } // !irccd
145 }