/
tester.js
152 lines (146 loc) Β· 4.07 KB
/
tester.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
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
(function() {
function printValue(txt) {
if (txt.indexOf('rgb(') === 0) {
let hex = txt
.replace('rgb(', '')
.replace(')', '').trim()
.split(',')
.map(function(c) {
let x = parseInt(c.trim(), 10).toString(16);
return x.length < 2 ? '0' + x : x;
})
.join('');
return '#' + hex;
} else {
return txt;
}
}
const assert = {
compare: {
truthy() {
return {
check: function(x) {
return !!x
},
description: 'Should be non-empty'
};
},
gt(num) {
return {
check: function(x) {
return parseFloat(x, 10) > num;
},
description: 'Greater than ' + num
};
},
lt(num) {
return {
check: function(x) {
return parseFloat(x, 10) < num;
},
description: 'Less than ' + num
};
},
gte(num) {
return {
check: function(x) {
return parseFloat(x, 10) >= num;
},
description: 'Greater than or equal to' + num
};
},
lte(num) {
return {
check: function(x) {
return parseFloat(x, 10) <= num;
},
description: 'Less than or equal to' + num
};
},
eq(y) {
return {
check: function(x) {
return x == y;
},
description: 'Equal to ' + y
};
}
},
ok: function(check, description) {
if (!check) {
throw { description };
}
},
equal: function(a, b, description) {
if (a !== b) {
throw { description };
}
},
hasStyle: function(selector, styles, description) {
var elem = document.querySelector(selector);
if (!elem) {
throw {
description: 'No element found: ' + selector
};
}
var elemStyles = window.getComputedStyle(elem);
for (let s in styles) {
if (styles.hasOwnProperty(s)) {
switch (typeof styles[s]) {
case 'object':
if (!styles[s].check(elemStyles[s])) {
throw { description,
detail: 'Expected $("' + selector + '") to meet the following condition for ' + s + ':\n' + styles[s].description + '\nFound: ' + elemStyles[s]
};
}
break;
default:
if (elemStyles[s] !== styles[s] && styles[s] !== printValue(elemStyles[s])) {
throw { description,
detail: 'Expected $("' + selector + '") to have value for ' + s + ':\n' + styles[s] + '.\n' + printValue(elemStyles[s]) + ' was found instead'
}
}
break;
}
}
}
}
};
var _errorContainer = null;
function setupErrorList() {
_errorContainer = document.createElement('ul');
_errorContainer.className = 'test-failures';
_errorContainer.style = 'position: fixed;bottom: 0; max-height: 100vh; overflow-y: scroll; right: 0; width: 200px; padding-left:0; background: red; list-style: none; color: white; font-family: Sans-Serif';
document.body.appendChild(_errorContainer);
return _errorContainer;
}
function addErrorToList(info) {
var li = document.createElement('li');
li.className = 'test-failure';
li.style = 'margin: 10px; background: #f55; padding: 10px'
var s = '<h4 style="text-transform: uppercase">' + info.name + '</h4><b >' + info.description + '</b>';
if (info.detail) {
s += '<pre style="font-size: 0.8rem; font-family: Monospace; white-space: pre-wrap">' + info.detail + '</pre>';
}
li.innerHTML = s;
_errorContainer.appendChild(li);
}
function onFail(info) {
if (!_errorContainer) {
setupErrorList();
}
addErrorToList(info);
}
function test(name, cb) {
try {
cb(assert);
} catch(e) {
console.error(e);
onFail(Object.assign(e, { name }));
}
};
function start() {
window.test = test;
};
window.onload = start();
}());