1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// 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"
#include "common_classes.hpp"
#include <catch2/catch_all.hpp>
#include <iterator>
#include <vector>
#include <list>
#include <forward_list>
#include <map>
#include <deque>
#include <array>
#include <unordered_map>
#include <set>
#include <unordered_set>
TEST_CASE("containers/vector roundtrip", "make sure vectors can be round-tripped") {
sol::state lua;
std::vector<int> v { 1, 2, 3 };
lua.set_function("f", [&]() -> std::vector<int>& { return v; });
auto result1 = lua.safe_script("x = f()", sol::script_pass_on_error);
REQUIRE(result1.valid());
std::vector<int> x = lua["x"];
bool areequal = x == v;
REQUIRE(areequal);
}
TEST_CASE("containers/deque roundtrip", "make sure deques can be round-tripped") {
sol::state lua;
std::deque<int> v { 1, 2, 3 };
lua.set_function("f", [&]() -> std::deque<int>& { return v; });
auto result1 = lua.safe_script("x = f()", sol::script_pass_on_error);
REQUIRE(result1.valid());
std::deque<int> x = lua["x"];
bool areequal = x == v;
REQUIRE(areequal);
}
TEST_CASE("containers/array roundtrip", "make sure arrays can be round-tripped") {
sol::state lua;
std::array<int, 3> v { { 1, 2, 3 } };
lua.set_function("f", [&]() -> std::array<int, 3>& { return v; });
auto result1 = lua.safe_script("x = f()", sol::script_pass_on_error);
REQUIRE(result1.valid());
std::array<int, 3> x = lua["x"];
bool areequal = x == v;
REQUIRE(areequal);
}
TEST_CASE("containers/list roundtrip", "make sure lists can be round-tripped") {
sol::state lua;
std::list<int> v { 1, 2, 3 };
lua.set_function("f", [&]() -> std::list<int>& { return v; });
auto result1 = lua.safe_script("x = f()", sol::script_pass_on_error);
REQUIRE(result1.valid());
std::list<int> x = lua["x"];
bool areequal = x == v;
REQUIRE(areequal);
}
TEST_CASE("containers/forward_list roundtrip", "make sure forward_lists can be round-tripped") {
sol::state lua;
std::forward_list<int> v { 1, 2, 3 };
lua.set_function("f", [&]() -> std::forward_list<int>& { return v; });
auto result1 = lua.safe_script("x = f()", sol::script_pass_on_error);
REQUIRE(result1.valid());
std::forward_list<int> x = lua["x"];
bool areequal = x == v;
REQUIRE(areequal);
}
TEST_CASE("containers/map roundtrip", "make sure maps can be round-tripped") {
sol::state lua;
std::map<std::string, int> v { { "a", 1 }, { "b", 2 }, { "c", 3 } };
lua.set_function("f", [&]() -> std::map<std::string, int>& { return v; });
auto result1 = lua.safe_script("x = f()", sol::script_pass_on_error);
REQUIRE(result1.valid());
std::map<std::string, int> x = lua["x"];
bool areequal = x == v;
REQUIRE(areequal);
}
TEST_CASE("containers/unordered_map roundtrip", "make sure unordered_maps can be round-tripped") {
sol::state lua;
std::unordered_map<std::string, int> v { { "a", 1 }, { "b", 2 }, { "c", 3 } };
lua.set_function("f", [&]() -> std::unordered_map<std::string, int>& { return v; });
auto result1 = lua.safe_script("x = f()", sol::script_pass_on_error);
REQUIRE(result1.valid());
std::unordered_map<std::string, int> x = lua["x"];
bool areequal = x == v;
REQUIRE(areequal);
}
TEST_CASE("containers/unordered_set roundtrip", "make sure unordered_sets can be round-tripped") {
sol::state lua;
std::unordered_set<int> v { 1, 2, 3 };
lua.set_function("f", [&]() -> std::unordered_set<int>& { return v; });
auto result1 = lua.safe_script("x = f()", sol::script_pass_on_error);
REQUIRE(result1.valid());
std::unordered_set<int> x = lua["x"];
bool areequal = x == v;
REQUIRE(areequal);
}
TEST_CASE("containers/set roundtrip", "make sure sets can be round-tripped") {
sol::state lua;
std::set<int> v { 1, 2, 3 };
lua.set_function("f", [&]() -> std::set<int>& { return v; });
auto result1 = lua.safe_script("x = f()", sol::script_pass_on_error);
REQUIRE(result1.valid());
std::set<int> x = lua["x"];
bool areequal = x == v;
REQUIRE(areequal);
}
TEST_CASE("containers/ipairs test", "ensure that abstractions roundtrip properly") {
struct thing {
int x = 20;
};
thing t {};
sol::state lua;
lua.open_libraries();
lua.set_function("f", [&t]() { return std::vector<thing*>(5, &t); });
auto result1 = lua.safe_script(R"(
c = f()
)",
sol::script_pass_on_error);
REQUIRE(result1.valid());
auto result2 = lua.safe_script(R"(
check = {}
local i = 1
while c[i] do
check[i] = c[i]
i = i + 1
end
)",
sol::script_pass_on_error);
REQUIRE(result2.valid());
sol::table c = lua["check"];
for (std::size_t i = 1; i < 6; ++i) {
thing& ct = c[i];
REQUIRE(&t == &ct);
REQUIRE(ct.x == 20);
}
}
|