forked from elelel/luacpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluacpp_test.cpp
More file actions
469 lines (439 loc) · 16 KB
/
luacpp_test.cpp
File metadata and controls
469 lines (439 loc) · 16 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <catch.hpp>
#include <luacpp/luacpp>
SCENARIO("Test state (only native calls)") {
GIVEN("New Lua state") {
lua::state s;
WHEN("Pushing a number") {
lua_Number initial = 0x12345678;
s.pushnumber(initial);
THEN("Number on stack should match the number pushed") {
REQUIRE(s.isnumber(-1));
auto actual = s.tonumber(-1);
REQUIRE(actual == initial);
WHEN("Pushing another number") {
lua_Number another_initial = 0x1ABCDEF9;
s.pushnumber(another_initial);
THEN("The numbers on stack should match") {
REQUIRE(s.isnumber(-1));
auto actual = s.tonumber(-1);
REQUIRE(actual == another_initial);
REQUIRE(s.isnumber(-2));
actual = s.tonumber(-2);
REQUIRE(actual == initial);
WHEN("Poping a number") {
s.pop(1);
THEN("Only the first number should remain") {
REQUIRE(s.isnumber(-1));
auto actual = s.tonumber(-1);
REQUIRE(actual == initial);
}
}
}
}
}
}
WHEN("Pushing a string") {
const char * initial = "Test string";
s.pushstring(initial);
THEN("String on stack should match the string pushed") {
REQUIRE(s.isstring(-1));
auto actual = s.tostring(-1);
REQUIRE(strcmp(actual, initial) == 0);
}
}
WHEN("Pushing a boolean") {
bool initial = true;
s.pushboolean(initial);
THEN("Boolean on stack should match the boolean pushed") {
REQUIRE(s.isboolean(-1));
auto actual = (bool)s.toboolean(-1);
REQUIRE(actual == initial);
}
}
}
}
SCENARIO("Test basic type entities") {
lua::state s;
GIVEN("A bool") {
WHEN("is pushed as literal") {
s.push<>(true);
THEN("The last item on stack should be boolean") {
REQUIRE(s.isboolean(-1));
}
}
WHEN("is pushed as named param") {
bool initial = true;
s.push<>(initial);
THEN("The last item on stack should be boolean") {
REQUIRE(s.isboolean(-1));
THEN("Read it, the booleans should match") {
auto actual_unsafe = s.at<decltype(initial)>(-1).get_unsafe();
REQUIRE(actual_unsafe == initial);
auto actual_safe = s.at<decltype(initial)>(-1).get();
REQUIRE(actual_safe == initial);
}
}
}
}
GIVEN("An int") {
WHEN("is pushed as literal") {
s.push<>(123);
THEN("The last item on stack should be number") {
REQUIRE(s.isnumber(-1));
}
}
WHEN("is pushed as named param") {
int initial = 0x12345678;
s.push<>(initial);
THEN("It should be on stack as number") {
REQUIRE(s.isnumber(-1));
THEN("Read it, the numbers should match") {
auto actual_unsafe = s.at<decltype(initial)>(-1).get_unsafe();
REQUIRE(actual_unsafe == initial);
auto actual_safe = s.at<decltype(initial)>(-1).get();
REQUIRE(actual_safe == initial);
}
}
}
}
GIVEN("A double") {
WHEN("is pushed as literal") {
s.push<>(123.45);
THEN("The last item on stack should be number") {
REQUIRE(s.isnumber(-1));
}
}
WHEN("Writing it to stack") {
double initial{1.0/3.0};
s.push<>(initial);
THEN("The last item on stack should be number") {
REQUIRE(s.isnumber(-1));
THEN("Read it, the numbers should match") {
auto actual_unsafe = s.at<decltype(initial)>(-1).get_unsafe();
REQUIRE(actual_unsafe == initial);
auto actual_safe = s.at<decltype(initial)>(-1).get();
REQUIRE(actual_safe == initial);
}
}
}
}
GIVEN("A char* literal") {
WHEN("Writing it to stack") {
s.push<>("Testing...");
THEN("It should be on stack as string") {
REQUIRE(s.isstring(-1));
THEN("Read it, the strings should match") {
auto actual_unsafe = s.at<decltype("Testing...")>(-1).get_unsafe();
REQUIRE(strcmp(actual_unsafe, "Testing...") == 0);
auto actual_safe = s.at<decltype("Testing...")>(-1).get();
REQUIRE(strcmp(actual_safe, "Testing...") == 0);
}
}
}
}
GIVEN("A char[]") {
char initial[] = "Testing...";
WHEN("Writing it to stack") {
s.push<>(initial);
THEN("It should be on stack as string") {
REQUIRE(s.isstring(-1));
THEN("Read it, the strings should match") {
auto actual_unsafe = s.at<decltype(initial)>(-1).get_unsafe();
REQUIRE(strcmp(actual_unsafe, initial) == 0);
auto actual_safe = s.at<decltype(initial)>(-1).get();
REQUIRE(strcmp(actual_safe, initial) == 0);
}
}
}
}
GIVEN("A const char[]") {
const char initial[] = "Testing...";
WHEN("Writing it to stack") {
s.push<>(initial);
THEN("It should be on stack as string") {
REQUIRE(s.isstring(-1));
THEN("Read it, the strings should match") {
auto actual_unsafe = s.at<decltype(initial)>(-1).get_unsafe();
REQUIRE(strcmp(actual_unsafe, initial) == 0);
auto actual_safe = s.at<decltype(initial)>(-1).get();
REQUIRE(strcmp(actual_safe, initial) == 0);
}
}
}
}
GIVEN("A char*") {
char initial_array[] = "Testing...";
char* initial = initial_array;
WHEN("Writing it to stack") {
s.push<>(initial);
THEN("It should be on stack as string") {
REQUIRE(s.isstring(-1));
THEN("Read it, the strings should match") {
auto actual_unsafe = s.at<decltype(initial)>(-1).get_unsafe();
REQUIRE(strcmp(actual_unsafe, initial) == 0);
auto actual_safe = s.at<decltype(initial)>(-1).get();
REQUIRE(strcmp(actual_safe, initial) == 0);
}
}
}
}
GIVEN("A const char*") {
const char initial_array[] = "Testing...";
const char* initial = initial_array;
WHEN("Writing it to stack") {
s.push<>(initial);
THEN("It should be on stack as string") {
REQUIRE(s.isstring(-1));
THEN("Read it, the strings should match") {
auto actual_unsafe = s.at<decltype(initial)>(-1).get_unsafe();
REQUIRE(strcmp(actual_unsafe, initial) == 0);
auto actual_safe = s.at<decltype(initial)>(-1).get();
REQUIRE(strcmp(actual_safe, initial) == 0);
}
}
}
}
GIVEN("An std::string") {
std::string initial{"Testing"};
WHEN("Writing it to stack") {
s.push<>(initial);
THEN("It should be on stack as string") {
REQUIRE(s.isstring(-1));
THEN("Read it, the strings should match") {
auto actual_unsafe = s.at<decltype(initial)>(-1).get_unsafe();
REQUIRE(actual_unsafe == initial);
auto actual_safe = s.at<decltype(initial)>(-1).get();
REQUIRE(actual_safe == initial);
}
}
}
}
}
SCENARIO("Vector test") {
GIVEN("Lua state") {
lua::state s;
WHEN("We have an empty new table") {
s.newtable();
THEN("Create vector abstraction from it") {
::lua::vector<int> v(s, -1);
THEN("Push back a few things") {
v.push_back(123);
REQUIRE(v.size() == 1);
v.push_back(456);
REQUIRE(v.size() == 2);
v.push_back(789);
REQUIRE(v.size() == 3);
THEN("Check the values") {
REQUIRE(v.at(0).get() == 123);
REQUIRE(v[1]() == 456);
REQUIRE(v.at(2)() == 789);
WHEN("The values stored match the pushed") {
THEN("Remove the last element") {
v.pop();
REQUIRE(v.size() == 2);
v.pop();
REQUIRE(v.size() == 1);
v.pop();
REQUIRE(v.size() == 0);
}
THEN("Modify the values") {
v[0].set(0xABC);
v[1] = 0xDEF;
REQUIRE(v[0].get() == 0xABC);
REQUIRE(v[0]() == 0xABC);
REQUIRE(v[1].get() == 0xDEF);
REQUIRE(v[1]() == 0xDEF);
}
}
}
}
}
}
}
}
// Create table structs
namespace bogus_namespace_to_test_macros {
LUACPP_STATIC_TABLE_BEGIN(inner_table);
LUACPP_TABLE_FIELD(name, std::string);
LUACPP_TABLE_FIELD(price, double);
LUACPP_STATIC_TABLE_END();
}
// This HAS to be invoked from a source root namespace
LUACPP_STATIC_TABLE_TYPE_POLICY(bogus_namespace_to_test_macros::inner_table)
namespace bogus_namespace_to_test_macros {
// Declare the struct called my_table
LUACPP_STATIC_TABLE_BEGIN(my_table);
LUACPP_TABLE_FIELD_STR_KEY(name, std::string, std::string);
// LUACPP_TABLE_FIELD is an alias to LUACPP_TABLE_FIELD_STR_KEY with const char* key
LUACPP_TABLE_FIELD(ticker, std::string);
LUACPP_TABLE_FIELD_STR_KEY(rating, const char*, const char*);
LUACPP_TABLE_FIELD_STR_KEY(price, std::string, double);
LUACPP_TABLE_FIELD_STR_KEY(exchange_code, std::string, std::string);
LUACPP_TABLE_FIELD(ticks, std::vector<double>);
LUACPP_TABLE_FIELD(inner_table, inner_table);
LUACPP_STATIC_TABLE_END();
}
// This HAS to be invoked from a source root namespace
LUACPP_STATIC_TABLE_TYPE_POLICY(bogus_namespace_to_test_macros::my_table)
SCENARIO("Table test") {
GIVEN("Lua state and custom table class") {
lua::state s;
WHEN("Creating a raw table") {
s.newtable();
THEN("It shoule be on stack as table") {
REQUIRE(s.istable(-1));
WHEN("Creating my_table struct and setting value") {
// Instantiate my_table
bogus_namespace_to_test_macros::my_table t(s, -1);
WHEN("Setting table's fields") {
const std::string initial_name = "Test stock exchange instrument";
const std::string initial_ticker = "TICKR";
const char* initial_rating = "B++";
double initial_price{1.2345};
const std::string initial_exchange_code = "NYSE";
// Test setters
t.name.set(initial_name);
t.ticker.set(initial_ticker);
t.rating.set(initial_rating);
t.price.set(initial_price);
// Test operator=() overloads
t.exchange_code = initial_exchange_code;
WHEN("Reading table's fields") {
// Test getters
std::string actual_name = t.name.get();
std::string actual_ticker = t.ticker.get();
const char* actual_rating = t.rating.get();
double actual_price = t.price.get();
// Test operator()() overlaods
std::string actual_exchange_code = t.exchange_code();
THEN("Try to get it") {
REQUIRE(actual_name == initial_name);
REQUIRE(actual_ticker == initial_ticker);
REQUIRE(strcmp(actual_rating, initial_rating) == 0);
REQUIRE(actual_price == initial_price);
REQUIRE(actual_exchange_code == initial_exchange_code);
}
}
THEN("Accessing another table within the table") {
auto initial_name = "That is a name in inner table";
t.inner_table().name = initial_name;
auto actual_name = t.inner_table().name();
REQUIRE(actual_name == initial_name);
auto initial_price{765.32};
t.inner_table().price = initial_price;
auto actual_price = t.inner_table().price();
REQUIRE(actual_price == initial_price);
}
THEN("Accessing vector field") {
t.ticks().push_back(1);
t.ticks().push_back(2);
t.ticks().push_back(3);
REQUIRE(t.ticks()[0].get() == 1);
}
}
}
}
}
}
}
// Raw C function: takes a string and a number, returns modified string an modified number
static int test_c_function(lua_State* l) {
lua::state s(l);
const int n_args = s.gettop();
if (n_args == 2) {
// First argument has to be string
auto str = s.at<std::string>(1).get();
// Second argument has to be number
auto num = s.at<int>(2).get();
s.pop(n_args);
std::string rslt1 = str + " - length " + std::to_string(str.size());
s.push<>(rslt1);
int rslt2 = num + str.size();
s.push<>(rslt2);
return 2;
} else {
throw std::runtime_error("Test C function expects 2 arguments, but received " + std::to_string(n_args));
}
}
namespace my_very_special_namespace {
// Same function at a higher level of abstraction
static std::tuple<std::string, int> test_cpp_function(::lua::state st,
::lua::entity<::lua::type_policy<std::string>> s,
::lua::entity<::lua::type_policy<int>> i) {
auto str = s();
auto num = i();
std::string rslt1 = str + " - length " + std::to_string(str.size());
int rslt2 = num + str.size();
return std::make_tuple(rslt1, rslt2);
}
}
// Declare test_cpp_function class in ::lua::function namespace
LUACPP_STATIC_FUNCTION4(test_cpp_function, // How the function will be called in Lua
::my_very_special_namespace::test_cpp_function, // What function in C++ should handle the call
std::string, int // Arguments that the handler will received wrapped in Lua entities
)
// For testing functions returning void
static void my_void_function(::lua::state st) {
std::cout << "Void function called\n";
}
LUACPP_STATIC_FUNCTION2(my_void_function, my_void_function)
SCENARIO("Functions test") {
GIVEN("Lua state") {
lua::state s;
typedef std::tuple<std::string, int> result_type;
WHEN("Register raw c function") {
s.register_lua("test_c_function", &test_c_function);
THEN("Call, get and check results") {
auto rslt = s.call<result_type>("test_c_function", std::string("Test"), 123);
auto& actual_str = std::get<0>(rslt);
auto& actual_num = std::get<1>(rslt);
REQUIRE(actual_str == std::string("Test - length 4"));
REQUIRE(actual_num == 127);
}
THEN("Call, apply lambda and check results") {
std::string actual_str;
int actual_num{0};
const int n_result = 2;
// Version with caller clean stack responsibility
auto callback = [&actual_str, &actual_num] (const lua::state& s) {
// First argument has to be string
auto actual_str = s.at<std::string>(1).get();
// Second argument has to be number
auto actual_num = s.at<int>(0).get();
s.pop(n_result);
return 0; // We've balanced the stack, so inform that there're 0 items are to be corrected
};
s.call_and_apply<>(callback, 2, "test_c_function", std::string("Test"), 123);
}
}
WHEN("Register cpp function") {
using namespace my_very_special_namespace;
::lua::function::test_cpp_function().register_in_lua(s, test_cpp_function);
THEN("Address for the function stored in Lua should match the real one") {
s.getglobal(::lua::function::test_cpp_function().desc_table_name());
bool is_desc_table_nil = s.isnil(-1);
REQUIRE(!is_desc_table_nil);
bool is_desc_table_table = s.istable(-1);
REQUIRE(is_desc_table_table);
s.push<>("test_cpp_function");
s.gettable(-2);
bool is_pfun_lightuserdata = s.islightuserdata(-1);
REQUIRE(is_pfun_lightuserdata);
auto fd = (::lua::function::test_cpp_function_function_descriptor*)s.at<void*>(-1)();
}
THEN("Call the cpp function") {
auto rslt = s.call<result_type>("test_cpp_function", std::string("Test"), 123);
auto& actual_str = std::get<0>(rslt);
auto& actual_num = std::get<1>(rslt);
REQUIRE(actual_str == std::string("Test - length 4"));
REQUIRE(actual_num == 127);
}
THEN("Unregister function") {
::lua::function::test_cpp_function().unregister_from_lua(s);
}
}
WHEN("Testing function returning void") {
::lua::function::my_void_function().register_in_lua(s, my_void_function);
}
}
}