Skip to content

Commit

Permalink
add LuaObject test
Browse files Browse the repository at this point in the history
  • Loading branch information
Habbie committed Mar 24, 2017
1 parent f0ecafe commit b6af634
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ add_executable(tests
tests/movable.cpp
tests/metatables.cpp
tests/threads.cpp
tests/luaobject.cpp
)

target_link_libraries(tests
Expand Down
30 changes: 30 additions & 0 deletions tests/luaobject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <LuaContext.hpp>
#include <gtest/gtest.h>

TEST(LuaObject, RoundTripPlusRefCounting) {
LuaContext context;

LuaContext::LuaObject v1, v2;

context.executeCode("function regsize() local count=0 for k,v in pairs(debug.getregistry()) do count=count+1 end return count end");
context.executeCode("function f1() return {foo='bar'} end");
context.executeCode("function f2(o) return type(o) end");

auto regsize = context.readVariable<std::function<int()>>("regsize");

auto f1 = context.readVariable<std::function<LuaContext::LuaObject()>>("f1");
auto f2 = context.readVariable<std::function<std::string(LuaContext::LuaObject)>>("f2");

int regsize0 = regsize();

v1 = f1();
EXPECT_EQ("table", f2(v1));

EXPECT_EQ(regsize0 + 1, regsize());

v2 = f1();
EXPECT_EQ(regsize0 + 2, regsize());

v1 = v2;
EXPECT_EQ(regsize0 + 1, regsize());
}

0 comments on commit b6af634

Please sign in to comment.