-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmicronit.js
More file actions
105 lines (90 loc) · 2.52 KB
/
Copy pathmicronit.js
File metadata and controls
105 lines (90 loc) · 2.52 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
const categoryContainer = {
name: "",
units: "",
buildHTML() {
return `<div>
<h2>${this.name}</h2>
${this.units}
</div>`;
},
addUnit(outcome, value) {
this.units += unit.buildHTML(outcome, value);
},
addToPage() {
micronit.innerHTML += this.buildHTML();
},
reset() {
this.name = "";
this.units = "";
}
};
const unit = {
buildHTML(outcome, value) {
return `<div class=${outcome}>${value}</div>`
},
test(tests) {
document.addEventListener("DOMContentLoaded", () => {
document.head.innerHTML += `
<style>
#micronit { color: #111 }
h2 { margin-bottom: 0 }
.unit-passed:before {
content: "\u2714 ";
color: #2ECC40;
}
.unit-failed:before {
content: "\u2716 ";
color: #FF4136;
}
.unit-error, .unit-error-stack {
margin-left: 3ch;
/* Needed to preserve error stack newlines */
white-space: pre-line;
}
</style>`;
for (const category in tests) {
categoryContainer.name = category;
for (const unit in tests[category]) {
const unitName = unit;
const unitFunction = tests[category][unit];
try {
unitFunction();/*✔*/
categoryContainer.addUnit(`unit-passed`, unitName);
}
catch (error) {
categoryContainer.addUnit(`unit-failed`, unitName);
categoryContainer.addUnit("unit-error", error);
categoryContainer.addUnit("unit-error-stack", error.stack);
/*✖*/
}
}
categoryContainer.addToPage();
categoryContainer.reset();
}
});
},
assert: (expression, message) => {
if (!expression) unit.fail(message);
},
assertEqual: (expected, actual) => {
if (expected != actual) unit.fail(`${expected} != ${actual}`)
},
assertNotEqual: (expected, actual) => {
if (expected == actual) unit.fail(`${expected} == ${actual}`)
},
assertStrictEqual: (expected, actual) => {
if (expected !== actual) unit.fail(`${expected} !== ${actual}`)
},
assertNotStrictEqual: (expected, actual) => {
if (expected === actual) unit.fail(`${expected} === ${actual}`)
},
assertTrue: object => {
if (object !== true) unit.fail(`${object} !== true`)
},
assertFalse: object => {
if (object !== false) unit.fail(`${object} !== false`)
},
fail: message => {
throw new Error(message);
},
};