view tests/libclient/js-line/main.cpp @ 63:96ba0c5cf893

Misc: update duktape.hpp to new style
author David Demelier <markand@malikania.fr>
date Fri, 16 Dec 2016 16:11:24 +0100
parents fe7e3524e571
children 858621081b95
line wrap: on
line source

/*
 * main.cpp -- test Line (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.
 */

#define BOOST_TEST_MODULE "Javascript Line"
#include <boost/test/unit_test.hpp>

#include <malikania/js_line.hpp>

class test_line {
protected:
    dukx_context m_ctx;

public:
    test_line()
    {
        duk_push_object(m_ctx);
        duk_put_global_string(m_ctx, "Malikania");
        mlk::dukx_load_line(m_ctx);
    }
};

BOOST_FIXTURE_TEST_SUITE(test_line_suite, test_line)

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

BOOST_AUTO_TEST_SUITE(constructors)

BOOST_AUTO_TEST_CASE(constructor_default)
{
    try {
        auto ret = duk_peval_string(m_ctx,
            "r = Malikania.Line();"
            "x1 = r.x1;"
            "y1 = r.y1;"
            "x2 = r.x2;"
            "y2 = r.y2;"
        );

        if (ret != 0) {
            throw dukx_get_exception(m_ctx, -1);
        }

        duk_get_global_string(m_ctx, "x1");
        BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y1");
        BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "x2");
        BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y2");
        BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
    } catch (const std::exception &ex) {
        BOOST_FAIL(ex.what());
    }
}

