changeset 17:63ba461b7f84

Client: add JavaScript bindings for Line, #465
author David Demelier <markand@malikania.fr>
date Sun, 03 Apr 2016 14:50:03 +0200
parents a8aabea64f17
children cc13926bed59
files libclient/CMakeLists.txt libclient/malikania/js-line.cpp libclient/malikania/js-line.h tests/libclient/CMakeLists.txt tests/libclient/js-line/CMakeLists.txt tests/libclient/js-line/main.cpp
diffstat 6 files changed, 529 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libclient/CMakeLists.txt	Sun Apr 03 12:20:12 2016 +0200
+++ b/libclient/CMakeLists.txt	Sun Apr 03 14:50: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-line.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-point.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-rectangle.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-window.h
@@ -48,6 +49,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-line.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-point.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-rectangle.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-size.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/js-line.cpp	Sun Apr 03 14:50:03 2016 +0200
@@ -0,0 +1,135 @@
+/*
+ * js-line.cpp -- line 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-line.h"
+
+namespace malikania {
+
+namespace {
+
+duk::Ret constructor(duk::ContextPtr ctx)
+{
+	Line line;
+
+	if (duk::top(ctx) == 4) {
+		line = Line(
+			duk::get<int>(ctx, 0),
+			duk::get<int>(ctx, 1),
+			duk::get<int>(ctx, 2),
+			duk::get<int>(ctx, 3)
+		);
+	} else if (duk::top(ctx) == 1) {
+		line = duk::require<Line>(ctx, 0);
+	}
+
+	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<Line>::put(ctx, line);
+		duk::pop(ctx);
+		ret = 0;
+	} else {
+		duk::push(ctx, line);
+		ret = 1;
+	}
+
+	return ret;
+}
+
+} // !namespace
+
+namespace duk {
+
+Line TypeTraits<Line>::get(ContextPtr ctx, Index index)
+{
+	duk::StackAssert sa(ctx);
+
+	return Line(
+		duk::getProperty<int>(ctx, index, "x1"),
+		duk::getProperty<int>(ctx, index, "y1"),
+		duk::getProperty<int>(ctx, index, "x2"),
+		duk::getProperty<int>(ctx, index, "y2")
+	);
+}
+
+Line TypeTraits<Line>::require(ContextPtr ctx, Index index)
+{
+	duk::StackAssert sa(ctx);
+
+	auto get = [&] (const std::string &property) -> int {
+		if (!duk::hasProperty(ctx, index, property)) {
+			duk::raise(ctx, DUK_ERR_ERROR, "missing %s property in line description", property.c_str());
+		}
+
+		duk::getProperty<void>(ctx, index, property);
+
+		if (!duk::is<int>(ctx, -1)) {
+			duk::pop(ctx);
+			duk::raise(ctx, DUK_ERR_TYPE_ERROR, "property %s is not an int", property.c_str());
+		}
+
+		int value = duk::get<int>(ctx, -1);
+
+		duk::pop(ctx);
+
+		return value;
+	};
+
+	return Line(get("x1"), get("y1"), get("x2"), get("y2"));
+}
+
+Line TypeTraits<Line>::optional(ContextPtr ctx, Index index, Line def)
+{
+	return duk::is<duk::Object>(ctx, index) ? get(ctx, index) : def;
+}
+
+void TypeTraits<Line>::push(ContextPtr ctx, const Line &line)
+{
+	duk::StackAssert sa(ctx, 1);
+
+	duk::push(ctx, duk::Object());
+	duk::TypeTraits<Line>::put(ctx, line);
+}
+
+void TypeTraits<Line>::put(ContextPtr ctx, const Line &line)
+{
+	assert(duk::is<duk::Object>(ctx, -1));
+
+	duk::StackAssert sa(ctx);
+
+	duk::putProperty(ctx, -1, "x1", line.x1());
+	duk::putProperty(ctx, -1, "y1", line.y1());
+	duk::putProperty(ctx, -1, "x2", line.x2());
+	duk::putProperty(ctx, -1, "y2", line.y2());
+}
+
+} // !duk
+
+void loadMalikaniaLine(duk::ContextPtr ctx)
+{
+	duk::StackAssert sa(ctx, 0);
+
+	duk::getGlobal<void>(ctx, "Malikania");
+	duk::putProperty(ctx, -1, "Line", duk::Function{constructor, DUK_VARARGS});
+	duk::pop(ctx);
+}
+
+} // !malikania
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/js-line.h	Sun Apr 03 14:50:03 2016 +0200
@@ -0,0 +1,99 @@
+/*
+ * js-line.h -- line 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_LINE_H
+#define MALIKANIA_JS_LINE_H
+
+#include "js.h"
+#include "line.h"
+
+namespace malikania {
+
+namespace duk {
+
+/**
+ * @brief JavaScript binding for Line.
+ *
+ * Lines are plain objects.
+ *
+ * ````
+ * {
+ *   x1: 10,
+ *   y1: 10,
+ *   x2: 50,
+ *   y2: 50
+ * }
+ * ````
+ */
+template <>
+class TypeTraits<Line> {
+public:
+	/**
+	 * Get a line.
+	 *
+	 * @param ctx the context
+	 * @param index the index
+	 * @return the line
+	 */
+	static Line get(ContextPtr ctx, Index index);
+
+	/**
+	 * Require a line.
+	 *
+	 * If value is not an object or any property is invalid, raise a JavaScript error.
+	 *
+	 * @param ctx the context
+	 * @param index the index
+	 * @return the line
+	 */
+	static Line require(ContextPtr ctx, Index index);
+
+	/**
+	 * Like get but return def if the value at the given index is not an object.
+	 *
+	 * @param ctx the context
+	 * @param index the index
+	 * @param def the default value
+	 * @return the line
+	 */
+	static Line optional(ContextPtr ctx, Index index, Line def);
+
+	/**
+	 * Push the line as object.
+	 *
+	 * @param ctx the context
+	 * @param line the line
+	 */
+	static void push(ContextPtr ctx, const Line &line);
+
+	/**
+	 * Put the line properties into the object at the top of the stack.
+	 *
+	 * @param ctx the context
+	 * @param line the line
+	 */
+	static void put(ContextPtr ctx, const Line &line);
+};
+
+} // !duk
+
+void loadMalikaniaLine(duk::ContextPtr ctx);
+
+} // !malikania
+
+#endif // !MALIKANIA_JS_LINE_H
--- a/tests/libclient/CMakeLists.txt	Sun Apr 03 12:20:12 2016 +0200
+++ b/tests/libclient/CMakeLists.txt	Sun Apr 03 14:50:03 2016 +0200
@@ -28,6 +28,7 @@
 
 # JavaScript bindings
 add_subdirectory(js-color)
