diff tests/libcommon/js-size/main.cpp @ 182:3107ce017c3a

Misc: switch back to SDL Qt Quick and QML was an exciting experiment but it's definitely not enough flexible and easy to use for game development. Using SDL2 will let us focusing on our own drawing functions without any kind of overhead. While here, start massive cleanup.
author David Demelier <markand@malikania.fr>
date Fri, 19 Oct 2018 20:18:19 +0200
parents
children eaa7f85bfc22
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/libcommon/js-size/main.cpp	Fri Oct 19 20:18:19 2018 +0200
@@ -0,0 +1,400 @@
+/*
+ * main.cpp -- test Size (JavaScript binding)
+ *
+ * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
+ *
+ * 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.
+ */
+
+#define BOOST_TEST_MODULE "Javascript Size"
+#include <boost/test/unit_test.hpp>
+
+#include <malikania/js_size.hpp>
+
+namespace mlk {
+
+namespace {
+
+class test_size {
+protected:
+	dukx_context m_ctx;
+
+public:
+	test_size()
+	{
+		duk_push_object(m_ctx);
+		duk_put_global_string(m_ctx, "Malikania");
+		dukx_load_size(m_ctx);
+	}
+};
+
+BOOST_FIXTURE_TEST_SUITE(test_size_suite, test_size)
+
+/*
+ * Valid constructors.
+ * ------------------------------------------------------------------
+ */
+
+BOOST_AUTO_TEST_SUITE(constructors)
+
+BOOST_AUTO_TEST_CASE(constructor_default)
+{
+	try {
+		const auto ret = duk_peval_string(m_ctx,
+			"s = Malikania.Size();"
+			"w = s.width;"
+			"h = s.height;"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "w");
+		BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "h");
+		BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_CASE(constructor_2_args)
+{
+	try {
+		const auto ret = duk_peval_string(m_ctx,
+			"s = Malikania.Size(100, 200);"
+			"w = s.width;"
+			"h = s.height;"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "w");
+		BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "h");
+		BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_CASE(constructor_object)
+{
+	try {
+		const auto ret = duk_peval_string(m_ctx,
+			"s = Malikania.Size({ width: 100, height: 200 });"
+			"w = s.width;"
+			"h = s.height;"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "w");
+		BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "h");
+		BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_CASE(constructor_new)
+{
+	try {
+		const auto ret = duk_peval_string(m_ctx,
+			"s = new Malikania.Size({ width: 100, height: 200 });"
+			"w = s.width;"
+			"h = s.height;"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "w");
+		BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "h");
+		BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+/*
+ * Invalid constructors.
+ * ------------------------------------------------------------------
+ */
+
+BOOST_AUTO_TEST_SUITE(invalid_constructors)
+
+BOOST_AUTO_TEST_CASE(constructor_arg_1)
+{
+	try {
+		const auto ret = duk_peval_string(m_ctx,
+			"try {"
+			"  Malikania.Size(null);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof TypeError);"
+			"}"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "name");
+		BOOST_REQUIRE_EQUAL("TypeError", duk_to_string(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "correct");
+		BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_CASE(constructor_range_1)
+{
+	try {
+		const auto ret = duk_peval_string(m_ctx,
+			"try {"
+			"  Malikania.Size(-1, 200);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "name");
+		BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "correct");
+		BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_CASE(constructor_range_2)
+{
+	try {
+		const auto ret = duk_peval_string(m_ctx,
+			"try {"
+			"  Malikania.Size(100, -1);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "name");
+		BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "correct");
+		BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_CASE(constructor_range_3)
+{
+	try {
+		const auto ret = duk_peval_string(m_ctx,
+			"try {"
+			"  Malikania.Size({ width: -1, height: 200 });"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "name");
+		BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "correct");
+		BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_CASE(constructor_range_4)
+{
+	try {
+		const auto ret = duk_peval_string(m_ctx,
+			"try {"
+			"  Malikania.Size({ width: 100, height: -1 });"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "name");
+		BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "correct");
+		BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+/*
+ * Require.
+ * ------------------------------------------------------------------
+ */
+
+BOOST_AUTO_TEST_SUITE(require)
+
+BOOST_AUTO_TEST_CASE(success)
+{
+	try {
+		duk_push_c_function(m_ctx, [] (auto ctx) {
+			const auto size = dukx_require_size(ctx, 0);
+
+			duk_push_uint(ctx, size.width);
+			duk_put_global_string(ctx, "w");
+			duk_push_uint(ctx, size.height);
+			duk_put_global_string(ctx, "h");
+
+			return 0;
+		}, 1);
+		duk_put_global_string(m_ctx, "build");
+
+		const auto ret = duk_peval_string(m_ctx, "build({ width: 100, height: 200 });");
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "w");
+		BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "h");
+		BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_CASE(fail)
+{
+	try {
+		duk_push_c_function(m_ctx, [] (auto ctx) {
+			dukx_require_size(ctx, 0);
+
+			return 0;
+		}, 1);
+		duk_put_global_string(m_ctx, "build");
+
+		const auto ret = duk_peval_string(m_ctx,
+			"try {"
+			"  build({});"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof Error);"
+			"}"
+		);
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "name");
+		BOOST_REQUIRE_EQUAL("Error", duk_to_string(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "correct");
+		BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+/*
+ * Get.
+ * ------------------------------------------------------------------
+ */
+
+BOOST_AUTO_TEST_SUITE(get)
+
+BOOST_AUTO_TEST_CASE(adjust_all)
+{
+	try {
+		duk_push_c_function(m_ctx, [] (auto ctx) {
+			const auto size = dukx_get_size(ctx, 0);
+
+			duk_push_uint(ctx, size.width);
+			duk_put_global_string(ctx, "w");
+			duk_push_uint(ctx, size.height);
+			duk_put_global_string(ctx, "h");
+
+			return 0;
+		}, 1);
+		duk_put_global_string(m_ctx, "build");
+
+		const auto ret = duk_peval_string(m_ctx, "build({});");
+
+		if (ret != 0)
+			throw dukx_get_exception(m_ctx, -1);
+
+		duk_get_global_string(m_ctx, "w");
+		BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+		duk_get_global_string(m_ctx, "h");
+		BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1));
+		duk_pop(m_ctx);
+	} catch (const std::exception &ex) {
+		BOOST_FAIL(ex.what());
+	}
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // !namespace
+
+} // !mlk