Mercurial > irccd
diff tests/js-elapsedtimer/main.cpp @ 75:f8160d515a76
Irccd: rework a lot the JavaScript library
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 30 Mar 2016 13:52:47 +0200 |
parents | 1158cffe5a5e |
children | 1125d90b3b44 |
line wrap: on
line diff
--- a/tests/js-elapsedtimer/main.cpp Sat Mar 26 14:41:53 2016 +0100 +++ b/tests/js-elapsedtimer/main.cpp Wed Mar 30 13:52:47 2016 +0200 @@ -20,8 +20,8 @@ #include <thread> -#include <js-irccd.h> -#include <js-elapsed-timer.h> +#include <irccd/js-irccd.h> +#include <irccd/js-elapsed-timer.h> using namespace irccd; using namespace std::chrono_literals; @@ -33,7 +33,7 @@ class TestElapsedTimer : public testing::Test { protected: - js::Context m_context; + duk::Context m_context; TestElapsedTimer() { @@ -50,62 +50,93 @@ TEST_F(TestElapsedTimer, standard) { - m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); + try { + if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) + throw duk::error(m_context, -1); + + std::this_thread::sleep_for(300ms); - std::this_thread::sleep_for(300ms); + if (duk::pevalString(m_context, "result = timer.elapsed();") != 0) + throw duk::error(m_context, -1); - m_context.peval(js::Script{"result = timer.elapsed();"}); - assertRange(m_context.getGlobal<int>("result"), 300); + assertRange(duk::getGlobal<int>(m_context, "result"), 300); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } } TEST_F(TestElapsedTimer, reset) { - m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); + try { + if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) + throw duk::error(m_context, -1); + + std::this_thread::sleep_for(300ms); - std::this_thread::sleep_for(300ms); + if (duk::pevalString(m_context, "timer.reset(); result = timer.elapsed();") != 0) + throw duk::error(m_context, -1); - m_context.peval(js::Script{"timer.reset(); result = timer.elapsed();"}); - assertRange(m_context.getGlobal<int>("result"), 0); + assertRange(duk::getGlobal<int>(m_context, "result"), 0); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } } TEST_F(TestElapsedTimer, pause) { - m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); + try { + if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) + throw duk::error(m_context, -1); + + /* + * Simulate a pause in the game like this: + * + * start pause restart elapsed + * | 10ms |.5ms.| 6ms | + * + * Since the game was paused, the 5ms must not be totalized. + */ + std::this_thread::sleep_for(10ms); - /* - * Simulate a pause in the game like this: - * - * start pause restart elapsed - * | 10ms |.5ms.| 6ms | - * - * Since the game was paused, the 5ms must not be totalized. - */ - std::this_thread::sleep_for(10ms); + if (duk::pevalString(m_context, "timer.pause();") != 0) + throw duk::error(m_context, -1); + + std::this_thread::sleep_for(5ms); + + if (duk::pevalString(m_context, "timer.restart();") != 0) + throw duk::error(m_context, -1); - m_context.peval(js::Script{"timer.pause();"}); + std::this_thread::sleep_for(6ms); - std::this_thread::sleep_for(5ms); + if (duk::pevalString(m_context, "result = timer.elapsed()") != 0) + throw duk::error(m_context, -1); - m_context.peval(js::Script{"timer.restart();"}); - - std::this_thread::sleep_for(6ms); - - m_context.peval(js::Script{"result = timer.elapsed()"}); - assertRange(m_context.getGlobal<int>("result"), 16); + assertRange(duk::getGlobal<int>(m_context, "result"), 16); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } } TEST_F(TestElapsedTimer, doublecheck) { - m_context.peval(js::Script{"timer = new Irccd.ElapsedTimer();"}); + try { + if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) + throw duk::error(m_context, -1); - std::this_thread::sleep_for(50ms); + std::this_thread::sleep_for(50ms); + + if (duk::pevalString(m_context, "result = timer.elapsed()") != 0) + throw duk::error(m_context, -1); - m_context.peval(js::Script{"result = timer.elapsed()"}); + std::this_thread::sleep_for(50ms); + + if (duk::pevalString(m_context, "result = timer.elapsed()") != 0) + throw duk::error(m_context, -1); - std::this_thread::sleep_for(50ms); - - m_context.peval(js::Script{"result = timer.elapsed()"}); - assertRange(m_context.getGlobal<int>("result"), 100); + assertRange(duk::getGlobal<int>(m_context, "result"), 100); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } } int main(int argc, char **argv)