Mercurial > malikania
view tests/libclient/js-color/main.cpp @ 43:fabbe1759cec
Misc: switch to mlk namespace, closes #589
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 29 Nov 2016 22:25:17 +0100 |
parents | a47a4477f347 |
children | f30c84b4b9ed |
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. */ #include <gtest/gtest.h> #include <malikania/js_color.hpp> class TestColor : public testing::Test { protected: UniqueContext m_ctx; public: TestColor() { 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; } }; /* * Valid constructors * ------------------------------------------------------------------ */ TEST_F(TestColor, ConstructorNoArgs) { 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); ASSERT_EQ(0, component("r")); ASSERT_EQ(0, component("g")); ASSERT_EQ(0, component("b")); ASSERT_EQ(255, component("a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, ConstructorString) { 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); ASSERT_EQ(255, component("r")); ASSERT_EQ(255, component("g")); ASSERT_EQ(255, component("b")); ASSERT_EQ(255, component("a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, ConstructorStringRgb) { 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); ASSERT_EQ(0, component("r")); ASSERT_EQ(0, component("g")); ASSERT_EQ(255, component("b")); ASSERT_EQ(255, component("a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, Constructor3Args) { 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); ASSERT_EQ(10, component("r")); ASSERT_EQ(20, component("g")); ASSERT_EQ(30, component("b")); ASSERT_EQ(255, component("a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, Constructor4Args) { 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); ASSERT_EQ(10, component("r")); ASSERT_EQ(20, component("g")); ASSERT_EQ(30, component("b")); ASSERT_EQ(40, component("a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, ConstructorObjectNoAlpha) { 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); ASSERT_EQ(10, component("r")); ASSERT_EQ(20, component("g")); ASSERT_EQ(30, component("b")); ASSERT_EQ(255, component("a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, ConstructorObjectAlpha) { 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); ASSERT_EQ(10, component("r")); ASSERT_EQ(20, component("g")); ASSERT_EQ(30, component("b")); ASSERT_EQ(40, component("a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, ConstructorNew) { /* * 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); ASSERT_EQ(10, component("r")); ASSERT_EQ(20, component("g")); ASSERT_EQ(30, component("b")); ASSERT_EQ(40, component("a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * Valid constructors * ------------------------------------------------------------------ */ TEST_F(TestColor, InvalidConstructorArg1) { 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"); ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorArg2) { 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"); ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange1) { 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"); ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange2) { 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"); ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange3) { 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"); ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange4) { 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"); ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange5) { 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"); ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorStringUnknown) { 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"); ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorStringRgb) { 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"); ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * 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. */ TEST_F(TestColor, requireSuccess) { 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"); ASSERT_EQ(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); ASSERT_EQ(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, requireFail) { 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"); ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, requireFailAlpha) { 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"); ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "correct"); ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * Get. * ------------------------------------------------------------------ * * TypeTraits<Color>::get readjust any invalid values. */ TEST_F(TestColor, getNormal) { 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"); ASSERT_EQ(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); ASSERT_EQ(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, getAdjustRgb) { 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"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); ASSERT_EQ(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, getAdjustAll) { 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"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); ASSERT_EQ(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); ASSERT_EQ(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, getSomethingElse) { 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"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "g"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "b"); ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); duk_get_global_string(m_ctx, "a"); ASSERT_EQ(255U, duk_to_uint(m_ctx, -1)); duk_pop(m_ctx); } catch (const std::exception &ex) { FAIL() << ex.what(); } } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }