view tests/libclient/js-rectangle/main.cpp @ 33:d4f5f7231b84

Misc: switch to .hpp, dos2unix everything while here, #478
author David Demelier <markand@malikania.fr>
date Fri, 17 Jun 2016 13:12:35 +0200
parents a1e80d991968
children 9af360f34c7d
line wrap: on
line source

/*
 * main.cpp -- test Rectangle (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-rectangle.hpp>

using namespace malikania;

class TestRectangle : public testing::Test {
protected:
    duk::Context m_ctx;

public:
    TestRectangle()
    {
        duk::putGlobal(m_ctx, "Malikania", duk::Object());

        loadMalikaniaRectangle(m_ctx);
    }
};

/*
 * Valid constructors
 * ------------------------------------------------------------------
 */

TEST_F(TestRectangle, ConstructorNoArgs)
{
    try {
        auto ret = duk::pevalString(m_ctx,
            "r = Malikania.Rectangle();"
            "x = r.x;"
            "y = r.y;"
            "w = r.width;"
            "h = r.height;"
        );

        if (ret != 0) {
            throw duk::exception(m_ctx, -1);
        }

        ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x"));
        ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y"));
        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(TestRectangle, Constructor4Args)
{
    try {
        auto ret = duk::pevalString(m_ctx,
            "r = Malikania.Rectangle(10, 20, 30, 40);"
            "x = r.x;"
            "y = r.y;"
            "w = r.width;"
            "h = r.height;"
        );

        if (ret != 0) {
            throw duk::exception(m_ctx, -1);
        }

        ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x"));
        ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y"));
        ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "w"));
        ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "h"));
    } catch (const std::exception &ex) {
        FAIL() << ex.what();
    }
}

TEST_F(TestRectangle, ConstructorObject)
{
    try {
        auto ret = duk::pevalString(m_ctx,
            "r = Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });"
            "x = r.x;"
            "y = r.y;"
            "w = r.width;"
            "h = r.height;"
        );

        if (ret != 0) {
            throw duk::exception(m_ctx, -1);
        }

        ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x"));
        ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y"));
        ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "w"));
        ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "h"));
    } catch (const std::exception &ex) {
        FAIL() << ex.what();
    }
}

TEST_F(TestRectangle, ConstructorNew)
{
    try {
        auto ret = duk::pevalString(m_ctx,
            "r = new Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });"
            "x = r.x;"
            "y = r.y;"
            "w = r.width;"
            "h = r.height;"
        );

        if (ret != 0) {
            throw duk::exception(m_ctx, -1);
        }

        ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x"));
        ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y"));
        ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "w"));
        ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "h"));
    } catch (const std::exception &ex) {
        FAIL() << ex.what();
    }
}

/*
 * Invalid constructors
 * ------------------------------------------------------------------
 */

TEST_F(TestRectangle, InvalidConstructorArg1)
{
    try {
        auto ret = duk::pevalString(m_ctx,
            "try {"
            "  Malikania.Rectangle(null);"
            "} catch (e) {"
            "  name = e.name;"
            "  correct = (e instanceof TypeError);"
            "}"
        );

        if (ret != 0) {
            throw duk::exception(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(TestRectangle, InvalidConstructorRange1)
{
    try {
        auto ret = duk::pevalString(m_ctx,
            "try {"
            "  Malikania.Rectangle(0, 0, -10, -10);"
            "} catch (e) {"
            "  name = e.name;"
            "  correct = (e instanceof RangeError);"
            "}"
        );

        if (ret != 0) {
            throw duk::exception(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(TestRectangle, requireSuccess)
{
    try {
        duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
            Rectangle rect = duk::require<Rectangle>(ctx, 0);

            duk::putGlobal(ctx, "x", rect.x());
            duk::putGlobal(ctx, "y", rect.y());
            duk::putGlobal(ctx, "w", static_cast<int>(rect.width()));
            duk::putGlobal(ctx, "h", static_cast<int>(rect.height()));

            return 0;
        }, 1});

        auto ret = duk::pevalString(m_ctx, "build({ x: 50, y: 80, width: 100, height: 200 });");

        if (ret != 0) {
            throw duk::exception(m_ctx, -1);
        }

        ASSERT_EQ(50, duk::getGlobal<int>(m_ctx, "x"));
        ASSERT_EQ(80, duk::getGlobal<int>(m_ctx, "y"));
        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(TestRectangle, requireFail)
{
    try {
        duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
            duk::require<Rectangle>(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::exception(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(TestRectangle, getAdjustAll)
{
    try {
        duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
            Rectangle rect = duk::get<Rectangle>(ctx, 0);

            duk::putGlobal(ctx, "x", rect.x());
            duk::putGlobal(ctx, "y", rect.y());
            duk::putGlobal(ctx, "w", static_cast<int>(rect.width()));
            duk::putGlobal(ctx, "h", static_cast<int>(rect.height()));

            return 0;
        }, 1});

        auto ret = duk::pevalString(m_ctx, "build({});");

        if (ret != 0) {
            throw duk::exception(m_ctx, -1);
        }

        ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x"));
        ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y"));
        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();
    }
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}