changeset 13:fb7c2c096101

Client: add JavaScript bindings for Size, #460
author David Demelier <markand@malikania.fr>
date Sat, 02 Apr 2016 16:05:44 +0200
parents 0c62e0af6af7
children 26efd2928f01
files libclient/CMakeLists.txt libclient/malikania/js-size.cpp libclient/malikania/js-size.h tests/libclient/CMakeLists.txt tests/libclient/js-size/CMakeLists.txt tests/libclient/js-size/main.cpp
diffstat 6 files changed, 600 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libclient/CMakeLists.txt	Fri Apr 01 13:45:45 2016 +0200
+++ b/libclient/CMakeLists.txt	Sat Apr 02 16:05:44 2016 +0200
@@ -45,6 +45,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-size.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/label.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/sprite.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/window.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/js-size.cpp	Sat Apr 02 16:05:44 2016 +0200
@@ -0,0 +1,138 @@
+/*
+ * js-size.cpp -- size 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-size.h"
+
+namespace malikania {
+
+namespace {
+
+unsigned clamp(duk::ContextPtr ctx, int value, bool required)
+{
+	if (value < 0) {
+		if (required) {
+			duk::raise(ctx, DUK_ERR_RANGE_ERROR, "%d can not be negative", value);
+		} else {
+			value = 0;
+		}
+	}
+
+	return static_cast<unsigned>(value);
+}
+
+Size parse(duk::ContextPtr ctx, duk::Index index, bool required, Size size = {})
+{
+	duk::StackAssert sa(ctx);
+
+	if (duk::type(ctx, index) == DUK_TYPE_OBJECT) {
+		if (required && !duk::hasProperty(ctx, index, "width")) {
+			duk::raise(ctx, DUK_ERR_ERROR, "missing width property in size description");
+		} else if (required && !duk::hasProperty(ctx, index, "height")) {
+			duk::raise(ctx, DUK_ERR_ERROR, "missing height property in size description");
+		}
+
+		size = Size(
+			clamp(ctx, duk::getProperty<int>(ctx, index, "width"), required),
+			clamp(ctx, duk::getProperty<int>(ctx, index, "height"), required)
+		);
+	} else if (required) {
+		duk::raise(ctx, DUK_ERR_TYPE_ERROR, "size object expected");
+	}
+
+	return size;
+}
+
+duk::Ret constructor(duk::ContextPtr ctx)
+{
+	malikania::Size size;
+
+	if (duk::top(ctx) == 2) {
+		size = malikania::Size(
+			clamp(ctx, duk::require<int>(ctx, 0), true),
+			clamp(ctx, duk::require<int>(ctx, 1), true)
+		);
+	} else if (duk::top(ctx) == 1) {
+		size = 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<malikania::Size>::put(ctx, size);
+		duk::pop(ctx);
+		ret = 0;
+	} else {
+		duk::push(ctx, size);
+		ret = 1;
+	}
+
+	return ret;
+}
+
+} // !namespace
+
+namespace duk {
+
+malikania::Size TypeTraits<malikania::Size>::get(ContextPtr ctx, Index index)
+{
+	return parse(ctx, index, false);
+}
+
+malikania::Size TypeTraits<malikania::Size>::require(ContextPtr ctx, Index index)
+{
+	return parse(ctx, index, true);
+}
+
+malikania::Size TypeTraits<malikania::Size>::optional(ContextPtr ctx, Index index, malikania::Size def)
+{
+	return parse(ctx, index, false, std::move(def));
+}
+
+void TypeTraits<malikania::Size>::push(ContextPtr ctx, const malikania::Size &size)
+{
+	StackAssert sa(ctx, 1);
+
+	duk::push(ctx, duk::Object());
+	duk::TypeTraits<malikania::Size>::put(ctx, size);
+}
+
+void TypeTraits<malikania::Size>::put(ContextPtr ctx, const malikania::Size &size)
+{
+	assert(type(ctx, -1) == DUK_TYPE_OBJECT);
+
+	StackAssert sa(ctx, 0);
+
+	duk::putProperty(ctx, -1, "width", static_cast<int>(size.width()));
+	duk::putProperty(ctx, -1, "height", static_cast<int>(size.height()));
+}
+
+} // !duk
+
+void loadMalikaniaSize(duk::ContextPtr ctx)
+{
+	duk::StackAssert sa(ctx, 0);
+
+	duk::getGlobal<void>(ctx, "Malikania");
+	duk::putProperty(ctx, -1, "Size", duk::Function{constructor, DUK_VARARGS});
+	duk::pop(ctx);
+}
+
+} // !malikania
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/js-size.h	Sat Apr 02 16:05:44 2016 +0200
@@ -0,0 +1,99 @@
+/*
+ * js-size.h -- size 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_SIZE_H
+#define MALIKANIA_JS_SIZE_H
+
+#include "js.h"
+#include "size.h"
+
+namespace malikania {
+
+namespace duk {
+
+/**
+ * @brief JavaScript binding for Size.
+ *
+ * Size are plain objects.
+ *
+ * ````
+ * {
+ *   width: 1000,
+ *   height: 2000
+ * }
+ * ````
+ */
+template <>
+class TypeTraits<malikania::Size> {
+public:
+	/**
+	 * Get a size.
+	 *
+	 * The size may be adjusted if any values are incorrect.
+	 *
+	 * @param ctx the context
+	 * @param index the value index
+	 * @return the size
+	 */
+	static malikania::Size get(ContextPtr ctx, Index index);
+
+	/**
+	 * Require a size
+	 *
+	 * If the object is not a size, raise a JavaScript error.
+	 *
+	 * @param ctx the context
+	 * @param index the index
+	 * @return the size
+	 */
+	static malikania::Size require(ContextPtr ctx, Index index);
+
+	/**
+	 * Like get but return the default value if the value at the given index is not an object.
+	 *
+	 * @param ctx the context
+	 * @param index the idnex
+	 * @param def the default value
+	 * @return the size
+	 */
+	static malikania::Size optional(ContextPtr ctx, Index index, malikania::Size def);
+
+	/**
+	 * Push the size as object.
+	 *
+	 * @param ctx the context
+	 * @param size the size
+	 */
+	static void push(ContextPtr ctx, const malikania::Size &size);
+
+	/**
+	 * Put the size properties into the object at the top of the stack.
+	 *
+	 * @param ctx the context
+	 * @param size the size
+	 */
+	static void put(ContextPtr ctx, const malikania::Size &size);
+};
+
+} // !duk
+
+void loadMalikaniaSize(duk::ContextPtr ctx);
+
+} // !malikania
+
+#endif // !MALIKANIA_JS_SIZE_H
--- a/tests/libclient/CMakeLists.txt	Fri Apr 01 13:45:45 2016 +0200
+++ b/tests/libclient/CMakeLists.txt	Sat Apr 02 16:05:44 2016 +0200
@@ -27,4 +27,5 @@
 add_subdirectory(sprite)
 
 # JavaScript bindings
