-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
284 lines (269 loc) · 8.88 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"use strict"
function test(testname = "", testcode = function() {
throw("TEST NOT IMPLEMENTED")
}) {
test.prepareresults().then(function() {
Object.defineProperty(testcode, "name", {
configurable: true, value: testname
})
testcode.status = "running"
test.tests[testname] = testcode
test.total = test.total + 1
test.totalcounter.textContent = test.total
test.running = test.running + 1
test.runningcounter.textContent = test.running
let item = document.createElement("li")
item.id = testname
item.textContent = testname
test.runninglist.appendChild(item)
test.setpadding("running")
let result, failed
try {
result = testcode(function(result) {
test.pass(testname, result)
}, function(error) {
test.fail(testname, error)
})
} catch(error) {
test.fail(testname, error)
failed = true
}
if(testcode.length == 0) {
if(result && typeof(result.then) == "function") {
result.then(function(result) {
test.pass(testname, result)
}).catch(function(error) {
test.fail(testname, error)
})
} else if(failed != true) {
test.pass(testname, result)
}
} else if(result && typeof(result.catch) == "function") {
result.catch(function(error) {
test.fail(testname, error)
}) } }) }
test.end = function(testname) {
test.running = test.running - 1
test.runningcounter.textContent = test.running
test.runninglist.querySelector('[id="' + testname + '"]').remove()
test.setpadding("running")
}
test.pass = function(testname, result) {
if(test.tests[testname].status == "running") {
test.tests[testname].status = "passed"
test.tests[testname].result = result
test.end(testname)
test.passed = test.passed + 1
test.passedcounter.textContent = test.passed
let item = document.createElement("li")
item.id = testname
if(result !== undefined) {
item.textContent = testname + " : " + result
} else {
item.textContent = testname
}
test.passedlist.appendChild(item)
test.setpadding("passed")
} }
test.fail = function(testname, error, consoleerror) {
if(error && error.message) {
error.message = error.message.split("\n").join(" ")
} else if(typeof(error) == "string") {
error = error.split("\n").join(" ")
}
if(consoleerror != false) {
console.error(error)
}
if(test.tests[testname].status == "running") {
test.end(testname)
} else if(test.tests[testname].status == "passed") {
test.passed = test.passed - 1
test.passedcounter.textContent = test.passed
test.passedlist.querySelector('[id="' + testname + '"]').remove()
test.setpadding("passed")
}
test.tests[testname].status = "failed"
test.tests[testname].result = error
test.failed = test.failed + 1
test.failedcounter.textContent = test.failed
let item = document.createElement("li")
item.id = testname
item.textContent = testname + " : " + error
test.failedlist.appendChild(item)
test.setpadding("failed")
}
test.catch = function(error, file, line, column) {
let source = test.sources[file]
let getsource = Promise.resolve(source)
if(source == null) {
getsource = fetch(file).then(function(response) {
if(response.ok) {
return(response.text())
} else {
throw(Error(response.statusText))
}
}).then(function(source) {
test.sources[file] = source
return(source)
}).catch(function() {
return("")
}) }
getsource.then(function(source) {
if(source && source.includes("test(")) {
let errorindex = 0
let lines = source.split("\n")
for(let lineindex = 0; lineindex < line; lineindex++) {
if(lineindex + 1 == line) {
errorindex = errorindex + lineindex + column
} else {
errorindex = errorindex + lines[lineindex].length
} }
let testsources = source.split("test(")
testsources.shift()
let tests = {}
for(let testsource of testsources) {
let stringsymbol = testsource[0]
let testname = testsource.slice(1, testsource.indexOf(stringsymbol, 1))
tests[testname] = source.indexOf("test(" + stringsymbol + testname + stringsymbol)
}
for(let testname of Object.keys(tests).reverse()) {
if(errorindex > tests[testname]) {
if(test.tests[testname]) {
test.fail(testname, error, false)
}
break
} } } }) }
test.setpadding = function(list) {
let item = document.createElement("li")
item.textContent = test[list + "list"].children.length + "."
item.style.display = "inline-block"
item.style.visibility = "hidden"
document.body.appendChild(item)
test[list + "list"].style.paddingLeft = item.clientWidth + 4 + "px"
item.remove()
}
test.prepareresults = function() {
return(new Promise(function(resolver) {
if(test.prepareresults.done) {
resolver()
} else {
if(test.prepareresults.resolvers == null) {
test.prepareresults.resolvers = []
}
test.prepareresults.resolvers.push(resolver)
} })) }
test.mainscript = document.currentScript
test.mainscript.remove()
test.testsscript = document.querySelector('script[src="tests.js"]')
if(test.testsscript) {
test.testsscript.remove()
}
test.tests = {}
test.sources = {}
test.total = 0
test.running = 0
test.passed = 0
test.failed = 0
Array.prototype.toString = function() {
return(JSON.stringify(this, null, 1))
}
Object.prototype.toString = function() {
return(JSON.stringify(this, null, 1))
}
Error.prototype.toString = function() {
let error = this
let result = error.name
if(error.message) {
result = result + ": " + error.message
}
if(error.stack) {
let stack = error.stack.slice(error.message.length + error.name.length + 3)
if(stack.includes("\n")) {
stack = stack.slice(0, stack.indexOf("\n"))
}
let location = stack.slice(stack.lastIndexOf(" (") + 2, -1)
if(stack.includes(" (") == false) {
location = stack.slice(stack.indexOf("at") + 3)
}
let locationparts = location.split(":")
if(locationparts.length > 2 && location.includes("/")) {
let column = locationparts[locationparts.length - 1]
let line = locationparts[locationparts.length - 2]
let file = location.slice(location.lastIndexOf("/") + 1, -(line.length + column.length + 2))
result = result + " at " + file + ":" + line + ":" + column
} }
return(result)
}
fetch("test.html").then(function(response) {
if(response.ok) {
return(response)
} else {
throw(Error(response.statusText))
}
}).catch(function() {
return(fetch("https://danielherr.github.io/Test/test.html").then(function(response) {
if(response.ok) {
return(response)
} else {
throw(Error(response.statusText))
} }))
}).then(function(response) {
return(response.text())
}).then(function(text) {
let resultsview = new DOMParser().parseFromString(text, "text/html")
test.totalcounter = resultsview.querySelector("total-tests")
test.runningcounter = resultsview.querySelector("running-total")
test.passedcounter = resultsview.querySelector("passed-total")
test.failedcounter = resultsview.querySelector("failed-total")
test.runninglist = resultsview.querySelector("#running")
test.passedlist = resultsview.querySelector("#passed")
test.failedlist = resultsview.querySelector("#failed")
if(document.documentElement.createShadowRoot) {
let shadow = document.documentElement.createShadowRoot()
resultsview.head.insertBefore(test.mainscript, resultsview.head.children[1])
if(test.testsscript) {
resultsview.head.appendChild(test.testsscript)
}
shadow.appendChild(resultsview.head)
shadow.appendChild(resultsview.body)
shadow.appendChild(document.createElement("content"))
} else {
if(document.querySelector("meta[http-equiv=Content-Security-Policy]") == null) {
let csp = resultsview.querySelector("meta")
document.head.appendChild(csp)
csp.remove()
}
let resultselements = resultsview.querySelector("test-results")
resultselements.insertBefore(resultsview.querySelector("style"), resultselements.firstElementChild)
document.body.insertBefore(resultselements, document.body.firstElementChild)
}
self.addEventListener("error", function(event) {
test.catch(event.error, event.filename, event.lineno, event.colno)
})
self.addEventListener("unhandledrejection", function(event) {
if(event.reason && event.reason.stack) {
let stack = event.reason.stack.slice(event.reason.message.length + event.reason.name.length + 3)
if(stack.includes("\n")) {
stack = stack.slice(0, stack.indexOf("\n"))
}
let location = stack.slice(stack.lastIndexOf(" (") + 2, -1)
if(stack.includes(" (") == false) {
location = stack.slice(stack.indexOf("at") + 3)
}
let locationparts = location.split(":")
if(locationparts.length > 2 && location.includes("/")) {
let column = locationparts[locationparts.length - 1]
let line = locationparts[locationparts.length - 2]
let file = location.slice(0, -(line.length + column.length + 2))
line = Number(line)
column = Number(column)
test.catch(event.reason, file, line, column)
} } })
test.prepareresults.done = true
for(let resolver of test.prepareresults.resolvers || []) {
resolver()
}
}).catch(function(error) {
console.error(error)
document.body.textContent = "Unable to load test results HTML: " + error
})