mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Add tests for issue #25
This commit is contained in:
parent
6fbae52e9f
commit
7976b280e9
36
tests.cpp
36
tests.cpp
|
@ -623,3 +623,39 @@ TEST_CASE("tables/arbitrary-creation", "tables should be created from standard c
|
||||||
REQUIRE(c.get<std::string>("name") == "Rapptz");
|
REQUIRE(c.get<std::string>("name") == "Rapptz");
|
||||||
REQUIRE(c.get<std::string>("project") == "sol");
|
REQUIRE(c.get<std::string>("project") == "sol");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("tables/issue-number-twenty-five", "Using pointers and references from C++ classes in Lua") {
|
||||||
|
struct test {
|
||||||
|
int x = 0;
|
||||||
|
test& set() {
|
||||||
|
x = 10;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
test* clone() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fun(int x) {
|
||||||
|
return x * 10;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
sol::state lua;
|
||||||
|
lua.open_libraries(sol::lib::base);
|
||||||
|
lua.new_userdata<test>("test", "set", &test::set, "get", &test::get, "clone", &test::clone, "fun", &test::fun);
|
||||||
|
REQUIRE_NOTHROW(lua.script("x = test.new()\n"
|
||||||
|
"x:set():get()"));
|
||||||
|
REQUIRE_NOTHROW(lua.script("y = x:clone()"));
|
||||||
|
REQUIRE_NOTHROW(lua.script("y:set():get()"));
|
||||||
|
REQUIRE_NOTHROW(lua.script("y:fun(10)"));
|
||||||
|
REQUIRE_NOTHROW(lua.script("x:fun(10)"));
|
||||||
|
REQUIRE_NOTHROW(lua.script("assert(y:fun(10) == x:fun(10), '...')"));
|
||||||
|
REQUIRE_NOTHROW(lua.script("assert(y:fun(10) == 100, '...')"));
|
||||||
|
REQUIRE_NOTHROW(lua.script("assert(y:set():get() == y:set():get(), '...')"));
|
||||||
|
REQUIRE_NOTHROW(lua.script("assert(y:set():get() == 10, '...')"));
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user