Mercurial > malikania
view tests/libclient/js-color/main.cpp @ 14:26efd2928f01
Client: add some JavaScript bindings for Window, #462
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 03 Apr 2016 10:23:23 +0200 |
parents | 0c62e0af6af7 |
children | 0a1adf7dcca0 |
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.h> using namespace malikania; class TestColor : public testing::Test { protected: duk::Context m_ctx; public: TestColor() { duk::putGlobal(m_ctx, "Malikania", duk::Object()); loadMalikaniaColor(m_ctx); } }; /* * Valid constructors * ------------------------------------------------------------------ */ TEST_F(TestColor, ConstructorNoArgs) { try { auto ret = duk::pevalString(m_ctx, "c = Malikania.Color();" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, ConstructorString) { try { auto ret = duk::pevalString(m_ctx, "c = Malikania.Color('white');" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, ConstructorStringRgb) { try { auto ret = duk::pevalString(m_ctx, "c = Malikania.Color('#0000ff');" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, Constructor3Args) { try { auto ret = duk::pevalString(m_ctx, "c = Malikania.Color(10, 20, 30);" "r = c.red;" "g = c.green;" "b = c.blue;" "a = c.alpha;" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, Constructor4Args) { try { auto ret = duk::pevalString(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 duk::error(m_ctx, -1); } ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, ConstructorObjectNoAlpha) { try { auto ret = duk::pevalString(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 duk::error(m_ctx, -1); } ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, ConstructorObjectAlpha) { try { auto ret = duk::pevalString(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 duk::error(m_ctx, -1); } ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "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::pevalString(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 duk::error(m_ctx, -1); } ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * Valid constructors * ------------------------------------------------------------------ */ TEST_F(TestColor, InvalidConstructorArg1) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Color(null);" "} catch (e) {" " name = e.name;" " correct = (e instanceof TypeError);" "}" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorArg2) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Color(10, null, 30);" "} catch (e) {" " name = e.name;" " correct = (e instanceof TypeError);" "}" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange1) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Color(-1, 20, 30);" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange2) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Color(10, 20, 256);" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange3) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Color(10, 20, 30, 800);" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange4) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Color({ red: -1, green: 20, blue: 30 });" "} catch (e) {" " name = e.name;" " correct = (e instanceof RangeError);" "}" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorRange5) { try { auto ret = duk::pevalString(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 duk::error(m_ctx, -1); } ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorStringUnknown) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Color('does not exist');" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, InvalidConstructorStringRgb) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Color('#ghijkl');" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } 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::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { Color color = duk::require<Color>(ctx, 0); duk::putGlobal(ctx, "r", static_cast<int>(color.red())); duk::putGlobal(ctx, "g", static_cast<int>(color.green())); duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); return 0; }, 1}); auto ret = duk::pevalString(m_ctx, "draw('#ff0000');"); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, requireFail) { try { duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { duk::require<Color>(ctx, 0); return 0; }, 1}); auto ret = duk::pevalString(m_ctx, "try {" " draw('#ghijkl');" "} catch (e) {" " name = e.name;" " correct = (e instanceof Error);" "}" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, requireFailAlpha) { try { duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { duk::require<Color>(ctx, 0); return 0; }, 1}); auto ret = duk::pevalString(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 duk::error(m_ctx, -1); } ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * Get. * ------------------------------------------------------------------ * * TypeTraits<Color>::get readjust any invalid values. */ TEST_F(TestColor, getNormal) { try { duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { Color color = duk::get<Color>(ctx, 0); duk::putGlobal(ctx, "r", static_cast<int>(color.red())); duk::putGlobal(ctx, "g", static_cast<int>(color.green())); duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); return 0; }, 1}); auto ret = duk::pevalString(m_ctx, "draw('#ff0000');"); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, getAdjustRgb) { try { duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { Color color = duk::get<Color>(ctx, 0); duk::putGlobal(ctx, "r", static_cast<int>(color.red())); duk::putGlobal(ctx, "g", static_cast<int>(color.green())); duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); return 0; }, 1}); auto ret = duk::pevalString(m_ctx, "draw('#ghijkl');"); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, getAdjustAll) { try { duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { Color color = duk::get<Color>(ctx, 0); duk::putGlobal(ctx, "r", static_cast<int>(color.red())); duk::putGlobal(ctx, "g", static_cast<int>(color.green())); duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); return 0; }, 1}); auto ret = duk::pevalString(m_ctx, "draw({ red: -1, green: 256, blue: 100, alpha: 800 });"); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestColor, getSomethingElse) { try { duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { Color color = duk::get<Color>(ctx, 0); duk::putGlobal(ctx, "r", static_cast<int>(color.red())); duk::putGlobal(ctx, "g", static_cast<int>(color.green())); duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); return 0; }, 1}); auto ret = duk::pevalString(m_ctx, "draw(null);"); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }