forked from ThePhD/sol2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_variadics.cpp
More file actions
182 lines (159 loc) · 4.18 KB
/
test_variadics.cpp
File metadata and controls
182 lines (159 loc) · 4.18 KB
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
173
174
175
176
177
178
179
180
181
182
#define SOL_CHECK_ARGUMENTS
#include <catch.hpp>
#include <sol.hpp>
#include <deque>
#include <set>
#include <functional>
#include <string>
TEST_CASE("variadics/variadic_args", "Check to see we can receive multiple arguments through a variadic") {
struct structure {
int x;
bool b;
};
sol::state lua;
lua.open_libraries(sol::lib::base);
lua.set_function("v", [](sol::this_state, sol::variadic_args va) -> structure {
int r = 0;
for (auto v : va) {
int value = v;
r += value;
}
return{ r, r > 200 };
});
lua.script("x = v(25, 25)");
lua.script("x2 = v(25, 25, 100, 50, 250, 150)");
lua.script("x3 = v(1, 2, 3, 4, 5, 6)");
structure& lx = lua["x"];
structure& lx2 = lua["x2"];
structure& lx3 = lua["x3"];
REQUIRE(lx.x == 50);
REQUIRE(lx2.x == 600);
REQUIRE(lx3.x == 21);
REQUIRE_FALSE(lx.b);
REQUIRE(lx2.b);
REQUIRE_FALSE(lx3.b);
}
TEST_CASE("variadics/required with variadic_args", "Check if a certain number of arguments can still be required even when using variadic_args") {
sol::state lua;
lua.set_function("v",
[](sol::this_state, sol::variadic_args, int, int) {
}
);
REQUIRE_NOTHROW(lua.script("v(20, 25, 30)"));
REQUIRE_NOTHROW(lua.script("v(20, 25)"));
#ifndef SOL_LUAJIT
REQUIRE_THROWS(lua.script("v(20)"));
#endif // LuaJIT has problems with exceptions, as fucking usual
}
TEST_CASE("variadics/variadic_args get type", "Make sure we can inspect types proper from variadic_args") {
sol::state lua;
lua.set_function("f", [](sol::variadic_args va) {
sol::type types[] = {
sol::type::number,
sol::type::string,
sol::type::boolean
};
bool working = true;
auto b = va.begin();
for (std::size_t i = 0; i < va.size(); ++i, ++b) {
sol::type t1 = va.get_type(i);
sol::type t2 = b->get_type();
working &= types[i] == t1;
working &= types[i] == t2;
}
REQUIRE(working);
});
lua.script("f(1, 'bark', true)");
lua.script("f(2, 'wuf', false)");
}
TEST_CASE("variadics/variadic_results", "returning a variable amount of arguments from C++") {
SECTION("as_returns - containers") {
sol::state lua;
lua.set_function("f", []() {
std::set<std::string> results{ "arf", "bark", "woof" };
return sol::as_returns(std::move(results));
});
lua.set_function("g", []() {
static const std::deque<int> results{ 25, 82 };
return sol::as_returns(std::ref(results));
});
REQUIRE_NOTHROW([&]() {
lua.script(R"(
v1, v2, v3 = f()
v4, v5 = g()
)");
}());
std::string v1 = lua["v1"];
std::string v2 = lua["v2"];
std::string v3 = lua["v3"];
int v4 = lua["v4"];
int v5 = lua["v5"];
REQUIRE(v1 == "arf");
REQUIRE(v2 == "bark");
REQUIRE(v3 == "woof");
REQUIRE(v4 == 25);
REQUIRE(v5 == 82);
}
SECTION("variadic_results - variadic_args") {
sol::state lua;
lua.set_function("f", [](sol::variadic_args args) {
return sol::variadic_results(args.cbegin(), args.cend());
});
REQUIRE_NOTHROW([&]() {
lua.script(R"(
v1, v2, v3 = f(1, 'bark', true)
v4, v5 = f(25, 82)
)");
}());
int v1 = lua["v1"];
std::string v2 = lua["v2"];
bool v3 = lua["v3"];
int v4 = lua["v4"];
int v5 = lua["v5"];
REQUIRE(v1 == 1);
REQUIRE(v2 == "bark");
REQUIRE(v3);
REQUIRE(v4 == 25);
REQUIRE(v5 == 82);
}
SECTION("variadic_results") {
sol::state lua;
lua.set_function("f", [](sol::this_state ts, bool maybe) {
if (maybe) {
sol::variadic_results vr;
vr.push_back({ ts, sol::in_place, 1 });
vr.push_back({ ts, sol::in_place, 2 });
vr.insert(vr.cend(), { ts, sol::in_place, 3 });
return vr;
}
else {
sol::variadic_results vr;
vr.push_back({ ts, sol::in_place, "bark" });
vr.push_back({ ts, sol::in_place, "woof" });
vr.insert(vr.cend(), { ts, sol::in_place, "arf" });
vr.push_back({ ts, sol::in_place, "borf" });
return vr;
}
});
REQUIRE_NOTHROW([&]() {
lua.script(R"(
v1, v2, v3 = f(true)
v4, v5, v6, v7 = f(false)
)");
}());
int v1 = lua["v1"];
int v2 = lua["v2"];
int v3 = lua["v3"];
std::string v4 = lua["v4"];
std::string v5 = lua["v5"];
std::string v6 = lua["v6"];
std::string v7 = lua["v7"];
REQUIRE(v1 == 1);
REQUIRE(v2 == 2);
REQUIRE(v3 == 3);
REQUIRE(v4 == "bark");
REQUIRE(v5 == "woof");
REQUIRE(v6 == "arf");
REQUIRE(v7 == "borf");
}
}