# HG changeset patch # User David Demelier # Date 1459942410 -7200 # Node ID dc47ce56ce360171cb8d9614a5cf7ff89075d336 # Parent 7f7c2607ace3815761d4f791ebe9ccc052dbdc8b Client: add properties for Malikania.Sprite, #461 - cell, - columns, - margins, - rows, - space. diff -r 7f7c2607ace3 -r dc47ce56ce36 libclient/malikania/js-sprite.cpp --- a/libclient/malikania/js-sprite.cpp Wed Apr 06 13:17:33 2016 +0200 +++ b/libclient/malikania/js-sprite.cpp Wed Apr 06 13:33:30 2016 +0200 @@ -26,6 +26,41 @@ namespace { +duk::Ret cell(duk::ContextPtr ctx) +{ + duk::push(ctx, duk::self>(ctx)->cell()); + + return 1; +} + +duk::Ret columns(duk::ContextPtr ctx) +{ + duk::push(ctx, static_cast(duk::self>(ctx)->columns())); + + return 1; +} + +duk::Ret margins(duk::ContextPtr ctx) +{ + duk::push(ctx, duk::self>(ctx)->margin()); + + return 1; +} + +duk::Ret rows(duk::ContextPtr ctx) +{ + duk::push(ctx, static_cast(duk::self>(ctx)->rows())); + + return 1; +} + +duk::Ret space(duk::ContextPtr ctx) +{ + duk::push(ctx, duk::self>(ctx)->space()); + + return 1; +} + duk::Ret constructor(duk::ContextPtr ctx) { if (!duk_is_constructor_call(ctx)) { @@ -37,6 +72,34 @@ auto sprite = loader->loadSprite(duk::require(ctx, 0)); duk::construct(ctx, duk::Pointer{new Sprite(std::move(sprite))}); + duk::push(ctx, duk::This()); + + /* Cell */ + duk::push(ctx, "cell"); + duk::push(ctx, duk::Function{cell}); + duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER); + + /* Columns */ + duk::push(ctx, "columns"); + duk::push(ctx, duk::Function{columns}); + duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER); + + /* Margins */ + duk::push(ctx, "margins"); + duk::push(ctx, duk::Function{margins}); + duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER); + + /* Rows */ + duk::push(ctx, "rows"); + duk::push(ctx, duk::Function{rows}); + duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER); + + /* Space */ + duk::push(ctx, "space"); + duk::push(ctx, duk::Function{space}); + duk::defineProperty(ctx, -3, DUK_DEFPROP_HAVE_GETTER); + + duk::pop(ctx); } catch (const std::exception &ex) { duk::raise(ctx, DUK_ERR_ERROR, "%s", ex.what()); } diff -r 7f7c2607ace3 -r dc47ce56ce36 tests/libclient/js-sprite/main.cpp --- a/tests/libclient/js-sprite/main.cpp Wed Apr 06 13:17:33 2016 +0200 +++ b/tests/libclient/js-sprite/main.cpp Wed Apr 06 13:33:30 2016 +0200 @@ -54,6 +54,102 @@ } }; +TEST_F(TestSprite, cell) +{ + try { + auto ret = duk::pevalString(m_ctx, + "s = new Malikania.Sprite('sprites/margins.json');" + "w = s.cell.width;" + "h = s.cell.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(32, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(32, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestSprite, columns) +{ + try { + auto ret = duk::pevalString(m_ctx, + "s = new Malikania.Sprite('sprites/margins.json');" + "n = s.columns;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(4, duk::getGlobal(m_ctx, "n")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestSprite, margins) +{ + try { + auto ret = duk::pevalString(m_ctx, + "s = new Malikania.Sprite('sprites/margins.json');" + "w = s.margins.width;" + "h = s.margins.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(4, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(6, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestSprite, rows) +{ + try { + auto ret = duk::pevalString(m_ctx, + "s = new Malikania.Sprite('sprites/margins.json');" + "n = s.rows;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(3, duk::getGlobal(m_ctx, "n")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestSprite, space) +{ + try { + auto ret = duk::pevalString(m_ctx, + "s = new Malikania.Sprite('sprites/margins.json');" + "w = s.space.width;" + "h = s.space.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(2, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(3, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + TEST_F(TestSprite, basic) { try {