diff 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 diff
--- a/tests/libclient/js-point/main.cpp	Fri Jul 01 13:32:08 2016 +0200
+++ b/tests/libclient/js-point/main.cpp	Wed Aug 10 14:30:51 2016 +0200
@@ -24,14 +24,14 @@
 
 class TestPoint : public testing::Test {
 protected:
-    duk::Context m_ctx;
+    UniqueContext m_ctx;
 
 public:
     TestPoint()
     {
-        duk::putGlobal(m_ctx, "Malikania", duk::Object());
-
-        loadMalikaniaPoint(m_ctx);
+        duk_push_object(m_ctx);
+        duk_put_global_string(m_ctx, "Malikania");
+        dukx_load_point(m_ctx);
     }
 };
 
@@ -43,18 +43,21 @@
 TEST_F(TestPoint, ConstructorNoArgs)
 {
     try {
-        auto ret = duk::pevalString(m_ctx,
+        auto ret = duk_peval_string(m_ctx,
             "p = Malikania.Point();"
             "x = p.x;"
             "y = p.y;"
         );
 
-        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"));
+        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();
     }
@@ -63,18 +66,21 @@
 TEST_F(TestPoint, Constructor2Args)
 {
     try {
-        auto ret = duk::pevalString(m_ctx,
+        auto ret = duk_peval_string(m_ctx,
             "p = Malikania.Point(-10, -20);"
             "x = p.x;"
             "y = p.y;"
         );
 
-        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"));
+        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();
     }
@@ -83,18 +89,21 @@
 TEST_F(TestPoint, ConstructorObject)
 {
     try {
-        auto ret = duk::pevalString(m_ctx,
+        auto ret = duk_peval_string(m_ctx,
             "p = Malikania.Point({ x: 100, y: 200 });"
             "x = p.x;"
             "y = p.y;"
         );
 
-        if (ret != 0) {
-            throw duk::exception(m_ctx, -1);
-        }
+        if (ret != 0)
+            throw dukx_exception(m_ctx, -1);
 
-        ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x"));
-        ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y"));
+        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();
     }
@@ -103,18 +112,21 @@
 TEST_F(TestPoint, ConstructorNew)
 {
     try {
-        auto ret = duk::pevalString(m_ctx,
+        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 duk::exception(m_ctx, -1);
-        }
+        if (ret != 0)
+            throw dukx_exception(m_ctx, -1);
 
-        ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x"));
-        ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y"));
+        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();
     }
@@ -128,7 +140,7 @@
 TEST_F(TestPoint, InvalidConstructorArg1)
 {
     try {
-        auto ret = duk::pevalString(m_ctx,
+        auto ret = duk_peval_string(m_ctx,
             "try {"
             "  Malikania.Point(null);"
             "} catch (e) {"
@@ -137,12 +149,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();
     }
@@ -156,23 +171,29 @@
 TEST_F(TestPoint, requireSuccess)
 {
     try {
-        duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
-            Point point = duk::require<Point>(ctx, 0);
+        duk_push_c_function(m_ctx, [] (auto ctx) {
+            Point point = dukx_require_point(ctx, 0);
 
-            duk::putGlobal(ctx, "x", point.x());
-            duk::putGlobal(ctx, "y", point.y());
+            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});
+        }, 1);
+        duk_put_global_string(m_ctx, "build");
 
-        auto ret = duk::pevalString(m_ctx, "build({ x: 100, y: 200 });");
+        auto ret = duk_peval_string(m_ctx, "build({ x: 100, y: 200 });");
 
-        if (ret != 0) {
-            throw duk::exception(m_ctx, -1);
-        }
+        if (ret != 0)
+            throw dukx_exception(m_ctx, -1);
 
-        ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x"));
-        ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y"));
+        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();
     }
@@ -181,13 +202,14 @@
 TEST_F(TestPoint, requireFail)
 {
     try {
-        duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
-            duk::require<Point>(ctx, 0);
+        duk_push_c_function(m_ctx, [] (auto ctx) {
+            dukx_require_point(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) {"
@@ -196,12 +218,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();
     }
@@ -215,23 +240,29 @@
 TEST_F(TestPoint, getAdjustAll)
 {
     try {
-        duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
-            Point point = duk::get<Point>(ctx, 0);
+        duk_push_c_function(m_ctx, [] (auto ctx) {
+            Point point = dukx_get_point(ctx, 0);
 
-            duk::putGlobal(ctx, "x", point.x());
-            duk::putGlobal(ctx, "y", point.y());
+            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});
+        }, 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 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"));
+        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();
     }