Mercurial > malikania
diff tests/libclient/js-rectangle/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/libclient/js-rectangle/main.cpp Fri Jul 01 13:32:08 2016 +0200 +++ b/tests/libclient/js-rectangle/main.cpp Wed Aug 10 14:30:51 2016 +0200 @@ -24,14 +24,14 @@ class TestRectangle : public testing::Test { protected: - duk::Context m_ctx; + UniqueContext m_ctx; public: TestRectangle() { - duk::putGlobal(m_ctx, "Malikania", duk::Object()); - - loadMalikaniaRectangle(m_ctx); + duk_push_object(m_ctx); + duk_put_global_string(m_ctx, "Malikania"); + dukx_load_rect(m_ctx); } }; @@ -43,7 +43,7 @@ TEST_F(TestRectangle, ConstructorNoArgs) { try { - auto ret = duk::pevalString(m_ctx, + auto ret = duk_peval_string(m_ctx, "r = Malikania.Rectangle();" "x = r.x;" "y = r.y;" @@ -51,14 +51,21 @@ "h = r.height;" ); - if (ret != 0) { - throw duk::exception(m_ctx, -1); - } + if (ret != 0) + throw dukx_exception(m_ctx, -1); - ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); - ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); - ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "w")); - ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "h")); + 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); + duk_get_global_string(m_ctx, "w"); + ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); + duk_get_global_string(m_ctx, "h"); + ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } @@ -67,7 +74,7 @@ TEST_F(TestRectangle, Constructor4Args) { try { - auto ret = duk::pevalString(m_ctx, + auto ret = duk_peval_string(m_ctx, "r = Malikania.Rectangle(10, 20, 30, 40);" "x = r.x;" "y = r.y;" @@ -75,14 +82,21 @@ "h = r.height;" ); - if (ret != 0) { - throw duk::exception(m_ctx, -1); - } + if (ret != 0) + throw dukx_exception(m_ctx, -1); - ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x")); - ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y")); - ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "w")); - ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "h")); + 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); + duk_get_global_string(m_ctx, "w"); + ASSERT_EQ(30U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); + duk_get_global_string(m_ctx, "h"); + ASSERT_EQ(40U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } @@ -91,7 +105,7 @@ TEST_F(TestRectangle, ConstructorObject) { try { - auto ret = duk::pevalString(m_ctx, + auto ret = duk_peval_string(m_ctx, "r = Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" "x = r.x;" "y = r.y;" @@ -99,14 +113,21 @@ "h = r.height;" ); - if (ret != 0) { - throw duk::exception(m_ctx, -1); - } + if (ret != 0) + throw dukx_exception(m_ctx, -1); - ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x")); - ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y")); - ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "w")); - ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "h")); + 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); + duk_get_global_string(m_ctx, "w"); + ASSERT_EQ(30U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); + duk_get_global_string(m_ctx, "h"); + ASSERT_EQ(40U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } @@ -115,7 +136,7 @@ TEST_F(TestRectangle, ConstructorNew) { try { - auto ret = duk::pevalString(m_ctx, + auto ret = duk_peval_string(m_ctx, "r = new Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" "x = r.x;" "y = r.y;" @@ -123,14 +144,21 @@ "h = r.height;" ); - if (ret != 0) { - throw duk::exception(m_ctx, -1); - } + if (ret != 0) + throw dukx_exception(m_ctx, -1); - ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x")); - ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y")); - ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "w")); - ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "h")); + 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); + duk_get_global_string(m_ctx, "w"); + ASSERT_EQ(30U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); + duk_get_global_string(m_ctx, "h"); + ASSERT_EQ(40U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } @@ -144,7 +172,7 @@ TEST_F(TestRectangle, InvalidConstructorArg1) { try { - auto ret = duk::pevalString(m_ctx, + auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Rectangle(null);" "} catch (e) {" @@ -153,12 +181,15 @@ "}" ); - if (ret != 0) { - throw duk::exception(m_ctx, -1); - } + if (ret != 0) + throw dukx_exception(m_ctx, -1); - ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name")); - ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); + 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(); } @@ -167,7 +198,7 @@ TEST_F(TestRectangle, InvalidConstructorRange1) { try { - auto ret = duk::pevalString(m_ctx, + auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Rectangle(0, 0, -10, -10);" "} catch (e) {" @@ -176,12 +207,15 @@ "}" ); - if (ret != 0) { - throw duk::exception(m_ctx, -1); - } + if (ret != 0) + throw dukx_exception(m_ctx, -1); - ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); - ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); + duk_get_global_string(m_ctx, "name"); + ASSERT_STREQ("RangeError", 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(); } @@ -195,27 +229,39 @@ TEST_F(TestRectangle, requireSuccess) { try { - duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { - Rectangle rect = duk::require<Rectangle>(ctx, 0); + duk_push_c_function(m_ctx, [] (auto ctx) { + Rectangle rect = dukx_require_rect(ctx, 0); - duk::putGlobal(ctx, "x", rect.x()); - duk::putGlobal(ctx, "y", rect.y()); - duk::putGlobal(ctx, "w", static_cast<int>(rect.width())); - duk::putGlobal(ctx, "h", static_cast<int>(rect.height())); + duk_push_int(ctx, rect.x()); + duk_put_global_string(ctx, "x"); + duk_push_int(ctx, rect.y()); + duk_put_global_string(ctx, "y"); + duk_push_uint(ctx, rect.width()); + duk_put_global_string(ctx, "w"); + duk_push_uint(ctx, rect.height()); + duk_put_global_string(ctx, "h"); return 0; - }, 1}); + }, 1); + duk_put_global_string(m_ctx, "build"); - auto ret = duk::pevalString(m_ctx, "build({ x: 50, y: 80, width: 100, height: 200 });"); + auto ret = duk_peval_string(m_ctx, "build({ x: 50, y: 80, width: 100, height: 200 });"); + + if (ret != 0) + throw dukx_exception(m_ctx, -1); - if (ret != 0) { - throw duk::exception(m_ctx, -1); - } - - ASSERT_EQ(50, duk::getGlobal<int>(m_ctx, "x")); - ASSERT_EQ(80, duk::getGlobal<int>(m_ctx, "y")); - ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); - ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); + duk_get_global_string(m_ctx, "x"); + ASSERT_EQ(50, duk_to_int(m_ctx, -1)); + duk_pop(m_ctx); + duk_get_global_string(m_ctx, "y"); + ASSERT_EQ(80, duk_to_int(m_ctx, -1)); + duk_pop(m_ctx); + duk_get_global_string(m_ctx, "w"); + ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); + duk_get_global_string(m_ctx, "h"); + ASSERT_EQ(200U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } @@ -224,13 +270,14 @@ TEST_F(TestRectangle, requireFail) { try { - duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { - duk::require<Rectangle>(ctx, 0); + duk_push_c_function(m_ctx, [] (auto ctx) { + dukx_require_rect(ctx, 0); return 0; - }, 1}); + }, 1); + duk_put_global_string(m_ctx, "build"); - auto ret = duk::pevalString(m_ctx, + auto ret = duk_peval_string(m_ctx, "try {" " build({});" "} catch (e) {" @@ -239,12 +286,15 @@ "}" ); - if (ret != 0) { - throw duk::exception(m_ctx, -1); - } + if (ret != 0) + throw dukx_exception(m_ctx, -1); - ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); - ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); + 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(); } @@ -258,27 +308,38 @@ TEST_F(TestRectangle, getAdjustAll) { try { - duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { - Rectangle rect = duk::get<Rectangle>(ctx, 0); + duk_push_c_function(m_ctx, [] (auto ctx) { + Rectangle rect = dukx_get_rect(ctx, 0); - duk::putGlobal(ctx, "x", rect.x()); - duk::putGlobal(ctx, "y", rect.y()); - duk::putGlobal(ctx, "w", static_cast<int>(rect.width())); - duk::putGlobal(ctx, "h", static_cast<int>(rect.height())); + duk_push_int(ctx, rect.x()); + duk_put_global_string(ctx, "x"); + duk_push_int(ctx, rect.y()); + duk_put_global_string(ctx, "y"); + duk_push_uint(ctx, rect.width()); + duk_put_global_string(ctx, "w"); + duk_push_uint(ctx, rect.height()); return 0; - }, 1}); + }, 1); + duk_put_global_string(m_ctx, "build"); - auto ret = duk::pevalString(m_ctx, "build({});"); + auto ret = duk_peval_string(m_ctx, "build({});"); + + if (ret != 0) + throw dukx_exception(m_ctx, -1); - if (ret != 0) { - throw duk::exception(m_ctx, -1); - } - - ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); - ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); - ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "w")); - ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "h")); + 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); + duk_get_global_string(m_ctx, "w"); + ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); + duk_get_global_string(m_ctx, "h"); + ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); + duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); }