BOOST_AUTO_TEST_CASE(constructor_4_args)
{
    try {
        auto ret = duk_peval_string(m_ctx,
            "r = Malikania.Line(10, 20, 30, 40);"
            "x1 = r.x1;"
            "y1 = r.y1;"
            "x2 = r.x2;"
            "y2 = r.y2;"
        );

        if (ret != 0) {
            throw dukx_get_exception(m_ctx, -1);
        }

        duk_get_global_string(m_ctx, "x1");
        BOOST_REQUIRE_EQUAL(10, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y1");
        BOOST_REQUIRE_EQUAL(20, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "x2");
        BOOST_REQUIRE_EQUAL(30, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y2");
        BOOST_REQUIRE_EQUAL(40, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
    } catch (const std::exception &ex) {
        BOOST_FAIL(ex.what());
    }
}

BOOST_AUTO_TEST_CASE(constructor_object)
{
    try {
        auto ret = duk_peval_string(m_ctx,
            "r = Malikania.Line({ x1: 10, y1: 20, x2: 30, y2: 40 });"
            "x1 = r.x1;"
            "y1 = r.y1;"
            "x2 = r.x2;"
            "y2 = r.y2;"
        );

        if (ret != 0) {
            throw dukx_get_exception(m_ctx, -1);
        }

        duk_get_global_string(m_ctx, "x1");
        BOOST_REQUIRE_EQUAL(10, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y1");
        BOOST_REQUIRE_EQUAL(20, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "x2");
        BOOST_REQUIRE_EQUAL(30, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y2");
        BOOST_REQUIRE_EQUAL(40, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
    } catch (const std::exception &ex) {
        BOOST_FAIL(ex.what());
    }
}

BOOST_AUTO_TEST_CASE(constructor_new)
{
    try {
        auto ret = duk_peval_string(m_ctx,
            "r = new Malikania.Line({ x1: 10, y1: 20, x2: 30, y2: 40 });"
            "x1 = r.x1;"
            "y1 = r.y1;"
            "x2 = r.x2;"
            "y2 = r.y2;"
        );

        if (ret != 0) {
            throw dukx_get_exception(m_ctx, -1);
        }

        duk_get_global_string(m_ctx, "x1");
        BOOST_REQUIRE_EQUAL(10, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y1");
        BOOST_REQUIRE_EQUAL(20, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "x2");
        BOOST_REQUIRE_EQUAL(30, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y2");
        BOOST_REQUIRE_EQUAL(40, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
    } catch (const std::exception &ex) {
        BOOST_FAIL(ex.what());
    }
}

BOOST_AUTO_TEST_SUITE_END()

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

BOOST_AUTO_TEST_SUITE(invalid_constructors)

BOOST_AUTO_TEST_CASE(arg_1)
{
    try {
        auto ret = duk_peval_string(m_ctx,
            "try {"
            "  Malikania.Line(null);"
            "} catch (e) {"
            "  name = e.name;"
            "  correct = (e instanceof TypeError);"
            "}"
        );

        if (ret != 0) {
            throw dukx_get_exception(m_ctx, -1);
        }

        duk_get_global_string(m_ctx, "name");
        BOOST_REQUIRE_EQUAL("TypeError", duk_to_string(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "correct");
        BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
        duk_pop(m_ctx);
    } catch (const std::exception &ex) {
        BOOST_FAIL(ex.what());
    }
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * Require.
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(require)

BOOST_AUTO_TEST_CASE(success)
{
    try {
        duk_push_c_function(m_ctx, [] (auto ctx) {
            auto line = mlk::dukx_require_line(ctx, 0);

            duk_push_int(ctx, line.x1());
            duk_put_global_string(ctx, "x1");
            duk_push_int(ctx, line.y1());
            duk_put_global_string(ctx, "y1");
            duk_push_int(ctx, line.x2());
            duk_put_global_string(ctx, "x2");
            duk_push_int(ctx, line.y2());
            duk_put_global_string(ctx, "y2");

            return 0;
        }, 1);
        duk_put_global_string(m_ctx, "build");

        auto ret = duk_peval_string(m_ctx, "build({ x1: 50, y1: 80, x2: 100, y2: 200 });");

        if (ret != 0) {
            throw dukx_get_exception(m_ctx, -1);
        }

        duk_get_global_string(m_ctx, "x1");
        BOOST_REQUIRE_EQUAL(50, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y1");
        BOOST_REQUIRE_EQUAL(80, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "x2");
        BOOST_REQUIRE_EQUAL(100, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y2");
        BOOST_REQUIRE_EQUAL(200, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
    } catch (const std::exception &ex) {
        BOOST_FAIL(ex.what());
    }
}

BOOST_AUTO_TEST_CASE(fail)
{
    try {
        duk_push_c_function(m_ctx, [] (auto ctx) {
            mlk::dukx_require_line(ctx, 0);

            return 0;
        }, 1);
        duk_put_global_string(m_ctx, "build");

        auto ret = duk_peval_string(m_ctx,
            "try {"
            "  build({});"
            "} catch (e) {"
            "  name = e.name;"
            "  correct = (e instanceof Error);"
            "}"
        );

        if (ret != 0) {
            throw dukx_get_exception(m_ctx, -1);
        }

        duk_get_global_string(m_ctx, "name");
        BOOST_REQUIRE_EQUAL("Error", duk_to_string(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "correct");
        BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
        duk_pop(m_ctx);
    } catch (const std::exception &ex) {
        BOOST_FAIL(ex.what());
    }
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * Get.
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(get)

BOOST_AUTO_TEST_CASE(adjust_all)
{
    try {
        duk_push_c_function(m_ctx, [] (auto ctx) {
            auto line = mlk::dukx_get_line(ctx, 0);

            duk_push_int(ctx, line.x1());
            duk_put_global_string(ctx, "x1");
            duk_push_int(ctx, line.y1());
            duk_put_global_string(ctx, "y1");
            duk_push_int(ctx, line.x2());
            duk_put_global_string(ctx, "x2");
            duk_push_int(ctx, line.y2());
            duk_put_global_string(ctx, "y2");

            return 0;
        }, 1);
        duk_put_global_string(m_ctx, "build");

        auto ret = duk_peval_string(m_ctx, "build({});");

        if (ret != 0) {
            throw dukx_get_exception(m_ctx, -1);
        }

        duk_get_global_string(m_ctx, "x1");
        BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y1");
        BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "x2");
        BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
        duk_get_global_string(m_ctx, "y2");
        BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
        duk_pop(m_ctx);
    } catch (const std::exception &ex) {
        BOOST_FAIL(ex.what());
    }
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()