view tests/libcommon/js-rectangle/main.cpp @ 187:eaa7f85bfc22

Tests: use BOOST_TEST, closes #917 @1h
author David Demelier <markand@malikania.fr>
date Sat, 20 Oct 2018 21:58:32 +0200
parents 16ff680a8a94
children 0cecdadfb5c4
line wrap: on
line source

/*
 * main.cpp -- test Rectangle (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 Rectangle"
#include <boost/test/unit_test.hpp>

#include <malikania/js_rectangle.hpp>

namespace mlk {

namespace {

class test_rectangle {
protected:
	dukx_context m_ctx;

public:
	test_rectangle()
	{
		duk_push_object(m_ctx);
		duk_put_global_string(m_ctx, "Malikania");
		dukx_load_rect(m_ctx);
	}
};

BOOST_FIXTURE_TEST_SUITE(test_rectangle_suite, test_rectangle)

/*
 * Valid constructors.
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(constructors)

BOOST_AUTO_TEST_CASE(constructor_default)
{
	const auto ret = duk_peval_string(m_ctx,
		"r = Malikania.Rectangle();"
		"x = r.x;"
		"y = r.y;"
		"w = r.width;"
		"h = r.height;"
	);

	if (ret != 0)
		throw dukx_get_exception(m_ctx, -1);

	duk_get_global_string(m_ctx, "x");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 0);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "y");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 0);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "w");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 0U);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "h");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 0U);
	duk_pop(m_ctx);
}

BOOST_AUTO_TEST_CASE(constructor_4_args)
{
	const auto ret = duk_peval_string(m_ctx,
		"r = Malikania.Rectangle(10, 20, 30, 40);"
		"x = r.x;"
		"y = r.y;"
		"w = r.width;"
		"h = r.height;"
	);

	if (ret != 0)
		throw dukx_get_exception(m_ctx, -1);

	duk_get_global_string(m_ctx, "x");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 10);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "y");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 20);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "w");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 30U);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "h");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 40U);
	duk_pop(m_ctx);
}

BOOST_AUTO_TEST_CASE(constructor_object)
{
	const auto ret = duk_peval_string(m_ctx,
		"r = Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });"
		"x = r.x;"
		"y = r.y;"
		"w = r.width;"
		"h = r.height;"
	);

	if (ret != 0)
		throw dukx_get_exception(m_ctx, -1);

	duk_get_global_string(m_ctx, "x");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 10);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "y");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 20);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "w");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 30U);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "h");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 40U);
	duk_pop(m_ctx);
}

BOOST_AUTO_TEST_CASE(constructor_new)
{
	const auto ret = duk_peval_string(m_ctx,
		"r = new Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });"
		"x = r.x;"
		"y = r.y;"
		"w = r.width;"
		"h = r.height;"
	);

	if (ret != 0)
		throw dukx_get_exception(m_ctx, -1);

	duk_get_global_string(m_ctx, "x");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 10);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "y");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 20);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "w");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 30U);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "h");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 40U);
	duk_pop(m_ctx);
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * Invalid constructors.
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(invalid_constructors)

BOOST_AUTO_TEST_CASE(constructor_arg_1)
{
	const auto ret = duk_peval_string(m_ctx,
		"try {"
		"  Malikania.Rectangle(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_TEST(duk_to_string(m_ctx, -1) == "TypeError");
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "correct");
	BOOST_TEST(duk_to_boolean(m_ctx, -1));
	duk_pop(m_ctx);
}

BOOST_AUTO_TEST_CASE(constructor_range_1)
{
	const auto ret = duk_peval_string(m_ctx,
		"try {"
		"  Malikania.Rectangle(0, 0, -10, -10);"
		"} 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_TEST(duk_to_string(m_ctx, -1) == "RangeError");
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "correct");
	BOOST_TEST(duk_to_boolean(m_ctx, -1));
	duk_pop(m_ctx);
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * Require.
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(require)

BOOST_AUTO_TEST_CASE(success)
{
	duk_push_c_function(m_ctx, [] (auto ctx) {
		const auto rect = dukx_require_rect(ctx, 0);

		duk_push_int(ctx, rect.x);
		duk_put_global_string(ctx, "x");
		duk_push_int(ctx, rect.y);
		duk_put_global_string(ctx, "y");
		duk_push_uint(ctx, rect.width);
		duk_put_global_string(ctx, "w");
		duk_push_uint(ctx, rect.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({ x: 50, y: 80, width: 100, height: 200 });");

	if (ret != 0)
		throw dukx_get_exception(m_ctx, -1);

	duk_get_global_string(m_ctx, "x");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 50);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "y");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 80);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "w");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 100U);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "h");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 200U);
	duk_pop(m_ctx);
}

BOOST_AUTO_TEST_CASE(fail)
{
	duk_push_c_function(m_ctx, [] (auto ctx) {
		dukx_require_rect(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_TEST(duk_to_string(m_ctx, -1) == "Error");
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "correct");
	BOOST_TEST(duk_to_boolean(m_ctx, -1));
	duk_pop(m_ctx);
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * Get.
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(get)

BOOST_AUTO_TEST_CASE(adjust_all)
{
	duk_push_c_function(m_ctx, [] (auto ctx) {
		const auto rect = dukx_get_rect(ctx, 0);

		duk_push_int(ctx, rect.x);
		duk_put_global_string(ctx, "x");
		duk_push_int(ctx, rect.y);
		duk_put_global_string(ctx, "y");
		duk_push_uint(ctx, rect.width);
		duk_put_global_string(ctx, "w");
		duk_push_uint(ctx, rect.height);

		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, "x");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 0);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "y");
	BOOST_TEST(duk_to_int(m_ctx, -1) == 0);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "w");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 0U);
	duk_pop(m_ctx);
	duk_get_global_string(m_ctx, "h");
	BOOST_TEST(duk_to_uint(m_ctx, -1) == 0U);
	duk_pop(m_ctx);
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

} // !namespace

} // !mlk