From 7976b280e9722aec09cfc361aff11aafc5172120 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 9 Jun 2014 09:18:29 -0400 Subject: [PATCH] Add tests for issue #25 --- tests.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests.cpp b/tests.cpp index 63fdbce8..2445801d 100644 --- a/tests.cpp +++ b/tests.cpp @@ -623,3 +623,39 @@ TEST_CASE("tables/arbitrary-creation", "tables should be created from standard c REQUIRE(c.get("name") == "Rapptz"); REQUIRE(c.get("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", "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, '...')")); +}