2016-09-18 10:15:46 +08:00
|
|
|
#define SOL_CHECK_ARGUMENTS
|
|
|
|
|
|
|
|
#include <sol.hpp>
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
TEST_CASE("inheritance/basic", "test that metatables are properly inherited") {
|
|
|
|
struct A {
|
|
|
|
int a = 5;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct B {
|
|
|
|
int b() {
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct C : B, A {
|
|
|
|
double c = 2.4;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct D : C {
|
|
|
|
bool d() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.new_usertype<A>("A",
|
2017-09-17 02:18:45 +08:00
|
|
|
"a", &A::a);
|
2016-09-18 10:15:46 +08:00
|
|
|
lua.new_usertype<B>("B",
|
2017-09-17 02:18:45 +08:00
|
|
|
"b", &B::b);
|
2016-09-18 10:15:46 +08:00
|
|
|
lua.new_usertype<C>("C",
|
|
|
|
"c", &C::c,
|
2017-09-17 02:18:45 +08:00
|
|
|
sol::base_classes, sol::bases<B, A>());
|
2016-09-18 10:15:46 +08:00
|
|
|
lua.new_usertype<D>("D",
|
|
|
|
"d", &D::d,
|
2017-09-17 02:18:45 +08:00
|
|
|
sol::base_classes, sol::bases<C, B, A>());
|
2016-09-18 10:15:46 +08:00
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script("obj = D.new()");
|
|
|
|
lua.safe_script("d = obj:d()");
|
2016-09-18 10:15:46 +08:00
|
|
|
bool d = lua["d"];
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script("c = obj.c");
|
2016-09-18 10:15:46 +08:00
|
|
|
double c = lua["c"];
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script("b = obj:b()");
|
2016-09-18 10:15:46 +08:00
|
|
|
int b = lua["b"];
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script("a = obj.a");
|
2016-09-18 10:15:46 +08:00
|
|
|
int a = lua["a"];
|
|
|
|
|
|
|
|
REQUIRE(d);
|
|
|
|
REQUIRE(c == 2.4);
|
|
|
|
REQUIRE(b == 10);
|
|
|
|
REQUIRE(a == 5);
|
|
|
|
}
|
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
TEST_CASE("inheritance/multi base", "test that multiple bases all work and overloading for constructors works with them") {
|
2016-09-18 10:15:46 +08:00
|
|
|
class TestClass00 {
|
|
|
|
public:
|
2017-09-17 02:18:45 +08:00
|
|
|
int Thing() const {
|
|
|
|
return 123;
|
|
|
|
}
|
2016-09-18 10:15:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class TestClass01 : public TestClass00 {
|
|
|
|
public:
|
2017-09-17 02:18:45 +08:00
|
|
|
TestClass01()
|
|
|
|
: a(1) {
|
|
|
|
}
|
|
|
|
TestClass01(const TestClass00& other)
|
|
|
|
: a(other.Thing()) {
|
|
|
|
}
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestClass02 : public TestClass01 {
|
|
|
|
public:
|
2017-09-17 02:18:45 +08:00
|
|
|
TestClass02()
|
|
|
|
: b(2) {
|
|
|
|
}
|
|
|
|
TestClass02(const TestClass01& other)
|
|
|
|
: b(other.a) {
|
|
|
|
}
|
|
|
|
TestClass02(const TestClass00& other)
|
|
|
|
: b(other.Thing()) {
|
|
|
|
}
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
int b;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestClass03 : public TestClass02 {
|
|
|
|
public:
|
2017-09-17 02:18:45 +08:00
|
|
|
TestClass03()
|
|
|
|
: c(2) {
|
|
|
|
}
|
|
|
|
TestClass03(const TestClass02& other)
|
|
|
|
: c(other.b) {
|
|
|
|
}
|
|
|
|
TestClass03(const TestClass01& other)
|
|
|
|
: c(other.a) {
|
|
|
|
}
|
|
|
|
TestClass03(const TestClass00& other)
|
|
|
|
: c(other.Thing()) {
|
|
|
|
}
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
int c;
|
|
|
|
};
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
|
|
|
|
sol::usertype<TestClass00> s_TestUsertype00(
|
|
|
|
sol::call_constructor, sol::constructors<sol::types<>>(),
|
2017-09-17 02:18:45 +08:00
|
|
|
"Thing", &TestClass00::Thing);
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
lua.set_usertype("TestClass00", s_TestUsertype00);
|
|
|
|
|
|
|
|
sol::usertype<TestClass01> s_TestUsertype01(
|
|
|
|
sol::call_constructor, sol::constructors<sol::types<>, sol::types<const TestClass00&>>(),
|
|
|
|
sol::base_classes, sol::bases<TestClass00>(),
|
2017-09-17 02:18:45 +08:00
|
|
|
"a", &TestClass01::a);
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
lua.set_usertype("TestClass01", s_TestUsertype01);
|
|
|
|
|
|
|
|
sol::usertype<TestClass02> s_TestUsertype02(
|
|
|
|
sol::call_constructor, sol::constructors<sol::types<>, sol::types<const TestClass01&>, sol::types<const TestClass00&>>(),
|
|
|
|
sol::base_classes, sol::bases<TestClass01, TestClass00>(),
|
2017-09-17 02:18:45 +08:00
|
|
|
"b", &TestClass02::b);
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
lua.set_usertype("TestClass02", s_TestUsertype02);
|
|
|
|
|
|
|
|
sol::usertype<TestClass03> s_TestUsertype03(
|
|
|
|
sol::call_constructor, sol::constructors<sol::types<>, sol::types<const TestClass02&>, sol::types<const TestClass01&>, sol::types<const TestClass00&>>(),
|
|
|
|
sol::base_classes, sol::bases<TestClass02, TestClass01, TestClass00>(),
|
2017-09-17 02:18:45 +08:00
|
|
|
"c", &TestClass03::c);
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
lua.set_usertype("TestClass03", s_TestUsertype03);
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script(R"(
|
2016-09-18 10:15:46 +08:00
|
|
|
tc0 = TestClass00()
|
|
|
|
)");
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script(R"(
|
2016-09-18 10:15:46 +08:00
|
|
|
tc2 = TestClass02(tc0)
|
|
|
|
)");
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script(R"(
|
2016-09-18 10:15:46 +08:00
|
|
|
tc1 = TestClass01()
|
|
|
|
)");
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script(R"(
|
2016-09-18 10:15:46 +08:00
|
|
|
tc3 = TestClass03(tc1)
|
|
|
|
)");
|
|
|
|
|
|
|
|
TestClass00& tc0 = lua["tc0"];
|
|
|
|
TestClass01& tc1 = lua["tc1"];
|
|
|
|
TestClass02& tc2 = lua["tc2"];
|
|
|
|
TestClass03& tc3 = lua["tc3"];
|
2016-11-26 09:47:15 +08:00
|
|
|
REQUIRE(tc0.Thing() == 123);
|
2016-09-18 10:15:46 +08:00
|
|
|
REQUIRE(tc1.a == 1);
|
|
|
|
REQUIRE(tc2.a == 1);
|
|
|
|
REQUIRE(tc2.b == 123);
|
|
|
|
REQUIRE(tc3.a == 1);
|
|
|
|
REQUIRE(tc3.b == 2);
|
|
|
|
REQUIRE(tc3.c == 1);
|
|
|
|
}
|
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
TEST_CASE("inheritance/simple multi base", "test that multiple bases all work and overloading for constructors works with them") {
|
2016-09-18 10:15:46 +08:00
|
|
|
class TestClass00 {
|
|
|
|
public:
|
2017-09-17 02:18:45 +08:00
|
|
|
int Thing() const {
|
|
|
|
return 123;
|
|
|
|
}
|
2016-09-18 10:15:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class TestClass01 : public TestClass00 {
|
|
|
|
public:
|
2017-09-17 02:18:45 +08:00
|
|
|
TestClass01()
|
|
|
|
: a(1) {
|
|
|
|
}
|
|
|
|
TestClass01(const TestClass00& other)
|
|
|
|
: a(other.Thing()) {
|
|
|
|
}
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestClass02 : public TestClass01 {
|
|
|
|
public:
|
2017-09-17 02:18:45 +08:00
|
|
|
TestClass02()
|
|
|
|
: b(2) {
|
|
|
|
}
|
|
|
|
TestClass02(const TestClass01& other)
|
|
|
|
: b(other.a) {
|
|
|
|
}
|
|
|
|
TestClass02(const TestClass00& other)
|
|
|
|
: b(other.Thing()) {
|
|
|
|
}
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
int b;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestClass03 : public TestClass02 {
|
|
|
|
public:
|
2017-09-17 02:18:45 +08:00
|
|
|
TestClass03()
|
|
|
|
: c(2) {
|
|
|
|
}
|
|
|
|
TestClass03(const TestClass02& other)
|
|
|
|
: c(other.b) {
|
|
|
|
}
|
|
|
|
TestClass03(const TestClass01& other)
|
|
|
|
: c(other.a) {
|
|
|
|
}
|
|
|
|
TestClass03(const TestClass00& other)
|
|
|
|
: c(other.Thing()) {
|
|
|
|
}
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
int c;
|
|
|
|
};
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
|
2017-09-17 02:18:45 +08:00
|
|
|
sol::simple_usertype<TestClass00> s_TestUsertype00(lua,
|
2016-09-18 10:15:46 +08:00
|
|
|
sol::call_constructor, sol::constructors<sol::types<>>(),
|
2017-09-17 02:18:45 +08:00
|
|
|
"Thing", &TestClass00::Thing);
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
lua.set_usertype("TestClass00", s_TestUsertype00);
|
|
|
|
|
2017-09-17 02:18:45 +08:00
|
|
|
sol::simple_usertype<TestClass01> s_TestUsertype01(lua,
|
2016-09-18 10:15:46 +08:00
|
|
|
sol::call_constructor, sol::constructors<sol::types<>, sol::types<const TestClass00&>>(),
|
|
|
|
sol::base_classes, sol::bases<TestClass00>(),
|
2017-09-17 02:18:45 +08:00
|
|
|
"a", &TestClass01::a);
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
lua.set_usertype("TestClass01", s_TestUsertype01);
|
|
|
|
|
2017-09-17 02:18:45 +08:00
|
|
|
sol::simple_usertype<TestClass02> s_TestUsertype02(lua,
|
2016-09-18 10:15:46 +08:00
|
|
|
sol::call_constructor, sol::constructors<sol::types<>, sol::types<const TestClass01&>, sol::types<const TestClass00&>>(),
|
|
|
|
sol::base_classes, sol::bases<TestClass01, TestClass00>(),
|
2017-09-17 02:18:45 +08:00
|
|
|
"b", &TestClass02::b);
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
lua.set_usertype("TestClass02", s_TestUsertype02);
|
|
|
|
|
2017-09-17 02:18:45 +08:00
|
|
|
sol::simple_usertype<TestClass03> s_TestUsertype03(lua,
|
2016-09-18 10:15:46 +08:00
|
|
|
sol::call_constructor, sol::constructors<sol::types<>, sol::types<const TestClass02&>, sol::types<const TestClass01&>, sol::types<const TestClass00&>>(),
|
|
|
|
sol::base_classes, sol::bases<TestClass02, TestClass01, TestClass00>(),
|
2017-09-17 02:18:45 +08:00
|
|
|
"c", &TestClass03::c);
|
2016-09-18 10:15:46 +08:00
|
|
|
|
|
|
|
lua.set_usertype("TestClass03", s_TestUsertype03);
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script(R"(
|
2016-09-18 10:15:46 +08:00
|
|
|
tc0 = TestClass00()
|
|
|
|
)");
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script(R"(
|
2016-09-18 10:15:46 +08:00
|
|
|
tc2 = TestClass02(tc0)
|
|
|
|
)");
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script(R"(
|
2016-09-18 10:15:46 +08:00
|
|
|
tc1 = TestClass01()
|
|
|
|
)");
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script(R"(
|
2016-09-18 10:15:46 +08:00
|
|
|
tc3 = TestClass03(tc1)
|
|
|
|
)");
|
|
|
|
|
|
|
|
TestClass00& tc0 = lua["tc0"];
|
|
|
|
TestClass01& tc1 = lua["tc1"];
|
|
|
|
TestClass02& tc2 = lua["tc2"];
|
|
|
|
TestClass03& tc3 = lua["tc3"];
|
|
|
|
REQUIRE(tc1.a == 1);
|
|
|
|
REQUIRE(tc2.a == 1);
|
|
|
|
REQUIRE(tc2.b == 123);
|
|
|
|
REQUIRE(tc3.a == 1);
|
|
|
|
REQUIRE(tc3.b == 2);
|
|
|
|
REQUIRE(tc3.c == 1);
|
|
|
|
}
|