2019-08-15 13:26:52 +08:00
|
|
|
// sol3
|
2017-12-20 17:58:32 +08:00
|
|
|
|
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2020-08-13 22:42:37 +08:00
|
|
|
// Copyright (c) 2013-2020 Rapptz, ThePhD and contributors
|
2017-12-20 17:58:32 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
#include "sol_test.hpp"
|
2016-08-24 09:42:27 +08:00
|
|
|
|
2017-11-10 06:41:46 +08:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
2016-03-31 04:52:51 +08:00
|
|
|
#include <iostream>
|
2016-06-20 07:02:40 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <numeric>
|
|
|
|
#include <vector>
|
2016-08-24 09:42:27 +08:00
|
|
|
|
2016-03-31 04:52:51 +08:00
|
|
|
std::string free_function() {
|
2016-08-23 01:36:27 +08:00
|
|
|
INFO("free_function()");
|
2016-06-07 03:46:53 +08:00
|
|
|
return "test";
|
2016-03-31 04:52:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct object {
|
2017-09-17 02:18:45 +08:00
|
|
|
std::string operator()() {
|
2016-08-23 01:36:27 +08:00
|
|
|
INFO("member_test()");
|
2016-06-07 03:46:53 +08:00
|
|
|
return "test";
|
|
|
|
}
|
2016-03-31 04:52:51 +08:00
|
|
|
};
|
|
|
|
|
2016-09-20 11:37:00 +08:00
|
|
|
TEST_CASE("tables/cleanup", "make sure tables leave the stack balanced") {
|
|
|
|
sol::state lua;
|
2019-03-18 19:41:51 +08:00
|
|
|
sol::stack_guard luasg(lua);
|
2016-09-20 11:37:00 +08:00
|
|
|
lua.open_libraries();
|
|
|
|
|
|
|
|
auto f = [] { return 5; };
|
|
|
|
for (int i = 0; i < 30; i++) {
|
|
|
|
std::string name = std::string("init") + std::to_string(i);
|
|
|
|
int top = lua_gettop(lua);
|
|
|
|
lua[name] = f;
|
|
|
|
int aftertop = lua_gettop(lua);
|
|
|
|
REQUIRE(aftertop == top);
|
|
|
|
int val = lua[name]();
|
|
|
|
REQUIRE(val == 5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
TEST_CASE("tables/nested cleanup", "make sure tables leave the stack balanced") {
|
2016-09-20 11:37:00 +08:00
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries();
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script("A={}");
|
2016-09-20 11:37:00 +08:00
|
|
|
auto f = [] { return 5; };
|
|
|
|
for (int i = 0; i < 30; i++) {
|
|
|
|
std::string name = std::string("init") + std::to_string(i);
|
|
|
|
int top = lua_gettop(lua);
|
|
|
|
auto A = lua["A"];
|
|
|
|
int beforetop = lua_gettop(lua);
|
|
|
|
REQUIRE(beforetop == top);
|
|
|
|
A[name] = f;
|
|
|
|
int aftertop = lua_gettop(lua);
|
|
|
|
REQUIRE(aftertop == top);
|
|
|
|
int val = A[name]();
|
|
|
|
REQUIRE(val == 5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 04:52:51 +08:00
|
|
|
TEST_CASE("tables/variables", "Check if tables and variables work as intended") {
|
2016-06-07 03:46:53 +08:00
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base, sol::lib::os);
|
|
|
|
lua.get<sol::table>("os").set("name", "windows");
|
2017-12-07 21:24:50 +08:00
|
|
|
{
|
|
|
|
auto result = lua.safe_script("assert(os.name == \"windows\")", sol::script_pass_on_error);
|
|
|
|
REQUIRE(result.valid());
|
|
|
|
}
|
2016-03-31 04:52:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("tables/create", "Check if creating a table is kosher") {
|
2016-06-07 03:46:53 +08:00
|
|
|
sol::state lua;
|
|
|
|
lua["testtable"] = sol::table::create(lua.lua_state(), 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));
|
2016-03-31 04:52:51 +08:00
|
|
|
}
|
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
TEST_CASE("tables/create local", "Check if creating a table is kosher") {
|
2016-06-07 03:46:53 +08:00
|
|
|
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));
|
2016-03-31 04:52:51 +08:00
|
|
|
}
|
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
TEST_CASE("tables/create local named", "Check if creating a table is kosher") {
|
2016-06-07 03:46:53 +08:00
|
|
|
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((testobj == testtable));
|
|
|
|
REQUIRE_FALSE((testobj != testtable));
|
|
|
|
REQUIRE((testtable["Woof"] == std::string("Bark")));
|
|
|
|
REQUIRE((testtable[1] == 2));
|
|
|
|
REQUIRE((testtable[3] == 4));
|
2016-03-31 04:52:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("tables/create-with-local", "Check if creating a table is kosher") {
|
2016-06-07 03:46:53 +08:00
|
|
|
sol::state lua;
|
|
|
|
lua["testtable"] = lua.create_table_with("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));
|
2016-03-31 04:52:51 +08:00
|
|
|
}
|
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
TEST_CASE("tables/function variables", "Check if tables and function calls work as intended") {
|
2016-06-07 03:46:53 +08:00
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base, sol::lib::os);
|
2019-08-15 13:26:52 +08:00
|
|
|
auto run_script = [](sol::state& lua) -> void { lua.safe_script("assert(os.fun() == \"test\")"); };
|
2016-06-07 03:46:53 +08:00
|
|
|
|
2019-08-15 13:26:52 +08:00
|
|
|
lua.get<sol::table>("os").set_function("fun", []() {
|
|
|
|
INFO("stateless lambda()");
|
|
|
|
return "test";
|
|
|
|
});
|
2016-06-07 03:46:53 +08:00
|
|
|
REQUIRE_NOTHROW(run_script(lua));
|
|
|
|
|
|
|
|
lua.get<sol::table>("os").set_function("fun", &free_function);
|
|
|
|
REQUIRE_NOTHROW(run_script(lua));
|
|
|
|
|
|
|
|
// l-value, canNOT optimise
|
|
|
|
// prefer value semantics unless wrapped with std::reference_wrapper
|
|
|
|
{
|
|
|
|
auto lval = object();
|
|
|
|
lua.get<sol::table>("os").set_function("fun", &object::operator(), lval);
|
|
|
|
}
|
|
|
|
REQUIRE_NOTHROW(run_script(lua));
|
|
|
|
|
|
|
|
auto reflval = object();
|
|
|
|
lua.get<sol::table>("os").set_function("fun", &object::operator(), std::ref(reflval));
|
|
|
|
REQUIRE_NOTHROW(run_script(lua));
|
|
|
|
|
|
|
|
// stateful lambda: non-convertible, cannot be optimised
|
|
|
|
int breakit = 50;
|
2019-08-15 13:26:52 +08:00
|
|
|
lua.get<sol::table>("os").set_function("fun", [&breakit]() {
|
|
|
|
INFO("stateful lambda() with breakit:" << breakit);
|
|
|
|
return "test";
|
|
|
|
});
|
2016-06-07 03:46:53 +08:00
|
|
|
REQUIRE_NOTHROW(run_script(lua));
|
|
|
|
|
|
|
|
// r-value, cannot optimise
|
|
|
|
lua.get<sol::table>("os").set_function("fun", &object::operator(), object());
|
|
|
|
REQUIRE_NOTHROW(run_script(lua));
|
|
|
|
|
|
|
|
// r-value, cannot optimise
|
|
|
|
auto rval = object();
|
|
|
|
lua.get<sol::table>("os").set_function("fun", &object::operator(), std::move(rval));
|
|
|
|
REQUIRE_NOTHROW(run_script(lua));
|
2016-03-31 04:52:51 +08:00
|
|
|
}
|
|
|
|
|
2016-06-20 07:02:40 +08:00
|
|
|
TEST_CASE("tables/add", "Basic test to make sure the 'add' feature works") {
|
|
|
|
static const int sz = 120;
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
sol::table t = lua.create_table(sz, 0);
|
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
std::vector<int> bigvec(sz);
|
2016-06-20 07:02:40 +08:00
|
|
|
std::iota(bigvec.begin(), bigvec.end(), 1);
|
2017-09-07 03:09:51 +08:00
|
|
|
|
2016-06-20 07:02:40 +08:00
|
|
|
for (std::size_t i = 0; i < bigvec.size(); ++i) {
|
|
|
|
t.add(bigvec[i]);
|
|
|
|
}
|
|
|
|
for (std::size_t i = 0; i < bigvec.size(); ++i) {
|
|
|
|
int val = t[i + 1];
|
|
|
|
REQUIRE(val == bigvec[i]);
|
|
|
|
}
|
|
|
|
}
|
2017-06-12 22:45:51 +08:00
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
TEST_CASE("tables/raw set and raw get", "ensure raw setting and getting works through metatables") {
|
|
|
|
sol::state lua;
|
|
|
|
sol::table t = lua.create_table();
|
2019-08-15 13:26:52 +08:00
|
|
|
t[sol::metatable_key] = lua.create_table_with(sol::meta_function::new_index,
|
|
|
|
[](lua_State* L) { return luaL_error(L, "nay"); },
|
|
|
|
sol::meta_function::index,
|
|
|
|
[](lua_State* L) { return luaL_error(L, "nay"); });
|
2017-09-07 03:09:51 +08:00
|
|
|
t.raw_set("a", 2.5);
|
|
|
|
double la = t.raw_get<double>("a");
|
|
|
|
REQUIRE(la == 2.5);
|
|
|
|
}
|
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
TEST_CASE("tables/boolean keys", "make sure boolean keys don't get caught up in `is_integral` specializations") {
|
2017-06-12 22:45:51 +08:00
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script(R"(
|
2017-06-12 22:45:51 +08:00
|
|
|
tbl = {}
|
|
|
|
tbl[true] = 10
|
|
|
|
tbl[1] = 20
|
|
|
|
|
|
|
|
print(tbl[true])
|
|
|
|
print(tbl[1])
|
|
|
|
)");
|
|
|
|
sol::table tbl = lua["tbl"];
|
|
|
|
int v1 = tbl[true];
|
|
|
|
int v2 = tbl[1];
|
|
|
|
REQUIRE(v1 == 10);
|
2017-06-12 22:49:38 +08:00
|
|
|
REQUIRE(v2 == 20);
|
|
|
|
|
|
|
|
tbl[true] = 30;
|
|
|
|
tbl[1] = 40;
|
|
|
|
v1 = tbl[true];
|
|
|
|
v2 = tbl[1];
|
|
|
|
REQUIRE(v1 == 30);
|
|
|
|
REQUIRE(v2 == 40);
|
2017-06-12 22:45:51 +08:00
|
|
|
}
|
2017-07-06 22:19:44 +08:00
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
TEST_CASE("tables/optional move", "ensure pushing a sol::optional<T> rvalue correctly moves the contained object into tables") {
|
2017-07-06 22:19:44 +08:00
|
|
|
sol::state sol_state;
|
2017-08-06 07:20:28 +08:00
|
|
|
struct move_only {
|
2017-07-06 22:19:44 +08:00
|
|
|
int secret_code;
|
2019-09-15 12:43:44 +08:00
|
|
|
move_only(int sc) : secret_code(sc) {
|
|
|
|
}
|
2019-08-15 13:26:52 +08:00
|
|
|
|
2017-07-06 22:19:44 +08:00
|
|
|
move_only(const move_only&) = delete;
|
|
|
|
move_only(move_only&&) = default;
|
2019-08-15 13:26:52 +08:00
|
|
|
move_only& operator=(const move_only&) = delete;
|
|
|
|
move_only& operator=(move_only&&) = default;
|
2017-07-06 22:19:44 +08:00
|
|
|
};
|
2019-08-15 13:26:52 +08:00
|
|
|
sol_state["requires_move"] = sol::optional<move_only>(move_only(0x4D));
|
2017-07-06 22:19:44 +08:00
|
|
|
REQUIRE(sol_state["requires_move"].get<move_only>().secret_code == 0x4D);
|
|
|
|
}
|
2019-09-15 12:43:44 +08:00
|
|
|
|
|
|
|
TEST_CASE("table/stack table size", "make sure size() works correctly on stack tables") {
|
|
|
|
sol::state lua;
|
|
|
|
sol::stack_guard luasg(lua);
|
|
|
|
sol::stack_table t(lua, sol::create);
|
|
|
|
t[1] = 42;
|
|
|
|
auto sz1 = t.size();
|
|
|
|
REQUIRE(sz1 == 1);
|
|
|
|
sol::stack::push(lua, "some string");
|
|
|
|
auto sz2 = t.size();
|
|
|
|
REQUIRE(sz2 == 1);
|
|
|
|
std::string s = sol::stack::pop<std::string>(lua);
|
|
|
|
REQUIRE(s == "some string");
|
|
|
|
sol::table t2 = sol::stack::pop<sol::table>(lua);
|
|
|
|
auto sz3 = t2.size();
|
|
|
|
REQUIRE(sz1 == sz3);
|
|
|
|
REQUIRE(sz1 == sz2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("table/proxy call", "test proxy calls put the variable in the right place") {
|
|
|
|
sol::state lua;
|
|
|
|
sol::stack_guard luasg(lua);
|
|
|
|
{
|
|
|
|
sol::stack_guard tsg(lua);
|
|
|
|
lua["t"] = std::initializer_list<std::pair<int, std::string_view>>{ { 1, "borf" }, { 2, "bjork" }, { 3, "waf" } };
|
|
|
|
}
|
|
|
|
{
|
|
|
|
sol::stack_guard fsg(lua);
|
|
|
|
lua["f"] = [](std::string bjork) { REQUIRE(bjork == std::string("borf")); };
|
|
|
|
}
|
|
|
|
auto prox = lua["t"][1];
|
|
|
|
lua["f"](prox);
|
|
|
|
}
|