Repository navigation
Expand file tree
/
Copy pathrun-test.js
More file actions
79 lines (71 loc) · 2.51 KB
/
Copy pathrun-test.js
File metadata and controls
79 lines (71 loc) · 2.51 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
'use strict'
module.exports = runCases
var testCases = require('./node-test/index')
// A browser gives each case a fresh page, but here every case shares one process, so
// what a case does to the built-ins or adds to the global object is undone when it ends.
// quickCheckAPI.js replaces Array.from, for example, and many helpers assign undeclared globals.
var BUILT_INS = [
Object, Function, Array, String, Number, Boolean, Symbol, Date, RegExp, Error, Promise,
Map, Set, WeakMap, ArrayBuffer, DataView, Int8Array, Uint8Array, Uint8ClampedArray,
Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array
].reduce(function(list, type) {
return list.concat([type, type.prototype])
}, [Math, JSON, Reflect])
function sameDescriptor(a, b) {
return Object.is(a.value, b.value) && a.get === b.get && a.set === b.set &&
a.writable === b.writable && a.enumerable === b.enumerable && a.configurable === b.configurable
}
function saveGlobals() {
return {
globalNames: new Set(Object.getOwnPropertyNames(global)),
builtIns: BUILT_INS.map(function(object) {
var descriptors = new Map()
Reflect.ownKeys(object).forEach(function(key) {
descriptors.set(key, Object.getOwnPropertyDescriptor(object, key))
})
return descriptors
})
}
}
function restoreGlobals(saved) {
BUILT_INS.forEach(function(object, i) {
var descriptors = saved.builtIns[i]
Reflect.ownKeys(object).forEach(function(key) {
if (!descriptors.has(key)) {
Reflect.deleteProperty(object, key)
}
})
descriptors.forEach(function(descriptor, key) {
var current = Object.getOwnPropertyDescriptor(object, key)
if (!current || !sameDescriptor(current, descriptor)) {
Object.defineProperty(object, key, descriptor)
}
})
})
Object.getOwnPropertyNames(global).forEach(function(name) {
if (!saved.globalNames.has(name)) {
Reflect.deleteProperty(global, name)
}
})
}
function runCases(environment) {
var tape = environment.tape
var createContext = environment.createContext
var filter = environment.filter || function() { return true }
testCases.forEach(function(testCase) {
if(filter(testCase[0])) {
tape(testCase[0], function(t) {
var saved = saveGlobals()
t.on('end', function() {
restoreGlobals(saved)
})
var ENVIRONMENT = {
tape: t,
createContext: createContext,
}
var run = testCase[1]
run(ENVIRONMENT)
})
}
})
}