-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
90 lines (81 loc) · 2.1 KB
/
test.js
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
import fs from 'fs'
import {webcrypto} from 'node:crypto'
import init, {resolve_if} from './pkg/conditions.js'
globalThis.crypto = webcrypto
const wasmBuf = fs.readFileSync('./pkg/conditions_bg.wasm')
const wasm = await WebAssembly.compile(wasmBuf)
await init(wasm)
var tests = {
'true': {
result: true,
script: 'success',
data: {'success': true}
},
'false': {
result: false,
script: 'success',
data: {'success': false}
},
'test AND pass': {
result: true,
script: 'success && notFailed',
data: {'success': true, 'notFailed': true}
},
'test OR pass': {
result: true,
script: 'success || notFailed',
data: {'success': false, 'notFailed': true}
},
'test string pass': {
result: true,
script: 'name == "test"',
data: {'name': 'test'}
},
'test string fail': {
result: false,
script: 'name == "test"',
data: {'name': 'not test'}
},
'test string not match pass': {
result: false,
script: 'name != "test"',
data: {'name': 'test'}
},
'throws if invalid': {
result: false,
script: '==',
data: {},
throws: true
},
'test arrays': {
result: true,
script: '["1", "2", "3"].contains(needle)',
data: {'needle': '2'}
},
'test single quotes': {
result: true,
script: 'loader == \'vanilla\'',
data: {'loader': 'vanilla'}
}
}
for (const testsKey in tests) {
let test = tests[testsKey]
let threw = false
let result = false;
console.log('------------------------------------')
try {
result = resolve_if(test.script, test.data)
} catch (e) {
console.log(e)
threw = true
}
console.log('Test: ' + testsKey)
console.log('Expected: ' + test.result)
console.log('Actual: ' + result)
console.log('Threw: ' + threw)
if (threw && !test.throws) {
throw 'test threw an error, was not expected'
} else if (test.result !== result) {
throw 'Test failed'
}
}