Mercurial > malikania
view tests/libclient/js-point/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 source
/* * main.cpp -- test Point (JavaScript binding) * * Copyright (c) 2013-2016 Malikania Authors * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <gtest/gtest.h> #include <malikania/js-point.hpp> using namespace malikania; class TestPoint : public testing::Test { protected: UniqueContext m_ctx; public: TestPoint() { duk_push_object(m_ctx); duk_put_global_string(m_ctx, "Malikania"); dukx_load_point(m_ctx); } }; /* * Valid constructors * ------------------------------------------------------------------ */ TEST_F(TestPoint, ConstructorNoArgs) { try { auto ret = duk_peval_string(m_ctx, "p = Malikania.Point();" "x = p.x;" "y = p.y;" ); if (ret != 0) throw dukx_exception(m_ctx, -1); duk_get_global_string(m_ctx, "x"); ASSERT_EQ(0, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "y"); ASSERT_EQ(0, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestPoint, Constructor2Args) { try { auto ret = duk_peval_string(m_ctx, "p = Malikania.Point(-10, -20);" "x = p.x;" "y = p.y;" ); if (ret != 0) throw dukx_exception(m_ctx, -1); duk_get_global_string(m_ctx, "x"); ASSERT_EQ(-10, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "y"); ASSERT_EQ(-20, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestPoint, ConstructorObject) { try { auto ret = duk_peval_string(m_ctx, "p = Malikania.Point({ x: 100, y: 200 });" "x = p.x;" "y = p.y;" ); if (ret != 0) throw dukx_exception(m_ctx, -1); duk_get_global_string(m_ctx, "x"); ASSERT_EQ(100, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "y"); ASSERT_EQ(200, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestPoint, ConstructorNew) { try { auto ret = duk_peval_string(m_ctx, "p = new Malikania.Point({ x: 100, y: 200 });" "x = p.x;" "y = p.y;" ); if (ret != 0) throw dukx_exception(m_ctx, -1); duk_get_global_string(m_ctx, "x"); ASSERT_EQ(100, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "y"); ASSERT_EQ(200, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * Invalid constructors * ------------------------------------------------------------------ */ TEST_F(TestPoint, InvalidConstructorArg1) { try { auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Point(null);" "} catch (e) {" " name = e.name;" " correct = (e instanceof TypeError);" "}" ); if (ret != 0) throw dukx_exception(m_ctx, -1); duk_get_global_string(m_ctx, "name"); ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * Require. * ------------------------------------------------------------------ */ TEST_F(TestPoint, requireSuccess) { try { duk_push_c_function(m_ctx, [] (auto ctx) { Point point = dukx_require_point(ctx, 0); duk_push_int(ctx, point.x()); duk_put_global_string(ctx, "x"); duk_push_int(ctx, point.y()); duk_put_global_string(ctx, "y"); return 0; }, 1); duk_put_global_string(m_ctx, "build"); auto ret = duk_peval_string(m_ctx, "build({ x: 100, y: 200 });"); if (ret != 0) throw dukx_exception(m_ctx, -1); duk_get_global_string(m_ctx, "x"); ASSERT_EQ(100, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "y"); ASSERT_EQ(200, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestPoint, requireFail) { try { duk_push_c_function(m_ctx, [] (auto ctx) { dukx_require_point(ctx, 0); return 0; }, 1); duk_put_global_string(m_ctx, "build"); auto ret = duk_peval_string(m_ctx, "try {" " build({});" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) throw dukx_exception(m_ctx, -1); duk_get_global_string(m_ctx, "name"); ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * Get. * ------------------------------------------------------------------ */ TEST_F(TestPoint, getAdjustAll) { try { duk_push_c_function(m_ctx, [] (auto ctx) { Point point = dukx_get_point(ctx, 0); duk_push_int(ctx, point.x()); duk_put_global_string(ctx, "x"); duk_push_int(ctx, point.y()); duk_put_global_string(ctx, "y"); return 0; }, 1); duk_put_global_string(m_ctx, "build"); auto ret = duk_peval_string(m_ctx, "build({});"); if (ret != 0) throw dukx_exception(m_ctx, -1); duk_get_global_string(m_ctx, "x"); ASSERT_EQ(0, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "y"); ASSERT_EQ(0, duk_to_int(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }