forked from ChaiScript/ChaiScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiled_tests.cpp
More file actions
804 lines (584 loc) · 20 KB
/
compiled_tests.cpp
File metadata and controls
804 lines (584 loc) · 20 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
// All of these are necessary because of catch.hpp. It's OK, they'll be
// caught in other cpp files if chaiscript causes them
#include <chaiscript/utility/utility.hpp>
#ifdef CHAISCRIPT_MSVC
#pragma warning(push)
#pragma warning(disable : 4190 4640 28251 4702 6330)
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
#ifdef __llvm__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#pragma clang diagnostic ignored "-Wfloat-equal"
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
// lambda_tests
TEST_CASE("C++11 Lambdas Can Be Registered")
{
// We cannot deduce the type of a lambda expression, you must either wrap it
// in an std::function or provide the signature
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun([]()->std::string { return "hello"; } ), "f1");
// wrap
chai.add(chaiscript::fun(std::function<std::string ()>([] { return "world"; } )), "f2");
CHECK(chai.eval<std::string>("f1()") == "hello");
CHECK(chai.eval<std::string>("f2()") == "world");
}
// dynamic_object tests
TEST_CASE("Dynamic_Object attributes can be shared with C++")
{
chaiscript::ChaiScript chai;
chai("attr bob::z; def bob::bob() { this.z = 10 }; auto x = bob()");
chaiscript::dispatch::Dynamic_Object &mydo = chai.eval<chaiscript::dispatch::Dynamic_Object &>("x");
CHECK(mydo.get_type_name() == "bob");
CHECK(chaiscript::boxed_cast<int>(mydo.get_attr("z")) == 10);
chai("x.z = 15");
CHECK(chaiscript::boxed_cast<int>(mydo.get_attr("z")) == 15);
int &z = chaiscript::boxed_cast<int&>(mydo.get_attr("z"));
CHECK(z == 15);
z = 20;
CHECK(z == 20);
CHECK(chaiscript::boxed_cast<int>(chai("x.z")) == 20);
}
TEST_CASE("Function objects can be created from chaiscript functions")
{
chaiscript::ChaiScript chai;
chai.eval("def func() { print(\"Hello World\"); } ");
std::function<void ()> f = chai.eval<std::function<void ()> >("func");
f();
CHECK(chai.eval<std::function<std::string (int)> >("to_string")(6) == "6");
CHECK(chai.eval<std::function<std::string (const chaiscript::Boxed_Value &)> >("to_string")(chaiscript::var(6)) == "6");
}
TEST_CASE("ChaiScript can be created and destroyed on heap")
{
chaiscript::ChaiScript *chai = new chaiscript::ChaiScript();
delete chai;
}
///////// Arithmetic Conversions
// Tests to make sure that type conversions happen only when they should
void arithmetic_conversions_f1(int)
{
}
void arithmetic_conversions_f4(std::string)
{
}
void arithmetic_conversions_f2(int)
{
}
void arithmetic_conversions_f3(double)
{
}
void arithmetic_conversions_f_func_return(const std::function<unsigned int (unsigned long)> &f)
{
// test the ability to return an unsigned with auto conversion
f(4);
}
TEST_CASE("Test automatic arithmetic conversions")
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&arithmetic_conversions_f1), "f1");
chai.add(chaiscript::fun(&arithmetic_conversions_f2), "f2");
chai.add(chaiscript::fun(&arithmetic_conversions_f3), "f2");
chai.add(chaiscript::fun(&arithmetic_conversions_f1), "f3");
chai.add(chaiscript::fun(&arithmetic_conversions_f4), "f3");
chai.add(chaiscript::fun(&arithmetic_conversions_f_func_return), "func_return");
// no overloads
chai.eval("f1(0)");
chai.eval("f1(0l)");
chai.eval("f1(0ul)");
chai.eval("f1(0ll)");
chai.eval("f1(0ull)");
chai.eval("f1(0.0)");
chai.eval("f1(0.0f)");
chai.eval("f1(0.0l)");
// expected overloads
chai.eval("f2(1)");
chai.eval("f2(1.0)");
// 1 non-arithmetic overload
chai.eval("f2(1.0)");
// various options for returning with conversions from chaiscript
chai.eval("func_return(fun(x) { return 5u; })");
chai.eval("func_return(fun(x) { return 5; })");
chai.eval("func_return(fun(x) { return 5.0f; })");
CHECK_THROWS(chai.eval("f2(1.0l)"));
}
/////// Exception handling
TEST_CASE("Generic exception handling with C++")
{
chaiscript::ChaiScript chai;
try {
chai.eval("throw(runtime_error(\"error\"));");
REQUIRE(false);
} catch (const chaiscript::Boxed_Value &bv) {
const std::exception &e = chai.boxed_cast<const std::exception &>(bv);
CHECK(e.what() == std::string("error"));
}
}
TEST_CASE("Throw an int")
{
chaiscript::ChaiScript chai;
try {
chai.eval("throw(1)", chaiscript::exception_specification<int>());
REQUIRE(false);
} catch (int e) {
CHECK(e == 1);
}
}
TEST_CASE("Throw int or double")
{
chaiscript::ChaiScript chai;
try {
chai.eval("throw(1.0)", chaiscript::exception_specification<int, double>());
REQUIRE(false);
} catch (const double e) {
CHECK(e == 1.0);
}
}
TEST_CASE("Throw a runtime_error")
{
chaiscript::ChaiScript chai;
try {
chai.eval("throw(runtime_error(\"error\"))", chaiscript::exception_specification<int, double, float, const std::string &, const std::exception &>());
REQUIRE(false);
} catch (const double) {
REQUIRE(false);
} catch (int) {
REQUIRE(false);
} catch (float) {
REQUIRE(false);
} catch (const std::string &) {
REQUIRE(false);
} catch (const std::exception &) {
REQUIRE(true);
}
}
TEST_CASE("Throw unhandled type")
{
chaiscript::ChaiScript chai;
try {
chai.eval("throw(\"error\")", chaiscript::exception_specification<int, double, float, const std::exception &>());
REQUIRE(false);
} catch (double) {
REQUIRE(false);
} catch (int) {
REQUIRE(false);
} catch (float) {
REQUIRE(false);
} catch (const std::exception &) {
REQUIRE(false);
} catch (const chaiscript::Boxed_Value &) {
REQUIRE(true);
}
}
///////////// Tests to make sure no arity, dispatch or guard errors leak up past eval
int expected_eval_errors_test_one(const int &)
{
return 1;
}
TEST_CASE("No unexpected exceptions leak")
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&expected_eval_errors_test_one), "test_fun");
chai.eval("def guard_fun(i) : i.get_type_info().is_type_arithmetic() {} ");
//// Dot notation
// non-existent function
CHECK_THROWS_AS(chai.eval("\"test\".test_one()"), chaiscript::exception::eval_error);
// wrong parameter type
CHECK_THROWS_AS(chai.eval("\"test\".test_fun()"), chaiscript::exception::eval_error);
// wrong number of parameters
CHECK_THROWS_AS(chai.eval("\"test\".test_fun(1)"), chaiscript::exception::eval_error);
// guard failure
CHECK_THROWS_AS(chai.eval("\"test\".guard_fun()"), chaiscript::exception::eval_error);
// regular notation
// non-existent function
CHECK_THROWS_AS(chai.eval("test_one(\"test\")"), chaiscript::exception::eval_error);
// wrong parameter type
CHECK_THROWS_AS(chai.eval("test_fun(\"test\")"), chaiscript::exception::eval_error);
// wrong number of parameters
CHECK_THROWS_AS(chai.eval("test_fun(\"test\")"), chaiscript::exception::eval_error);
// guard failure
CHECK_THROWS_AS(chai.eval("guard_fun(\"test\")"), chaiscript::exception::eval_error);
// index operator
CHECK_THROWS_AS(chai.eval("var a = [1,2,3]; a[\"bob\"];"), chaiscript::exception::eval_error);
// unary operator
CHECK_THROWS_AS(chai.eval("++\"bob\""), chaiscript::exception::eval_error);
// binary operator
CHECK_THROWS_AS(chai.eval("\"bob\" + 1"), chaiscript::exception::eval_error);
}
//////// Tests to make sure that the order in which function dispatches occur is correct
#include <chaiscript/utility/utility.hpp>
int function_ordering_test_one(const int &)
{
return 1;
}
int function_ordering_test_two(int &)
{
return 2;
}
TEST_CASE("Function ordering")
{
chaiscript::ChaiScript chai;
chai.eval("def test_fun(x) { return 3; }");
chai.eval("def test_fun(x) : x == \"hi\" { return 4; }");
// chai.eval("def test_fun(x) { return 5; }");
chai.add(chaiscript::fun(&function_ordering_test_one), "test_fun");
chai.add(chaiscript::fun(&function_ordering_test_two), "test_fun");
CHECK(chai.eval<int>("test_fun(1)") == 1);
CHECK(chai.eval<int>("auto i = 1; test_fun(i)") == 2);
CHECK(chai.eval<int>("test_fun(\"bob\")") == 3);
CHECK(chai.eval<int>("test_fun(\"hi\")") == 4);
}
int functor_cast_test_call(const std::function<int (int)> &f, int val)
{
return f(val);
}
TEST_CASE("Functor cast")
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&functor_cast_test_call), "test_call");
chai.eval("def func(i) { return i * 6; };");
int d = chai.eval<int>("test_call(func, 3)");
CHECK(d == 3 * 6);
}
int set_state_test_myfun()
{
return 2;
}
TEST_CASE("Set and restore chai state")
{
chaiscript::ChaiScript chai;
// save the initial state of globals and locals
chaiscript::ChaiScript::State firststate = chai.get_state();
std::map<std::string, chaiscript::Boxed_Value> locals = chai.get_locals();
// add some new globals and locals
chai.add(chaiscript::var(1), "i");
chai.add(chaiscript::fun(&set_state_test_myfun), "myfun");
CHECK(chai.eval<int>("myfun()") == 2);
CHECK(chai.eval<int>("i") == 1);
chai.set_state(firststate);
// set state should have reverted the state of the functions and dropped
// the 'myfun'
CHECK_THROWS_AS(chai.eval<int>("myfun()"), chaiscript::exception::eval_error &);
// set state should not affect the local variables
CHECK(chai.eval<int>("i") == 1);
// After resetting the locals we expect the 'i' to be gone
chai.set_locals(locals);
CHECK_THROWS_AS(chai.eval<int>("i"), chaiscript::exception::eval_error &);
}
//// Short comparisons
class Short_Comparison_Test {
public:
Short_Comparison_Test() : value_(5) {}
short get_value() { return value_; }
short value_;
};
TEST_CASE("Short comparison with int")
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::user_type<Short_Comparison_Test>(), "Test");
chai.add(chaiscript::constructor<Short_Comparison_Test()>(), "Test");
chai.add(chaiscript::fun(&Short_Comparison_Test::get_value), "get_value");
chai.eval("auto &t = Test();");
CHECK(chai.eval<bool>("t.get_value() == 5"));
}
///// Test lookup of type names
class Type_Name_MyClass
{
};
TEST_CASE("Test lookup of type names")
{
chaiscript::ChaiScript chai;
auto type = chaiscript::user_type<Type_Name_MyClass>();
chai.add(type, "MyClass");
CHECK(chai.get_type_name(type) == "MyClass");
CHECK(chai.get_type_name<Type_Name_MyClass>() == "MyClass");
}
/////// make sure many chaiscript objects can exist simultaneously
int simultaneous_chaiscript_do_something(int i)
{
return i + 2;
}
int simultaneous_chaiscript_do_something_else(int i)
{
return i * 2;
}
TEST_CASE("Simultaneous ChaiScript tests")
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&simultaneous_chaiscript_do_something), "do_something");
chai.add(chaiscript::var(1), "i");
for (int i = 0; i < 10; ++i)
{
chaiscript::ChaiScript chai2;
chai2.add(chaiscript::fun(&simultaneous_chaiscript_do_something_else), "do_something_else");
CHECK(chai.eval<int>("do_something(" + std::to_string(i) + ")") == i + 2);
CHECK(chai2.eval<int>("do_something_else(" + std::to_string(i) + ")") == i * 2);
CHECK_THROWS_AS(chai2.eval("do_something(1)"), chaiscript::exception::eval_error &);
CHECK_THROWS_AS(chai2.eval("i"), chaiscript::exception::eval_error &);
CHECK_NOTHROW(chai2.eval("do_something_else(1)"));
}
}
/////////////// test utility functions
class Utility_Test
{
public:
void function() {}
std::string function2() { return "Function2"; }
void function3() {}
std::string functionOverload(double) { return "double"; }
std::string functionOverload(int) { return "int"; }
};
TEST_CASE("Utility_Test utility class wrapper")
{
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
using namespace chaiscript;
/// \todo fix overload resolution for fun<>
chaiscript::utility::add_class<Utility_Test>(*m,
"Utility_Test",
{ constructor<Utility_Test ()>(),
constructor<Utility_Test (const Utility_Test &)>() },
{ {fun(&Utility_Test::function), "function"},
{fun(&Utility_Test::function2), "function2"},
{fun(&Utility_Test::function3), "function3"},
{fun(static_cast<std::string(Utility_Test::*)(double)>(&Utility_Test::functionOverload)), "functionOverload" },
{fun(static_cast<std::string(Utility_Test::*)(int)>(&Utility_Test::functionOverload)), "functionOverload" },
{fun(static_cast<Utility_Test & (Utility_Test::*)(const Utility_Test &)>(&Utility_Test::operator=)), "=" }
}
);
chaiscript::ChaiScript chai;
chai.add(m);
CHECK(chai.eval<std::string>("auto t = Utility_Test(); t.function2(); ") == "Function2");
CHECK(chai.eval<std::string>("auto t2 = Utility_Test(); t2.functionOverload(1); ") == "int");
CHECK(chai.eval<std::string>("auto t3 = Utility_Test(); t3.functionOverload(1.1); ") == "double");
chai.eval("t = Utility_Test();");
}
////// Object copy count test
class Object_Copy_Count_Test
{
public:
Object_Copy_Count_Test()
{
std::cout << "Object_Copy_Count_Test()\n";
++constructcount();
}
Object_Copy_Count_Test(const Object_Copy_Count_Test &)
{
std::cout << "Object_Copy_Count_Test(const Object_Copy_Count_Test &)\n";
++copycount();
}
Object_Copy_Count_Test(Object_Copy_Count_Test &&)
{
std::cout << "Object_Copy_Count_Test(Object_Copy_Count_Test &&)\n";
++movecount();
}
~Object_Copy_Count_Test()
{
std::cout << "~Object_Copy_Count_Test()\n";
++destructcount();
}
static int& constructcount()
{
static int c = 0;
return c;
}
static int& copycount()
{
static int c = 0;
return c;
}
static int& movecount()
{
static int c = 0;
return c;
}
static int& destructcount()
{
static int c = 0;
return c;
}
};
Object_Copy_Count_Test object_copy_count_create()
{
return Object_Copy_Count_Test();
}
TEST_CASE("Object copy counts")
{
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
m->add(chaiscript::user_type<Object_Copy_Count_Test>(), "Object_Copy_Count_Test");
m->add(chaiscript::constructor<Object_Copy_Count_Test()>(), "Object_Copy_Count_Test");
m->add(chaiscript::constructor<Object_Copy_Count_Test(const Object_Copy_Count_Test &)>(), "Object_Copy_Count_Test");
m->add(chaiscript::fun(&object_copy_count_create), "create");
chaiscript::ChaiScript chai;
chai.add(m);
chai.eval(" { auto i = create(); } ");
CHECK(Object_Copy_Count_Test::copycount() == 0);
CHECK(Object_Copy_Count_Test::constructcount() == 1);
#ifdef CHAISCRIPT_MSVC
CHECK(Object_Copy_Count_Test::destructcount() == 3);
CHECK(Object_Copy_Count_Test::movecount() == 2);
#else
CHECK(Object_Copy_Count_Test::destructcount() == 2);
CHECK(Object_Copy_Count_Test::movecount() == 1);
#endif
}
///////////////////// Object lifetime test 1
class Object_Lifetime_Test
{
public:
Object_Lifetime_Test()
{
++count();
}
Object_Lifetime_Test(const Object_Lifetime_Test &)
{
++count();
}
~Object_Lifetime_Test()
{
--count();
}
static int& count()
{
static int c = 0;
return c;
}
};
TEST_CASE("Object lifetime tests")
{
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
m->add(chaiscript::user_type<Object_Lifetime_Test>(), "Object_Lifetime_Test");
m->add(chaiscript::constructor<Object_Lifetime_Test()>(), "Object_Lifetime_Test");
m->add(chaiscript::constructor<Object_Lifetime_Test(const Object_Lifetime_Test &)>(), "Object_Lifetime_Test");
m->add(chaiscript::fun(&Object_Lifetime_Test::count), "count");
chaiscript::ChaiScript chai;
chai.add(m);
CHECK(chai.eval<int>("count()") == 0);
CHECK(chai.eval<int>("auto i = 0; { auto t = Object_Lifetime_Test(); } return i;") == 0);
CHECK(chai.eval<int>("i = 0; { auto t = Object_Lifetime_Test(); i = count(); } return i;") == 1);
CHECK(chai.eval<int>("i = 0; { auto t = Object_Lifetime_Test(); { auto t2 = Object_Lifetime_Test(); i = count(); } } return i;") == 2);
CHECK(chai.eval<int>("i = 0; { auto t = Object_Lifetime_Test(); { auto t2 = Object_Lifetime_Test(); } i = count(); } return i;") == 1);
CHECK(chai.eval<int>("i = 0; { auto t = Object_Lifetime_Test(); { auto t2 = Object_Lifetime_Test(); } } i = count(); return i;") == 0);
}
//// Object lifetime tests 2
template<typename T>
struct Object_Lifetime_Vector2
{
Object_Lifetime_Vector2() : x(0), y(0) {}
Object_Lifetime_Vector2(T px, T py) : x(px), y(py) {}
Object_Lifetime_Vector2(const Object_Lifetime_Vector2& cp) : x(cp.x), y(cp.y) {}
Object_Lifetime_Vector2& operator+=(const Object_Lifetime_Vector2& vec_r)
{
x += vec_r.x;
y += vec_r.y;
return *this;
}
Object_Lifetime_Vector2 operator+(const Object_Lifetime_Vector2& vec_r)
{
return Object_Lifetime_Vector2(*this += vec_r);
}
Object_Lifetime_Vector2 &operator=(const Object_Lifetime_Vector2& ver_r)
{
x = ver_r.x;
y = ver_r.y;
return *this;
}
T x;
T y;
};
Object_Lifetime_Vector2<float> Object_Lifetime_Vector2_GetValue()
{
return Object_Lifetime_Vector2<float>(10,15);
}
TEST_CASE("Object lifetime test 2")
{
chaiscript::ChaiScript _script;
//Registering stuff
_script.add(chaiscript::user_type<Object_Lifetime_Vector2<float>>(), "Object_Lifetime_Vector2f");
_script.add(chaiscript::constructor<Object_Lifetime_Vector2<float> ()>(), "Object_Lifetime_Vector2f");
_script.add(chaiscript::constructor<Object_Lifetime_Vector2<float> (float, float)>(), "Object_Lifetime_Vector2f");
_script.add(chaiscript::constructor<Object_Lifetime_Vector2<float> (const Object_Lifetime_Vector2<float>&)>(), "Object_Lifetime_Vector2f");
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::x), "x");
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::y), "y");
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::operator +), "+");
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::operator +=), "+=");
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::operator =), "=");
_script.add(chaiscript::fun(&Object_Lifetime_Vector2_GetValue), "getValue");
_script.eval(R"(
var test = 0.0
var test2 = Object_Lifetime_Vector2f(10,10)
test = getValue().x
print(test)
print(test2.x)
)");
CHECK(_script.eval<std::string>("to_string(test)") == "10");
CHECK(_script.eval<std::string>("to_string(test2.x)") == "10");
}
///// Non-polymorphic base class conversions
class Non_Poly_Base {};
class Non_Poly_Derived : public Non_Poly_Base {};
int myfunction(Non_Poly_Base *)
{
return 2;
}
TEST_CASE("Test Derived->Base with non-polymorphic classes")
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::base_class<Non_Poly_Base, Non_Poly_Derived>());
Non_Poly_Derived d;
chai.add(chaiscript::var(&d), "d");
chai.add(chaiscript::fun(&myfunction), "myfunction");
CHECK(chai.eval<int>("myfunction(d)") == 2);
}
struct TestCppVariableScope
{
void print()
{
std::cout << "Printed" << std::endl;
}
};
TEST_CASE("Variable Scope When Calling From C++")
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::user_type<TestCppVariableScope>(), "Test");
chai.add(chaiscript::constructor<TestCppVariableScope()>(), "Test");
chai.add(chaiscript::fun(&TestCppVariableScope::print), "print");
chai.eval(R"(var t := Test();
def func()
{
t.print();
}
)");
CHECK_THROWS(chai.eval("func()"));
chai.eval("dump_object(t)");
auto func = chai.eval<std::function<void()>>("func");
CHECK_THROWS(func());
}
TEST_CASE("Variable Scope When Calling From C++ 2")
{
chaiscript::ChaiScript chai;
chai.eval("var obj = 2;");
auto func = chai.eval<std::function<void()>>("fun(){ return obj; }");
CHECK_THROWS(func());
}
void ulonglong(unsigned long long i) {
std::cout << i << '\n';
}
void longlong(long long i) {
std::cout << i << '\n';
}
TEST_CASE("Test long long dispatch")
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&longlong), "longlong");
chai.add(chaiscript::fun(&ulonglong), "ulonglong");
chai.eval("longlong(15)");
chai.eval("ulonglong(15)");
}