-
Notifications
You must be signed in to change notification settings - Fork 1
/
winxedxx_builtin_test_more.cxx
237 lines (199 loc) · 6 KB
/
winxedxx_builtin_test_more.cxx
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
// winxedxx_builtin_test_more.h
// (C) 2012 Julián Albo "NotFound"
#include "winxedxx_types.h"
#include "winxedxx_object.h"
#include "winxedxx_default.h"
#include "winxedxx_integer.h"
#include "winxedxx_namespace.h"
#include <iostream>
namespace WinxedXX
{
namespace BuiltinModule
{
namespace Test
{
namespace More
{
static int counter;
WxxObjectPtr diag(const WxxObjectArray &args)
{
std::string msg;
for (int i = 0; i < args.elements(); ++i)
msg += args.get_pmc_keyed(i).get_string();
size_t pos = msg.find('\n');
while (pos != std::string::npos) {
std::string part = msg.substr(0, pos);
if (part.length() == 0 || part[0] != '#')
std::cout << "# ";
std::cout << part << '\n';
msg.erase(pos + 1);
pos = msg.find('\n');
}
if (msg != "") {
if (msg[0] != '#')
std::cout << "# ";
std::cout << msg << '\n';
}
return winxedxxnull;
}
WxxObjectPtr plan(const WxxObjectArray &args)
{
counter = 0;
int numtests = args.get_pmc_keyed(0).get_integer();
std::cout << "1.." << numtests << "\n";
return winxedxxnull;
}
WxxObjectPtr done_testing(const WxxObjectArray &args)
{
std::cout << "1.." << counter << "\n";
return winxedxxnull;
}
static int numNewTest()
{
return ++counter;
}
WxxObjectPtr ok(const WxxObjectArray &args)
{
int numargs = args.elements();
int value = args.get_pmc_keyed(0).get_integer();
if (! value)
std::cout << "not ";
std::cout << "ok " << numNewTest();
if (numargs > 1)
std::cout << " - " << args.get_pmc_keyed(1).get_string();
std::cout << "\n";
return winxedxxnull;
}
WxxObjectPtr nok(const WxxObjectArray &args)
{
int numargs = args.elements();
int value = args.get_pmc_keyed(0).get_integer();
if (value)
std::cout << "not ";
std::cout << "ok " << numNewTest();
if (numargs > 1)
std::cout << " - " << args.get_pmc_keyed(1).get_string();
std::cout << "\n";
return winxedxxnull;
}
static bool args_equal(WxxObjectPtr &value, WxxObjectPtr &expected)
{
bool result;
// Poor man's "multi" dispatch
bool is_integer =
value.instanceof("Integer") && expected.instanceof("Integer");
bool is_number = is_integer || (
(value.instanceof("Float") || value.instanceof("Integer")) &&
(expected.instanceof("Float") ||expected.instanceof("Integer") ));
bool is_string = (! is_number) &&
(value.instanceof("String") && expected.instanceof("String"));
if (is_integer)
result = value.get_integer() == expected.get_integer();
else if (is_number)
result = value.get_number() == expected.get_number();
else if (is_string)
result = value.get_string() == expected.get_string();
else
result = value.is_equal(expected);
return result;
}
WxxObjectPtr is(const WxxObjectArray &args)
{
int numargs = args.elements();
WxxObjectPtr value = args.get_pmc_keyed(0);
WxxObjectPtr expected = args.get_pmc_keyed(1);
std::string msg = numargs > 2 ? args.get_pmc_keyed(2).get_string() : "";
bool result = args_equal(value, expected);
if (! result)
std::cout << "not ";
std::cout << "ok " << numNewTest();
if (msg != "")
std::cout << " - " << msg;
std::cout << "\n";
if (! result)
std::cout << "# Have: " << value.get_string() <<
"\n# Want: " << expected.get_string() << "\n";
return winxedxxnull;
}
WxxObjectPtr isnt(const WxxObjectArray &args)
{
int numargs = args.elements();
WxxObjectPtr value = args.get_pmc_keyed(0);
WxxObjectPtr expected = args.get_pmc_keyed(1);
std::string msg = numargs > 2 ? args.get_pmc_keyed(2).get_string() : "";
bool result = args_equal(value, expected);
if (result)
std::cout << "not ";
std::cout << "ok " << numNewTest();
if (msg != "")
std::cout << " - " << msg;
std::cout << "\n";
if (result) {
std::string s = value.get_string();
std::cout << "# Have: " << s << "\n# Want: not " << s << "\n";
}
return winxedxxnull;
}
WxxObjectPtr is_null(const WxxObjectArray &args)
{
int numargs = args.elements();
WxxObjectPtr value = args.get_pmc_keyed(0);
std::string msg = numargs > 1 ? args.get_pmc_keyed(1).get_string() : "";
bool result = value.is_null();
if (! result)
std::cout << "not ";
std::cout << "ok " << numNewTest();
if (msg != "")
std::cout << " - " << msg;
std::cout << "\n";
return winxedxxnull;
}
WxxObjectPtr throws_type(const WxxObjectArray &args)
{
int numargs = args.elements();
WxxObjectPtr fun = args.get_pmc_keyed(0);
int type = args.get_pmc_keyed(1).get_integer();
std::string msg = numargs > 2 ? args.get_pmc_keyed(2).get_string() : "";
std::string diag;
int result = 0;
try {
fun(WxxObjectArray());
diag = "not thrown";
}
catch (WxxObjectPtr ex) {
if (ex.get_pmc_keyed("type").get_integer() == type)
result = 1;
else
diag = "not the expected type";
}
if (! result)
std::cout << "not ";
std::cout << "ok " << numNewTest();
if (msg != "")
std::cout << " - " << msg;
std::cout << "\n";
if (diag != "")
std::cout << "# " << diag << "\n";
return winxedxxnull;
}
//******************************************************
void initialize()
{
counter = 0;
WxxNamespace &ns = getRootNamespace().
childNamespace("Test").childNamespace("More");
ns.set("diag", new WxxSub(&diag));
ns.set("plan", new WxxSub(&plan));
ns.set("done_testing", new WxxSub(&done_testing));
ns.set("ok", new WxxSub(&ok));
ns.set("nok", new WxxSub(&nok));
ns.set("is", new WxxSub(&is));
ns.set("isnt", new WxxSub(&isnt));
ns.set("is_null", new WxxSub(&is_null));
ns.set("throws_type", new WxxSub(&throws_type));
}
} // namespace Test
} // namespace More
} // namespace BuiltinModule
} // namespace WinxedXX
// End of winxedxx_builtin_test_more.h