-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTestCommon.js
More file actions
432 lines (407 loc) · 18.6 KB
/
TestCommon.js
File metadata and controls
432 lines (407 loc) · 18.6 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @format
*/
import EventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter';
function stringify(val) {
if (val == null) return 'null';
else if (typeof(val) == 'object') return JSON.stringify(val);
return val.toString();
}
function checkEquals(lhs, rhs) {
if (typeof(lhs) != typeof(rhs)) {
return {
success: false,
msg: 'Types are not the same. \'' + typeof(lhs) + '\' (' + stringify(lhs) + ') vs \'' + typeof(rhs) + '\' (' + stringify(rhs) + ')'
};
} else if ((lhs == null) && (rhs == null)) {
return { success: true };
} else if ((lhs == null) || (rhs == null)) {
return {
success: false,
msg: '\'' + stringify(lhs) + '\' =/= \'' + stringify(rhs) + '\''
};
}
if (Array.isArray(lhs) && Array.isArray(rhs)) {
// console.log(lhs, ' -> ', rhs);
if (lhs.length != rhs.length) {
return {
success: false,
msg: 'Arrays have different length. ' + lhs.length + ' =/= ' + rhs.length
};
}
// TODO: This technically won't work for sparse arrays
for (var i = 0; i < lhs.length; ++i) {
var result = checkEquals(lhs[i], rhs[i]);
if (!result.success) {
result.msg = 'Mismatched array elements at index ' + i + ': ' + result.msg;
return result;
}
}
} else if (typeof(lhs) === 'object') {
var props = new Set();
var lhsProps = new Set(); // For removal, to validate equality
for (var prop in lhs) {
props.add(prop);
lhsProps.add(prop);
}
for (var prop in rhs) {
if (!lhsProps.delete(prop)) {
return {
success: false,
msg: 'Object does not have property \'' + prop + '\''
};
}
}
if (lhsProps.size != 0) {
return {
success: false,
msg: 'Object does not have property \'' + lhsProps.values().next().value + '\''
};
}
for (var prop of props) {
var result = checkEquals(lhs[prop], rhs[prop]);
if (!result.success) return result;
}
} else {
if (lhs != rhs) {
return {
success: false,
msg: '\'' + stringify(lhs) + '\' =/= \'' + stringify(rhs) + '\''
};
}
}
return { success: true };
}
export const assert = {
isTrue(val) {
if (!val) {
throw new Error('Assertion failed!');
}
},
undefined(val) {
if (typeof val !== "undefined") {
throw new Error('assertUndefined failed!');
}
},
equal(lhs, rhs) {
var result = checkEquals(lhs, rhs);
if (!result.success) {
throw new Error('assertEqual failed! ' + result.msg);
}
},
notEqual(lhs, rhs) {
var result = checkEquals(lhs, rhs);
if (result.success) {
throw new Error('assertNotEqual failed! \'' + stringify(lhs) + '\' == \'' + stringify(rhs) + '\'');
}
},
throwsError(functionToExecute, errorName, errorMessage) {
let isFunctionExecuted = false;
try {
functionToExecute();
isFunctionExecuted = true;
} catch (error) {
if (errorName && error.name !== errorName) {
throw new Error('throwsError failed! Actual error name: ' + error.name + '. Expected error name: ' + errorName);
}
if (errorMessage && error.message !== errorMessage) {
throw new Error('throwsError failed! Actual error message: ' + error.message + '. Expected error message: ' + errorMessage);
}
}
if (isFunctionExecuted) {
throw new Error('throwsError failed! Function did not throw any error.')
}
}
};
export function guidFromString(str) {
// XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
assert.equal(str.length, 36);
assert.equal(str.charAt(8), '-');
assert.equal(str.charAt(13), '-');
assert.equal(str.charAt(18), '-');
assert.equal(str.charAt(23), '-');
return {
data1: parseInt(str.substr(0, 8), 16),
data2: parseInt(str.substr(9, 4), 16),
data3: parseInt(str.substr(14, 4), 16),
data4: [
parseInt(str.substr(19, 2), 16),
parseInt(str.substr(21, 2), 16),
parseInt(str.substr(24, 2), 16),
parseInt(str.substr(26, 2), 16),
parseInt(str.substr(28, 2), 16),
parseInt(str.substr(30, 2), 16),
parseInt(str.substr(32, 2), 16),
parseInt(str.substr(34, 2), 16),
]
};
}
export function makeGuid(data1, data2, data3, data4) {
var result = '';
var append = (val, len) => {
var str = val.toString(16).toUpperCase();
while (str.length < len) str = '0' + str;
result += str;
};
append(data1, 8);
result += '-';
append(data2, 4);
result += '-';
append(data3, 4);
result += '-';
append(data4[0], 2);
append(data4[1], 2);
result += '-';
append(data4[2], 2);
append(data4[3], 2);
append(data4[4], 2);
append(data4[5], 2);
append(data4[6], 2);
append(data4[7], 2);
return result;
}
export function stringFromGuid(guid) {
return makeGuid(guid.data1, guid.data2, guid.data3, guid.data4);
}
export const TestResult = {
NotRun: 0,
Pass: 1,
Fail: 2,
}
export class TestScenario extends EventEmitter {
name;
invoke;
result = TestResult.NotRun;
failureText = '';
constructor(name, invoke) {
super();
this.name = name;
this.invoke = invoke;
}
}
export const zeroGuid = '00000000-0000-0000-0000-000000000000';
export const allSetGuid = 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF';
export const TestValues = {
bools: {
valid: [true, false],
invalid: ['true', 0],
validArrays: [[], [true], [true, false, true], [true, false]],
invalidArrays: [true, ['true'], [1], ['true', 0]],
},
chars: {
valid: ['\0', 'F', '⚾'],
invalid: ['', 65, 'ABC'],
validArrays: [[], ['F'], ['A', 'B', 'C', 'D', 'E', 'F', 'G'], ['\0', 'F', '⚾']],
invalidArrays: ['F', 'ABCD', [65], [true], ['ABCD'], ['', 65, 'ABC']],
},
u8: {
valid: [-0, 0, 127, 128, 255],
invalid: [NaN, Infinity, -1, 0.5, 256],
},
u16: {
valid: [-0, 0, 32767, 32768, 65535],
invalid: [NaN, Infinity, -1, 0.5, 65536],
},
u32: {
valid: [-0, 0, 0x7FFFFFFF, 0x80000000, 0xFFFFFFFF],
invalid: [NaN, Infinity, -1, 0.5, 0x100000000],
},
u64: {
valid: [-0, 0, 0x1FFFFFFFFFFFFF, 0xFFFFFFFFFFFFF800], // 64-bit float has 53-bit mantissa (52 explicit)
invalid: [NaN, Infinity, -1, 0.5, 18446744073709551616], // 1.0p64
},
s16: {
valid: [-32768, -0, 0, 32767],
invalid: [NaN, Infinity, -32769, 0.5, 32768],
},
s32: {
valid: [-0x80000000, -0, 0, 0x7FFFFFFF],
invalid: [NaN, Infinity, -0x80000001, 0.5, 0x80000000, '42', true],
validArrays: [[], [42], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [-0x80000000, -0, 0, 0x7FFFFFFF]],
invalidArrays: [42, ['A'], [true], ['42'], [NaN, Infinity, -0x80000001, 0.5, 0x80000000]],
},
s64: {
valid: [-0x7FFFFFFFFFFFFC00, -0x1FFFFFFFFFFFFF, -0, 0, 0x1FFFFFFFFFFFFF, 0x7FFFFFFFFFFFFC00],
invalid: [NaN, Infinity, -9223372036854777856, 0.5, 9223372036854775808],
},
f32: {
valid: [Infinity, -3.40282346638528859811704183485e+38, -1.40129846432481707092372958329e-45, -0, 0, 1.40129846432481707092372958329e-45, 3.40282346638528859811704183485e+38],
invalid: [-3.40282346638528897590636046442e+38, -1.40129846432481691534834763676e-45, 1.40129846432481691534834763676e-45, 3.40282346638528897590636046442e+38],
},
f64: {
valid: [Infinity, -1.79769313486231570814527423732e+308, -4.94065645841246544176568792868e-324, -0, 0, 4.94065645841246544176568792868e-324, 1.79769313486231570814527423732e+308],
},
strings: {
valid: ['', '\0', 'foo', 'bar', 'foo\0bar', 'foo bar'],
validArrays: [[], ['foo'], ['', '\0', 'foo', 'bar', 'foo\0bar'], ['', '\0', 'foo', 'bar', 'foo\0bar', 'foo bar']],
invalidArrays: ['foo', [true], [42]],
},
guids: {
// NOTE: Guids seem to always come back as uppercase. This assumption makes for easier comparisons, but is probably not guaranteed
valid: [zeroGuid, allSetGuid, '01234567-89AB-CDEF-0123-456789ABCDEF'],
invalid: ['', '{00000000-0000-0000-0000-000000000000}', '00000000 0000 0000 0000 000000000000', '01234567-89AB-CDEF-GHIJ-KLMNOPQRSTUV'],
validArrays: [[], [zeroGuid], [zeroGuid, allSetGuid, '01234567-89AB-CDEF-0123-456789ABCDEF']],
invalidArrays: [zeroGuid, ['{00000000-0000-0000-0000-000000000000}'], ['', '{00000000-0000-0000-0000-000000000000}', '00000000 0000 0000 0000 000000000000', '01234567-89AB-CDEF-GHIJ-KLMNOPQRSTUV']],
},
enums: {
// NOTE: Enums are just represented as numbers
valid: [ 1, TestComponent.TestEnum.first, TestComponent.TestEnum.second, TestComponent.TestEnum.third, TestComponent.TestEnum.fourth ],
invalid: [ true, 'First', 'first', 'TestComponent.TestEnum.First', 'TestComponent.TestEnum.first' ],
validArrays: [[], [1], [ 1, TestComponent.TestEnum.first, TestComponent.TestEnum.second, TestComponent.TestEnum.third, TestComponent.TestEnum.fourth ]],
invalidArrays: [1, [true], [ true, 'First', 'first', 'TestComponent.TestEnum.First', 'TestComponent.TestEnum.first' ]],
},
numericTypes: {
valid: [
{ u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
{ u8: 255, u16: 65535, u32: 0xFFFFFFFF, u64: 0xFFFFFFFFFFFFF800, s16: -32768, s32: -0x80000000, s64: -0x7FFFFFFFFFFFFC00, f32: 9.80000019073486328125, f64: 6.67408e-11, enum: TestComponent.TestEnum.second },
],
invalid: [
{},
{ u8: '8', u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.14159, f64: 6.02e23, enum: TestComponent.TestEnum.first },
{ u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.14159, f64: 6.02e23, enum: TestComponent.TestEnum.first, extraProperty: 'This should not be here' },
{ u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.14159, f64: 6.02e23 },
],
},
stringTypes: {
valid: [
{ char: '\0', string: '\0', guid: zeroGuid },
{ char: 'F', string: 'Hello, world!', guid: '01234567-89AB-CDEF-0123-456789ABCDEF' }
],
invalid: [
{},
{ char: 65, string: 'Test', guid: zeroGuid },
{ char: 'A', string: 'Test', guid: zeroGuid, extraProperty: 42 },
{ char: 'A', string: 'Test' }
],
},
boolTypes: {
valid: [ { value: true }, { value: false } ],
invalid: [ {}, { value: 'true' }, { value: 1 }, { value: true, extraProperty: 42 } ],
},
dates: {
valid: [new Date(2020, 2, 3, 4, 5, 6, 7)],
cppValuesForValidDates: ["132277107060070000"],
invalid: [132323, "21321321"]
},
timeSpans: {
valid: [1726, 0, -218761],
cppValuesForValidTimeSpans: ["17260000", "0", "-2187610000"],
invalid: [{duration: 98217}, new Date()]
},
hResults: {
valid: [1726, 0, -218761],
invalid: [{hresult: 98217}, {}]
},
propertyValues: {
valid: [true, 12723, "hello", { x: 12, y: 23 }, { width: 10, height: 20 }, { x: 12, y: 23, width: 10, height: 20 },
[true, false], [21, 23, 43], ["sasa", "sa"], [{ x: 12, y: 23 }, {x: 24, y: 34}], [{width: 10, height: 20}],
[{ x: 12, y: 23, width: 10, height: 20 }, { x: 12, y: 23, width: 10, height: 20 }], [new TestComponent.TestObject(1)]
],
validValueTypePairs: [
[8, "UInt8"], [8, "Int16"], [8, "UInt16"], [8, "Int32"], [8, "UInt32"], [8, "Int64"], [8, "UInt64"], [8, "Single"], [8, "Double"],
["a", "Char16"], [true, "Boolean"], ["Hello", "String"], [new Date(2020, 2, 3, 4, 5, 6, 7), "DateTime"], [223213, "TimeSpan"],
[{x: 10, y: 20}, "Point"], [{width: 100, height: 200}, "Size"], [{x: 10, y: 20, width: 100, height: 200}, "Rect"],
[[8, 9], "UInt8Array"], [[8, 9], "Int16Array"], [[8, 9], "UInt16Array"], [[8, 9], "Int32Array"], [[8, 9], "UInt32Array"], [[8, 9], "Int64Array"], [[8, 9], "UInt64Array"],
[[8, 9], "SingleArray"], [[8, 9], "DoubleArray"], [["a", "b"], "Char16Array"], [[true, false], "BooleanArray"], [["Hello", "World"], "StringArray"],
[[new Date(2020, 2, 3, 4, 5, 6, 7), new Date(2021, 2, 2, 1, 5, 6, 7)], "DateTimeArray"], [[81212, 932322], "TimeSpanArray"],
[[{x: 10, y: 20}, {x: 10, y: 20}], "PointArray"], [[{width: 100, height: 200}, {width: 100, height: 200}], "SizeArray"],
[[{x: 10, y: 20, width: 100, height: 200}], "RectArray"],
],
invalid: [{}, ["hi", true], [{ width: 10, height: 20 }, { x: 12, y: 23, width: 10, height: 20 }]]
},
composite: {
valid: [
{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid },
bools: { value: true }
},
{
numerics: { u8: 255, u16: 65535, u32: 0xFFFFFFFF, u64: 0xFFFFFFFFFFFFF800, s16: -32768, s32: -0x80000000, s64: -0x7FFFFFFFFFFFFC00, f32: 9.80000019073486328125, f64: 6.67408e-11, enum: TestComponent.TestEnum.second },
strings: { char: 'F', string: 'Hello, world!', guid: allSetGuid },
bools: { value: false }
},
{
numerics: { u8: 0, u16: 0, u32: 0, u64: 0, s16: 0, s32: 0, s64: 0, f32: 0, f64: 0, enum: TestComponent.TestEnum.third },
strings: { char: '⚾', string: 'foo\0bar', guid: '01234567-89AB-CDEF-0123-456789ABCDEF' },
bools: { value: true }
}
],
invalid: [
{}, { numerics: {}, strings: {}, bools: {} },
{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid }
},
{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid },
bools: { value: true },
extraProperty: 'This should not be here'
},
{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid },
bools: { value: true, extraProperty: 'This should not be here' }
}
],
validArrays: [
[],
[{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid },
bools: { value: true }
}],
[
{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid },
bools: { value: true }
},
{
numerics: { u8: 255, u16: 65535, u32: 0xFFFFFFFF, u64: 0xFFFFFFFFFFFFF800, s16: -32768, s32: -0x80000000, s64: -0x7FFFFFFFFFFFFC00, f32: 9.80000019073486328125, f64: 6.67408e-11, enum: TestComponent.TestEnum.second },
strings: { char: 'F', string: 'Hello, world!', guid: allSetGuid },
bools: { value: false }
},
{
numerics: { u8: 0, u16: 0, u32: 0, u64: 0, s16: 0, s32: 0, s64: 0, f32: 0, f64: 0, enum: TestComponent.TestEnum.third },
strings: { char: '⚾', string: 'foo\0bar', guid: '01234567-89AB-CDEF-0123-456789ABCDEF' },
bools: { value: true }
}
]
],
invalidArrays: [
{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid },
bools: { value: true }
},
[{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid },
bools: { value: true },
extraProperty: 'This should not be here'
}],
[
{}, { numerics: {}, strings: {}, bools: {} },
{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid }
},
{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid },
bools: { value: true },
extraProperty: 'This should not be here'
},
{
numerics: { u8: 8, u16: 16, u32: 32, u64: 64, s16: -16, s32: -32, s64: -64, f32: 3.141590118408203125, f64: 6.02e23, enum: TestComponent.TestEnum.first },
strings: { char: '\0', string: '\0', guid: zeroGuid },
bools: { value: true, extraProperty: 'This should not be here' }
}
]
],
}
}