Additional function creation tables and tests to keep them there.

This commit is contained in:
ThePhD 2016-03-15 07:32:59 -04:00
parent c3bf2e04f8
commit f43adf9a3d
3 changed files with 45 additions and 0 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);