Skip to content

Commit

Permalink
additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsiaw committed May 18, 2016
1 parent 6169777 commit ba63bfb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
8 changes: 6 additions & 2 deletions LuaCppInterface/luaerror.cpp
Expand Up @@ -14,14 +14,18 @@ std::string LuaGetLastError(lua_State* state)
}
}

luaL_traceback(state, state, NULL, 1);
printf("%s\n", lua_tostring(state, -1));

lua_Debug debugInfo;
for (int level = 0; lua_getstack(state, level, &debugInfo); level++)
{
lua_getinfo(state, "nSlf", &debugInfo);
ss << "Line: " << debugInfo.currentline << std::endl;
ss << "Source: " << debugInfo.source << std::endl;
ss << "Source: " << debugInfo.short_src << std::endl;

const char* function = lua_tostring(state, -1);
lua_getinfo(state, ">nS", &debugInfo);
const char* function = debugInfo.name;
if (function)
{
ss << "Function: " << function << std::endl;
Expand Down
5 changes: 4 additions & 1 deletion tests/Makefile.am
Expand Up @@ -3,7 +3,7 @@ if COND_GCOV
MAYBE_COVERAGE=-fprofile-arcs
endif

TESTS = demonstration1 demonstration2 demonstration3 demonstration4 luacpp testcallabletable testchartowideconvert testcoroutineexception testcoroutineexpectedbutnot testcreate testctoluafunction testexceptioninluafunction testextractingasfunctionbutistable testfunction testfunctionexpectedbutnotcallable testfunctionparams testfunctionparamsreturn testfunctionreturn testgetmetatableonclosedstate testgetsetfunction testgetsetfunction2 testgetsetinsidetable testgetsetinteger testgetsetstring testgetsettable testgetstate testgettypeofvalueat testinvalidscript testpassingfunction testreadinglightuserdatafromuserdata testregistry testresumenoscript testreturnfromnative testreturnfromscript testreturnfromyieldingfunction testreturntablefromlua testreturntablefromnative testscriptexception testsetstring testsetwstring testtable testtableexpectedbutnotable testtableforeachintegerkey testtableforeachstringkey testtypeintstringmorph testtypemorphintwstring testtypestringintmorph testuserdata testuserdataconstructor testuserdatadispose testuserdatamethod testwidetocharconvert
TESTS = demonstration1 demonstration2 demonstration3 demonstration4 luacpp testcallabletable testchartowideconvert testcoroutineexception testcoroutineexpectedbutnot testcreate testctoluafunction testexceptioninluafunction testextractingasfunctionbutistable testfunction testfunctionerror testfunctionexpectedbutnotcallable testfunctionparams testfunctionparamsreturn testfunctionreturn testgetmetatableonclosedstate testgetsetfunction testgetsetfunction2 testgetsetinsidetable testgetsetinteger testgetsetstring testgetsettable testgetstate testgettypeofvalueat testinvalidscript testpassingfunction testreadinglightuserdatafromuserdata testregistry testresumenoscript testreturnfromnative testreturnfromscript testreturnfromyieldingfunction testreturntablefromlua testreturntablefromnative testscriptexception testsetstring testsetwstring testtable testtableexpectedbutnotable testtableforeachintegerkey testtableforeachstringkey testtypeintstringmorph testtypemorphintwstring testtypestringintmorph testuserdata testuserdataconstructor testuserdatadispose testuserdatamethod testwidetocharconvert

check_PROGRAMS = $(TESTS)

Expand Down Expand Up @@ -53,6 +53,9 @@ testextractingasfunctionbutistable_LDADD = $(REQUIRED_LIBS)
testfunction_SOURCES = testfunction.cpp lua
testfunction_LDADD = $(REQUIRED_LIBS)

testfunctionerror_SOURCES = testfunctionerror.cpp lua
testfunctionerror_LDADD = $(REQUIRED_LIBS)

testfunctionexpectedbutnotcallable_SOURCES = testfunctionexpectedbutnotcallable.cpp lua
testfunctionexpectedbutnotcallable_LDADD = $(REQUIRED_LIBS)

Expand Down
51 changes: 51 additions & 0 deletions tests/testfunctionerror.cpp
@@ -0,0 +1,51 @@
// Test running a script that has a few functions and fails


#include <iostream>
#include <luacppinterface.h>

int main()
{
Lua lua;
auto global = lua.GetGlobalEnvironment();

auto function = lua.CreateFunction< void() >([&]()
{
global.Set("x", 5);
auto table = global.Get<LuaTable>("x");
table.Set("a", 5);
});


auto function2 = lua.CreateFunction< void() >([&]()
{
global.Get< LuaFunction<void()> >("meow").Invoke();
});


global.Set("func", function);
global.Set("func2", function2);

lua.RunScript(R"(
woof = function ()
func2()
end
function meow()
func()
end
)");

try
{
global.Get< LuaFunction<void()> >("woof").Invoke();
std::cout << LuaGetLastError(function.GetState().get()) << std::endl;
}
catch(LuaError e)
{
std::cout << e.GetMessage() << std::endl;
}

return 0;
}

0 comments on commit ba63bfb

Please sign in to comment.