Mercurial > malikania
diff tests/libcommon/js-elapsed-timer/main.cpp @ 36:9af360f34c7d
Misc: use raw duktape API
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 10 Aug 2016 14:30:51 +0200 |
parents | d4f5f7231b84 |
children | a47a4477f347 |
line wrap: on
line diff
--- a/tests/libcommon/js-elapsed-timer/main.cpp Fri Jul 01 13:32:08 2016 +0200 +++ b/tests/libcommon/js-elapsed-timer/main.cpp Wed Aug 10 14:30:51 2016 +0200 @@ -33,111 +33,119 @@ class TestElapsedTimer : public testing::Test { protected: - duk::Context m_ctx; - - TestElapsedTimer() - { - duk::putGlobal(m_ctx, "Malikania", duk::Object()); + UniqueContext m_ctx; - loadJsElapsedTimer(m_ctx); - } + TestElapsedTimer() + { + duk_push_object(m_ctx); + duk_put_global_string(m_ctx, "Malikania"); + dukx_load_elapsedtimer(m_ctx); + } - inline void assertRange(int value, int expected) const noexcept - { - if (value < (expected - margin) || value > (expected + margin)) - FAIL() << value << " is bigger than [" << (expected - margin) << ", " << (expected + margin) << "]"; - } + inline void assertRange(int value, int expected) const noexcept + { + if (value < (expected - margin) || value > (expected + margin)) + FAIL() << value << " is bigger than [" << (expected - margin) << ", " << (expected + margin) << "]"; + } }; TEST_F(TestElapsedTimer, standard) { - try { - if (duk::pevalString(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) - throw duk::exception(m_ctx, -1); + try { + if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) + throw dukx_exception(m_ctx, -1); - std::this_thread::sleep_for(300ms); + std::this_thread::sleep_for(300ms); - if (duk::pevalString(m_ctx, "result = timer.elapsed();") != 0) - throw duk::exception(m_ctx, -1); + if (duk_peval_string(m_ctx, "result = timer.elapsed();") != 0) + throw dukx_exception(m_ctx, -1); - assertRange(duk::getGlobal<int>(m_ctx, "result"), 300); - } catch (const std::exception &ex) { - FAIL() << ex.what(); - } + duk_get_global_string(m_ctx, "result"); + assertRange(duk_to_int(m_ctx, -1), 300); + duk_pop(m_ctx); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } } TEST_F(TestElapsedTimer, reset) { - try { - if (duk::pevalString(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) - throw duk::exception(m_ctx, -1); + try { + if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) + throw dukx_exception(m_ctx, -1); - std::this_thread::sleep_for(300ms); + std::this_thread::sleep_for(300ms); - if (duk::pevalString(m_ctx, "timer.reset(); result = timer.elapsed();") != 0) - throw duk::exception(m_ctx, -1); + if (duk_peval_string(m_ctx, "timer.reset(); result = timer.elapsed();") != 0) + throw dukx_exception(m_ctx, -1); - assertRange(duk::getGlobal<int>(m_ctx, "result"), 0); - } catch (const std::exception &ex) { - FAIL() << ex.what(); - } + duk_get_global_string(m_ctx, "result"); + assertRange(duk_to_int(m_ctx, -1), 0); + duk_pop(m_ctx); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } } TEST_F(TestElapsedTimer, pause) { - try { - if (duk::pevalString(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) - throw duk::exception(m_ctx, -1); + try { + if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) + throw dukx_exception(m_ctx, -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_peval_string(m_ctx, "timer.pause();") != 0) + throw dukx_exception(m_ctx, -1); - if (duk::pevalString(m_ctx, "timer.pause();") != 0) - throw duk::exception(m_ctx, -1); + std::this_thread::sleep_for(5ms); - std::this_thread::sleep_for(5ms); + if (duk_peval_string(m_ctx, "timer.restart();") != 0) + throw dukx_exception(m_ctx, -1); - if (duk::pevalString(m_ctx, "timer.restart();") != 0) - throw duk::exception(m_ctx, -1); + std::this_thread::sleep_for(6ms); - std::this_thread::sleep_for(6ms); - - if (duk::pevalString(m_ctx, "result = timer.elapsed()") != 0) - throw duk::exception(m_ctx, -1); + if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) + throw dukx_exception(m_ctx, -1); - assertRange(duk::getGlobal<int>(m_ctx, "result"), 16); - } catch (const std::exception &ex) { - FAIL() << ex.what(); - } + duk_get_global_string(m_ctx, "result"); + assertRange(duk_to_int(m_ctx, -1), 16); + duk_pop(m_ctx); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } } TEST_F(TestElapsedTimer, doublecheck) { - try { - if (duk::pevalString(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) - throw duk::exception(m_ctx, -1); + try { + if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) + throw dukx_exception(m_ctx, -1); - std::this_thread::sleep_for(50ms); + std::this_thread::sleep_for(50ms); - if (duk::pevalString(m_ctx, "result = timer.elapsed()") != 0) - throw duk::exception(m_ctx, -1); + if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) + throw dukx_exception(m_ctx, -1); - std::this_thread::sleep_for(50ms); + std::this_thread::sleep_for(50ms); - if (duk::pevalString(m_ctx, "result = timer.elapsed()") != 0) - throw duk::exception(m_ctx, -1); + if (duk_peval_string(m_ctx, "result = timer.elapsed()") != 0) + throw dukx_exception(m_ctx, -1); - assertRange(duk::getGlobal<int>(m_ctx, "result"), 100); - } catch (const std::exception &ex) { - FAIL() << ex.what(); - } + duk_get_global_string(m_ctx, "result"); + assertRange(duk_to_int(m_ctx, -1), 100); + duk_pop(m_ctx); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } } int main(int argc, char **argv)