Mercurial > malikania
diff tests/libclient/js-point/main.cpp @ 15:3b9ea4072263
Client: add JavaScript bindings for Point, #458
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 03 Apr 2016 11:42:03 +0200 |
parents | |
children | 0a1adf7dcca0 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libclient/js-point/main.cpp Sun Apr 03 11:42:03 2016 +0200 @@ -0,0 +1,245 @@ +/* + * main.cpp -- test Point (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-point.h> + +using namespace malikania; + +class TestPoint : public testing::Test { +protected: + duk::Context m_ctx; + +public: + TestPoint() + { + duk::putGlobal(m_ctx, "Malikania", duk::Object()); + + loadMalikaniaPoint(m_ctx); + } +}; + +/* + * Valid constructors + * ------------------------------------------------------------------ + */ + +TEST_F(TestPoint, ConstructorNoArgs) +{ + try { + auto ret = duk::pevalString(m_ctx, + "p = Malikania.Point();" + "x = p.x;" + "y = p.y;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); + ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestPoint, Constructor2Args) +{ + try { + auto ret = duk::pevalString(m_ctx, + "p = Malikania.Point(-10, -20);" + "x = p.x;" + "y = p.y;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(-10, duk::getGlobal<int>(m_ctx, "x")); + ASSERT_EQ(-20, duk::getGlobal<int>(m_ctx, "y")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestPoint, ConstructorObject) +{ + try { + auto ret = duk::pevalString(m_ctx, + "p = Malikania.Point({ x: 100, y: 200 });" + "x = p.x;" + "y = p.y;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x")); + ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestPoint, ConstructorNew) +{ + try { + auto ret = duk::pevalString(m_ctx, + "p = new Malikania.Point({ x: 100, y: 200 });" + "x = p.x;" + "y = p.y;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x")); + ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +/* + * Invalid constructors + * ------------------------------------------------------------------ + */ + +TEST_F(TestPoint, InvalidConstructorArg1) +{ + try { + auto ret = duk::pevalString(m_ctx, + "try {" + " Malikania.Point(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(); + } +} + +/* + * Require. + * ------------------------------------------------------------------ + */ + +TEST_F(TestPoint, requireSuccess) +{ + try { + duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { + Point point = duk::require<Point>(ctx, 0); + + duk::putGlobal(ctx, "x", point.x()); + duk::putGlobal(ctx, "y", point.y()); + + return 0; + }, 1}); + + auto ret = duk::pevalString(m_ctx, "build({ x: 100, y: 200 });"); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x")); + ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestPoint, requireFail) +{ + try { + duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { + duk::require<Point>(ctx, 0); + + return 0; + }, 1}); + + auto ret = duk::pevalString(m_ctx, + "try {" + " build({});" + "} 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(); + } +} + +/* + * Get. + * ------------------------------------------------------------------ + */ + +TEST_F(TestPoint, getAdjustAll) +{ + try { + duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { + Point point = duk::get<Point>(ctx, 0); + + duk::putGlobal(ctx, "x", point.x()); + duk::putGlobal(ctx, "y", point.y()); + + return 0; + }, 1}); + + auto ret = duk::pevalString(m_ctx, "build({});"); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); + ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}