-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-lua.cpp
202 lines (166 loc) · 5.14 KB
/
test-lua.cpp
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
// Copyright 2015 Red Blob Games <[email protected]>
// https://github.com/redblobgames/cpp-traverse
// License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
#include <lua.hpp>
#include "traverse.h"
#include "traverse-lua.h"
#include "test.h"
#include "lua-util.h"
/** Test LuaWriter can handle various data types */
void writer_unit_tests() {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
traverse::LuaWriter writer{L};
// Ints
visit(writer, 5);
TEST_EQ(lua_repr(L, -1), "5");
lua_pop(L, 1);
// Floats
visit(writer, 0.5);
TEST_EQ(lua_repr(L, -1), "0.5");
lua_pop(L, 1);
// Strings, including making sure \0 doesn't truncate it
visit(writer, std::string("\0hello\xff", 7));
TEST_EQ(lua_repr(L, -1), std::string("\"\\0hello\xff\"", 10));
lua_pop(L, 1);
// Structs, enums, enum classes, strings with quotes, vectors
visit(writer, Polygon{BLUE, Mood::HULK_SMASH, Charred::END, "UFO\"1942\"", {{3, 5}, {4, 6}, {5, 7}}});
TEST_EQ(lua_repr(L, -1), "{charred = 1, color = 1, mood = 2, name = \"UFO\\\"1942\\\"\", points = {{x = 3, y = 5}, {x = 4, y = 6}, {x = 5, y = 7}}}");
lua_pop(L, 1);
// Each test should leave the stack alone
TEST_EQ(lua_gettop(L), 0);
lua_close(L);
}
/** Test LuaReader can handle various data types */
void reader_unit_tests() {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
traverse::LuaReader reader{L, std::cerr};
// Ints
int i;
lua_eval(L, "5");
visit(reader, i);
TEST_EQ(i, 5);
// Floats
float f;
lua_eval(L, "0.5");
visit(reader, f);
TEST_EQ(f, 0.5);
// Strings
std::string s;
lua_eval(L, "\"hello\"");
visit(reader, s);
TEST_EQ(s, "hello");
// Structs, enums, enum classes, strings with quotes, vectors
Polygon p1;
const Polygon p2 = {BLUE, Mood::HULK_SMASH, Charred::END, "UFO\"1942\"", {{3, 5}, {4, 6}, {5, 7}}};
lua_eval(L, "{charred = 1, color = 1, mood = 2, name = \"UFO\\\"1942\\\"\", points = {{x = 3, y = 5}, {x = 4, y = 6}, {x = 5, y = 7}}}");
visit(reader, p1);
std::stringstream s1, s2;
s1 << p1; s2 << p2;
TEST_EQ(s1.str(), s2.str());
// Each test should leave the stack alone
TEST_EQ(lua_gettop(L), 0);
lua_close(L);
}
/** Test that mismatched Lua/C++ data doesn't silently go through */
void mismatch_unit_tests() {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
std::stringstream errors;
traverse::LuaReader reader{L, errors};
int i; std::string s; std::vector<int> v; Point p;
#define NO(LUA, VAR) { int top = lua_gettop(L); lua_eval(L, LUA); visit(reader, VAR); std::cerr << std::dec; if (errors.str().size() > 0) { std::cout << " PASS " << __FILE__ << ':' << __LINE__ << ' ' << errors.str(); } else { std::cerr << " * FAIL " << __FILE__ << ':' << __LINE__ << std::endl; } errors.str(""); if (lua_gettop(L) != top) { std::cerr << " + FAIL stack not preserved" << std::endl; } }
// Make sure ints and strings don't convert to each other
NO("5", s);
NO("\"5\"", i);
// Catch inappropriate vector indices
NO("{[-3] = 0}", v);
NO("{[100] = 0}", v);
NO("{a = 0}", v);
NO("{[false] = 0}", v);
// Catch extra lua fields
NO("{x=3, y=4, z=5}", p);
NO("{x=3, y=4, [1]=5}", p);
// Catch missing lua fields
NO("{x=3}", p);
// Catch other type mismatches
NO("1", v);
NO("1", p);
NO("\"hi\"", v);
NO("\"hi\"", p);
NO("{}", i);
NO("{}", s);
NO("print", i);
NO("print", s);
NO("print", v);
NO("print", p);
NO("false", i);
NO("false", s);
NO("false", v);
NO("false", p);
NO("nil", i);
NO("nil", s);
NO("nil", v);
NO("nil", p);
#undef NO
lua_close(L);
}
/** Test the ignore_* flags on LuaReader */
void ignore_flag_unit_tests() {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
std::stringstream errors;
traverse::LuaReader reader{L, errors};
int i; std::string s; std::vector<int> v; Point p;
#define YES(LUA, VAR) lua_eval(L, LUA); visit(reader, VAR); std::cerr << std::dec; if (errors.str().size() == 0) { std::cout << " PASS " << __FILE__ << ':' << __LINE__ << std::endl; } else { std::cerr << " * FAIL " << __FILE__ << ':' << __LINE__ << ' ' << errors.str(); } errors.str("")
// Test silencing wrong types
{
reader.ignore_wrong_type = true;
YES("5", s);
YES("\"5\"", i);
YES("1", v);
YES("1", p);
YES("\"hi\"", v);
YES("\"hi\"", p);
YES("{}", i);
YES("{}", s);
YES("print", i);
YES("print", s);
YES("print", v);
YES("print", p);
YES("false", i);
YES("false", s);
YES("false", v);
YES("false", p);
YES("nil", i);
YES("nil", s);
YES("nil", v);
YES("nil", p);
reader.ignore_wrong_type = false;
}
// Test silencing ignore_extra_field
{
reader.ignore_extra_field = true;
YES("{[-3] = 0}", v);
YES("{[100] = 0}", v);
YES("{a = 0}", v);
YES("{[false] = 0}", v);
YES("{x=3, y=4, z=5}", p);
YES("{x=3, y=4, [1]=5}", p);
reader.ignore_extra_field = false;
}
// Test silencing ignore_missing_field
{
reader.ignore_missing_field = true;
YES("{x=3}", p);
reader.ignore_missing_field = false;
}
lua_close(L);
}
int main() {
writer_unit_tests();
reader_unit_tests();
mismatch_unit_tests();
ignore_flag_unit_tests();
}