+add_subdirectory(js-line)
 add_subdirectory(js-point)
 add_subdirectory(js-rectangle)
 add_subdirectory(js-size)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/libclient/js-line/CMakeLists.txt	Sun Apr 03 14:50: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-line
+	LIBRARIES libclient
+	SOURCES main.cpp
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/libclient/js-line/main.cpp	Sun Apr 03 14:50:03 2016 +0200
@@ -0,0 +1,269 @@
+/*
+ * main.cpp -- test Line (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-line.h>
+
+using namespace malikania;
+
+class TestLine : public testing::Test {
+protected:
+	duk::Context m_ctx;
+
+public:
+	TestLine()
+	{
+		duk::putGlobal(m_ctx, "Malikania", duk::Object());
+
+		loadMalikaniaLine(m_ctx);
+	}
+};
+
+/*
+ * Valid constructors
+ * ------------------------------------------------------------------
+ */
+
+TEST_F(TestLine, ConstructorNoArgs)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"r = Malikania.Line();"
+			"x1 = r.x1;"
+			"y1 = r.y1;"
+			"x2 = r.x2;"
+			"y2 = r.y2;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x1"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y1"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x2"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y2"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestLine, Constructor4Args)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"r = Malikania.Line(10, 20, 30, 40);"
+			"x1 = r.x1;"
+			"y1 = r.y1;"
+			"x2 = r.x2;"
+			"y2 = r.y2;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x1"));
+		ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y1"));
+		ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "x2"));
+		ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "y2"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestLine, ConstructorObject)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"r = Malikania.Line({ x1: 10, y1: 20, x2: 30, y2: 40 });"
+			"x1 = r.x1;"
+			"y1 = r.y1;"
+			"x2 = r.x2;"
+			"y2 = r.y2;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x1"));
+		ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y1"));
+		ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "x2"));
+		ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "y2"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestLine, ConstructorNew)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"r = new Malikania.Line({ x1: 10, y1: 20, x2: 30, y2: 40 });"
+			"x1 = r.x1;"
+			"y1 = r.y1;"
+			"x2 = r.x2;"
+			"y2 = r.y2;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x1"));
+		ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y1"));
+		ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "x2"));
+		ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "y2"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+/*
+ * Invalid constructors
+ * ------------------------------------------------------------------
+ */
+
+TEST_F(TestLine, InvalidConstructorArg1)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Line(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(TestLine, requireSuccess)
+{
+	try {
+		duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			Line line = duk::require<Line>(ctx, 0);
+
+			duk::putGlobal(ctx, "x1", line.x1());
+			duk::putGlobal(ctx, "y1", line.y1());
+			duk::putGlobal(ctx, "x2", line.x2());
+			duk::putGlobal(ctx, "y2", line.y2());
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx, "build({ x1: 50, y1: 80, x2: 100, y2: 200 });");
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(50, duk::getGlobal<int>(m_ctx, "x1"));
+		ASSERT_EQ(80, duk::getGlobal<int>(m_ctx, "y1"));
+		ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x2"));
+		ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y2"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestLine, requireFail)
+{
+	try {
+		duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			duk::require<Line>(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(TestLine, getAdjustAll)
+{
+	try {
+		duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			Line line = duk::get<Line>(ctx, 0);
+
+			duk::putGlobal(ctx, "x1", line.x1());
+			duk::putGlobal(ctx, "y1", line.y1());
+			duk::putGlobal(ctx, "x2", line.x2());
+			duk::putGlobal(ctx, "y2", line.y2());
+
+			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, "x1"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y1"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x2"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y2"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+int main(int argc, char **argv)
+{
+	testing::InitGoogleTest(&argc, argv);
+
+	return RUN_ALL_TESTS();
+}