Mercurial > irccd
comparison tests/js-elapsedtimer/main.cpp @ 167:9f36f71a08c5
Irccd: unbreak test-js-elapsedtimer
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 25 May 2016 22:20:21 +0200 |
parents | dc7d6ba08122 |
children | ef527409e638 |
comparison
equal
deleted
inserted
replaced
166:2380d476f987 | 167:9f36f71a08c5 |
---|---|
18 | 18 |
19 #include <gtest/gtest.h> | 19 #include <gtest/gtest.h> |
20 | 20 |
21 #include <thread> | 21 #include <thread> |
22 | 22 |
23 #include <irccd/js-irccd.hpp> | 23 #include <irccd/irccd.hpp> |
24 #include <irccd/mod-irccd.hpp> | |
24 #include <irccd/mod-elapsed-timer.hpp> | 25 #include <irccd/mod-elapsed-timer.hpp> |
26 #include <irccd/plugin-js.hpp> | |
27 #include <irccd/service-module.hpp> | |
25 | 28 |
26 using namespace irccd; | 29 using namespace irccd; |
27 using namespace std::chrono_literals; | 30 using namespace std::chrono_literals; |
28 | 31 |
29 class TestElapsedTimer : public testing::Test { | 32 class TestElapsedTimer : public testing::Test { |
30 protected: | 33 protected: |
31 duk::Context m_context; | 34 Irccd m_irccd; |
35 std::shared_ptr<JsPlugin> m_plugin; | |
32 | 36 |
33 TestElapsedTimer() | 37 TestElapsedTimer() |
38 : m_plugin(std::make_shared<JsPlugin>("empty", SOURCEDIR "/empty.js")) | |
34 { | 39 { |
35 loadJsIrccd(m_context); | 40 m_irccd.moduleService().get("Irccd")->load(m_irccd, *m_plugin); |
36 loadJsElapsedTimer(m_context); | 41 m_irccd.moduleService().get("Irccd.ElapsedTimer")->load(m_irccd, *m_plugin); |
37 } | 42 } |
38 }; | 43 }; |
39 | 44 |
40 TEST_F(TestElapsedTimer, standard) | 45 TEST_F(TestElapsedTimer, standard) |
41 { | 46 { |
42 try { | 47 try { |
43 if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) { | 48 if (duk::pevalString(m_plugin->context(), "timer = new Irccd.ElapsedTimer();") != 0) |
44 throw duk::error(m_context, -1); | 49 throw duk::error(m_plugin->context(), -1); |
45 } | |
46 | 50 |
47 std::this_thread::sleep_for(300ms); | 51 std::this_thread::sleep_for(300ms); |
48 | 52 |
49 if (duk::pevalString(m_context, "result = timer.elapsed();") != 0) { | 53 if (duk::pevalString(m_plugin->context(), "result = timer.elapsed();") != 0) |
50 throw duk::error(m_context, -1); | 54 throw duk::error(m_plugin->context(), -1); |
51 } | |
52 | 55 |
53 ASSERT_GE(duk::getGlobal<int>(m_context, "result"), 250); | 56 ASSERT_GE(duk::getGlobal<int>(m_plugin->context(), "result"), 250); |
54 ASSERT_LE(duk::getGlobal<int>(m_context, "result"), 350); | 57 ASSERT_LE(duk::getGlobal<int>(m_plugin->context(), "result"), 350); |
55 } catch (const std::exception &ex) { | 58 } catch (const std::exception &ex) { |
56 FAIL() << ex.what(); | 59 FAIL() << ex.what(); |
57 } | 60 } |
58 } | 61 } |
59 | 62 |
60 TEST_F(TestElapsedTimer, reset) | 63 TEST_F(TestElapsedTimer, reset) |
61 { | 64 { |
62 try { | 65 try { |
63 if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) { | 66 if (duk::pevalString(m_plugin->context(), "timer = new Irccd.ElapsedTimer();") != 0) |
64 throw duk::error(m_context, -1); | 67 throw duk::error(m_plugin->context(), -1); |
65 } | |
66 | 68 |
67 std::this_thread::sleep_for(300ms); | 69 std::this_thread::sleep_for(300ms); |
68 | 70 |
69 if (duk::pevalString(m_context, "timer.reset(); result = timer.elapsed();") != 0) { | 71 if (duk::pevalString(m_plugin->context(), "timer.reset(); result = timer.elapsed();") != 0) |
70 throw duk::error(m_context, -1); | 72 throw duk::error(m_plugin->context(), -1); |
71 } | |
72 | 73 |
73 ASSERT_LE(duk::getGlobal<int>(m_context, "result"), 100); | 74 ASSERT_LE(duk::getGlobal<int>(m_plugin->context(), "result"), 100); |
74 } catch (const std::exception &ex) { | 75 } catch (const std::exception &ex) { |
75 FAIL() << ex.what(); | 76 FAIL() << ex.what(); |
76 } | 77 } |
77 } | 78 } |
78 | 79 |