mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Additional function creation tables and tests to keep them there.
This commit is contained in:
parent
c3bf2e04f8
commit
f43adf9a3d
@ -273,6 +273,16 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Name>
|
||||
table create_table(Name&& name, int narr = 0, int nrec = 0) {
|
||||
return global.create(std::forward<Name>(name), narr, nrec);
|
||||
}
|
||||
|
||||
template <typename Name, typename Key, typename Value, typename... Args>
|
||||
table create_table(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
|
||||
return global.create(std::forward<Name>(name), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
table create_table(int narr = 0, int nrec = 0) {
|
||||
return create_table(lua_state(), narr, nrec);
|
||||
}
|
||||
|
@ -271,6 +271,20 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
template <typename Name>
|
||||
table create(Name&& name, int narr = 0, int nrec = 0) {
|
||||
table x = create(lua_state(), narr, nrec);
|
||||
this->set(std::forward<Name>(name), x);
|
||||
return x;
|
||||
}
|
||||
|
||||
template <typename Name, typename Key, typename Value, typename... Args>
|
||||
table create(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
|
||||
table x = create(lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
||||
this->set(std::forward<Name>(name), x);
|
||||
return x;
|
||||
}
|
||||
|
||||
table create(int narr = 0, int nrec = 0) {
|
||||
return create(lua_state(), narr, nrec);
|
||||
}
|
||||
|
21
tests.cpp
21
tests.cpp
@ -516,6 +516,27 @@ TEST_CASE("tables/create", "Check if creating a table is kosher") {
|
||||
REQUIRE((testtable[3] == 4));
|
||||
}
|
||||
|
||||
TEST_CASE("tables/create-local", "Check if creating a table is kosher") {
|
||||
sol::state lua;
|
||||
lua["testtable"] = lua.create_table(0, 0, "Woof", "Bark", 1, 2, 3, 4);
|
||||
sol::object testobj = lua["testtable"];
|
||||
REQUIRE(testobj.is<sol::table>());
|
||||
sol::table testtable = testobj.as<sol::table>();
|
||||
REQUIRE((testtable["Woof"] == std::string("Bark")));
|
||||
REQUIRE((testtable[1] == 2));
|
||||
REQUIRE((testtable[3] == 4));
|
||||
}
|
||||
|
||||
TEST_CASE("tables/create-local-named", "Check if creating a table is kosher") {
|
||||
sol::state lua;
|
||||
sol::table testtable = lua.create_table("testtable", 0, 0, "Woof", "Bark", 1, 2, 3, 4);
|
||||
sol::object testobj = lua["testtable"];
|
||||
REQUIRE(testobj.is<sol::table>());
|
||||
REQUIRE((testtable["Woof"] == std::string("Bark")));
|
||||
REQUIRE((testtable[1] == 2));
|
||||
REQUIRE((testtable[3] == 4));
|
||||
}
|
||||
|
||||
TEST_CASE("tables/functions-variables", "Check if tables and function calls work as intended") {
|
||||
sol::state lua;
|
||||
lua.open_libraries(sol::lib::base, sol::lib::os);
|
||||
|
Loading…
x
Reference in New Issue
Block a user