2019-03-18 19:41:51 +08:00
|
|
|
// sol3
|
|
|
|
|
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
// Copyright (c) 2013-2021 Rapptz, ThePhD and contributors
|
2019-03-18 19:41:51 +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.
|
|
|
|
|
|
|
|
#include "sol_test.hpp"
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
#include <catch2/catch.hpp>
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
#include <vector>
|
2019-03-23 04:28:17 +08:00
|
|
|
#include <map>
|
2019-03-18 19:41:51 +08:00
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string_view>
|
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
struct int_entry {
|
|
|
|
int value;
|
|
|
|
|
|
|
|
int_entry() : value(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
int_entry(int v) : value(v) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const int_entry& e) const {
|
|
|
|
return value == e.value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::mutex lua_value_construct_require_mutex;
|
|
|
|
|
|
|
|
void lua_value_construct_race() {
|
|
|
|
sol::state lua;
|
|
|
|
try {
|
|
|
|
lua.open_libraries();
|
|
|
|
lua["a"] = sol::lua_value(lua, 24);
|
|
|
|
int a = lua["a"];
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lg(lua_value_construct_require_mutex);
|
|
|
|
REQUIRE(a == 24);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const sol::error& e) {
|
|
|
|
std::lock_guard<std::mutex> lg(lua_value_construct_require_mutex);
|
|
|
|
INFO(e.what());
|
|
|
|
REQUIRE(false);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
std::lock_guard<std::mutex> lg(lua_value_construct_require_mutex);
|
|
|
|
REQUIRE(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("lua_value/nested", "make nested values can be put in lua_value properly") {
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_STD_VARIANT_I_)
|
2019-03-18 19:41:51 +08:00
|
|
|
using mixed_table_entry = std::variant<int, int_entry, std::string>;
|
|
|
|
using nested_entry = std::variant<int, int_entry, std::string, std::vector<mixed_table_entry>>;
|
|
|
|
|
|
|
|
const std::vector<std::variant<int, int_entry>> mixed_table_truth = { 1, int_entry(2), 3, int_entry(4), 5 };
|
2020-03-31 12:22:46 +08:00
|
|
|
const std::vector<nested_entry> mixed_nested_table_truth
|
|
|
|
= { 1, int_entry(2), 3, int_entry(4), std::vector<mixed_table_entry> { 5, 6, int_entry(7), "8" } };
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
|
2020-03-31 12:22:46 +08:00
|
|
|
sol::lua_value lv_mixed_table(lua, sol::array_value { 1, int_entry(2), 3, int_entry(4), 5 });
|
|
|
|
sol::lua_value lv_mixed_nested_table(lua, sol::array_value { 1, int_entry(2), 3, int_entry(4), sol::array_value { 5, 6, int_entry(7), "8" } });
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
REQUIRE(lv_mixed_table.is<sol::table>());
|
|
|
|
REQUIRE(lv_mixed_nested_table.is<sol::table>());
|
|
|
|
|
|
|
|
std::vector<std::variant<int, int_entry>> mixed_table_value_lv = lv_mixed_table.as<std::vector<std::variant<int, int_entry>>>();
|
|
|
|
std::vector<nested_entry> mixed_nested_table_value_lv = lv_mixed_nested_table.as<std::vector<nested_entry>>();
|
|
|
|
|
|
|
|
REQUIRE(mixed_table_truth == mixed_table_value_lv);
|
|
|
|
REQUIRE(mixed_nested_table_truth == mixed_nested_table_value_lv);
|
|
|
|
|
|
|
|
SECTION("type check (object)") {
|
|
|
|
sol::object obj_mixed_table(lv_mixed_table.value());
|
|
|
|
sol::object obj_mixed_nested_table(lv_mixed_nested_table.value());
|
2019-03-23 04:28:17 +08:00
|
|
|
|
2019-03-18 19:41:51 +08:00
|
|
|
REQUIRE(obj_mixed_table.is<sol::table>());
|
|
|
|
REQUIRE(obj_mixed_nested_table.is<sol::table>());
|
2019-03-23 04:28:17 +08:00
|
|
|
|
2019-03-18 19:41:51 +08:00
|
|
|
std::vector<std::variant<int, int_entry>> mixed_table_value = obj_mixed_table.as<std::vector<std::variant<int, int_entry>>>();
|
|
|
|
std::vector<nested_entry> mixed_nested_table_value = obj_mixed_nested_table.as<std::vector<nested_entry>>();
|
2019-03-23 04:28:17 +08:00
|
|
|
|
2019-03-18 19:41:51 +08:00
|
|
|
REQUIRE(mixed_table_truth == mixed_table_value);
|
|
|
|
REQUIRE(mixed_nested_table_truth == mixed_nested_table_value);
|
|
|
|
}
|
|
|
|
SECTION("pushing/popping") {
|
|
|
|
lua["obj_mixed_table"] = lv_mixed_table;
|
|
|
|
lua["obj_mixed_nested_table"] = lv_mixed_nested_table;
|
|
|
|
|
|
|
|
sol::lua_value obj_mixed_table = lua["obj_mixed_table"];
|
|
|
|
sol::lua_value obj_mixed_nested_table = lua["obj_mixed_nested_table"];
|
|
|
|
|
|
|
|
REQUIRE(obj_mixed_table.is<sol::table>());
|
|
|
|
REQUIRE(obj_mixed_nested_table.is<sol::table>());
|
|
|
|
|
|
|
|
std::vector<std::variant<int, int_entry>> mixed_table_value = obj_mixed_table.as<std::vector<std::variant<int, int_entry>>>();
|
|
|
|
std::vector<nested_entry> mixed_nested_table_value = obj_mixed_nested_table.as<std::vector<nested_entry>>();
|
2019-03-23 04:28:17 +08:00
|
|
|
|
|
|
|
REQUIRE(mixed_table_truth == mixed_table_value);
|
|
|
|
REQUIRE(mixed_nested_table_truth == mixed_nested_table_value);
|
|
|
|
}
|
|
|
|
SECTION("pushing/popping (object)") {
|
|
|
|
lua["obj_mixed_table"] = lv_mixed_table;
|
|
|
|
lua["obj_mixed_nested_table"] = lv_mixed_nested_table;
|
|
|
|
|
|
|
|
sol::object obj_mixed_table = lua["obj_mixed_table"];
|
|
|
|
sol::object obj_mixed_nested_table = lua["obj_mixed_nested_table"];
|
|
|
|
|
|
|
|
REQUIRE(obj_mixed_table.is<sol::table>());
|
|
|
|
REQUIRE(obj_mixed_nested_table.is<sol::table>());
|
|
|
|
|
|
|
|
std::vector<std::variant<int, int_entry>> mixed_table_value = obj_mixed_table.as<std::vector<std::variant<int, int_entry>>>();
|
|
|
|
std::vector<nested_entry> mixed_nested_table_value = obj_mixed_nested_table.as<std::vector<nested_entry>>();
|
|
|
|
|
|
|
|
REQUIRE(mixed_table_truth == mixed_table_value);
|
|
|
|
REQUIRE(mixed_nested_table_truth == mixed_nested_table_value);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
REQUIRE(true);
|
|
|
|
#endif // C++17
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("lua_value/nested key value", "make nested values (key value) can be put in lua_value properly") {
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_STD_VARIANT_I_)
|
2019-03-23 04:28:17 +08:00
|
|
|
using mixed_table_entry = std::variant<int, int_entry, std::string>;
|
|
|
|
using nested_entry = std::variant<int, int_entry, std::string, std::vector<mixed_table_entry>>;
|
|
|
|
|
|
|
|
const std::vector<std::variant<int, int_entry>> mixed_table_truth = { 1, int_entry(2), 3, int_entry(4), 5 };
|
2020-03-31 12:22:46 +08:00
|
|
|
const std::vector<nested_entry> mixed_nested_table_truth
|
|
|
|
= { 1, int_entry(2), 3, int_entry(4), std::vector<mixed_table_entry> { 5, 6, int_entry(7), "8" } };
|
2019-03-23 04:28:17 +08:00
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
|
2020-03-31 12:22:46 +08:00
|
|
|
sol::lua_value lv_mixed_table(lua, sol::array_value { 1, int_entry(2), 3, int_entry(4), 5 });
|
|
|
|
sol::lua_value lv_mixed_nested_table(lua, sol::array_value { 1, int_entry(2), 3, int_entry(4), sol::array_value { 5, 6, int_entry(7), "8" } });
|
2019-03-23 04:28:17 +08:00
|
|
|
|
|
|
|
REQUIRE(lv_mixed_table.is<sol::table>());
|
|
|
|
REQUIRE(lv_mixed_nested_table.is<sol::table>());
|
|
|
|
|
|
|
|
std::vector<std::variant<int, int_entry>> mixed_table_value_lv = lv_mixed_table.as<std::vector<std::variant<int, int_entry>>>();
|
|
|
|
std::vector<nested_entry> mixed_nested_table_value_lv = lv_mixed_nested_table.as<std::vector<nested_entry>>();
|
|
|
|
|
|
|
|
REQUIRE(mixed_table_truth == mixed_table_value_lv);
|
|
|
|
REQUIRE(mixed_nested_table_truth == mixed_nested_table_value_lv);
|
|
|
|
|
|
|
|
SECTION("type check (object)") {
|
|
|
|
sol::object obj_mixed_table(lv_mixed_table.value());
|
|
|
|
sol::object obj_mixed_nested_table(lv_mixed_nested_table.value());
|
|
|
|
|
|
|
|
REQUIRE(obj_mixed_table.is<sol::table>());
|
|
|
|
REQUIRE(obj_mixed_nested_table.is<sol::table>());
|
|
|
|
|
|
|
|
std::vector<std::variant<int, int_entry>> mixed_table_value = obj_mixed_table.as<std::vector<std::variant<int, int_entry>>>();
|
|
|
|
std::vector<nested_entry> mixed_nested_table_value = obj_mixed_nested_table.as<std::vector<nested_entry>>();
|
|
|
|
|
|
|
|
REQUIRE(mixed_table_truth == mixed_table_value);
|
|
|
|
REQUIRE(mixed_nested_table_truth == mixed_nested_table_value);
|
|
|
|
}
|
|
|
|
SECTION("pushing/popping") {
|
|
|
|
lua["obj_mixed_table"] = lv_mixed_table;
|
|
|
|
lua["obj_mixed_nested_table"] = lv_mixed_nested_table;
|
|
|
|
|
|
|
|
sol::lua_value obj_mixed_table = lua["obj_mixed_table"];
|
|
|
|
sol::lua_value obj_mixed_nested_table = lua["obj_mixed_nested_table"];
|
|
|
|
|
|
|
|
REQUIRE(obj_mixed_table.is<sol::table>());
|
|
|
|
REQUIRE(obj_mixed_nested_table.is<sol::table>());
|
|
|
|
|
|
|
|
std::vector<std::variant<int, int_entry>> mixed_table_value = obj_mixed_table.as<std::vector<std::variant<int, int_entry>>>();
|
|
|
|
std::vector<nested_entry> mixed_nested_table_value = obj_mixed_nested_table.as<std::vector<nested_entry>>();
|
|
|
|
|
2019-03-18 19:41:51 +08:00
|
|
|
REQUIRE(mixed_table_truth == mixed_table_value);
|
|
|
|
REQUIRE(mixed_nested_table_truth == mixed_nested_table_value);
|
|
|
|
}
|
|
|
|
SECTION("pushing/popping (object)") {
|
|
|
|
lua["obj_mixed_table"] = lv_mixed_table;
|
|
|
|
lua["obj_mixed_nested_table"] = lv_mixed_nested_table;
|
|
|
|
|
|
|
|
sol::object obj_mixed_table = lua["obj_mixed_table"];
|
|
|
|
sol::object obj_mixed_nested_table = lua["obj_mixed_nested_table"];
|
|
|
|
|
|
|
|
REQUIRE(obj_mixed_table.is<sol::table>());
|
|
|
|
REQUIRE(obj_mixed_nested_table.is<sol::table>());
|
|
|
|
|
|
|
|
std::vector<std::variant<int, int_entry>> mixed_table_value = obj_mixed_table.as<std::vector<std::variant<int, int_entry>>>();
|
|
|
|
std::vector<nested_entry> mixed_nested_table_value = obj_mixed_nested_table.as<std::vector<nested_entry>>();
|
|
|
|
|
|
|
|
REQUIRE(mixed_table_truth == mixed_table_value);
|
|
|
|
REQUIRE(mixed_nested_table_truth == mixed_nested_table_value);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
REQUIRE(true);
|
|
|
|
#endif // C++17
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("lua_value/basic types", "make sure we can stick values and nested values in a lua_value and retrieve them") {
|
|
|
|
sol::state lua;
|
|
|
|
|
|
|
|
const int_entry userdata_truth = int_entry(3);
|
|
|
|
const std::vector<int> int_table_truth = { 1, 2, 3, 4, 5 };
|
2020-03-31 12:22:46 +08:00
|
|
|
const std::map<int, int> int_map_truth = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
sol::lua_value lv_int(lua, 1);
|
|
|
|
sol::lua_value lv_double(lua, 2.0);
|
|
|
|
sol::lua_value lv_string(lua, "heyo");
|
|
|
|
sol::lua_value lv_lstring(lua, L"hiyo");
|
|
|
|
sol::lua_value lv_bool(lua, true);
|
|
|
|
sol::lua_value lv_nil(lua, sol::lua_nil);
|
|
|
|
sol::lua_value lv_userdata(lua, int_entry(3));
|
|
|
|
sol::lua_value lv_int_table(lua, { 1, 2, 3, 4, 5 });
|
2020-03-31 12:22:46 +08:00
|
|
|
sol::lua_value lv_int_map(lua, { { 1, 2 }, { 3, 4 }, { 5, 6 } });
|
2019-03-18 19:41:51 +08:00
|
|
|
REQUIRE(lv_int.is<int>());
|
|
|
|
REQUIRE(lv_double.is<double>());
|
|
|
|
REQUIRE(lv_string.is<std::string>());
|
|
|
|
REQUIRE(lv_lstring.is<std::string>());
|
|
|
|
REQUIRE(lv_bool.is<bool>());
|
|
|
|
REQUIRE(lv_nil.is<sol::lua_nil_t>());
|
|
|
|
REQUIRE(lv_userdata.is<sol::userdata>());
|
|
|
|
REQUIRE(lv_userdata.is<int_entry>());
|
|
|
|
REQUIRE(lv_int_table.is<sol::table>());
|
2019-03-23 04:28:17 +08:00
|
|
|
REQUIRE(lv_int_map.is<sol::table>());
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
REQUIRE(lv_int.as<int>() == 1);
|
|
|
|
REQUIRE(lv_double.as<double>() == 2.0);
|
|
|
|
REQUIRE(lv_string.as<std::string>() == "heyo");
|
|
|
|
REQUIRE(lv_lstring.as<std::string>() == "hiyo");
|
|
|
|
REQUIRE(lv_lstring.as<std::wstring>() == L"hiyo");
|
|
|
|
REQUIRE(lv_bool.as<bool>());
|
|
|
|
REQUIRE(lv_nil.as<sol::lua_nil_t>() == sol::lua_nil);
|
|
|
|
REQUIRE(lv_userdata.as<int_entry>() == userdata_truth);
|
|
|
|
|
|
|
|
std::vector<int> int_table_value_lv = lv_int_table.as<std::vector<int>>();
|
|
|
|
REQUIRE(int_table_truth == int_table_value_lv);
|
2019-03-23 04:28:17 +08:00
|
|
|
std::map<int, int> int_map_value_lv = lv_int_map.as<std::map<int, int>>();
|
|
|
|
REQUIRE(int_map_truth == int_map_value_lv);
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
SECTION("type check (object)") {
|
|
|
|
sol::object obj_int(lv_int.value());
|
|
|
|
sol::object obj_double(lv_double.value());
|
|
|
|
sol::object obj_string(lv_string.value());
|
|
|
|
sol::object obj_lstring(lv_lstring.value());
|
|
|
|
sol::object obj_bool(lv_bool.value());
|
|
|
|
sol::object obj_nil(lv_nil.value());
|
|
|
|
sol::object obj_userdata(lv_userdata.value());
|
|
|
|
sol::object obj_int_table(lv_int_table.value());
|
2019-03-23 04:28:17 +08:00
|
|
|
sol::object obj_int_map(lv_int_map.value());
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
REQUIRE(obj_int.is<int>());
|
|
|
|
REQUIRE(obj_double.is<double>());
|
|
|
|
REQUIRE(obj_string.is<std::string>());
|
|
|
|
REQUIRE(obj_lstring.is<std::string>());
|
|
|
|
REQUIRE(obj_bool.is<bool>());
|
|
|
|
REQUIRE(obj_nil.is<sol::lua_nil_t>());
|
|
|
|
REQUIRE(obj_userdata.is<sol::userdata>());
|
|
|
|
REQUIRE(obj_userdata.is<int_entry>());
|
|
|
|
REQUIRE(obj_int_table.is<sol::table>());
|
2019-03-23 04:28:17 +08:00
|
|
|
REQUIRE(obj_int_map.is<sol::table>());
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
REQUIRE(obj_int.as<int>() == 1);
|
|
|
|
REQUIRE(obj_double.as<double>() == 2.0);
|
|
|
|
REQUIRE(obj_string.as<std::string>() == "heyo");
|
|
|
|
REQUIRE(obj_lstring.as<std::string>() == "hiyo");
|
|
|
|
REQUIRE(obj_lstring.as<std::wstring>() == L"hiyo");
|
|
|
|
REQUIRE(obj_bool.as<bool>());
|
|
|
|
REQUIRE(obj_userdata.as<int_entry>() == userdata_truth);
|
|
|
|
REQUIRE(obj_nil.as<sol::lua_nil_t>() == sol::lua_nil);
|
|
|
|
|
|
|
|
std::vector<int> int_table_value = obj_int_table.as<std::vector<int>>();
|
|
|
|
REQUIRE(int_table_truth == int_table_value);
|
2019-03-23 04:28:17 +08:00
|
|
|
std::map<int, int> int_map_value = obj_int_map.as<std::map<int, int>>();
|
|
|
|
REQUIRE(int_map_truth == int_map_value);
|
2019-03-18 19:41:51 +08:00
|
|
|
}
|
|
|
|
SECTION("push/popping") {
|
|
|
|
lua["obj_int"] = lv_int;
|
|
|
|
lua["obj_double"] = lv_double;
|
|
|
|
lua["obj_string"] = lv_string;
|
|
|
|
lua["obj_lstring"] = lv_lstring;
|
|
|
|
lua["obj_bool"] = lv_bool;
|
|
|
|
lua["obj_nil"] = lv_nil;
|
|
|
|
lua["obj_userdata"] = lv_userdata;
|
|
|
|
lua["obj_int_table"] = lv_int_table;
|
2019-03-23 04:28:17 +08:00
|
|
|
lua["obj_int_map"] = lv_int_map;
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
// these all actually invoke the constructor
|
|
|
|
// so do one .get<> explicitly to ensure it's
|
|
|
|
// working correctl for a few cases...
|
|
|
|
// but it's nice to make sure it's all there now
|
|
|
|
sol::lua_value obj_int = lua["obj_int"].get<sol::lua_value>();
|
|
|
|
sol::lua_value obj_double = lua["obj_double"].get<sol::lua_value>();
|
|
|
|
sol::lua_value obj_string = lua["obj_string"].get<sol::lua_value>();
|
|
|
|
sol::lua_value obj_lstring = lua["obj_lstring"].get<sol::lua_value>();
|
|
|
|
sol::lua_value obj_bool = lua["obj_bool"].get<sol::lua_value>();
|
|
|
|
sol::lua_value obj_nil = lua["obj_nil"];
|
|
|
|
sol::lua_value obj_userdata = lua["obj_userdata"];
|
|
|
|
sol::lua_value obj_int_table = lua["obj_int_table"];
|
2019-03-23 04:28:17 +08:00
|
|
|
sol::lua_value obj_int_map = lua["obj_int_map"];
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
REQUIRE(obj_int.is<int>());
|
|
|
|
REQUIRE(obj_double.is<double>());
|
|
|
|
REQUIRE(obj_string.is<std::string>());
|
|
|
|
REQUIRE(obj_lstring.is<std::string>());
|
|
|
|
REQUIRE(obj_bool.is<bool>());
|
|
|
|
REQUIRE(obj_nil.is<sol::lua_nil_t>());
|
|
|
|
REQUIRE(obj_int_table.is<sol::table>());
|
2019-03-23 04:28:17 +08:00
|
|
|
REQUIRE(obj_int_map.is<sol::table>());
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
REQUIRE(obj_int.as<int>() == 1);
|
|
|
|
REQUIRE(obj_double.as<double>() == 2.0);
|
|
|
|
REQUIRE(obj_string.as<std::string>() == "heyo");
|
|
|
|
REQUIRE(obj_lstring.as<std::string>() == "hiyo");
|
|
|
|
REQUIRE(obj_lstring.as<std::wstring>() == L"hiyo");
|
|
|
|
REQUIRE(obj_bool.as<bool>());
|
|
|
|
REQUIRE(obj_nil.as<sol::lua_nil_t>() == sol::lua_nil);
|
|
|
|
|
|
|
|
std::vector<int> int_table_value = obj_int_table.as<std::vector<int>>();
|
|
|
|
REQUIRE(int_table_truth == int_table_value);
|
2019-03-23 04:28:17 +08:00
|
|
|
std::map<int, int> int_map_value = obj_int_map.as<std::map<int, int>>();
|
|
|
|
REQUIRE(int_map_truth == int_map_value);
|
2019-03-18 19:41:51 +08:00
|
|
|
}
|
|
|
|
SECTION("push/popping (object)") {
|
|
|
|
lua["obj_int"] = lv_int;
|
|
|
|
lua["obj_double"] = lv_double;
|
|
|
|
lua["obj_string"] = lv_string;
|
|
|
|
lua["obj_lstring"] = lv_lstring;
|
|
|
|
lua["obj_bool"] = lv_bool;
|
|
|
|
lua["obj_nil"] = lv_nil;
|
|
|
|
lua["obj_userdata"] = lv_userdata;
|
|
|
|
lua["obj_int_table"] = lv_int_table;
|
2019-03-23 04:28:17 +08:00
|
|
|
lua["obj_int_map"] = lv_int_map;
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
sol::object obj_int = lua["obj_int"];
|
|
|
|
sol::object obj_double = lua["obj_double"];
|
|
|
|
sol::object obj_string = lua["obj_string"];
|
|
|
|
sol::object obj_lstring = lua["obj_lstring"];
|
|
|
|
sol::object obj_bool = lua["obj_bool"];
|
|
|
|
sol::object obj_nil = lua["obj_nil"];
|
|
|
|
sol::object obj_userdata = lua["obj_userdata"];
|
|
|
|
sol::object obj_int_table = lua["obj_int_table"];
|
2019-03-23 04:28:17 +08:00
|
|
|
sol::object obj_int_map = lua["obj_int_map"];
|
2019-03-18 19:41:51 +08:00
|
|
|
|
|
|
|
REQUIRE(obj_int.is<int>());
|
|
|
|
REQUIRE(obj_double.is<double>());
|
|
|
|
REQUIRE(obj_string.is<std::string>());
|
|
|
|
REQUIRE(obj_lstring.is<std::string>());
|
|
|
|
REQUIRE(obj_bool.is<bool>());
|
|
|
|
REQUIRE(obj_nil.is<sol::lua_nil_t>());
|
|
|
|
REQUIRE(obj_userdata.is<sol::userdata>());
|
|
|
|
REQUIRE(obj_userdata.is<int_entry>());
|
|
|
|
REQUIRE(obj_int_table.is<sol::table>());
|
|
|
|
|
|
|
|
REQUIRE(obj_int.as<int>() == 1);
|
|
|
|
REQUIRE(obj_double.as<double>() == 2.0);
|
|
|
|
REQUIRE(obj_string.as<std::string>() == "heyo");
|
|
|
|
REQUIRE(obj_lstring.as<std::string>() == "hiyo");
|
|
|
|
REQUIRE(obj_lstring.as<std::wstring>() == L"hiyo");
|
|
|
|
REQUIRE(obj_bool.as<bool>());
|
|
|
|
REQUIRE(obj_nil.as<sol::lua_nil_t>() == sol::lua_nil);
|
|
|
|
REQUIRE(obj_userdata.as<int_entry>() == userdata_truth);
|
|
|
|
|
|
|
|
std::vector<int> int_table_value = obj_int_table.as<std::vector<int>>();
|
|
|
|
REQUIRE(int_table_truth == int_table_value);
|
2019-03-23 04:28:17 +08:00
|
|
|
std::map<int, int> int_map_value = obj_int_map.as<std::map<int, int>>();
|
|
|
|
REQUIRE(int_map_truth == int_map_value);
|
2019-03-18 19:41:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("lua_value/threading", "test that thread_local in lua_value constructors do not race or clobber") {
|
|
|
|
REQUIRE_NOTHROW([]() {
|
|
|
|
std::thread thrds[24];
|
|
|
|
for (int i = 0; i < 24; i++) {
|
|
|
|
thrds[i] = std::thread(&lua_value_construct_race);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 24; i++) {
|
|
|
|
thrds[i].join();
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
}
|