changeset 15:3b9ea4072263

Client: add JavaScript bindings for Point, #458
author David Demelier <markand@malikania.fr>
date Sun, 03 Apr 2016 11:42:03 +0200
parents 26efd2928f01
children a8aabea64f17
files libclient/CMakeLists.txt libclient/malikania/js-point.cpp libclient/malikania/js-point.h tests/libclient/CMakeLists.txt tests/libclient/js-point/CMakeLists.txt tests/libclient/js-point/main.cpp
diffstat 6 files changed, 439 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libclient/CMakeLists.txt	Sun Apr 03 10:23:23 2016 +0200
+++ b/libclient/CMakeLists.txt	Sun Apr 03 11:42:03 2016 +0200
@@ -25,6 +25,7 @@
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/font.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/image.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-color.h
+	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-point.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-window.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/label.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/line.h
@@ -46,6 +47,7 @@
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/font.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/image.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-color.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-point.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-size.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-window.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/label.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/js-point.cpp	Sun Apr 03 11:42:03 2016 +0200
@@ -0,0 +1,119 @@
+/*
+ * js-point.cpp -- point description (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 "js-point.h"
+
+namespace malikania {
+
+namespace {
+
+Point parse(duk::ContextPtr ctx, duk::Index index, bool required, Point point = {})
+{
+	duk::StackAssert sa(ctx);
+
+	if (duk::type(ctx, index) == DUK_TYPE_OBJECT){
+		if (required && !duk::hasProperty(ctx, index, "x")) {
+			duk::raise(ctx, DUK_ERR_ERROR, "missing x property in point description");
+		} else if (required && !duk::hasProperty(ctx, index, "y")) {
+			duk::raise(ctx, DUK_ERR_ERROR, "missing y property in point description");
+		}
+
+		point = Point(duk::getProperty<int>(ctx, index, "x"), duk::getProperty<int>(ctx, index, "y"));
+	} else if (required) {
+		duk::raise(ctx, DUK_ERR_TYPE_ERROR, "point object expected");
+	}
+
+	return point;
+}
+
+duk::Ret constructor(duk::ContextPtr ctx)
+{
+	Point point;
+
+	if (duk::top(ctx) == 2) {
+		point = Point(duk::require<int>(ctx, 0), duk::require<int>(ctx, 1));
+	} else if (duk::top(ctx) == 1) {
+		point = parse(ctx, 0, true);
+	}
+
+	duk::Ret ret;
+
+	/* Allow both constructor and non constructor calls */
+	if (duk_is_constructor_call(ctx)) {
+		duk::push(ctx, duk::This());
+		/* TODO: use duk::put when available */
+		duk::TypeTraits<Point>::put(ctx, point);
+		duk::pop(ctx);
+		ret = 0;
+	} else {
+		duk::push(ctx, point);
+		ret = 1;
+	}
+
+	return ret;
+}
+
+} // !namespace
+
+namespace duk {
+
+Point TypeTraits<Point>::get(ContextPtr ctx, Index index)
+{
+	return parse(ctx, index, false);
+}
+
+Point TypeTraits<Point>::require(ContextPtr ctx, Index index)
+{
+	return parse(ctx, index, true);
+}
+
+Point TypeTraits<Point>::optional(ContextPtr ctx, Index index, Point def)
+{
+	return parse(ctx, index, false, std::move(def));
+}
+
+void TypeTraits<Point>::push(ContextPtr ctx, const Point &point)
+{
+	duk::StackAssert sa(ctx, 1);
+
+	duk::push(ctx, duk::Object());
+	duk::TypeTraits<Point>::put(ctx, point);
+}
+
+void TypeTraits<Point>::put(ContextPtr ctx, const Point &point)
+{
+	assert(duk::type(ctx, -1) == DUK_TYPE_OBJECT);
+
+	duk::StackAssert sa(ctx);
+
+	duk::putProperty(ctx, -1, "x", point.x());
+	duk::putProperty(ctx, -1, "y", point.y());
+}
+
+} // !duk
+
+void loadMalikaniaPoint(duk::ContextPtr ctx)
+{
+	duk::StackAssert sa(ctx, 0);
+
+	duk::getGlobal<void>(ctx, "Malikania");
+	duk::putProperty(ctx, -1, "Point", duk::Function{constructor, DUK_VARARGS});
+	duk::pop(ctx);
+}
+
+} // !malikania
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/js-point.h	Sun Apr 03 11:42:03 2016 +0200
@@ -0,0 +1,49 @@
+/*
+ * js-point.h -- point description (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.
+ */
+
+#ifndef MALIKANIA_JS_POINT_H
+#define MALIKANIA_JS_POINT_H
+
+#include "js.h"
+#include "point.h"
+
+namespace malikania {
+
+namespace duk {
+
+template <>
+class TypeTraits<Point> {
+public:
+	static Point get(ContextPtr ctx, Index index);
+
+	static Point require(ContextPtr ctx, Index index);
+
+	static Point optional(ContextPtr ctx, Index index, Point def);
+
+	static void push(ContextPtr ctx, const Point &point);
+
+	static void put(ContextPtr ctx, const Point &point);
+};
+
+} // !duk
+
+void loadMalikaniaPoint(duk::ContextPtr ctx);
+
+} // !malikania
+
+#endif // !MALIKANIA_JS_POINT_H
--- a/tests/libclient/CMakeLists.txt	Sun Apr 03 10:23:23 2016 +0200
+++ b/tests/libclient/CMakeLists.txt	Sun Apr 03 11:42:03 2016 +0200
@@ -28,5 +28,6 @@
 
 # JavaScript bindings
 add_subdirectory(js-color)
