comparison tests/js-elapsedtimer/main.cpp @ 492:173c52d3120b

Tests: create js_test fixture, closes #688 Create a js_test fixture class which generates a fake javascript plugin and loads appropriate javascript modules to test.
author David Demelier <markand@malikania.fr>
date Wed, 27 Sep 2017 09:42:57 +0200
parents 349fe29d86d5
children b3a0f61a35fe
comparison
equal deleted inserted replaced
491:7f2ebbb7a45d 492:173c52d3120b
1 /* 1 /*
2 * main.cpp -- test irccd.ElapsedTimer API 2 * main.cpp -- test Irccd.ElapsedTimer API
3 * 3 *
4 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> 4 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
5 * 5 *
6 * Permission to use, copy, modify, and/or distribute this software for any 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 7 * purpose with or without fee is hereby granted, provided that the above
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 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 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
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 <gtest/gtest.h> 19 #define BOOST_TEST_MODULE "ElapsedTimer Javascript API"
20 #include <boost/test/unit_test.hpp>
20 21
21 #include <thread> 22 #include <thread>
22 23
23 #include <irccd/irccd.hpp>
24 #include <irccd/js_irccd_module.hpp>
25 #include <irccd/js_elapsed_timer_module.hpp> 24 #include <irccd/js_elapsed_timer_module.hpp>
26 #include <irccd/js_plugin.hpp>
27 #include <irccd/service.hpp>
28 25
29 using namespace irccd; 26 #include <js_test.hpp>
27
30 using namespace std::chrono_literals; 28 using namespace std::chrono_literals;
31 29
32 class TestElapsedTimer : public testing::Test { 30 namespace irccd {
33 protected:
34 irccd::irccd m_irccd;
35 std::shared_ptr<js_plugin> m_plugin;
36 31
37 TestElapsedTimer() 32 BOOST_FIXTURE_TEST_SUITE(js_elapsed_timer_suite, js_test<js_elapsed_timer_module>)
38 : m_plugin(std::make_shared<js_plugin>("empty", SOURCEDIR "/empty.js"))
39 {
40 js_irccd_module().load(m_irccd, m_plugin);
41 js_elapsed_timer_module().load(m_irccd, m_plugin);
42 }
43 };
44 33
45 TEST_F(TestElapsedTimer, standard) 34 BOOST_AUTO_TEST_CASE(standard)
46 { 35 {
47 try { 36 if (duk_peval_string(plugin_->context(), "timer = new Irccd.ElapsedTimer();") != 0)
48 if (duk_peval_string(m_plugin->context(), "timer = new Irccd.ElapsedTimer();") != 0) 37 throw dukx_exception(plugin_->context(), -1);
49 throw dukx_exception(m_plugin->context(), -1);
50 38
51 std::this_thread::sleep_for(300ms); 39 std::this_thread::sleep_for(300ms);
52 40
53 if (duk_peval_string(m_plugin->context(), "result = timer.elapsed();") != 0) 41 if (duk_peval_string(plugin_->context(), "result = timer.elapsed();") != 0)
54 throw dukx_exception(m_plugin->context(), -1); 42 throw dukx_exception(plugin_->context(), -1);
55 43
56 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "result")); 44 BOOST_REQUIRE(duk_get_global_string(plugin_->context(), "result"));
57 ASSERT_GE(duk_get_int(m_plugin->context(), -1), 250); 45 BOOST_REQUIRE_GE(duk_get_int(plugin_->context(), -1), 250);
58 ASSERT_LE(duk_get_int(m_plugin->context(), -1), 350); 46 BOOST_REQUIRE_LE(duk_get_int(plugin_->context(), -1), 350);
59 } catch (const std::exception &ex) {
60 FAIL() << ex.what();
61 }
62 } 47 }
63 48
64 int main(int argc, char **argv) 49 } // !irccd
65 {
66 testing::InitGoogleTest(&argc, argv);
67 50
68 return RUN_ALL_TESTS(); 51 BOOST_AUTO_TEST_SUITE_END()
69 }