forked from ekaterinaasf/calculator-refactor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.js
More file actions
77 lines (63 loc) · 2.72 KB
/
Copy pathtesting.js
File metadata and controls
77 lines (63 loc) · 2.72 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
function testing(func, tests) {
const encodedFunc = encodeURIComponent(func.toString());
const sanitizedFunc = encodedFunc
.replace(/\(/g, '%28').replace(/\)/g, '%29')
.replace(/%09/g, '%20%20');
const jsTutorURL = "http://www.pythontutor.com/live.html#code=" + sanitizedFunc + "&cumulative=false&curInstr=2&heapPrimitives=nevernest&mode=display&origin=opt-live.js&py=js&rawInputLstJSON=%5B%5D&textReferences=false";
// const jsTutorURL = "http://www.pythontutor.com/javascript.html#code=" + sanitizedFunc + "&curInstr=0&mode=display&origin=opt-frontend.js&py=js&rawInputLstJSON=%5B%5D";
console.group('%cTESTING: ' + func.name, 'font-weight:bold;');
console.log(jsTutorURL);
console.groupEnd();
if (!tests) {
try {
const returned = func();
console.log('args: ', []);
console.log('returned: ', returned);
} catch (err) {
console.log(err);
};
} else {
if (tests.length === 0) {
console.log('- no tests!');
return;
}
tests.forEach((test, i) => {
if (test.constructor.name !== 'Object') {
console.log(`%c-- tests[${i}] is not an Object`, 'color:purple;font-weight:bold;');
return;
} else if (typeof test.name !== 'string') {
console.log(`%c-- tests[${i}].name is not a String`, 'color:purple;font-weight:bold;');
return;
} else if (!Array.isArray(test.args)) {
console.log(`%c-- tests[${i}].args is not an Array`, 'color:purple;font-weight:bold;');
return;
} else if (!test.hasOwnProperty('expected')) {
console.log(`%c-- tests[${i}].expected does not exist`, 'color:purple;font-weight:bold;');
return;
}
console.groupCollapsed('%c------ ' + test.name + ' ------', 'font-weight:bold');
console.log('%c- arguments:', 'color:blue;font-weight:bold;', test.args);
let result = null;
try {
result = func(...test.args);
console.log('%c- returned:', 'font-weight:bold;', '(' + typeof result + '),', result);
} catch (err) {
result = err;
console.log(err);
}
console.log('%c- expected:', 'color:blue;font-weight:bold;', '(' + typeof test.expected + '),', test.expected);
console.groupEnd();
if (result instanceof Error) {
console.log('%c- ERROR', 'color:red;font-weight:bold;');
} else if (test.expected !== test.expected // is the expected value NaN?
? result !== result // then check if the returned one is
: result === test.expected) { // otherwise compare them directly
console.log('%c- PASS', 'color:green;font-weight:bold;');
} else {
console.log('%c- FAIL', 'color:orange;font-weight:bold;');
}
});
}
console.log('\n');
return { func, tests }
}