+add_subdirectory(js-point)
 add_subdirectory(js-size)
 add_subdirectory(js-window)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/libclient/js-point/CMakeLists.txt	Sun Apr 03 11:42:03 2016 +0200
@@ -0,0 +1,23 @@
+#
+# CMakeLists.txt -- CMake build system for malikania
+#
+# 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.
+#
+
+malikania_create_test(
+	NAME js-point
+	LIBRARIES libclient
+	SOURCES main.cpp
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/libclient/js-point/main.cpp	Sun Apr 03 11:42:03 2016 +0200
@@ -0,0 +1,245 @@
+/*
+ * 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.h>
+
+using namespace malikania;
+
+class TestPoint : public testing::Test {
+protected:
+	duk::Context m_ctx;
+
+public:
+	TestPoint()
+	{
+		duk::putGlobal(m_ctx, "Malikania", duk::Object());
+
+		loadMalikaniaPoint(m_ctx);
+	}
+};
+
+/*
+ * Valid constructors
+ * ------------------------------------------------------------------
+ */
+
+TEST_F(TestPoint, ConstructorNoArgs)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"p = Malikania.Point();"
+			"x = p.x;"
+			"y = p.y;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestPoint, Constructor2Args)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"p = Malikania.Point(-10, -20);"
+			"x = p.x;"
+			"y = p.y;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(-10, duk::getGlobal<int>(m_ctx, "x"));
+		ASSERT_EQ(-20, duk::getGlobal<int>(m_ctx, "y"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestPoint, ConstructorObject)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"p = Malikania.Point({ x: 100, y: 200 });"
+			"x = p.x;"
+			"y = p.y;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x"));
+		ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestPoint, ConstructorNew)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"p = new Malikania.Point({ x: 100, y: 200 });"
+			"x = p.x;"
+			"y = p.y;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x"));
+		ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+/*
+ * Invalid constructors
+ * ------------------------------------------------------------------
+ */
+
+TEST_F(TestPoint, InvalidConstructorArg1)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Point(null);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof TypeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+/*
+ * Require.
+ * ------------------------------------------------------------------
+ */
+
+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::putGlobal(ctx, "x", point.x());
+			duk::putGlobal(ctx, "y", point.y());
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx, "build({ x: 100, y: 200 });");
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x"));
+		ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestPoint, requireFail)
+{
+	try {
+		duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			duk::require<Point>(ctx, 0);
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  build({});"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof Error);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+/*
+ * Get.
+ * ------------------------------------------------------------------
+ */
+
+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::putGlobal(ctx, "x", point.x());
+			duk::putGlobal(ctx, "y", point.y());
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx, "build({});");
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+int main(int argc, char **argv)
+{
+	testing::InitGoogleTest(&argc, argv);
+
+	return RUN_ALL_TESTS();
+}