comparison tests/src/plugins/joke/main.cpp @ 773:8c44bbcbbab9

Misc: style, cleanup and update
author David Demelier <markand@malikania.fr>
date Fri, 26 Oct 2018 13:01:00 +0200
parents 791d7591eca7
children f362994133ca
comparison
equal deleted inserted replaced
772:f5ccf65ae929 773:8c44bbcbbab9
25 25
26 namespace { 26 namespace {
27 27
28 class joke_test : public js_plugin_fixture { 28 class joke_test : public js_plugin_fixture {
29 public: 29 public:
30 joke_test() 30 joke_test()
31 : js_plugin_fixture(PLUGIN_PATH) 31 : js_plugin_fixture(PLUGIN_PATH)
32 { 32 {
33 plugin_->set_formats({ 33 plugin_->set_formats({
34 { "error", "error=#{server}:#{channel}:#{origin}:#{nickname}" } 34 { "error", "error=#{server}:#{channel}:#{origin}:#{nickname}" }
35 }); 35 });
36 } 36 }
37 37
38 void load(plugin::map config = {}) 38 void load(plugin::map config = {})
39 { 39 {
40 // Add file if not there. 40 // Add file if not there.
41 if (config.count("file") == 0) 41 if (config.count("file") == 0)
42 config.emplace("file", CMAKE_CURRENT_SOURCE_DIR "/jokes.json"); 42 config.emplace("file", CMAKE_CURRENT_SOURCE_DIR "/jokes.json");
43 43
44 plugin_->set_options(config); 44 plugin_->set_options(config);
45 plugin_->handle_load(irccd_); 45 plugin_->handle_load(irccd_);
46 } 46 }
47 }; 47 };
48 48
49 BOOST_FIXTURE_TEST_SUITE(joke_test_suite, joke_test) 49 BOOST_FIXTURE_TEST_SUITE(joke_test_suite, joke_test)
50 50
51 BOOST_AUTO_TEST_CASE(simple) 51 BOOST_AUTO_TEST_CASE(simple)
52 { 52 {
53 /* 53 /*
54 * Jokes.json have two jokes. 54 * Jokes.json have two jokes.
55 * 55 *
56 * aaa 56 * aaa
57 * 57 *
58 * And 58 * And
59 * 59 *
60 * bbbb 60 * bbbb
61 * bbbb 61 * bbbb
62 */ 62 */
63 std::unordered_map<std::string, int> said{ 63 std::unordered_map<std::string, int> said{
64 { "aaa", 0 }, 64 { "aaa", 0 },
65 { "bbbb", 0 } 65 { "bbbb", 0 }
66 }; 66 };
67 67
68 load(); 68 load();
69 69
70 const auto call = [&] () { 70 const auto call = [&] () {
71 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" }); 71 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" });
72 72
73 const auto cmd = server_->find("message").back(); 73 const auto cmd = server_->find("message").back();
74 const auto msg = std::any_cast<std::string>(cmd[1]); 74 const auto msg = std::any_cast<std::string>(cmd[1]);
75 75
76 // "bbbb" is two lines. 76 // "bbbb" is two lines.
77 if (msg == "bbbb") { 77 if (msg == "bbbb") {
78 const auto first = server_->find("message").front(); 78 const auto first = server_->find("message").front();
79 79
80 BOOST_TEST(std::any_cast<std::string>(first[0]) == "#joke"); 80 BOOST_TEST(std::any_cast<std::string>(first[0]) == "#joke");
81 BOOST_TEST(std::any_cast<std::string>(first[1]) == "bbbb"); 81 BOOST_TEST(std::any_cast<std::string>(first[1]) == "bbbb");
82 } else 82 } else
83 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "aaa"); 83 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "aaa");
84 84
85 said[msg] += 1; 85 said[msg] += 1;
86 server_->clear(); 86 server_->clear();
87 }; 87 };
88 88
89 call(); 89 call();
90 call(); 90 call();
91 91
92 BOOST_TEST(said.size() == 2U); 92 BOOST_TEST(said.size() == 2U);
93 BOOST_TEST(said["aaa"] == 1U); 93 BOOST_TEST(said["aaa"] == 1U);
94 BOOST_TEST(said["bbbb"] == 1U); 94 BOOST_TEST(said["bbbb"] == 1U);
95 } 95 }
96 96
97 BOOST_AUTO_TEST_CASE(toobig) 97 BOOST_AUTO_TEST_CASE(toobig)
98 { 98 {
99 // xxx and yyy are both 3-lines which we disallow. only a must be said. 99 // xxx and yyy are both 3-lines which we disallow. only a must be said.
100 load({ 100 load({
101 { "file", CMAKE_CURRENT_SOURCE_DIR "/error-toobig.json" }, 101 { "file", CMAKE_CURRENT_SOURCE_DIR "/error-toobig.json" },
102 { "max-list-lines", "2" } 102 { "max-list-lines", "2" }
103 }); 103 });
104 104
105 std::unordered_map<std::string, int> said{ 105 std::unordered_map<std::string, int> said{
106 { "a", 0 } 106 { "a", 0 }
107 }; 107 };
108 108
109 const auto call = [&] () { 109 const auto call = [&] () {
110 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" }); 110 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" });
111 111
112 const auto cmd = server_->find("message").back(); 112 const auto cmd = server_->find("message").back();
113 113
114 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke"); 114 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke");
115 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "a"); 115 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "a");
116 116
117 said[std::any_cast<std::string>(cmd[1])] += 1; 117 said[std::any_cast<std::string>(cmd[1])] += 1;
118 server_->clear(); 118 server_->clear();
119 }; 119 };
120 120
121 call(); 121 call();
122 call(); 122 call();
123 call(); 123 call();
124 124
125 BOOST_TEST(said.size() == 1U); 125 BOOST_TEST(said.size() == 1U);
126 BOOST_TEST(said["a"] == 3U); 126 BOOST_TEST(said["a"] == 3U);
127 } 127 }
128 128
129 BOOST_AUTO_TEST_CASE(invalid) 129 BOOST_AUTO_TEST_CASE(invalid)
130 { 130 {
131 // Only a is the valid joke in this file. 131 // Only a is the valid joke in this file.
132 load({ 132 load({{ "file", CMAKE_CURRENT_SOURCE_DIR "/error-invalid.json" }});
133 { "file", CMAKE_CURRENT_SOURCE_DIR "/error-invalid.json" }, 133
134 }); 134 std::unordered_map<std::string, int> said{
135 135 { "a", 0 }
136 std::unordered_map<std::string, int> said{ 136 };
137 { "a", 0 } 137
138 }; 138 const auto call = [&] () {
139 139 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" });
140 const auto call = [&] () { 140
141 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" }); 141 const auto cmd = server_->find("message").back();
142 142
143 const auto cmd = server_->find("message").back(); 143 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke");
144 144 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "a");
145 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke"); 145
146 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "a"); 146 server_->clear();
147 147 said[std::any_cast<std::string>(cmd[1])] += 1;
148 server_->clear(); 148 };
149 said[std::any_cast<std::string>(cmd[1])] += 1; 149
150 }; 150 call();
151 151 call();
152 call(); 152 call();
153 call(); 153
154 call(); 154 BOOST_TEST(said.size() == 1U);
155 155 BOOST_TEST(said["a"] == 3U);
156 BOOST_TEST(said.size() == 1U);
157 BOOST_TEST(said["a"] == 3U);
158 } 156 }
159 157
160 BOOST_AUTO_TEST_SUITE(errors) 158 BOOST_AUTO_TEST_SUITE(errors)
161 159
162 BOOST_AUTO_TEST_CASE(not_found) 160 BOOST_AUTO_TEST_CASE(not_found)
163 { 161 {
164 load({{"file", "doesnotexist.json"}}); 162 load({{"file", "doesnotexist.json"}});
165 163
166 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" }); 164 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" });
167 165
168 const auto cmd = server_->find("message").back(); 166 const auto cmd = server_->find("message").back();
169 167
170 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke"); 168 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke");
171 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "error=test:#joke:jean!jean@localhost:jean"); 169 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "error=test:#joke:jean!jean@localhost:jean");
172 } 170 }
173 171
174 BOOST_AUTO_TEST_CASE(not_array) 172 BOOST_AUTO_TEST_CASE(not_array)
175 { 173 {
176 load({{"file", CMAKE_CURRENT_SOURCE_DIR "/error-not-array.json"}}); 174 load({{"file", CMAKE_CURRENT_SOURCE_DIR "/error-not-array.json"}});
177 175
178 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" }); 176 plugin_->handle_command(irccd_, { server_, "jean!jean@localhost", "#joke", "" });
179 177
180 const auto cmd = server_->find("message").back(); 178 const auto cmd = server_->find("message").back();
181 179
182 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke"); 180 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke");
183 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "error=test:#joke:jean!jean@localhost:jean"); 181 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "error=test:#joke:jean!jean@localhost:jean");
184 } 182 }
185 183
186 BOOST_AUTO_TEST_CASE(empty) 184 BOOST_AUTO_TEST_CASE(empty)
187 { 185 {
188 load({{"file", CMAKE_CURRENT_SOURCE_DIR "/error-empty.json"}}); 186 load({{"file", CMAKE_CURRENT_SOURCE_DIR "/error-empty.json"}});
189 187
190 plugin_->handle_command(irccd_, {server_, "jean!jean@localhost", "#joke", ""}); 188 plugin_->handle_command(irccd_, {server_, "jean!jean@localhost", "#joke", ""});
191 189
192 const auto cmd = server_->find("message").back(); 190 const auto cmd = server_->find("message").back();
193 191
194 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke"); 192 BOOST_TEST(std::any_cast<std::string>(cmd[0]) == "#joke");
195 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "error=test:#joke:jean!jean@localhost:jean"); 193 BOOST_TEST(std::any_cast<std::string>(cmd[1]) == "error=test:#joke:jean!jean@localhost:jean");
196 } 194 }
197 195
198 BOOST_AUTO_TEST_SUITE_END() 196 BOOST_AUTO_TEST_SUITE_END()
199 197
200 BOOST_AUTO_TEST_SUITE_END() 198 BOOST_AUTO_TEST_SUITE_END()