-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathextension-test.js
More file actions
237 lines (208 loc) · 6.42 KB
/
Copy pathextension-test.js
File metadata and controls
237 lines (208 loc) · 6.42 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
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
/// <reference path="typings/ue.d.ts" />
"use strict"
const async_task_timeout_in_seconds = 10
let _ = require('lodash')
let root_path = Root.GetDir('GameContent') + 'Scripts/tests'
// sync run with timer support
function run_sync(P,timeout) {
let alive = true
P().then(_ => {
alive = false
})
function pump() {
let delegate = Root.OnTick.toJSON()
let prev = Date.now()
let deadline = $time + async_task_timeout_in_seconds
while (alive) {
let cur = Date.now()
let elapsed = cur - prev
delegate(elapsed / 1000.0)
prev = cur
if ($time > deadline) {
return false
}
}
return true
}
return pump()
}
function load(file) {
let tests = {}
let pending = []
let scope = null
let instances = []
function describe(name, opts, body) {
if (!body) {
body = opts
opts = {}
}
let prev = scope
scope = {
name: name,
full: prev ? [prev.full, name].join('.') : name,
parent: prev,
open: 0
}
body.call(scope)
scope = prev
if (!prev) {
// FJavascriptAutomatedTest (V8/Public/JavascriptTestLibrary.h)
let running
let delegate = Root.OnTick.toJSON()
let prev, deadline
function start() {
prev = Date.now()
deadline = $time + async_task_timeout_in_seconds
}
function poll() {
let cur = Date.now()
let elapsed = cur - prev
delegate(elapsed / 1000.0)
prev = cur
return ($time < deadline)
}
let recipe = _.extend({
Name: name.replace(/[ \t]/g, '_'),
bComplexTask: false,
TestFlags: 0x02000000 | 0x00000001 | 0x00000002,
RequiredDeviceNum: 1,
TestFunctionNames: pending.slice(),
Function: function (params) {
let {TestFunctionName, Tester} = params
if (!running) {
start()
Tester.SetContinue(true)
running = tests[TestFunctionName](Tester).then(_ => {
Tester.SetContinue(false)
running = false
}).catch(_ => {
Tester.SetContinue(false)
running = false
})
} else {
if (poll()) {
Tester.SetContinue(true)
} else {
Tester.AddError("Async time out")
running = false
}
}
}
},opts)
pending.length = 0
let instance = JavascriptTestLibrary.Create(recipe)
instances.push(instance)
}
}
function run(scope, body, Tester) {
function this_scope() {
function open() {
if (scope.open++ == 0) {
if (scope.before) {
return scope.before()
}
}
return Promise.resolve()
}
function close() {
if (--scope.open == 0) {
if (scope.after) {
return scope.after()
}
}
return Promise.resolve()
}
function pause() {
return new Promise(resolve => process.nextTick(resolve))
}
return open()
.then(body)
.catch(e => {
Tester.AddError(String(e))
})
.then(pause)
.then(close)
}
if (scope.parent) {
return run(scope.parent, this_scope, Tester)
} else {
return this_scope()
}
}
function func_to_promise(body) {
let async = false
String(body).replace(/.*function.*\(([^\)]*)\)/, function (a, b) {
async = (b.replace(/\t /g, '').length > 0)
})
let org = body
if (async) {
return function p_body() {
return new Promise(resolve => {
org(resolve)
})
}
} else {
return function p_body() {
return new Promise(resolve => {
org()
resolve()
})
}
}
}
function it(name, body) {
let bound = scope
body = func_to_promise(body)
let key = [scope.full, name].join('.')
let [a, ...b] = key.split('.')
key = b.join('.')
pending.push(key)
tests[key] = function (Tester) {
return run(bound, body, Tester)
}
}
function before(body) {
scope.before = func_to_promise(body)
}
function after(body) {
scope.after = func_to_promise(body)
}
eval("(function () { " + Root.ReadStringFromFile(root_path + '/' + file) + "})()")
return function () {
instances.forEach(instance => instance.Destroy())
}
}
function shot() {
let test_files = []
function read_dir(dir) {
let out = Root.ReadDirectory(dir)
if (out.$) {
let items = _.filter(out.OutItems, (item) => !item.bIsDirectory && /^((?!node_modules).)*$/.test(item.Name) && /[\^\/]test\-[^\.]*\.js$/.test(item.Name))
test_files = test_files.concat(items.map((item) => item.Name.substr(root_path.length + 1)))
out.OutItems.forEach((item) => {
if (item.bIsDirectory) {
read_dir(item.Name)
}
})
}
}
read_dir(root_path)
let byes = test_files.map(load)
return function () {
byes.forEach(fn => fn())
}
}
module.exports = function () {
let bye = shot()
function refresh() {
bye && bye()
bye = shot()
}
let w = new DirectoryWatcher()
w.OnChanged.Add(refresh)
w.Watch(root_path)
return function () {
w.Unwatch()
bye && bye()
}
}