Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satoren committed Jun 11, 2016
1 parent 116d724 commit 25a9a54
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
54 changes: 51 additions & 3 deletions test/test_04_lua_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,38 @@ void corresult_to_main2(kaguya::VariadicArgType args)
}


int coroutine_exec(kaguya::LuaThread cor)
{
return cor.resume<int>() * 2;
}

KAGUYA_TEST_FUNCTION_DEF(coroutine_on_the_coroutine)(kaguya::State& state)
{
TEST_CHECK(state("cor1 = coroutine.create( function()"
"coroutine.yield(32) "
"coroutine.yield(53) "
"return 2 "
" end)"));

state["coroutine_exec"] = coroutine_exec;

TEST_CHECK(state("cor2 = coroutine.wrap( function()"
"coroutine.yield(coroutine_exec(cor1)) "
"coroutine.yield(coroutine_exec(cor1)) "
"return 4 "
" end)"));

kaguya::LuaRef cor = state["cor2"];
int r1 = cor();
int r2 = cor();
int r3 = cor();

TEST_EQUAL(r1, 32*2);
TEST_EQUAL(r2, 53*2);
TEST_EQUAL(r3, 4);
}


KAGUYA_TEST_FUNCTION_DEF(coroutine_stack)(kaguya::State& state)
{
state["corresult_to_main"] = &corresult_to_main;
Expand Down Expand Up @@ -226,7 +258,7 @@ KAGUYA_TEST_FUNCTION_DEF(bind_to_std_function)(kaguya::State& state)
}


KAGUYA_TEST_FUNCTION_DEF(zero_to_nullpointer)(kaguya::State& state)
KAGUYA_TEST_FUNCTION_DEF(call_error)(kaguya::State& state)
{
state.setErrorHandler(ignore_error_fun);

Expand All @@ -243,12 +275,28 @@ KAGUYA_TEST_FUNCTION_DEF(zero_to_nullpointer)(kaguya::State& state)
kaguya::LuaFunction nilfn;
nilfn.call<void>();
}

{
last_error_message = "";
kaguya::LuaFunction notfn = state.newTable();
TEST_COMPARE_NE(last_error_message, "");
notfn.call<void>();
}
}


KAGUYA_TEST_FUNCTION_DEF(result_range_based_for)(kaguya::State& state)
KAGUYA_TEST_FUNCTION_DEF(function_result_for)(kaguya::State& state)
{
{
const kaguya::FunctionResults empty;
TEST_EQUAL(empty.result_size(), 0);
std::vector<int> res;
for (kaguya::FunctionResults::iterator it = empty.begin(); it != empty.end(); ++it)
{
res.push_back(*it);
}
TEST_EQUAL(res.size(), 0);
}

state("fn =function() return 1,2,4,8,16 end");
{
std::vector<int> res;
Expand Down
7 changes: 7 additions & 0 deletions test/test_05_lua_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ KAGUYA_TEST_FUNCTION_DEF(nostate_ref_error)(kaguya::State& state)
TEST_CHECK(!v.setFunctionEnv(state.newTable()));
TEST_CHECK(!v.getFunctionEnv());
TEST_CHECK(!v.getField("s"));
TEST_CHECK(!v.setField("s", 2));
TEST_CHECK(!v.setField("s", "a"));
TEST_CHECK(!v.setField("s", std::string("a")));
TEST_CHECK(!v.setField("s", table));
TEST_CHECK(!v.setField("s", kaguya::NilValue()));
TEST_CHECK(!v.setField(kaguya::NilValue(), kaguya::NilValue()));


TEST_CHECK(!table.getFunctionEnv());
Expand All @@ -523,6 +529,7 @@ KAGUYA_TEST_FUNCTION_DEF(nostate_ref_error)(kaguya::State& state)

TEST_CHECK(!cv.getFunctionEnv());
TEST_CHECK(!cv.getField("s"));
TEST_CHECK(!cv.getField<int>("s"));


TEST_CHECK(!ctable.getFunctionEnv());
Expand Down
26 changes: 26 additions & 0 deletions test/test_09_utility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "kaguya/kaguya.hpp"
#include "test_util.hpp"

KAGUYA_TEST_GROUP_START(test_09_utility)

using namespace kaguya_test_util;


KAGUYA_TEST_FUNCTION_DEF(resume)(kaguya::State& s)
{
using namespace kaguya;


LuaThread t = s.newThread(s.loadstring("v={...}"));

lua_State* co = t.get<lua_State*>();
lua_pushnumber(co, 2);
lua_pushnumber(co, 3);

lua_resume(co,s.state(),2);

TEST_EQUAL(s["v"][1], 2);
TEST_EQUAL(s["v"][2], 3);
}

KAGUYA_TEST_GROUP_END(test_09_utility)

0 comments on commit 25a9a54

Please sign in to comment.