2
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; version 2 of the
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
#include "tut_stdafx.h"
23
#include "grt_test_utility.h"
24
#include "grtpp_module_lua.h"
25
#include "structs.test.h"
28
BEGIN_TEST_DATA_CLASS(grtpp_lua_test)
31
LuaModuleLoader *loader;
35
TEST_MODULE(grtpp_lua_test, "GRT: lua support");
41
grt.add_module_loader(loader= new LuaModuleLoader(&grt));
42
ensure("init lua loader", loader != NULL);
44
grt.load_metaclasses("data/structs.test.xml");
45
grt.end_loading_metaclasses();
46
ensure_equals("load structs", grt.get_metaclasses().size(), 6U);
48
ensure_equals("module count", grt.get_modules().size(), 0U);
50
grt.load_module("../../library/grt/unit-tests/lua_module.lua", true);
52
ensure_equals("module count after load", grt.get_modules().size(), 1U);
58
test_BookRef object(&grt);
59
LuaContext *ctx= loader->get_lua_context();
61
ctx->push_wrap_value(object);
63
ValueRef value= ctx->pop_value();
64
ensure("value", value.is_valid());
65
ensure("is object", test_BookRef::can_wrap(value));
66
// ensure_equals("refcount", value.refcount(), 1);
68
BaseListRef list(&grt);
71
ctx->push_wrap_value(list);
72
value= ctx->pop_value();
74
ensure("value", value.is_valid());
75
ensure("is list", BaseListRef::can_wrap(value));
76
ensure("list", value == list);
82
test_BookRef object(&grt);
84
LuaContext *ctx= loader->get_lua_context();
86
ctx->push_wrap_value(object);
91
ctx->pop_args("O", &obj);
92
ensure("pop value", obj.is_valid());
95
ensure_equals("refcount", obj.refcount(), 2);
97
ensure_equals("final refcount", object.refcount(), 1);
103
Module *test_module= grt.get_module("LuaModuleTest");
104
ensure("get module LuaModuleTest", test_module!=NULL);
106
BaseListRef args(&grt);
109
args.ginsert(IntegerRef(2));
110
args.ginsert(IntegerRef(3));
111
res= test_module->call_function("add2Numbers", args);
112
ensure_equals("add2Numbers", (int)IntegerRef::cast_from(res), 5);
114
IntegerListRef nlist(&grt);
116
args= BaseListRef(&grt);
119
nlist.ginsert(IntegerRef(2));
120
nlist.ginsert(IntegerRef(3));
121
nlist.ginsert(IntegerRef(4));
122
nlist.ginsert(IntegerRef(5));
124
res= test_module->call_function("sumList", args);
126
ensure_equals("sumList", (int)IntegerRef::cast_from(res), 14);
132
to avoid code duplication for checking of test_module functions like testArgInt, testArgDouble.
136
template <class GrtType, class StdType>
137
void test_num_arg(GRT &grt, Module *test_module, const char *func_name)
139
BaseListRef args(&grt);
141
GrtType v1((StdType)2.1);
142
GrtType v2((StdType)3.1);
146
check_module_function_return(test_module, func_name, args, GrtType(((StdType)v1)+((StdType)v2)));
151
to avoid code duplication for checking of test_module functions like testArgIntList, testArgDoubleList.
153
create arithmethic progression of given length & pass it to mentioned functions.
157
template <class GrtType, class StdType>
158
void test_num_arg_list(GRT &grt, Module *test_module, const char *func_name, size_t count)
160
BaseListRef args(&grt);
162
Ref<GrtType> n= (StdType)1.1;
163
Ref<GrtType> sum= (StdType)0;
165
ListRef<GrtType> vlist(&grt);
168
for (size_t i= 0; i<count; i++)
169
vlist.ginsert(Ref<GrtType>((sum= sum+(n= n+1), n)));
171
args.ginsert(Ref<GrtType>((sum= sum+(n= n+1), n)));
173
check_module_function_return(test_module, func_name, args, sum);
177
{ // test passing of all types
178
Module *test_module= grt.get_module("LuaModuleTest");
179
ensure("get module", test_module!=NULL);
181
test_num_arg<IntegerRef, int>(grt, test_module, "testArgInt");
182
test_num_arg<DoubleRef, double>(grt, test_module, "testArgDouble");
183
test_num_arg_list<grt::internal::Integer, int>(grt, test_module, "testArgIntList", 4);
184
test_num_arg_list<grt::internal::Double, double>(grt, test_module, "testArgDoubleList", 4);
188
const std::string S1("unit-");
189
const std::string S2("test");
191
BaseListRef args(&grt, AnyType);
192
args.ginsert(StringRef(S1));
193
args.ginsert(StringRef(S2));
194
check_module_function_return(test_module, "testArgString", args, StringRef(S1+S2));
200
{ // test passing of all types
201
Module *test_module= grt.get_module("LuaModuleTest");
203
ensure("LuaModuleTest is not null", test_module != 0);
206
const int INDEX(2); // lua uses 1-based index
207
const size_t LIST_COUNT(4);
208
const std::string s[LIST_COUNT]= {"_0", "_1", "_2", "_3"};
210
BaseListRef args(&grt);
211
StringListRef list= list_from_array<grt::internal::String>(grt, s, LIST_COUNT);
213
args.ginsert(IntegerRef(INDEX+1));
214
check_module_function_return(test_module, "testArgStringList", args, list.get(INDEX));
219
{ // test passing of all types
220
Module *test_module= grt.get_module("LuaModuleTest");
222
ensure("LuaModuleTest is not null", test_module != 0);
225
const char *MEMBER_NAME("name");
226
const char *MEMBER_VAL("author");
227
const char *S(" wrote some stuff");
228
const char *OBJ_PATH("test.Author");
230
BaseListRef args(&grt, AnyType);
231
ObjectRef obj(grt.create_object<grt::internal::Object>(OBJ_PATH));
233
obj.set_member(MEMBER_NAME, StringRef(MEMBER_VAL));
234
args.ginsert(StringRef(S));
235
check_module_function_return(test_module, "testArgObject", args, StringRef(std::string(MEMBER_VAL)+std::string(S)));
240
{ // test passing of all types
241
Module *test_module= grt.get_module("LuaModuleTest");
243
ensure("LuaModuleTest is not null", test_module != 0);
246
const char *OBJ_PATH("test.Author");
247
const char *MEMBER_NAME("name");
248
const size_t VAL_COUNT= 4;
249
const size_t VAL_INDEX= 2;
250
const std::string VA[VAL_COUNT]= {"author1", "author2", "author3", "author4"};
252
BaseListRef args(&grt, AnyType);
253
ObjectListRef list(&grt);
255
for (size_t n= 0; n<VAL_COUNT; n++)
257
ObjectRef obj(grt.create_object<grt::internal::Object>(OBJ_PATH));
258
obj.set_member(MEMBER_NAME, StringRef(VA[n]));
261
args.ginsert(StringRef(VA[VAL_INDEX]));
262
check_module_function_return(test_module, "testArgObjectList", args, StringRef(VA[VAL_INDEX]));
267
{ // test passing of all types
268
Module *test_module= grt.get_module("LuaModuleTest");
270
ensure("LuaModuleTest is not null", test_module != 0);
273
const size_t KEY_COUNT(4);
275
std::string k[KEY_COUNT]= {"id", "code", "name", "material"};
276
std::string v[KEY_COUNT]= {"1", "001", "wrench", "metal"};
278
BaseListRef args(&grt, AnyType);
279
DictRef dict= dict_from_array<StringRef>(grt, k, v, KEY_COUNT);
281
args.ginsert(StringRef(k[INDEX]));
282
check_module_function_return(test_module, "testArgDict", args, StringRef(v[INDEX]));
288
{ // test passing of all types
289
Module *test_module= grt.get_module("LuaModuleTest");
291
ensure("LuaModuleTest is not null", test_module != 0);
294
const size_t DICT_COUNT(4);
295
const size_t KEY_COUNT(4);
296
const size_t SEARCHED_DICT(1);
297
const size_t NAME_KEY(2);
298
std::string k[KEY_COUNT]= {"id","code","name","material"};
299
std::string v[DICT_COUNT][KEY_COUNT]= {
300
{"1","001","fork","metal"},
301
{"2","002","ball","rubber"},
302
{"3","003","table","wood"},
303
{"4","004","window","glass"}};
305
BaseListRef args(&grt, AnyType);
306
DictRef dict_arr[DICT_COUNT];
307
BaseListRef list(&grt);
308
for (size_t n= 0; n<DICT_COUNT; n++)
310
dict_arr[n]= dict_from_array<StringRef>(grt, k, v[n], KEY_COUNT);
311
list.ginsert(dict_arr[n]);
314
args.ginsert(StringRef(v[SEARCHED_DICT][NAME_KEY]));
315
check_module_function_return(test_module, "testArgDictList", args, DictRef(dict_arr[SEARCHED_DICT]));
323
void test_return_of_all_types(GRT& grt, Module* test_module, BaseListRef& args, bool grt_value_return)
327
args.ginsert(IntegerRef(grt_value_return?0:1));
331
int v[]= {grt_value_return?123456:12345};
332
check_module_function_return(test_module, "testRetAny", args, list_from_array<grt::internal::Integer>(grt, v, 1));
334
check_module_function_return(test_module, "testRetInt", args, IntegerRef(123));
335
check_module_function_return(test_module, "testRetDouble", args, DoubleRef(123.456));
336
check_module_function_return(test_module, "testRetString", args, StringRef("hello"));
338
check_module_function_return(test_module, "testRetObject", args, grt.create_object<grt::internal::Object>("test.Book"));
342
const size_t KEY_COUNT(3);
343
std::string k1[KEY_COUNT]= {"k1","k2","k3"};
344
std::string k0[KEY_COUNT]= {"key1","key2","key3"};
345
std::string *k(grt_value_return?k0:k1);
346
int v[KEY_COUNT]= {1,2,3};
348
DictRef dict= dict_from_array<IntegerRef>(grt, k, v, KEY_COUNT);
349
check_module_function_return(test_module, "testRetDict", args, dict);
354
BaseListRef list(&grt, AnyType);
355
list.ginsert(IntegerRef(grt_value_return?2:1));
356
list.ginsert(DoubleRef(grt_value_return?3.1:2.1));
357
list.ginsert(StringRef(grt_value_return?"four":"three"));
358
check_module_function_return(test_module, "testRetList", args, list);
363
const size_t LIST_COUNT(4);
364
int v1[LIST_COUNT]= {1,2,3,4};
365
int v0[LIST_COUNT]= {2,3,4,5};
366
int *v(grt_value_return?v0:v1);
368
IntegerListRef list= list_from_array<grt::internal::Integer>(grt, v, LIST_COUNT);
369
check_module_function_return(test_module, "testRetIntList", args, list);
374
const size_t LIST_COUNT(4);
375
double v1[]= {1.1, 2.2, 3.3, 4.4};
376
double v0[]= {2.2, 3.3, 4.4, 5.5};
377
double *v(grt_value_return?v0:v1);
379
DoubleListRef list= list_from_array<grt::internal::Double>(grt, v, LIST_COUNT);
380
check_module_function_return(test_module, "testRetDoubleList", args, list);
385
const size_t LIST_COUNT(4);
386
std::string v1[]= {"one","two","three","four"};
387
std::string v0[]= {"two","three","four","five"};
388
std::string *v(grt_value_return?v0:v1);
390
StringListRef list= list_from_array<grt::internal::String>(grt, v, LIST_COUNT);
391
check_module_function_return(test_module, "testRetStringList", args, list);
396
const char *OBJ_PATH("test.Book");
397
const char *MEMBER_NAME("title");
398
const size_t VAL_COUNT(3);
399
const StringRef VA[VAL_COUNT]= {"Book1", "Book2", "Book3"};
401
ObjectListRef list(&grt);
402
for (size_t n= 0; n<VAL_COUNT; n++)
404
list.insert(grt.create_object<grt::internal::Object>(OBJ_PATH));
405
list.get(n).set_member(MEMBER_NAME, VA[n]);
407
check_module_function_return(test_module, "testRetObjectList", args, list);
412
const size_t DICT_COUNT(2);
413
const size_t KEY_COUNT(2);
414
std::string k1[KEY_COUNT]= {"k1","k2"};
415
std::string k0[KEY_COUNT]= {"key1","key2"};
416
std::string *k(grt_value_return?k0:k1);
417
int v[DICT_COUNT][KEY_COUNT]= {
421
DictRef dict_arr[DICT_COUNT];
422
BaseListRef list(&grt);
423
for (size_t n= 0; n<DICT_COUNT; n++)
425
dict_arr[n]= dict_from_array<IntegerRef>(grt, k, v[n], KEY_COUNT);
426
list.ginsert(dict_arr[n]);
428
check_module_function_return(test_module, "testRetDictList", args, list);
433
{ // test return of all types
434
Module *test_module= grt.get_module("LuaModuleTest");
435
ensure("get module", test_module!=NULL);
436
BaseListRef args(&grt, AnyType);
438
test_return_of_all_types(grt, test_module, args, false);
439
test_return_of_all_types(grt, test_module, args, true);
445
// test error catching
446
Module *test_module= grt.get_module("LuaModuleTest");
447
ensure("get module", test_module!=NULL);
449
BaseListRef args(&grt);
452
args.ginsert(IntegerRef(2));
453
args.ginsert(IntegerRef(3));
455
res= test_module->call_function("errorFunc", args);
456
ensure("lua module error handling", false);
457
} catch (grt::module_error&) {
458
// this is the correct exception to be raised
460
ensure("lua module error exception", false);
467
// some unit tests written in lua
468
Module *test_module= grt.get_module("LuaModuleTest");
469
ensure("LuaModuleTest is not null", test_module != 0);
470
BaseListRef args(&grt, AnyType);
472
ValueRef res= test_module->call_function("doLuaUnitTests", args);
474
ensure_equals("lua unit tests", IntegerRef::cast_from(res), 0);