Mercurial > malikania
view tests/libclient/js-color/main.cpp @ 57:9ccc36ba4725
CMake: use full path for a better vera++ integration
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 16 Dec 2016 14:54:29 +0100 |
parents | fe7e3524e571 |
children | 96ba0c5cf893 |
line wrap: on
line source
/* * main.cpp -- test Color (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. */ #define BOOST_TEST_MODULE "Javascript Color" #include <boost/test/unit_test.hpp> #include <malikania/js_color.hpp> class test_color { protected: UniqueContext m_ctx; public: test_color() { duk_push_object(m_ctx); duk_put_global_string(m_ctx, "Malikania"); mlk::dukx_load_color(m_ctx); } int component(const char *name) { duk_get_global_string(m_ctx, name); auto value = duk_require_int(m_ctx, -1); duk_pop(m_ctx); return value; } }; BOOST_FIXTURE_TEST_SUITE(test_color_suite, test_color) /* * Valid constructors. * ------------------------------------------------------------------ */ BOOST_AUTO_TEST_SUITE(constructors) BOOST_AUTO_TEST_CASE(constructor_default) { try { auto ret = duk_peval_string(m_ctx, "c = Malikania.Color();" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) { throw dukx_exception(m_ctx, -1); } BOOST_REQUIRE_EQUAL(0, component("r")); BOOST_REQUIRE_EQUAL(0, component("g")); BOOST_REQUIRE_EQUAL(0, component("b")); BOOST_REQUIRE_EQUAL(255, component("a")); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(constructor_string) { try { auto ret = duk_peval_string(m_ctx, "c = Malikania.Color('white');" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) { throw dukx_exception(m_ctx, -1); } BOOST_REQUIRE_EQUAL(255, component("r")); BOOST_REQUIRE_EQUAL(255, component("g")); BOOST_REQUIRE_EQUAL(255, component("b")); BOOST_REQUIRE_EQUAL(255, component("a")); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(constructor_string_rgb) { try { auto ret = duk_peval_string(m_ctx, "c = Malikania.Color('#0000ff');" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) { throw dukx_exception(m_ctx, -1); } BOOST_REQUIRE_EQUAL(0, component("r")); BOOST_REQUIRE_EQUAL(0, component("g")); BOOST_REQUIRE_EQUAL(255, component("b")); BOOST_REQUIRE_EQUAL(255, component("a")); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(constructor_3_args) { try { auto ret = duk_peval_string(m_ctx, "c = Malikania.Color(10, 20, 30);" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) { throw dukx_exception(m_ctx, -1); } BOOST_REQUIRE_EQUAL(10, component("r")); BOOST_REQUIRE_EQUAL(20, component("g")); BOOST_REQUIRE_EQUAL(30, component("b")); BOOST_REQUIRE_EQUAL(255, component("a")); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(constructor_4_args) { try { auto ret = duk_peval_string(m_ctx, "c = Malikania.Color(10, 20, 30, 40);" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) { throw dukx_exception(m_ctx, -1); } BOOST_REQUIRE_EQUAL(10, component("r")); BOOST_REQUIRE_EQUAL(20, component("g")); BOOST_REQUIRE_EQUAL(30, component("b")); BOOST_REQUIRE_EQUAL(40, component("a")); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(constructor_object_no_alpha) { try { auto ret = duk_peval_string(m_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 dukx_exception(m_ctx, -1); } BOOST_REQUIRE_EQUAL(10, component("r")); BOOST_REQUIRE_EQUAL(20, component("g")); BOOST_REQUIRE_EQUAL(30, component("b")); BOOST_REQUIRE_EQUAL(255, component("a")); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(constructor_object_alpha) { try { auto ret = duk_peval_string(m_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 dukx_exception(m_ctx, -1); } BOOST_REQUIRE_EQUAL(10, component("r")); BOOST_REQUIRE_EQUAL(20, component("g")); BOOST_REQUIRE_EQUAL(30, component("b")); BOOST_REQUIRE_EQUAL(40, component("a")); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(constructor_new) { /* * Just one test, function parse the same way, only 'this' or a new object is returned. */ try { auto ret = duk_peval_string(m_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 dukx_exception(m_ctx, -1); } BOOST_REQUIRE_EQUAL(10, component("r")); BOOST_REQUIRE_EQUAL(20, component("g")); BOOST_REQUIRE_EQUAL(30, component("b")); BOOST_REQUIRE_EQUAL(40, component("a")); } 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 { auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Color(null);" "} catch (e) {" " name = e.name;" " correct = (e instanceof TypeError);" "}" ); if (ret != 0) { throw dukx_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_arg_2) { try { auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Color(10, null, 30);" "} catch (e) {" " name = e.name;" " correct = (e instanceof TypeError);" "}" ); if (ret != 0) { throw dukx_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 { auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Color(-1, 20, 30);" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) { throw dukx_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 { auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Color(10, 20, 256);" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) { throw dukx_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 { auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Color(10, 20, 30, 800);" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) { throw dukx_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_object_range_red) { try { auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Color({ red: -1, green: 20, blue: 30 });" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) { throw dukx_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_object_range_alpha) { try { auto ret = duk_peval_string(m_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 dukx_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_string) { try { auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Color('does not exist');" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) { throw dukx_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_CASE(constructor_string_rgb) { try { auto ret = duk_peval_string(m_ctx, "try {" " Malikania.Color('#ghijkl');" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) { throw dukx_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() /* * 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) { try { duk_push_c_function(m_ctx, [] (auto ctx) { auto color = mlk::dukx_require_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(m_ctx, "draw"); auto ret = duk_peval_string(m_ctx, "draw('#ff0000');"); if (ret != 0) { throw dukx_exception(m_ctx, -1); } duk_get_global_string(m_ctx, "r"); BOOST_REQUIRE_EQUAL(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); BOOST_REQUIRE_EQUAL(255U, 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) { mlk::dukx_require_color(ctx, 0); return 0; }, 1); duk_put_global_string(m_ctx, "draw"); auto ret = duk_peval_string(m_ctx, "try {" " draw('#ghijkl');" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) { throw dukx_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_CASE(fail_alpha) { try { duk_push_c_function(m_ctx, [] (auto ctx) { mlk::dukx_require_color(ctx, 0); return 0; }, 1); duk_put_global_string(m_ctx, "draw"); auto ret = duk_peval_string(m_ctx, "try {" " draw({ red: 10, green: 20, blue: 30, alpha: 800 });" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) { throw dukx_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() /* * Get. * ------------------------------------------------------------------ * * TypeTraits<Color>::get readjust any invalid values. */ BOOST_AUTO_TEST_SUITE(get) BOOST_AUTO_TEST_CASE(normal) { try { duk_push_c_function(m_ctx, [] (auto ctx) { auto color = mlk::dukx_get_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(m_ctx, "draw"); auto ret = duk_peval_string(m_ctx, "draw('#ff0000');"); if (ret != 0) { throw dukx_exception(m_ctx, -1); } duk_get_global_string(m_ctx, "r"); BOOST_REQUIRE_EQUAL(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); BOOST_REQUIRE_EQUAL(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(adjust_rgb) { try { duk_push_c_function(m_ctx, [] (auto ctx) { auto color = mlk::dukx_get_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(m_ctx, "draw"); auto ret = duk_peval_string(m_ctx, "draw('#ghijkl');"); if (ret != 0) { throw dukx_exception(m_ctx, -1); } duk_get_global_string(m_ctx, "r"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); BOOST_REQUIRE_EQUAL(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(adjust_all) { try { duk_push_c_function(m_ctx, [] (auto ctx) { auto color = mlk::dukx_get_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(m_ctx, "draw"); auto ret = duk_peval_string(m_ctx, "draw({ red: -1, green: 256, blue: 100, alpha: 800 });"); if (ret != 0) { throw dukx_exception(m_ctx, -1); } duk_get_global_string(m_ctx, "r"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); BOOST_REQUIRE_EQUAL(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); BOOST_REQUIRE_EQUAL(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { BOOST_FAIL(ex.what()); } } BOOST_AUTO_TEST_CASE(something_else) { try { duk_push_c_function(m_ctx, [] (auto ctx) { auto color = mlk::dukx_get_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(m_ctx, "draw"); auto ret = duk_peval_string(m_ctx, "draw(null);"); if (ret != 0) { throw dukx_exception(m_ctx, -1); } duk_get_global_string(m_ctx, "r"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); BOOST_REQUIRE_EQUAL(255U, 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()