-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtestRunner.js
70 lines (62 loc) · 1.67 KB
/
testRunner.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
/*
Copyright 2023 Nito T.M.
License https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
Author Nito T.M. (https://github.com/nitotm)
Package npmjs.com/package/eld
*/
/**
* @param {Array} testCases
*/
export default function runTests (testCases) {
let totalTests = 0
let passedTests = 0
let failedTests = 0
const startTime = Date.now()
for (const testCase of testCases) {
totalTests++
const { name, assert, compare, func } = testCase
let actual
try {
actual = func()
} catch (error) {
console.error('ERROR! - ' + name + ': ', error) // error.message
failedTests++
continue
}
const result = evalOperator(actual, assert, compare) ? 'PASSED' : 'FAILED'
if (result === 'PASSED') {
passedTests++
} else {
failedTests++
}
console.log(result + ' - ' + name +
(result !== 'PASSED' ? ' (Expected: ' + assert + ' ' + compare + '; Actual: ' + actual + ')' : ''))
}
const endTime = Date.now() - startTime
console.log('\n\r-------- SUMMARY --------')
console.log(` Total Tests: ${totalTests}`)
console.log(` Passed Tests: ${passedTests}`)
console.log(` Failed Tests: ${failedTests}`)
console.log(` Total Execution Time: ${endTime}ms`)
}
/**
* @param {string} actual
* @param {string} assert
* @param {string} compare
* @returns {boolean}
*/
function evalOperator (actual, assert, compare) {
switch (assert // eval() is not a good idea, error prone
) {
case '===':
return actual === compare
case '>':
return actual > compare
case '<':
return actual < compare
default:
console.log('Invalid assert operator for the next test:')
return false
}
}
//}