Mercurial > malikania
view tests/libmlk-client/js-color/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/libclient/js-color/main.cpp@f28cb6d04731 |
children |
line wrap: on
line source
/* * main.cpp -- test Color (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 Color" #include <boost/test/unit_test.hpp> #include <malikania/client/js/test/js_api_fixture.hpp> namespace mlk::client { namespace { auto component(duk_context* ctx, const char* name) -> int { duk_get_global_string(ctx, name); auto value = duk_require_int(ctx, -1); duk_pop(ctx); return value; } BOOST_FIXTURE_TEST_SUITE(test_color_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_, "c = Malikania.Color();" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); BOOST_TEST(component(ctx_, "r") == 0); BOOST_TEST(component(ctx_, "g") == 0); BOOST_TEST(component(ctx_, "b") == 0); BOOST_TEST(component(ctx_, "a") == 255); } BOOST_AUTO_TEST_CASE(constructor_string) { const auto ret = duk_peval_string(ctx_, "c = Malikania.Color('white');" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); BOOST_TEST(component(ctx_, "r") == 255); BOOST_TEST(component(ctx_, "g") == 255); BOOST_TEST(component(ctx_, "b") == 255); BOOST_TEST(component(ctx_, "a") == 255); } BOOST_AUTO_TEST_CASE(constructor_string_rgb) { const auto ret = duk_peval_string(ctx_, "c = Malikania.Color('#0000ff');" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); BOOST_TEST(component(ctx_, "r") == 0); BOOST_TEST(component(ctx_, "g") == 0); BOOST_TEST(component(ctx_, "b") == 255); BOOST_TEST(component(ctx_, "a") == 255); } BOOST_AUTO_TEST_CASE(constructor_3_args) { const auto ret = duk_peval_string(ctx_, "c = Malikania.Color(10, 20, 30);" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); BOOST_TEST(component(ctx_, "r") == 10); BOOST_TEST(component(ctx_, "g") == 20); BOOST_TEST(component(ctx_, "b") == 30); BOOST_TEST(component(ctx_, "a") == 255); } BOOST_AUTO_TEST_CASE(constructor_4_args) { const auto ret = duk_peval_string(ctx_, "c = Malikania.Color(10, 20, 30, 40);" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); BOOST_TEST(component(ctx_, "r") == 10); BOOST_TEST(component(ctx_, "g") == 20); BOOST_TEST(component(ctx_, "b") == 30); BOOST_TEST(component(ctx_, "a") == 40); } BOOST_AUTO_TEST_CASE(constructor_object_no_alpha) { const auto ret = duk_peval_string(ctx_, "c = Malikania.Color({ red: 10, green: 20, blue: 30 });" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); BOOST_TEST(component(ctx_, "r") == 10); BOOST_TEST(component(ctx_, "g") == 20); BOOST_TEST(component(ctx_, "b") == 30); BOOST_TEST(component(ctx_, "a") == 255); } BOOST_AUTO_TEST_CASE(constructor_object_alpha) { const auto ret = duk_peval_string(ctx_, "c = Malikania.Color({ red: 10, green: 20, blue: 30, alpha: 40 });" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); BOOST_TEST(component(ctx_, "r") == 10); BOOST_TEST(component(ctx_, "g") == 20); BOOST_TEST(component(ctx_, "b") == 30); BOOST_TEST(component(ctx_, "a") == 40); } BOOST_AUTO_TEST_CASE(constructor_new) { /* * Just one test, function parse the same way, only 'this' or a new * object is returned. */ const auto ret = duk_peval_string(ctx_, "c = new Malikania.Color({ red: 10, green: 20, blue: 30, alpha: 40 });" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); BOOST_TEST(component(ctx_, "r") == 10); BOOST_TEST(component(ctx_, "g") == 20); BOOST_TEST(component(ctx_, "b") == 30); BOOST_TEST(component(ctx_, "a") == 40); } 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.Color(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_CASE(constructor_arg_2) { const auto ret = duk_peval_string(ctx_, "try {" " Malikania.Color(10, null, 30);" "} 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_); } #if 0 // TODO: determine what's the best thing to do with negative values BOOST_AUTO_TEST_CASE(constructor_range_1) { const auto ret = duk_peval_string(ctx_, "try {" " Malikania.Color(-1, 20, 30);" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "name"); BOOST_TEST(duk_to_string(ctx_, -1) == "RangeError"); duk_pop(ctx_); duk_get_global_string(ctx_, "correct"); BOOST_TEST(duk_to_boolean(ctx_, -1)); duk_pop(ctx_); } BOOST_AUTO_TEST_CASE(constructor_range_2) { const auto ret = duk_peval_string(ctx_, "try {" " Malikania.Color(10, 20, 256);" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "name"); BOOST_TEST(duk_to_string(ctx_, -1) == "RangeError"); duk_pop(ctx_); duk_get_global_string(ctx_, "correct"); BOOST_TEST(duk_to_boolean(ctx_, -1)); duk_pop(ctx_); } BOOST_AUTO_TEST_CASE(constructor_range_3) { const auto ret = duk_peval_string(ctx_, "try {" " Malikania.Color(10, 20, 30, 800);" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "name"); BOOST_TEST(duk_to_string(ctx_, -1) == "RangeError"); duk_pop(ctx_); duk_get_global_string(ctx_, "correct"); BOOST_TEST(duk_to_boolean(ctx_, -1)); duk_pop(ctx_); } BOOST_AUTO_TEST_CASE(constructor_object_range_red) { const auto ret = duk_peval_string(ctx_, "try {" " Malikania.Color({ red: -1, green: 20, blue: 30 });" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "name"); BOOST_TEST(duk_to_string(ctx_, -1) == "RangeError"); duk_pop(ctx_); duk_get_global_string(ctx_, "correct"); BOOST_TEST(duk_to_boolean(ctx_, -1)); duk_pop(ctx_); } BOOST_AUTO_TEST_CASE(constructor_object_range_alpha) { const auto ret = duk_peval_string(ctx_, "try {" " Malikania.Color({ red: 10, green: 20, blue: 30, alpha: 800 });" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "name"); BOOST_TEST(duk_to_string(ctx_, -1) == "RangeError"); duk_pop(ctx_); duk_get_global_string(ctx_, "correct"); BOOST_TEST(duk_to_boolean(ctx_, -1)); duk_pop(ctx_); } #endif BOOST_AUTO_TEST_CASE(constructor_string) { const auto ret = duk_peval_string(ctx_, "try {" " Malikania.Color('does not exist');" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "name"); BOOST_TEST(duk_to_string(ctx_, -1) == "Error"); duk_pop(ctx_); duk_get_global_string(ctx_, "correct"); BOOST_TEST(duk_to_boolean(ctx_, -1)); duk_pop(ctx_); } BOOST_AUTO_TEST_CASE(constructor_string_rgb) { const auto ret = duk_peval_string(ctx_, "try {" " Malikania.Color('#ghijkl');" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "name"); BOOST_TEST(duk_to_string(ctx_, -1) == "Error"); 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. * ------------------------------------------------------------------ * * TypeTraits<Color>::require expect to have a valid color, it raises an error * for any invalid component. Only alpha can be ommitted but if it is present * and incorrect, an exception is also raised. */ BOOST_AUTO_TEST_SUITE(require) BOOST_AUTO_TEST_CASE(success) { duk_push_c_function(ctx_, [] (auto ctx) { const auto color = mlk::js::duk::require<mlk::client::color>(ctx, 0); duk_push_uint(ctx, color.red); duk_put_global_string(ctx, "r"); duk_push_uint(ctx, color.green); duk_put_global_string(ctx, "g"); duk_push_uint(ctx, color.blue); duk_put_global_string(ctx, "b"); duk_push_uint(ctx, color.alpha); duk_put_global_string(ctx, "a"); return 0; }, 1); duk_put_global_string(ctx_, "draw"); const auto ret = duk_peval_string(ctx_, "draw('#ff0000');"); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "r"); BOOST_TEST(duk_to_uint(ctx_, -1) == 255U); duk_pop(ctx_); duk_get_global_string(ctx_, "g"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "b"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "a"); BOOST_TEST(duk_to_uint(ctx_, -1) == 255U); duk_pop(ctx_); } BOOST_AUTO_TEST_CASE(fail) { duk_push_c_function(ctx_, [] (auto ctx) { mlk::js::duk::require<mlk::client::color>(ctx, 0); return 0; }, 1); duk_put_global_string(ctx_, "draw"); const auto ret = duk_peval_string(ctx_, "try {" " draw('#ghijkl');" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "name"); BOOST_TEST(duk_to_string(ctx_, -1) == "Error"); duk_pop(ctx_); duk_get_global_string(ctx_, "correct"); BOOST_TEST(duk_to_boolean(ctx_, -1)); duk_pop(ctx_); } #if 0 TODO: check what to do with out of range value BOOST_AUTO_TEST_CASE(fail_alpha) { duk_push_c_function(ctx_, [] (auto ctx) { mlk::js::duk::require<mlk::client::color>(ctx, 0); return 0; }, 1); duk_put_global_string(ctx_, "draw"); const auto ret = duk_peval_string(ctx_, "try {" " draw({ red: 10, green: 20, blue: 30, alpha: 800 });" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "name"); BOOST_TEST(duk_to_string(ctx_, -1) == "RangeError"); duk_pop(ctx_); duk_get_global_string(ctx_, "correct"); BOOST_TEST(duk_to_boolean(ctx_, -1)); duk_pop(ctx_); } #endif BOOST_AUTO_TEST_SUITE_END() /* * Get. * ------------------------------------------------------------------ * * TypeTraits<Color>::get readjust any invalid values. */ BOOST_AUTO_TEST_SUITE(get) BOOST_AUTO_TEST_CASE(normal) { duk_push_c_function(ctx_, [] (auto ctx) { const auto color = mlk::js::duk::get<mlk::client::color>(ctx, 0); duk_push_uint(ctx, color.red); duk_put_global_string(ctx, "r"); duk_push_uint(ctx, color.green); duk_put_global_string(ctx, "g"); duk_push_uint(ctx, color.blue); duk_put_global_string(ctx, "b"); duk_push_uint(ctx, color.alpha); duk_put_global_string(ctx, "a"); return 0; }, 1); duk_put_global_string(ctx_, "draw"); const auto ret = duk_peval_string(ctx_, "draw('#ff0000');"); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "r"); BOOST_TEST(duk_to_uint(ctx_, -1) == 255U); duk_pop(ctx_); duk_get_global_string(ctx_, "g"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "b"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "a"); BOOST_TEST(duk_to_uint(ctx_, -1) == 255U); duk_pop(ctx_); } BOOST_AUTO_TEST_CASE(adjust_rgb) { duk_push_c_function(ctx_, [] (auto ctx) { const auto color = mlk::js::duk::get<mlk::client::color>(ctx, 0); duk_push_uint(ctx, color.red); duk_put_global_string(ctx, "r"); duk_push_uint(ctx, color.green); duk_put_global_string(ctx, "g"); duk_push_uint(ctx, color.blue); duk_put_global_string(ctx, "b"); duk_push_uint(ctx, color.alpha); duk_put_global_string(ctx, "a"); return 0; }, 1); duk_put_global_string(ctx_, "draw"); const auto ret = duk_peval_string(ctx_, "draw('#ghijkl');"); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "r"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "g"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "b"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "a"); BOOST_TEST(duk_to_uint(ctx_, -1) == 255U); duk_pop(ctx_); } #if 0 BOOST_AUTO_TEST_CASE(adjust_all) { duk_push_c_function(ctx_, [] (auto ctx) { const auto color = mlk::js::duk::get<mlk::client::color>(ctx, 0); duk_push_uint(ctx, color.red); duk_put_global_string(ctx, "r"); duk_push_uint(ctx, color.green); duk_put_global_string(ctx, "g"); duk_push_uint(ctx, color.blue); duk_put_global_string(ctx, "b"); duk_push_uint(ctx, color.alpha); duk_put_global_string(ctx, "a"); return 0; }, 1); duk_put_global_string(ctx_, "draw"); const auto ret = duk_peval_string(ctx_, "draw({ red: -1, green: 256, blue: 100, alpha: 800 });"); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "r"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "g"); BOOST_TEST(duk_to_uint(ctx_, -1) == 255U); duk_pop(ctx_); duk_get_global_string(ctx_, "b"); BOOST_TEST(duk_to_uint(ctx_, -1) == 100U); duk_pop(ctx_); duk_get_global_string(ctx_, "a"); BOOST_TEST(duk_to_uint(ctx_, -1) == 255U); duk_pop(ctx_); } #endif BOOST_AUTO_TEST_CASE(something_else) { duk_push_c_function(ctx_, [] (auto ctx) { const auto color = mlk::js::duk::get<mlk::client::color>(ctx, 0); duk_push_uint(ctx, color.red); duk_put_global_string(ctx, "r"); duk_push_uint(ctx, color.green); duk_put_global_string(ctx, "g"); duk_push_uint(ctx, color.blue); duk_put_global_string(ctx, "b"); duk_push_uint(ctx, color.alpha); duk_put_global_string(ctx, "a"); return 0; }, 1); duk_put_global_string(ctx_, "draw"); const auto ret = duk_peval_string(ctx_, "draw(null);"); if (ret != 0) throw mlk::js::duk::get_stack(ctx_, -1); duk_get_global_string(ctx_, "r"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "g"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "b"); BOOST_TEST(duk_to_uint(ctx_, -1) == 0U); duk_pop(ctx_); duk_get_global_string(ctx_, "a"); BOOST_TEST(duk_to_uint(ctx_, -1) == 255U); duk_pop(ctx_); } BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END() } // !namespace } // !mlk::client