Mercurial > code
diff C++/tests/Js/main.cpp @ 398:94bfe7ba9a13
Js:
- Rename some functions
- Cleanup a lot
- Add more documentation
- Add specialization of getProperty and getGlobal to void to push on the
stack
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 30 Sep 2015 19:59:12 +0200 |
parents | 6b2db5425836 |
children | 73517fd307dd |
line wrap: on
line diff
--- a/C++/tests/Js/main.cpp Tue Sep 29 12:50:28 2015 +0200 +++ b/C++/tests/Js/main.cpp Wed Sep 30 19:59:12 2015 +0200 @@ -111,13 +111,13 @@ ASSERT_EQ(current, context.top()); } -TEST(Basics, setObject) +TEST(Basics, putProperty) { Context context; context.push(Object{}); - context.setObject(-1, "x", 123); - ASSERT_EQ(123, context.getObject<int>(-1, "x")); + context.putProperty(-1, "x", 123); + ASSERT_EQ(123, context.getProperty<int>(-1, "x")); } TEST(Basics, enumerate) @@ -125,8 +125,8 @@ Context context; context.push(Object{}); - context.setObject(-1, "x", 123); - context.setObject(-1, "y", 456); + context.putProperty(-1, "x", 123); + context.putProperty(-1, "y", 456); context.enumerate(-1, 0, true, [] (Context &ctx) { ASSERT_EQ(DUK_TYPE_STRING, ctx.type(-2)); ASSERT_EQ(DUK_TYPE_NUMBER, ctx.type(-1)); @@ -142,9 +142,7 @@ { Context context; - context.push(Function{[] (ContextPtr pctx) -> duk_ret_t { - Context ctx{pctx}; - + context.push(Function{[] (Context &ctx) -> int { ctx.setGlobal("x", 123); return 0; @@ -162,7 +160,7 @@ { Context context; - context.evalString("x = 123;"); + context.eval(Script{"x = 123;"}); ASSERT_EQ(123, context.getGlobal<int>("x")); } @@ -170,7 +168,7 @@ { Context context; - context.evalString("function f() { x = 123; }; f();"); + context.eval(Script{"function f() { x = 123; }; f();"}); ASSERT_EQ(123, context.getGlobal<int>("x")); } @@ -178,14 +176,12 @@ { Context context; - context.setGlobal("f", Function{[] (ContextPtr pctx) -> duk_idx_t { - Context ctx{pctx}; - + context.setGlobal("f", Function{[] (Context &ctx) -> int { ctx.setGlobal("x", 123); return 0; }}); - context.evalString("f()"); + context.eval(Script{"f()"}); ASSERT_EQ(123, context.getGlobal<int>("x")); } @@ -199,7 +195,7 @@ Context context; try { - context.pevalString("x = 1"); + context.peval(Script{"x = 1"}); } catch (const ErrorInfo &info) { FAIL() << "error unexpected: " << info.what(); } @@ -210,10 +206,10 @@ Context context; try { - context.pevalString("doesnotexists()"); + context.peval(Script{"doesnotexists()"}); FAIL() << "expected exception"; - } catch (const ErrorInfo &) { + } catch (const std::exception &) { } } @@ -232,7 +228,7 @@ return 0; }}); - context.evalString( + context.eval(Script{ "try {" " f();" "} catch (ex) {" @@ -240,7 +236,7 @@ " message = ex.message;" " received = true;" "}" - ); + }); ASSERT_TRUE(context.getGlobal<bool>("received")); ASSERT_EQ("Error", context.getGlobal<std::string>("name")); @@ -265,7 +261,7 @@ ctx.push(Object{}); /* "update" method */ - ctx.setObject(-1, "update", Function{[] (ContextPtr pctx) -> duk_ret_t { + ctx.putProperty(-1, "update", Function{[] (ContextPtr pctx) -> duk_ret_t { Context{pctx}.self<std::shared_ptr<Player>>()->m_updated = true; return 0; @@ -293,7 +289,7 @@ try { ctx.setGlobal("player", p); - ctx.pevalString("player.update();"); + ctx.peval(Script{"player.update();"}); ASSERT_TRUE(p->m_updated); } catch (const std::exception &ex) {