Mercurial > malikania
view tests/libclient/js-size/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 | fb7c2c096101 |
children | 0a1adf7dcca0 |
line wrap: on
line source
/* * main.cpp -- test Size (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-size.h> using namespace malikania; class TestSize : public testing::Test { protected: duk::Context m_ctx; public: TestSize() { duk::putGlobal(m_ctx, "Malikania", duk::Object()); loadMalikaniaSize(m_ctx); } }; /* * Valid constructors * ------------------------------------------------------------------ */ TEST_F(TestSize, ConstructorNoArgs) { try { auto ret = duk::pevalString(m_ctx, "s = Malikania.Size();" "w = s.width;" "h = s.height;" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "w")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "h")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestSize, Constructor2Args) { try { auto ret = duk::pevalString(m_ctx, "s = Malikania.Size(100, 200);" "w = s.width;" "h = s.height;" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestSize, ConstructorObject) { try { auto ret = duk::pevalString(m_ctx, "s = Malikania.Size({ width: 100, height: 200 });" "w = s.width;" "h = s.height;" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestSize, ConstructorNew) { try { auto ret = duk::pevalString(m_ctx, "s = new Malikania.Size({ width: 100, height: 200 });" "w = s.width;" "h = s.height;" ); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } /* * Invalid constructors * ------------------------------------------------------------------ */ TEST_F(TestSize, InvalidConstructorArg1) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Size(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(TestSize, InvalidConstructorRange1) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Size(-1, 200);" "} 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(TestSize, InvalidConstructorRange2) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Size(100, -1);" "} 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(TestSize, InvalidConstructorRange3) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Size({ width: -1, height: 200 });" "} 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(TestSize, InvalidConstructorRange4) { try { auto ret = duk::pevalString(m_ctx, "try {" " Malikania.Size({ width: 100, height: -1 });" "} 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(); } } /* * Require. * ------------------------------------------------------------------ */ TEST_F(TestSize, requireSuccess) { try { duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { Size size = duk::require<Size>(ctx, 0); duk::putGlobal(ctx, "w", static_cast<int>(size.width())); duk::putGlobal(ctx, "h", static_cast<int>(size.height())); return 0; }, 1}); auto ret = duk::pevalString(m_ctx, "build({ width: 100, height: 200 });"); if (ret != 0) { throw duk::error(m_ctx, -1); } ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } TEST_F(TestSize, requireFail) { try { duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { duk::require<Size>(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(TestSize, getAdjustAll) { try { duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { Size size = duk::get<Size>(ctx, 0); duk::putGlobal(ctx, "w", static_cast<int>(size.width())); duk::putGlobal(ctx, "h", static_cast<int>(size.height())); 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, "r")); ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }