summaryrefslogtreecommitdiff
path: root/src/tests/hs_parse_overloaded_tests.c
blob: 784807a01d03d87a69dc11406e5f7b68d4c9ec7a (plain)
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
#include "hs_tests.h"

static int testfunc(lua_State *L) { return 0; }

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * tests for hs_parse_overloaded
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#define PARSE_OVERLOADED				\
   bool b; long int i; int ti, fi, ni; double f;	\
   char *str; lua_CFunction fn; void *user, *light;	\
   int choice = hs_parse_overloaded			\
      (L,						\
       hs_overload(hs_bool(b)),				\
       hs_overload(hs_int(i)),				\
       hs_overload(hs_num(f)),				\
       hs_overload(hs_str(str)),			\
       hs_overload(hs_tbl(ti)),				\
       hs_overload(hs_func(fi)),			\
       hs_overload(hs_cfunc(fn)),			\
       hs_overload(hs_user(user)),			\
       hs_overload(hs_light(light)),			\
       hs_overload(hs_nil(ni)));

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)
{
   luaL_loadstring(L, "print('hello, world!')");
   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;
}

TEST(parse_2_3_overload_0)
{
   lua_pushinteger(L, 69);
   lua_pushstring(L, "foo");
   long int i; char *str; void *data;
   int choice = hs_parse_overloaded
      (L,
       hs_overload(hs_int(i), hs_str(str)),
       hs_overload(hs_int(i), hs_str(str), hs_user(data)));

   mu_assert("incorrectly selected option other than 0!", choice == 0);
   mu_assert("failed to properly parse integer!", i == 69);
   mu_assert("failed to properly parse string!", strcmp(str, "foo") == 0);
   return 0;
}

TEST(parse_2_3_overload_1)
{
   lua_pushinteger(L, 69);
   lua_pushstring(L, "foo");
   void *userdata = lua_newuserdata(L, sizeof(char));
   long int i; char *str; void *data;
   int choice = hs_parse_overloaded
      (L,
       hs_overload(hs_int(i), hs_str(str)),
       hs_overload(hs_int(i), hs_str(str), hs_user(data)));

   mu_assert("incorrectly selected option other than 1!", choice == 1);
   mu_assert("failed to properly parse integer!", i == 69);
   mu_assert("failed to properly parse string!", strcmp(str, "foo") == 0);
   mu_assert("failed to properly parse userdata!", userdata == data);
   return 0;
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * test suite
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

void hs_parse_overloaded_tests()
{
   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);
   mu_run_test("parse 2/3 overload 0", parse_2_3_overload_0);
   mu_run_test("parse 2/3 overload 1", parse_2_3_overload_1);
}