-add_subdirectory(js-color)
\ No newline at end of file
+add_subdirectory(js-color)
+add_subdirectory(js-size)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/libclient/js-size/CMakeLists.txt	Sat Apr 02 16:05:44 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-size
+	LIBRARIES libclient
+	SOURCES main.cpp
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/libclient/js-size/main.cpp	Sat Apr 02 16:05:44 2016 +0200
@@ -0,0 +1,337 @@
+/*
+ * main.cpp -- test Size (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-size.h>
+
+using namespace malikania;
+
+class TestSize : public testing::Test {
+protected:
+	duk::Context m_ctx;
+
+public:
+	TestSize()
+	{
+		duk::putGlobal(m_ctx, "Malikania", duk::Object());
+
+		loadMalikaniaSize(m_ctx);
+	}
+};
+
+/*
+ * Valid constructors
+ * ------------------------------------------------------------------
+ */
+
+TEST_F(TestSize, ConstructorNoArgs)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"s = Malikania.Size();"
+			"w = s.width;"
+			"h = s.height;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "w"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "h"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSize, Constructor2Args)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"s = Malikania.Size(100, 200);"
+			"w = s.width;"
+			"h = s.height;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w"));
+		ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSize, ConstructorObject)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"s = Malikania.Size({ width: 100, height: 200 });"
+			"w = s.width;"
+			"h = s.height;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w"));
+		ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSize, ConstructorNew)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"s = new Malikania.Size({ width: 100, height: 200 });"
+			"w = s.width;"
+			"h = s.height;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w"));
+		ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+/*
+ * Invalid constructors
+ * ------------------------------------------------------------------
+ */
+
+TEST_F(TestSize, InvalidConstructorArg1)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Size(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();
+	}
+}
+
+TEST_F(TestSize, InvalidConstructorRange1)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Size(-1, 200);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSize, InvalidConstructorRange2)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Size(100, -1);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSize, InvalidConstructorRange3)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Size({ width: -1, height: 200 });"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSize, InvalidConstructorRange4)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Size({ width: 100, height: -1 });"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", 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(TestSize, requireSuccess)
+{
+	try {
+		duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			Size size = duk::require<Size>(ctx, 0);
+
+			duk::putGlobal(ctx, "w", static_cast<int>(size.width()));
+			duk::putGlobal(ctx, "h", static_cast<int>(size.height()));
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx, "build({ width: 100, height: 200 });");
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w"));
+		ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestSize, requireFail)
+{
+	try {
+		duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			duk::require<Size>(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(TestSize, getAdjustAll)
+{
+	try {
+		duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			Size size = duk::get<Size>(ctx, 0);
+
+			duk::putGlobal(ctx, "w", static_cast<int>(size.width()));
+			duk::putGlobal(ctx, "h", static_cast<int>(size.height()));
+
+			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, "r"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+int main(int argc, char **argv)
+{
+	testing::InitGoogleTest(&argc, argv);
+
+	return RUN_ALL_TESTS();
+}