Mercurial > malikania
diff tests/libmlk/js-point/main.cpp @ 193:209bdaa13a49
Tests: rename directories to match targets
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 27 Oct 2018 07:16:28 +0200 |
parents | tests/libcommon/js-point/main.cpp@f28cb6d04731 |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libmlk/js-point/main.cpp Sat Oct 27 07:16:28 2018 +0200 @@ -0,0 +1,253 @@ +/* + * main.cpp -- test Point (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 Point" +#include <boost/test/unit_test.hpp> + +#include <malikania/point.hpp> + +#include <malikania/js/test/js_api_fixture.hpp> + +namespace mlk { + +namespace { + +BOOST_FIXTURE_TEST_SUITE(test_point_suite, js::test::js_api_fixture) + +/* + * Valid constructors. + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(constructors) + +BOOST_AUTO_TEST_CASE(constructor_default) +{ + const auto ret = duk_peval_string(ctx_, + "p = Malikania.Point();" + "x = p.x;" + "y = p.y;" + ); + + if (ret != 0) + throw mlk::js::duk::get_stack(ctx_, -1); + + duk_get_global_string(ctx_, "x"); + BOOST_TEST(duk_to_int(ctx_, -1) == 0); + duk_pop(ctx_); + duk_get_global_string(ctx_, "y"); + BOOST_TEST(duk_to_int(ctx_, -1) == 0); + duk_pop(ctx_); +} + +BOOST_AUTO_TEST_CASE(constructor_2_args) +{ + const auto ret = duk_peval_string(ctx_, + "p = Malikania.Point(-10, -20);" + "x = p.x;" + "y = p.y;" + ); + + if (ret != 0) + throw mlk::js::duk::get_stack(ctx_, -1); + + duk_get_global_string(ctx_, "x"); + BOOST_TEST(duk_to_int(ctx_, -1) == -10); + duk_pop(ctx_); + duk_get_global_string(ctx_, "y"); + BOOST_TEST(duk_to_int(ctx_, -1) == -20); + duk_pop(ctx_); +} + +BOOST_AUTO_TEST_CASE(constructor_object) +{ + const auto ret = duk_peval_string(ctx_, + "p = Malikania.Point({ x: 100, y: 200 });" + "x = p.x;" + "y = p.y;" + ); + + if (ret != 0) + throw mlk::js::duk::get_stack(ctx_, -1); + + duk_get_global_string(ctx_, "x"); + BOOST_TEST(duk_to_int(ctx_, -1) == 100); + duk_pop(ctx_); + duk_get_global_string(ctx_, "y"); + BOOST_TEST(duk_to_int(ctx_, -1) == 200); + duk_pop(ctx_); +} + +BOOST_AUTO_TEST_CASE(constructor_new) +{ + const auto ret = duk_peval_string(ctx_, + "p = new Malikania.Point({ x: 100, y: 200 });" + "x = p.x;" + "y = p.y;" + ); + + if (ret != 0) + throw mlk::js::duk::get_stack(ctx_, -1); + + duk_get_global_string(ctx_, "x"); + BOOST_TEST(duk_to_int(ctx_, -1) == 100); + duk_pop(ctx_); + duk_get_global_string(ctx_, "y"); + BOOST_TEST(duk_to_int(ctx_, -1) == 200); + duk_pop(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(ctx_, + "try {" + " Malikania.Point(null);" + "} catch (e) {" + " name = e.name;" + " correct = (e instanceof TypeError);" + "}" + ); + + if (ret != 0) + throw mlk::js::duk::get_stack(ctx_, -1); + + duk_get_global_string(ctx_, "name"); + BOOST_TEST(duk_to_string(ctx_, -1) == "TypeError"); + duk_pop(ctx_); + duk_get_global_string(ctx_, "correct"); + BOOST_TEST(duk_to_boolean(ctx_, -1)); + duk_pop(ctx_); +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * Require. + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(require) + +BOOST_AUTO_TEST_CASE(success) +{ + duk_push_c_function(ctx_, [] (auto ctx) { + const auto point = mlk::js::duk::require<mlk::point>(ctx, 0); + + duk_push_int(ctx, point.x); + duk_put_global_string(ctx, "x"); + duk_push_int(ctx, point.y); + duk_put_global_string(ctx, "y"); + + return 0; + }, 1); + duk_put_global_string(ctx_, "build"); + + const auto ret = duk_peval_string(ctx_, "build({ x: 100, y: 200 });"); + + if (ret != 0) + throw mlk::js::duk::get_stack(ctx_, -1); + + duk_get_global_string(ctx_, "x"); + BOOST_TEST(duk_to_int(ctx_, -1) == 100); + duk_pop(ctx_); + duk_get_global_string(ctx_, "y"); + BOOST_TEST(duk_to_int(ctx_, -1) == 200); + duk_pop(ctx_); +} + +BOOST_AUTO_TEST_CASE(fail) +{ + duk_push_c_function(ctx_, [] (auto ctx) { + mlk::js::duk::require<mlk::point>(ctx, 0); + + return 0; + }, 1); + duk_put_global_string(ctx_, "build"); + + const auto ret = duk_peval_string(ctx_, + "try {" + " build({});" + "} catch (e) {" + " name = e.name;" + " correct = (e instanceof TypeError);" + "}" + ); + + if (ret != 0) + throw mlk::js::duk::get_stack(ctx_, -1); + + duk_get_global_string(ctx_, "name"); + BOOST_TEST(duk_to_string(ctx_, -1) == "TypeError"); + duk_pop(ctx_); + duk_get_global_string(ctx_, "correct"); + BOOST_TEST(duk_to_boolean(ctx_, -1)); + duk_pop(ctx_); +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * Get. + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(get) + +BOOST_AUTO_TEST_CASE(adjust_all) +{ + duk_push_c_function(ctx_, [] (auto ctx) { + const auto point = mlk::js::duk::get<mlk::point>(ctx, 0); + + duk_push_int(ctx, point.x); + duk_put_global_string(ctx, "x"); + duk_push_int(ctx, point.y); + duk_put_global_string(ctx, "y"); + + return 0; + }, 1); + duk_put_global_string(ctx_, "build"); + + const auto ret = duk_peval_string(ctx_, "build({});"); + + if (ret != 0) + throw mlk::js::duk::get_stack(ctx_, -1); + + duk_get_global_string(ctx_, "x"); + BOOST_TEST(duk_to_int(ctx_, -1) == 0); + duk_pop(ctx_); + duk_get_global_string(ctx_, "y"); + BOOST_TEST(duk_to_int(ctx_, -1) == 0); + duk_pop(ctx_); +} + +BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE_END() + +} // !namespace + +} // !mlk