view tests/libclient/sprite/main.cpp @ 42:a47a4477f347

Misc: new style, closes #578
author David Demelier <markand@malikania.fr>
date Tue, 29 Nov 2016 21:21:36 +0100
parents d4f5f7231b84
children fabbe1759cec
line wrap: on
line source

/*
 * main.cpp -- test Sprite
 *
 * 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 <chrono>
#include <thread>
#include <exception>

#include <gtest/gtest.h>

#include <malikania/client_resources_loader.hpp>
#include <malikania/resources_locator.hpp>
#include <malikania/sprite.hpp>
#include <malikania/window.hpp>

namespace mlk = malikania;

using namespace std::chrono_literals;

namespace {

mlk::window window(400, 400);

} // !namespace

class TestSprite : public testing::Test {
protected:
    mlk::directory_resources_locator m_locator;
    mlk::client_resources_loader m_loader;

public:
    TestSprite()
        : m_locator(SOURCE_DIRECTORY "/resources")
        , m_loader(m_locator)
    {
    }
};

/*
 * Missing properties
 * ------------------------------------------------------------------
 */

TEST_F(TestSprite, missingPropertyImage)
{
    try {
        m_loader.load_sprite("sprites/no-property-image.json");

        FAIL() << "exception expected";
    } catch (const std::exception &) {
    }
}

TEST_F(TestSprite, missingPropertyCell)
{
    try {
        m_loader.load_sprite("sprites/no-property-cell.json");

        FAIL() << "exception expected";
    } catch (const std::exception &) {
    }
}

/*
 * Invalid properties
 * ------------------------------------------------------------------
 */

TEST_F(TestSprite, imageNotString)
{
    try {
        m_loader.load_sprite("sprites/property-image-not-string.json");

        FAIL() << "exception expected";
    } catch (const std::exception &) {
    }
}

TEST_F(TestSprite, cellNotArray)
{
    try {
        m_loader.load_sprite("sprites/property-cell-not-array.json");

        FAIL() << "exception expected";
    } catch (const std::exception &) {
    }
}

TEST_F(TestSprite, cellNotArray2)
{
    try {
        m_loader.load_sprite("sprites/property-cell-not-array2.json");

        FAIL() << "exception expected";
    } catch (const std::exception &) {
    }
}

/*
 * Other errors
 * ------------------------------------------------------------------
 */

TEST_F(TestSprite, imageNotFound)
{
    try {
        m_loader.load_sprite("sprites/image-not-found.json");

        FAIL() << "exception expected";
    } catch (const std::exception &) {
    }
}

TEST_F(TestSprite, notObject)
{
    try {
        m_loader.load_sprite("sprites/not-object.json");

        FAIL() << "exception expected";
    } catch (const std::exception &) {
    }
}

/*
 * Valid sprites
 * ------------------------------------------------------------------
 */

TEST_F(TestSprite, standard)
{
    try {
        auto sprite = m_loader.load_sprite("sprites/simple.json");

        ASSERT_EQ(300U, sprite.cell().width());
        ASSERT_EQ(300U, sprite.cell().height());
    } catch (const std::exception &ex) {
        FAIL() << ex.what();
    }
}

TEST_F(TestSprite, margins)
{
    try {
        auto sprite = m_loader.load_sprite("sprites/margins.json");

        ASSERT_EQ(3U, sprite.rows());
        ASSERT_EQ(4U, sprite.columns());
    } catch (const std::exception &ex) {
        FAIL() << ex.what();
    }
}

TEST_F(TestSprite, draw)
{
    try {
        auto sprite = m_loader.load_sprite("sprites/margins.json");

        auto total = sprite.rows() * sprite.columns();
        auto x = (400 / 2) - (sprite.cell().width() / 2);
        auto y = (400 / 2) - (sprite.cell().height() / 2);

        for (unsigned c = 0; c < total; ++c) {
            window.clear();
            sprite.draw(window, c, mlk::point(x, y));
            window.present();

            std::this_thread::sleep_for(1s);
        }
    } catch (const std::exception &ex) {
        FAIL() << ex.what();
    }
}

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

    return RUN_ALL_TESTS();
}