forked from compat-table/compat-table
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtest.js
139 lines (119 loc) · 3.31 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
const testers = require('./testers.json')
const fs = require('fs')
const path = require('path')
var version = process.versions.node
var engine = process.jsEngine || 'v8'
var esStaging = /--es_staging/.test(process.execArgv) ? '--es_staging' : ''
if (esStaging) version += '--es_staging'
var harmony = /--harmony/.test(process.execArgv) ? '--harmony' : ''
if (harmony) version += '--harmony'
function silencer() {
console.log('test had an unhandled rejected promise');
}
process.addListener('unhandledRejection', silencer);
console.log('Testing ' + version)
// This function is needed to run the tests and was extracted from:
// https://github.com/kangax/compat-table/blob/gh-pages/node.js
global.__createIterableObject = function (arr, methods) {
methods = methods || {}
if (typeof Symbol !== 'function' || !Symbol.iterator) {
return {}
}
arr.length++
var iterator = {
next: function () {
return {
value: arr.shift(),
done: arr.length <= 0
}
},
'return': methods['return'],
'throw': methods['throw']
}
var iterable = {}
iterable[Symbol.iterator] = function () { return iterator }
return iterable
}
var output = {
_version: version,
_engine: engine + ' ' + process.versions[engine]
}
var versions = Object.keys(testers)
function next (ver) {
if (!ver) return write()
var completed = 0
var results = output[ver] = {
_successful: 0,
_count: Object.keys(testers[ver]).length,
_percent: 0
}
Object.keys(testers[ver]).forEach(function (name) {
var script = testers[ver][name].code
results[name] = false // make SURE it makes it to the output
run(script, function (result) {
// expected results: `e.message` or true/false
results[name] = typeof result === 'string' ? result : !!result
if (results[name] === true) results._successful++
if (++completed === results._count) {
results._percent = results._successful / results._count
setTimeout(next, 10, versions.pop())
}
})
})
}
setTimeout(next, 10, versions.pop())
function run (script, cb) {
// kangax's Promise tests reply on a asyncTestPassed function.
var async = /asyncTestPassed/.test(script)
if (async) {
runAsync(script, function (result) {
return cb(result)
})
} else {
var result = runSync(script)
return cb(result)
}
}
function runAsync (script, cb) {
/* eslint-disable no-new-func */
var timer = null
try {
var fn = new Function('asyncTestPassed', script)
fn(function () {
clearTimeout(timer)
process.nextTick(function () {
cb(true)
})
})
timer = setTimeout(function () {
cb(false)
}, 5000)
} catch (e) {
clearTimeout(timer)
process.nextTick(function () {
cb(e.message)
})
}
}
function runSync (script) {
try {
var fn = new Function(script)
return fn() || false
} catch (e) {
return e.message
}
}
function write () {
var json = JSON.stringify(output, null, 2)
if (/v8-build/.test(version)) {
version = 'bleeding'
}
if (/nightly/.test(version)) {
version = 'nightly'
if (esStaging) version += '--es_staging'
if (harmony) version += '--harmony'
}
var destination = path.resolve(__dirname, 'results/' + engine, version + '.json')
console.log('Saving', destination)
fs.writeFileSync(destination, json)
}