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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
|
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "honeysuckle.h"
#include "colors.h"
/* minunit testing macros modified from those at
www.jera.com/techinfo/jtns/jtn002.html */
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define mu_run_test(name, test) do { \
lua_State *L = luaL_newstate(); \
luaL_openlibs(L); \
char *message = test(L); \
lua_close(L); \
tests_run++; \
if (message) { \
printf(RED " test '%s' failed: %s\n" RESET, name, message); \
tests_failed++; \
} \
} while (0)
#define TEST(name) static char* name(lua_State *L)
int tests_run = 0;
int tests_failed = 0;
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* tests for hs_type_to_string
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
TEST(hs_bool_to_string)
{
mu_assert("HS_BOOL does not result in 'boolean'!",
strcmp(hs_type_to_string(HS_BOOL), "boolean") == 0);
return 0;
}
TEST(hs_int_to_string)
{
mu_assert("HS_INT does not result in 'integer'!",
strcmp(hs_type_to_string(HS_INT), "integer") == 0);
return 0;
}
TEST(hs_num_to_string)
{
mu_assert("HS_NUM does not result in 'number'!",
strcmp(hs_type_to_string(HS_NUM), "number") == 0);
return 0;
}
TEST(hs_str_to_string)
{
mu_assert("HS_STR does not result in 'string'!",
strcmp(hs_type_to_string(HS_STR), "string") == 0);
return 0;
}
TEST(hs_tbl_to_string)
{
mu_assert("HS_TBL does not result in 'table'!",
strcmp(hs_type_to_string(HS_TBL), "table") == 0);
return 0;
}
TEST(hs_func_to_string)
{
mu_assert("HS_FUNC does not result in 'function'!",
strcmp(hs_type_to_string(HS_FUNC), "function") == 0);
return 0;
}
TEST(hs_cfunc_to_string)
{
mu_assert("HS_CFUNC does not result in 'C function'!",
strcmp(hs_type_to_string(HS_CFUNC), "C function") == 0);
return 0;
}
TEST(hs_user_to_string)
{
mu_assert("HS_USER does not result in 'userdata'!",
strcmp(hs_type_to_string(HS_USER), "userdata") == 0);
return 0;
}
TEST(hs_light_to_string)
{
mu_assert("HS_LIGHT does not result in 'light userdata'!",
strcmp(hs_type_to_string(HS_LIGHT), "light userdata") == 0);
return 0;
}
TEST(hs_nil_to_string)
{
mu_assert("HS_NIL does not result in 'nil'!",
strcmp(hs_type_to_string(HS_NIL), "nil") == 0);
return 0;
}
TEST(hs_any_to_string)
{
mu_assert("HS_ANY does not result in 'any'!",
strcmp(hs_type_to_string(HS_ANY), "any") == 0);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* hs_parse_args typechecking tests
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
// typechecking macros
#define PARSE_TYPECHECK_TEST(name, type, constant) \
int name ## _testfunc(lua_State *L) { \
type test; hs_parse_args(L, constant, &test, HS_END); \
return 0; \
} \
TEST(name)
#define CHECK_FAIL_BOOL(name) \
lua_pushcfunction(L, name ## _testfunc); lua_pushboolean(L, true); \
mu_assert("incorrectly succeeded in parsing boolean!", \
lua_pcall(L, 1, 0, 0) != 0);
#define CHECK_FAIL_INT(name) \
lua_pushcfunction(L, name ## _testfunc); lua_pushinteger(L, 5); \
mu_assert("incorrectly succeeded in parsing integer!", \
lua_pcall(L, 1, 0, 0) != 0);
#define CHECK_FAIL_NUM(name) \
lua_pushcfunction(L, name ## _testfunc); lua_pushnumber(L, 42.0f); \
mu_assert("incorrectly succeeded in parsing number!", \
lua_pcall(L, 1, 0, 0) != 0);
#define CHECK_FAIL_STRING(name) \
lua_pushcfunction(L, name ## _testfunc); lua_pushstring(L, "hello!"); \
mu_assert("incorrectly succeeded in parsing string!", \
lua_pcall(L, 1, 0, 0) != 0);
#define CHECK_FAIL_TABLE(name) \
lua_pushcfunction(L, name ## _testfunc); lua_getglobal(L, "debug"); \
mu_assert("incorrectly succeeded in parsing table!", \
lua_pcall(L, 1, 0, 0) != 0);
#define CHECK_FAIL_FUNC(name) \
lua_pushcfunction(L, name ## _testfunc); lua_getglobal(L, "test"); \
mu_assert("incorrectly succeeded in parsing function!", \
lua_pcall(L, 1, 0, 0) != 0);
#define CHECK_FAIL_CFUNC(name) \
lua_pushcfunction(L, name ## _testfunc); lua_pushvalue(L, -1); \
mu_assert("incorrectly succeeded in parsing C function!", \
lua_pcall(L, 1, 0, 0) != 0);
#define CHECK_FAIL_USER(name) \
lua_pushcfunction(L, name ## _testfunc); lua_newuserdata(L, sizeof(char)); \
mu_assert("incorrectly succeeded in parsing userdata!", \
lua_pcall(L, 1, 0, 0) != 0);
#define CHECK_FAIL_LIGHT(name) \
int test_data = 5; \
lua_pushcfunction(L, name ## _testfunc); lua_pushlightuserdata(L, &test_data); \
mu_assert("incorrectly succeeded in parsing light userdata!", \
lua_pcall(L, 1, 0, 0) != 0);
#define CHECK_FAIL_NIL(name) \
lua_pushcfunction(L, name ## _testfunc); lua_pushnil(L); \
mu_assert("incorrectly succeeded in parsing nil!", \
lua_pcall(L, 1, 0, 0) != 0);
PARSE_TYPECHECK_TEST(parse_bool_typecheck, bool, HS_BOOL)
{
lua_pushcfunction(L, parse_bool_typecheck_testfunc);
lua_pushboolean(L, true);
mu_assert("typecheck for bool failed!",
lua_pcall(L, 1, 0, 0) == 0);
CHECK_FAIL_INT(parse_bool_typecheck);
CHECK_FAIL_NUM(parse_bool_typecheck);
CHECK_FAIL_STRING(parse_bool_typecheck);
CHECK_FAIL_TABLE(parse_bool_typecheck);
CHECK_FAIL_FUNC(parse_bool_typecheck);
CHECK_FAIL_CFUNC(parse_bool_typecheck);
CHECK_FAIL_USER(parse_bool_typecheck);
CHECK_FAIL_LIGHT(parse_bool_typecheck);
CHECK_FAIL_NIL(parse_bool_typecheck);
return 0;
}
PARSE_TYPECHECK_TEST(parse_int_typecheck, int, HS_INT)
{
CHECK_FAIL_BOOL(parse_int_typecheck);
lua_pushcfunction(L, parse_int_typecheck_testfunc);
lua_pushinteger(L, 5);
mu_assert("typecheck for int failed!",
lua_pcall(L, 1, 0, 0) == 0);
CHECK_FAIL_NUM(parse_int_typecheck);
CHECK_FAIL_STRING(parse_int_typecheck);
CHECK_FAIL_TABLE(parse_int_typecheck);
CHECK_FAIL_FUNC(parse_int_typecheck);
CHECK_FAIL_CFUNC(parse_int_typecheck);
CHECK_FAIL_USER(parse_int_typecheck);
CHECK_FAIL_LIGHT(parse_int_typecheck);
CHECK_FAIL_NIL(parse_int_typecheck);
return 0;
}
PARSE_TYPECHECK_TEST(parse_num_typecheck, lua_Number, HS_NUM)
{
CHECK_FAIL_BOOL(parse_num_typecheck);
CHECK_FAIL_INT(parse_num_typecheck);
lua_pushcfunction(L, parse_int_typecheck_testfunc);
lua_pushnumber(L, 42.0f);
mu_assert("typecheck for number failed!",
lua_pcall(L, 1, 0, 0) == 0);
CHECK_FAIL_STRING(parse_num_typecheck);
CHECK_FAIL_TABLE(parse_num_typecheck);
CHECK_FAIL_FUNC(parse_num_typecheck);
CHECK_FAIL_CFUNC(parse_num_typecheck);
CHECK_FAIL_USER(parse_num_typecheck);
CHECK_FAIL_LIGHT(parse_num_typecheck);
CHECK_FAIL_NIL(parse_num_typecheck);
return 0;
}
PARSE_TYPECHECK_TEST(parse_string_typecheck, char *, HS_STR)
{
CHECK_FAIL_BOOL(parse_string_typecheck);
CHECK_FAIL_INT(parse_string_typecheck);
CHECK_FAIL_NUM(parse_string_typecheck);
lua_pushcfunction(L, parse_string_typecheck_testfunc);
lua_pushstring(L, "hello!");
mu_assert("typecheck for string failed!",
lua_pcall(L, 1, 0, 0) == 0);
CHECK_FAIL_TABLE(parse_string_typecheck);
CHECK_FAIL_FUNC(parse_string_typecheck);
CHECK_FAIL_CFUNC(parse_string_typecheck);
CHECK_FAIL_USER(parse_string_typecheck);
CHECK_FAIL_LIGHT(parse_string_typecheck);
CHECK_FAIL_NIL(parse_string_typecheck);
return 0;
}
PARSE_TYPECHECK_TEST(parse_table_typecheck, int, HS_TBL)
{
CHECK_FAIL_BOOL(parse_table_typecheck);
CHECK_FAIL_INT(parse_table_typecheck);
CHECK_FAIL_NUM(parse_table_typecheck);
CHECK_FAIL_STRING(parse_table_typecheck);
lua_pushcfunction(L, parse_table_typecheck_testfunc);
lua_getglobal(L, "debug");
mu_assert("typecheck for table failed!",
lua_pcall(L, 1, 0, 0) == 0);
CHECK_FAIL_FUNC(parse_table_typecheck);
CHECK_FAIL_CFUNC(parse_table_typecheck);
CHECK_FAIL_USER(parse_table_typecheck);
CHECK_FAIL_LIGHT(parse_table_typecheck);
CHECK_FAIL_NIL(parse_table_typecheck);
return 0;
}
PARSE_TYPECHECK_TEST(parse_func_typecheck, int, HS_FUNC)
{
CHECK_FAIL_BOOL(parse_func_typecheck);
CHECK_FAIL_INT(parse_func_typecheck);
CHECK_FAIL_NUM(parse_func_typecheck);
CHECK_FAIL_STRING(parse_func_typecheck);
CHECK_FAIL_TABLE(parse_func_typecheck);
lua_pushcfunction(L, parse_func_typecheck_testfunc);
lua_getglobal(L, "test");
mu_assert("typecheck for function failed!",
lua_pcall(L, 1, 0, 0) == 0);
CHECK_FAIL_CFUNC(parse_func_typecheck);
CHECK_FAIL_USER(parse_func_typecheck);
CHECK_FAIL_LIGHT(parse_func_typecheck);
CHECK_FAIL_NIL(parse_func_typecheck);
return 0;
}
PARSE_TYPECHECK_TEST(parse_cfunc_typecheck, lua_CFunction, HS_CFUNC)
{
CHECK_FAIL_BOOL(parse_cfunc_typecheck);
CHECK_FAIL_INT(parse_cfunc_typecheck);
CHECK_FAIL_NUM(parse_cfunc_typecheck);
CHECK_FAIL_STRING(parse_cfunc_typecheck);
CHECK_FAIL_TABLE(parse_cfunc_typecheck);
CHECK_FAIL_FUNC(parse_cfunc_typecheck);
lua_pushcfunction(L, parse_cfunc_typecheck_testfunc);
lua_pushvalue(L, -1);
mu_assert("typecheck for C function failed!",
lua_pcall(L, 1, 0, 0) == 0);
CHECK_FAIL_USER(parse_cfunc_typecheck);
CHECK_FAIL_LIGHT(parse_cfunc_typecheck);
CHECK_FAIL_NIL(parse_cfunc_typecheck);
return 0;
}
PARSE_TYPECHECK_TEST(parse_user_typecheck, void *, HS_USER)
{
CHECK_FAIL_BOOL(parse_user_typecheck);
CHECK_FAIL_INT(parse_user_typecheck);
CHECK_FAIL_NUM(parse_user_typecheck);
CHECK_FAIL_STRING(parse_user_typecheck);
CHECK_FAIL_TABLE(parse_user_typecheck);
CHECK_FAIL_FUNC(parse_user_typecheck);
CHECK_FAIL_CFUNC(parse_user_typecheck);
lua_pushcfunction(L, parse_user_typecheck_testfunc);
lua_newuserdata(L, sizeof(char));
mu_assert("typecheck for userdata failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_user_typecheck_testfunc);
int testdata = 5; lua_pushlightuserdata(L, &testdata);
mu_assert("typecheck for light userdata failed!",
lua_pcall(L, 1, 0, 0) == 0);
CHECK_FAIL_NIL(parse_user_typecheck);
return 0;
}
PARSE_TYPECHECK_TEST(parse_light_typecheck, void *, HS_LIGHT)
{
CHECK_FAIL_BOOL(parse_light_typecheck);
CHECK_FAIL_INT(parse_light_typecheck);
CHECK_FAIL_NUM(parse_light_typecheck);
CHECK_FAIL_STRING(parse_light_typecheck);
CHECK_FAIL_TABLE(parse_light_typecheck);
CHECK_FAIL_FUNC(parse_light_typecheck);
CHECK_FAIL_CFUNC(parse_light_typecheck);
CHECK_FAIL_USER(parse_light_typecheck);
lua_pushcfunction(L, parse_light_typecheck_testfunc);
int testdata = 5; lua_pushlightuserdata(L, &testdata);
mu_assert("typecheck for light userdata failed!",
lua_pcall(L, 1, 0, 0) == 0);
CHECK_FAIL_NIL(parse_light_typecheck);
return 0;
}
PARSE_TYPECHECK_TEST(parse_nil_typecheck, int, HS_NIL)
{
CHECK_FAIL_BOOL(parse_nil_typecheck);
CHECK_FAIL_INT(parse_nil_typecheck);
CHECK_FAIL_NUM(parse_nil_typecheck);
CHECK_FAIL_STRING(parse_nil_typecheck);
CHECK_FAIL_TABLE(parse_nil_typecheck);
CHECK_FAIL_FUNC(parse_nil_typecheck);
CHECK_FAIL_CFUNC(parse_nil_typecheck);
CHECK_FAIL_USER(parse_nil_typecheck);
CHECK_FAIL_LIGHT(parse_nil_typecheck);
lua_pushcfunction(L, parse_nil_typecheck_testfunc);
lua_pushnil(L);
mu_assert("typecheck for nil failed!",
lua_pcall(L, 1, 0, 0) == 0);
return 0;
}
PARSE_TYPECHECK_TEST(parse_any_typecheck, int, HS_ANY)
{
lua_pushcfunction(L, parse_bool_typecheck_testfunc);
lua_pushboolean(L, true);
mu_assert("typecheck for bool failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_int_typecheck_testfunc);
lua_pushinteger(L, 5);
mu_assert("typecheck for int failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_int_typecheck_testfunc);
lua_pushnumber(L, 42.0f);
mu_assert("typecheck for number failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_string_typecheck_testfunc);
lua_pushstring(L, "hello!");
mu_assert("typecheck for string failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_table_typecheck_testfunc);
lua_getglobal(L, "debug");
mu_assert("typecheck for table failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_func_typecheck_testfunc);
lua_getglobal(L, "test");
mu_assert("typecheck for function failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_cfunc_typecheck_testfunc);
lua_pushvalue(L, -1);
mu_assert("typecheck for C function failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_user_typecheck_testfunc);
lua_newuserdata(L, sizeof(char));
mu_assert("typecheck for userdata failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_user_typecheck_testfunc);
int testdata = 5; lua_pushlightuserdata(L, &testdata);
mu_assert("typecheck for light userdata failed!",
lua_pcall(L, 1, 0, 0) == 0);
lua_pushcfunction(L, parse_nil_typecheck_testfunc);
lua_pushnil(L);
mu_assert("typecheck for nil failed!",
lua_pcall(L, 1, 0, 0) == 0);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* hs_parse_args parsing tests
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
TEST(parse_bool)
{
lua_pushboolean(L, true);
bool b = false;
hs_parse_args(L, HS_BOOL, &b, HS_END);
mu_assert("failed to properly parse boolean!", b);
return 0;
}
TEST(parse_int)
{
lua_pushinteger(L, 420);
int i = 0;
hs_parse_args(L, HS_INT, &i, HS_END);
mu_assert("failed to properly parse integer!", i == 420);
return 0;
}
TEST(parse_num)
{
lua_pushnumber(L, 40.5f);
lua_Number n = 0;
hs_parse_args(L, HS_NUM, &n, HS_END);
mu_assert("failed to properly parse number!", n == 40.5f);
return 0;
}
TEST(parse_str)
{
lua_pushstring(L, "hello, world!");
char *s = "";
hs_parse_args(L, HS_STR, &s, HS_END);
mu_assert("failed to properly parse string!",
strcmp(s, "hello, world!") == 0);
return 0;
}
TEST(parse_tbl)
{
lua_getglobal(L, "debug");
int correct_index = lua_gettop(L);
int tbl_index;
hs_parse_args(L, HS_TBL, &tbl_index, HS_END);
mu_assert("failed to properly parse table!", tbl_index == correct_index);
return 0;
}
TEST(parse_func)
{
lua_getglobal(L, "type");
int correct_index = lua_gettop(L);
int index = 0;
hs_parse_args(L, HS_FUNC, &index, HS_END);
mu_assert("failed to properly parse function!", index == correct_index);
return 0;
}
int testfunc(lua_State *L) { return 0; }
TEST(parse_cfunc)
{
lua_pushcfunction(L, testfunc);
lua_CFunction f;
hs_parse_args(L, HS_CFUNC, &f, HS_END);
mu_assert("failed to properly parse C function!", f == testfunc);
return 0;
}
int should_fail(lua_State *L)
{
lua_CFunction f;
hs_parse_args(L, HS_CFUNC, &f, HS_END);
return 0;
}
TEST(fail_parse_noncfunc)
{
lua_pushcfunction(L, should_fail);
lua_getglobal(L, "type");
mu_assert("incorrectly parsed non-C function!",
lua_pcall(L, 1, 0, 0) != 0);
return 0;
}
TEST(parse_userdata)
{
void *userdata = lua_newuserdata(L, sizeof(char));
void *parsed = NULL;
hs_parse_args(L, HS_USER, &parsed, HS_END);
mu_assert("failed to properly parse userdata!",
parsed == userdata);
return 0;
}
TEST(parse_lightuserdata)
{
int five = 5;
lua_pushlightuserdata(L, &five);
void *data;
hs_parse_args(L, HS_LIGHT, &data, HS_END);
mu_assert("failed to properly parse light userdata!",
data == &five);
return 0;
}
TEST(parse_nil)
{
lua_pushnil(L);
int correct_index = lua_gettop(L);
int index = 0;
hs_parse_args(L, HS_NIL, &index, HS_END);
mu_assert("failed to properly parse nil!",
index == correct_index);
return 0;
}
TEST(parse_any)
{
lua_pushnil(L);
int correct_index = lua_gettop(L);
int index = 0;
hs_parse_args(L, HS_ANY, &index, HS_END);
mu_assert("failed to properly parse [any]!",
index == correct_index);
return 0;
}
TEST(parse_all)
{
lua_pushboolean(L, true);
lua_pushinteger(L, 420);
lua_pushnumber(L, 40.5f);
lua_pushstring(L, "hello, world!");
lua_getglobal(L, "debug");
int tbl_index = lua_gettop(L);
lua_getglobal(L, "type");
int func_index = lua_gettop(L);
lua_pushcfunction(L, testfunc);
void *userdata = lua_newuserdata(L, sizeof(char));
int five = 5;
lua_pushlightuserdata(L, &five);
lua_pushnil(L);
int nil_index = lua_gettop(L);
lua_pushnil(L);
int any_index = lua_gettop(L);
bool b;
int i;
float f;
char *str;
int i_tbl;
int i_func;
lua_CFunction fn;
void *user;
void *light;
int i_nil;
int i_any;
hs_parse_args
(L,
HS_BOOL, &b,
HS_INT, &i,
HS_NUM, &f,
HS_STR, &str,
HS_TBL, &i_tbl,
HS_FUNC, &i_func,
HS_CFUNC, &fn,
HS_USER, &user,
HS_LIGHT, &light,
HS_NIL, &i_nil,
HS_ANY, &i_any,
HS_END);
mu_assert("failed to properly parse boolean!", b);
mu_assert("failed to properly parse integer!", i == 420);
mu_assert("failed to properly parse number!", f == 40.5f);
mu_assert("failed to properly parse string!",
strcmp(str, "hello, world!") == 0);
mu_assert("failed to properly parse table!", i_tbl == tbl_index);
mu_assert("failed to properly parse function!", func_index == i_func);
mu_assert("failed to properly parse C function!", fn == testfunc);
mu_assert("failed to properly parse userdata!", user == userdata);
mu_assert("failed to properly parse light userdata!", light == &five);
mu_assert("failed to properly parse nil!", nil_index == i_nil);
mu_assert("failed to properly parse [any]!", any_index == i_any);
return 0;
}
TEST(parse_readme_example)
{
lua_pushboolean(L, true);
lua_pushinteger(L, 7);
lua_getglobal(L, "debug");
int expected = lua_gettop(L);
lua_pushnumber(L, 3.1415f);
lua_pushstring(L, "c: c: c:");
void *userdata = lua_newuserdata(L, sizeof(char));
bool b; int i; int table_index; float f; char *str; void *user;
hs_parse_args
(L, HS_BOOL, &b, HS_INT, &i, HS_TBL, &table_index,
HS_NUM, &f, HS_STR, &str, HS_USER, &user, HS_END);
mu_assert("failed to properly parse boolean!", b);
mu_assert("failed to properly parse integer!", i == 7);
mu_assert("failed to properly parse table!", table_index == expected);
mu_assert("failed to properly parse number!", f == 3.1415f);
mu_assert("failed to properly parse string!", strcmp(str, "c: c: c:") == 0);
mu_assert("failed to properly parse userdata!", user == userdata);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* tests for hs_parse_overloaded
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#define PARSE_OVERLOADED \
bool b; int i, ti, fi, ni; float f; char *str; \
lua_CFunction fn; void *user, *light; \
int choice = hs_parse_overloaded \
(L, \
HS_BOOL, &b, HS_END, HS_INT, &i, HS_END, HS_NUM, &f, HS_END, \
HS_STR, &str, HS_END, HS_TBL, &ti, HS_END, HS_FUNC, &fi, \
HS_END, HS_CFUNC, &fn, HS_END, HS_USER, &user, HS_END, \
HS_LIGHT, &light, HS_END, HS_NIL, &ni, HS_END, HS_END);
TEST(parse_bool_overloaded)
{
lua_pushboolean(L, true);
PARSE_OVERLOADED;
mu_assert("boolean option was not chosen!", choice == 0);
mu_assert("failed to properly parse boolean!", b);
return 0;
}
TEST(parse_integer_overloaded)
{
lua_pushinteger(L, 5);
PARSE_OVERLOADED;
mu_assert("integer option was not chosen!", choice == 1);
mu_assert("failed to properly parse integer!", i == 5);
return 0;
}
TEST(parse_number_overloaded)
{
lua_pushnumber(L, 42.0f);
PARSE_OVERLOADED;
mu_assert("number option was not chosen!", choice == 2);
mu_assert("failed to properly parse boolean!", f == 42.0f);
return 0;
}
TEST(parse_string_overloaded)
{
lua_pushstring(L, "hello, world!");
PARSE_OVERLOADED;
mu_assert("string option was not chosen!", choice == 3);
mu_assert("failed to properly parse string!",
strcmp(str, "hello, world!") == 0);
return 0;
}
TEST(parse_table_overloaded)
{
lua_getglobal(L, "debug");
int expected = lua_gettop(L);
PARSE_OVERLOADED;
mu_assert("table option was not chosen!", choice == 4);
mu_assert("failed to properly parse table!", ti == expected);
return 0;
}
TEST(parse_function_overloaded)
{
lua_getglobal(L, "type");
int expected = lua_gettop(L);
PARSE_OVERLOADED;
mu_assert("function option was not chosen!", choice == 5);
mu_assert("failed to properly parse function!", fi == expected);
return 0;
}
TEST(parse_cfunction_overloaded)
{
lua_pushcfunction(L, testfunc);
PARSE_OVERLOADED;
mu_assert("C function option was not chosen!", choice == 6);
mu_assert("failed to properly parse C function!", fn == testfunc);
return 0;
}
TEST(parse_userdata_overloaded)
{
void *userdata = lua_newuserdata(L, sizeof(char));
PARSE_OVERLOADED;
mu_assert("userdata option was not chosen!", choice == 7);
mu_assert("failed to properly parse userdata!", user == userdata);
return 0;
}
TEST(parse_lightuserdata_overloaded)
{
int five = 5;
lua_pushlightuserdata(L, &five);
PARSE_OVERLOADED;
mu_assert("light userdata option was not chosen!", choice == 8);
mu_assert("failed to properly parse light userdata!", light == &five);
return 0;
}
TEST(parse_nil_overloaded)
{
lua_pushnil(L);
int expected = lua_gettop(L);
PARSE_OVERLOADED;
mu_assert("nil option was not chosen!", choice == 9);
mu_assert("failed to properly parse nil!", ni == expected);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* tests for hs_pushstring
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
TEST(push_noformat)
{
hs_pushstring(L, "a string");
mu_assert("no string at top of stack!", lua_isstring(L, -1));
const char *string = lua_tostring(L, -1);
mu_assert("string != 'a string'", strcmp(string, "a string") == 0);
return 0;
}
TEST(push_formatint)
{
hs_pushstring(L, "%d is 5", 5);
mu_assert("no string at top of stack!", lua_isstring(L, -1));
const char *string = lua_tostring(L, -1);
mu_assert("string != '5 is 5'", strcmp(string, "5 is 5") == 0);
return 0;
}
TEST(push_formatstring)
{
hs_pushstring(L, "%s is 'hello'", "hello");
mu_assert("no string at top of stack!", lua_isstring(L, -1));
const char *string = lua_tostring(L, -1);
mu_assert("string != 'hello is 'hello''",
strcmp(string, "hello is 'hello'") == 0);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* RUN TESTS
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int main()
{
printf("================ start tests ================\n\n");
printf("running hs_type_to_string() tests...\n");
mu_run_test("bool to string", hs_bool_to_string);
mu_run_test("int to string", hs_int_to_string);
mu_run_test("num to string", hs_num_to_string);
mu_run_test("str to string", hs_str_to_string);
mu_run_test("tbl to string", hs_tbl_to_string);
mu_run_test("func to string", hs_func_to_string);
mu_run_test("cfunc to string", hs_cfunc_to_string);
mu_run_test("user to string", hs_user_to_string);
mu_run_test("light to string", hs_light_to_string);
mu_run_test("nil to string", hs_nil_to_string);
mu_run_test("any to string", hs_any_to_string);
printf("running hs_parse_args() typechecking tests...\n");
mu_run_test("parse bool typecheck", parse_bool_typecheck);
mu_run_test("parse int typecheck", parse_int_typecheck);
mu_run_test("parse num typecheck", parse_num_typecheck);
mu_run_test("parse string typecheck", parse_string_typecheck);
mu_run_test("parse table typecheck", parse_table_typecheck);
mu_run_test("parse func typecheck", parse_func_typecheck);
mu_run_test("parse cfunc typecheck", parse_cfunc_typecheck);
mu_run_test("parse user typecheck", parse_user_typecheck);
mu_run_test("parse light typecheck", parse_light_typecheck);
mu_run_test("parse nil typecheck", parse_nil_typecheck);
mu_run_test("parse any typecheck", parse_any_typecheck);
printf("running hs_parse_args() parsing tests...\n");
mu_run_test("parse bool", parse_bool);
mu_run_test("parse int", parse_int);
mu_run_test("parse num", parse_num);
mu_run_test("parse str", parse_str);
mu_run_test("parse tbl", parse_tbl);
mu_run_test("parse func", parse_func);
mu_run_test("parse cfunc", parse_cfunc);
mu_run_test("fail parse noncfunc", fail_parse_noncfunc);
mu_run_test("parse userdata", parse_userdata);
mu_run_test("parse lightuserdata", parse_lightuserdata);
mu_run_test("parse nil", parse_nil);
mu_run_test("parse any", parse_any);
mu_run_test("parse all", parse_all);
mu_run_test("parse readme example", parse_readme_example);
printf("running hs_parse_overloaded() parsing tests...\n");
mu_run_test("parse bool overloaded", parse_bool_overloaded);
mu_run_test("parse integer overloaded", parse_integer_overloaded);
mu_run_test("parse number overloaded", parse_number_overloaded);
mu_run_test("parse string overloaded", parse_string_overloaded);
mu_run_test("parse table overloaded", parse_table_overloaded);
mu_run_test("parse function overloaded", parse_function_overloaded);
mu_run_test("parse cfunction overloaded", parse_cfunction_overloaded);
mu_run_test("parse userdata overloaded", parse_userdata_overloaded);
mu_run_test("parse lightuserdata overloaded", parse_lightuserdata_overloaded);
mu_run_test("parse nil overloaded", parse_nil_overloaded);
printf("running hs_pushstring() tests...\n");
mu_run_test("hs_pushstring (no printf formatting)", push_noformat);
mu_run_test("hs_pushstring (integer formatting)", push_formatint);
mu_run_test("hs_pushstring (string formatting)", push_formatstring);
printf("\n=============== tests finished ===============\n\n");
const char *color = tests_failed == 0 ? GREEN : RED;
printf("%sran %d tests, %d failed\n" RESET, color, tests_run, tests_failed);
return 0;
}
|