Mercurial > code
diff C++/tests/Js/main.cpp @ 397:6b2db5425836
Js: initial support for objects as shared_ptr
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 29 Sep 2015 12:50:28 +0200 |
parents | 69adcefe73ae |
children | 94bfe7ba9a13 |
line wrap: on
line diff
--- a/C++/tests/Js/main.cpp Mon Sep 28 19:40:26 2015 +0200 +++ b/C++/tests/Js/main.cpp Tue Sep 29 12:50:28 2015 +0200 @@ -247,6 +247,60 @@ ASSERT_EQ("error thrown", context.getGlobal<std::string>("message")); } +/* -------------------------------------------------------- + * TypeInfo with shared + * -------------------------------------------------------- */ + +class Player { +public: + bool m_updated{false}; +}; + +template <> +class TypeInfo<std::shared_ptr<Player>> : public TypeInfoShared<Player> { +public: + static void prototype(Context &ctx) + { + /* Create a temporary for the test */ + ctx.push(Object{}); + + /* "update" method */ + ctx.setObject(-1, "update", Function{[] (ContextPtr pctx) -> duk_ret_t { + Context{pctx}.self<std::shared_ptr<Player>>()->m_updated = true; + + return 0; + }}); + } +}; + +TEST(Shared, simple) +{ + Context ctx; + std::shared_ptr<Player> p{new Player}; + + ctx.push(p); + std::shared_ptr<Player> p2 = ctx.get<std::shared_ptr<Player>>(-1); + + p2->m_updated = true; + + ASSERT_TRUE(p->m_updated); +} + +TEST(Shared, self) +{ + Context ctx; + std::shared_ptr<Player> p{new Player}; + + try { + ctx.setGlobal("player", p); + ctx.pevalString("player.update();"); + + ASSERT_TRUE(p->m_updated); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv);