comparison tests/plugin-ask/main.cpp @ 487:beb6c638b841

Tests: create plugin_test fixture, closes #677 Improve plugin tests by using a dedicated fixture which automatically loads the plugin and enable some modules on it.
author David Demelier <markand@malikania.fr>
date Fri, 11 Aug 2017 13:45:42 +0200
parents 0b156b82b8c1
children 7e273b7f4f92
comparison
equal deleted inserted replaced
486:0b156b82b8c1 487:beb6c638b841
20 20
21 #include <irccd/irccd.hpp> 21 #include <irccd/irccd.hpp>
22 #include <irccd/server.hpp> 22 #include <irccd/server.hpp>
23 #include <irccd/service.hpp> 23 #include <irccd/service.hpp>
24 24
25 #include "plugin-tester.hpp" 25 #include "plugin_test.hpp"
26 26
27 using namespace irccd; 27 using namespace irccd;
28 28
29 class ServerTest : public Server { 29 class server_test : public Server {
30 private: 30 private:
31 std::string m_last; 31 std::string last_;
32 32
33 public: 33 public:
34 inline ServerTest() 34 inline server_test()
35 : Server("test") 35 : Server("test")
36 { 36 {
37 } 37 }
38 38
39 inline const std::string &last() const noexcept 39 inline const std::string& last() const noexcept
40 { 40 {
41 return m_last; 41 return last_;
42 } 42 }
43 43
44 void message(std::string target, std::string message) override 44 void message(std::string target, std::string message) override
45 { 45 {
46 m_last = util::join({target, message}); 46 last_ = util::join({target, message});
47 } 47 }
48 }; 48 };
49 49
50 class AskTest : public PluginTester { 50 class ask_test : public plugin_test {
51 protected: 51 protected:
52 std::shared_ptr<ServerTest> m_server; 52 std::shared_ptr<server_test> server_{new server_test};
53 std::shared_ptr<Plugin> m_plugin;
54 53
55 public: 54 public:
56 AskTest() 55 inline ask_test()
57 : m_server(std::make_shared<ServerTest>()) 56 : plugin_test(PLUGIN_NAME, PLUGIN_PATH)
58 { 57 {
59 m_irccd.plugins().setConfig("ask", {{"file", SOURCEDIR "/answers.conf"}}); 58 plugin_->set_config({
60 m_irccd.plugins().load("ask", PLUGINDIR "/ask.js"); 59 { "file", CMAKE_CURRENT_SOURCE_DIR "/answers.conf" }
61 m_plugin = m_irccd.plugins().require("ask"); 60 });
61 plugin_->on_load(irccd_);
62 } 62 }
63 }; 63 };
64 64
65 TEST_F(AskTest, basic) 65 TEST_F(ask_test, basic)
66 { 66 {
67 bool no = false; 67 bool no = false;
68 bool yes = false; 68 bool yes = false;
69 69
70 /* 70 /*
71 * Invoke the plugin 1000 times, it will be very unlucky to not have both 71 * Invoke the plugin 1000 times, it will be very unlucky to not have both
72 * answers in that amount of tries. 72 * answers in that amount of tries.
73 */ 73 */
74 for (int i = 0; i < 1000; ++i) { 74 for (int i = 0; i < 1000; ++i) {
75 m_plugin->onCommand(m_irccd, MessageEvent{m_server, "tester", "#dummy", ""}); 75 plugin_->on_command(irccd_, MessageEvent{server_, "tester", "#dummy", ""});
76 76
77 if (m_server->last() == "#dummy:tester, YES") 77 if (server_->last() == "#dummy:tester, YES")
78 yes = true; 78 yes = true;
79 if (m_server->last() == "#dummy:tester, NO") 79 if (server_->last() == "#dummy:tester, NO")
80 no = true; 80 no = true;
81 } 81 }
82 82
83 ASSERT_TRUE(no); 83 ASSERT_TRUE(no);
84 ASSERT_TRUE(yes); 84 ASSERT_TRUE(yes);
85 } 85 }
86 86
87 int main(int argc, char **argv) 87 int main(int argc, char** argv)
88 { 88 {
89 testing::InitGoogleTest(&argc, argv); 89 testing::InitGoogleTest(&argc, argv);
90 90
91 return RUN_ALL_TESTS(); 91 return RUN_ALL_TESTS();
92 } 92 }