| 1 |
/** |
| 2 |
* AUTO-GENERATED - DO NOT EDIT. Source: https://github.com/gpuweb/cts |
| 3 |
**/import { assert, unreachable } from '../util/util.js'; |
| 4 |
|
| 5 |
export class SkipTestCase extends Error {} |
| 6 |
export class UnexpectedPassError extends Error {} |
| 7 |
|
| 8 |
export { TestCaseRecorder } from '../internal/logging/test_case_recorder.js'; |
| 9 |
|
| 10 |
/** The fully-general type for params passed to a test function invocation. */ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
export class SubcaseBatchState { |
| 23 |
constructor( |
| 24 |
recorder, |
| 25 |
/** The case parameters for this test fixture shared state. Subcase params are not included. */ |
| 26 |
params) |
| 27 |
{this.recorder = recorder;this.params = params;} |
| 28 |
|
| 29 |
/** |
| 30 |
* Runs before the `.before()` function. |
| 31 |
* @internal MAINTENANCE_TODO: Make this not visible to test code? |
| 32 |
*/ |
| 33 |
async init() {} |
| 34 |
/** |
| 35 |
* Runs between the `.before()` function and the subcases. |
| 36 |
* @internal MAINTENANCE_TODO: Make this not visible to test code? |
| 37 |
*/ |
| 38 |
async postInit() {} |
| 39 |
/** |
| 40 |
* Runs after all subcases finish. |
| 41 |
* @internal MAINTENANCE_TODO: Make this not visible to test code? |
| 42 |
*/ |
| 43 |
async finalize() {} |
| 44 |
|
| 45 |
/** Throws an exception marking the subcase as skipped. */ |
| 46 |
skip(msg) { |
| 47 |
throw new SkipTestCase(msg); |
| 48 |
} |
| 49 |
|
| 50 |
/** Throws an exception making the subcase as skipped if condition is true */ |
| 51 |
skipIf(cond, msg = '') { |
| 52 |
if (cond) { |
| 53 |
this.skip(typeof msg === 'function' ? msg() : msg); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* A Fixture is a class used to instantiate each test sub/case at run time. |
| 60 |
* A new instance of the Fixture is created for every single test subcase |
| 61 |
* (i.e. every time the test function is run). |
| 62 |
*/ |
| 63 |
export class Fixture { |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Interface for recording logs and test status. |
| 68 |
* |
| 69 |
* @internal |
| 70 |
*/ |
| 71 |
|
| 72 |
eventualExpectations = []; |
| 73 |
numOutstandingAsyncExpectations = 0; |
| 74 |
objectsToCleanUp = []; |
| 75 |
|
| 76 |
static MakeSharedState(recorder, params) { |
| 77 |
return new SubcaseBatchState(recorder, params); |
| 78 |
} |
| 79 |
|
| 80 |
/** @internal */ |
| 81 |
constructor(sharedState, rec, params) { |
| 82 |
this._sharedState = sharedState; |
| 83 |
this.rec = rec; |
| 84 |
this._params = params; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the (case+subcase) parameters for this test function invocation. |
| 89 |
*/ |
| 90 |
get params() { |
| 91 |
return this._params; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Gets the test fixture's shared state. This object is shared between subcases |
| 96 |
* within the same testcase. |
| 97 |
*/ |
| 98 |
get sharedState() { |
| 99 |
return this._sharedState; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Override this to do additional pre-test-function work in a derived fixture. |
| 104 |
* This has to be a member function instead of an async `createFixture` function, because |
| 105 |
* we need to be able to ergonomically override it in subclasses. |
| 106 |
* |
| 107 |
* @internal MAINTENANCE_TODO: Make this not visible to test code? |
| 108 |
*/ |
| 109 |
async init() {} |
| 110 |
|
| 111 |
/** |
| 112 |
* Override this to do additional post-test-function work in a derived fixture. |
| 113 |
* |
| 114 |
* Called even if init was unsuccessful. |
| 115 |
* |
| 116 |
* @internal MAINTENANCE_TODO: Make this not visible to test code? |
| 117 |
*/ |
| 118 |
async finalize() { |
| 119 |
assert( |
| 120 |
this.numOutstandingAsyncExpectations === 0, |
| 121 |
'there were outstanding immediateAsyncExpectations (e.g. expectUncapturedError) at the end of the test' |
| 122 |
); |
| 123 |
|
| 124 |
// Loop to exhaust the eventualExpectations in case they chain off each other. |
| 125 |
while (this.eventualExpectations.length) { |
| 126 |
const p = this.eventualExpectations.shift(); |
| 127 |
try { |
| 128 |
await p; |
| 129 |
} catch (ex) { |
| 130 |
this.rec.threw(ex); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// And clean up any objects now that they're done being used. |
| 135 |
for (const o of this.objectsToCleanUp) { |
| 136 |
if ('getExtension' in o) { |
| 137 |
const WEBGL_lose_context = o.getExtension('WEBGL_lose_context'); |
| 138 |
if (WEBGL_lose_context) WEBGL_lose_context.loseContext(); |
| 139 |
} else if ('destroy' in o) { |
| 140 |
o.destroy(); |
| 141 |
} else if ('destroyAsync' in o) { |
| 142 |
await o.destroyAsync(); |
| 143 |
} else if ('close' in o) { |
| 144 |
o.close(); |
| 145 |
} else { |
| 146 |
// HTMLVideoElement |
| 147 |
o.src = ''; |
| 148 |
o.srcObject = null; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Tracks an object to be cleaned up after the test finishes. |
| 155 |
* |
| 156 |
* Usually when creating buffers/textures/query sets, you can use the helpers in GPUTest instead. |
| 157 |
*/ |
| 158 |
trackForCleanup(o) { |
| 159 |
if (o instanceof Promise) { |
| 160 |
this.eventualAsyncExpectation(() => |
| 161 |
o.then( |
| 162 |
(o) => this.trackForCleanup(o), |
| 163 |
() => {} |
| 164 |
) |
| 165 |
); |
| 166 |
return o; |
| 167 |
} |
| 168 |
|
| 169 |
if (o instanceof GPUDevice) { |
| 170 |
this.objectsToCleanUp.push({ |
| 171 |
async destroyAsync() { |
| 172 |
o.destroy(); |
| 173 |
await o.lost; |
| 174 |
} |
| 175 |
}); |
| 176 |
} else { |
| 177 |
this.objectsToCleanUp.push(o); |
| 178 |
} |
| 179 |
return o; |
| 180 |
} |
| 181 |
|
| 182 |
/** Tracks an object, if it's destroyable, to be cleaned up after the test finishes. */ |
| 183 |
tryTrackForCleanup(o) { |
| 184 |
if (typeof o === 'object' && o !== null) { |
| 185 |
if ( |
| 186 |
'destroy' in o || |
| 187 |
'close' in o || |
| 188 |
o instanceof WebGLRenderingContext || |
| 189 |
o instanceof WebGL2RenderingContext) |
| 190 |
{ |
| 191 |
this.objectsToCleanUp.push(o); |
| 192 |
} |
| 193 |
} |
| 194 |
return o; |
| 195 |
} |
| 196 |
|
| 197 |
/** Call requestDevice() and track the device for cleanup. */ |
| 198 |
requestDeviceTracked(adapter, desc = undefined) { |
| 199 |
|
| 200 |
return this.trackForCleanup(adapter.requestDevice(desc)); |
| 201 |
} |
| 202 |
|
| 203 |
/** Log a debug message. */ |
| 204 |
debug(msg) { |
| 205 |
this.rec.debug(new Error(msg)); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Log an info message. |
| 210 |
* **Use sparingly. Use `debug()` instead if logs are only needed with debug logging enabled.** |
| 211 |
*/ |
| 212 |
info(msg) { |
| 213 |
this.rec.info(new Error(msg)); |
| 214 |
} |
| 215 |
|
| 216 |
/** Throws an exception marking the subcase as skipped. */ |
| 217 |
skip(msg) { |
| 218 |
throw new SkipTestCase(msg); |
| 219 |
} |
| 220 |
|
| 221 |
/** Throws an exception marking the subcase as skipped if condition is true */ |
| 222 |
skipIf(cond, msg = '') { |
| 223 |
if (cond) { |
| 224 |
this.skip(typeof msg === 'function' ? msg() : msg); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** Log a warning and increase the result status to "Warn". */ |
| 229 |
warn(msg) { |
| 230 |
this.rec.warn(new Error(msg)); |
| 231 |
} |
| 232 |
|
| 233 |
/** Log an error and increase the result status to "ExpectFailed". */ |
| 234 |
fail(msg) { |
| 235 |
this.rec.expectationFailed(new Error(msg)); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Wraps an async function. Tracks its status to fail if the test tries to report a test status |
| 240 |
* before the async work has finished. |
| 241 |
*/ |
| 242 |
async immediateAsyncExpectation(fn) { |
| 243 |
this.numOutstandingAsyncExpectations++; |
| 244 |
const ret = await fn(); |
| 245 |
this.numOutstandingAsyncExpectations--; |
| 246 |
return ret; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Wraps an async function, passing it an `Error` object recording the original stack trace. |
| 251 |
* The async work will be implicitly waited upon before reporting a test status. |
| 252 |
*/ |
| 253 |
eventualAsyncExpectation(fn) { |
| 254 |
const promise = fn(new Error()); |
| 255 |
this.eventualExpectations.push(promise); |
| 256 |
} |
| 257 |
|
| 258 |
expectErrorValue(expectedError, ex, niceStack) { |
| 259 |
if (!(ex instanceof Error)) { |
| 260 |
niceStack.message = `THREW non-error value, of type ${typeof ex}: ${ex}`; |
| 261 |
this.rec.expectationFailed(niceStack); |
| 262 |
return; |
| 263 |
} |
| 264 |
const actualName = ex.name; |
| 265 |
if (expectedError !== true && actualName !== expectedError) { |
| 266 |
niceStack.message = `THREW ${actualName}, instead of ${expectedError}: ${ex}`; |
| 267 |
this.rec.expectationFailed(niceStack); |
| 268 |
} else { |
| 269 |
niceStack.message = `OK: threw ${actualName}: ${ex.message}`; |
| 270 |
this.rec.debug(niceStack); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/** Expect that the provided promise resolves (fulfills). */ |
| 275 |
shouldResolve(p, msg) { |
| 276 |
this.eventualAsyncExpectation(async (niceStack) => { |
| 277 |
const m = msg ? ': ' + msg : ''; |
| 278 |
try { |
| 279 |
await p; |
| 280 |
niceStack.message = 'resolved as expected' + m; |
| 281 |
} catch (ex) { |
| 282 |
niceStack.message = `REJECTED${m}`; |
| 283 |
if (ex instanceof Error) { |
| 284 |
niceStack.message += '\n' + ex.message; |
| 285 |
} |
| 286 |
this.rec.expectationFailed(niceStack); |
| 287 |
} |
| 288 |
}); |
| 289 |
} |
| 290 |
|
| 291 |
/** Expect that the provided promise rejects, with the provided exception name. */ |
| 292 |
shouldReject( |
| 293 |
expectedName, |
| 294 |
p, |
| 295 |
{ allowMissingStack = false, message } = {}) |
| 296 |
{ |
| 297 |
this.eventualAsyncExpectation(async (niceStack) => { |
| 298 |
const m = message ? ': ' + message : ''; |
| 299 |
try { |
| 300 |
await p; |
| 301 |
niceStack.message = 'DID NOT REJECT' + m; |
| 302 |
this.rec.expectationFailed(niceStack); |
| 303 |
} catch (ex) { |
| 304 |
this.expectErrorValue(expectedName, ex, niceStack); |
| 305 |
if (!allowMissingStack) { |
| 306 |
if (!(ex instanceof Error && typeof ex.stack === 'string')) { |
| 307 |
const exMessage = ex instanceof Error ? ex.message : '?'; |
| 308 |
niceStack.message = `rejected as expected, but missing stack (${exMessage})${m}`; |
| 309 |
this.rec.expectationFailed(niceStack); |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
}); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Expect that the provided function throws (if `true` or `string`) or not (if `false`). |
| 318 |
* If a string is provided, expect that the throw exception has that name. |
| 319 |
* |
| 320 |
* MAINTENANCE_TODO: Change to `string | false` so the exception name is always checked. |
| 321 |
*/ |
| 322 |
shouldThrow( |
| 323 |
expectedError, |
| 324 |
fn, |
| 325 |
{ allowMissingStack = false, message } = {}) |
| 326 |
{ |
| 327 |
const m = message ? ': ' + message : ''; |
| 328 |
try { |
| 329 |
fn(); |
| 330 |
if (expectedError === false) { |
| 331 |
this.rec.debug(new Error('did not throw, as expected' + m)); |
| 332 |
} else { |
| 333 |
this.rec.expectationFailed(new Error('unexpectedly did not throw' + m)); |
| 334 |
} |
| 335 |
} catch (ex) { |
| 336 |
if (expectedError === false) { |
| 337 |
this.rec.expectationFailed(new Error('threw unexpectedly' + m)); |
| 338 |
} else { |
| 339 |
this.expectErrorValue(expectedError, ex, new Error(m)); |
| 340 |
if (!allowMissingStack) { |
| 341 |
if (!(ex instanceof Error && typeof ex.stack === 'string')) { |
| 342 |
this.rec.expectationFailed(new Error('threw as expected, but missing stack' + m)); |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Expect that a condition is true. |
| 351 |
* |
| 352 |
* Note: You can pass a boolean condition, or a function that returns a boolean. |
| 353 |
* The advantage to passing a function is that if it's short it is self documenting. |
| 354 |
* |
| 355 |
* t.expect(size >= maxSize); // prints Expect OK: |
| 356 |
* t.expect(() => size >= maxSize) // prints Expect OK: () => size >= maxSize |
| 357 |
*/ |
| 358 |
expect(cond, msg) { |
| 359 |
if (typeof cond === 'function') { |
| 360 |
if (msg === undefined) { |
| 361 |
msg = cond.toString(); |
| 362 |
} |
| 363 |
cond = cond(); |
| 364 |
} |
| 365 |
if (cond) { |
| 366 |
const m = msg ? ': ' + msg : ''; |
| 367 |
this.rec.debug(new Error('expect OK' + m)); |
| 368 |
} else { |
| 369 |
this.rec.expectationFailed(new Error(msg)); |
| 370 |
} |
| 371 |
return cond; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* If the argument is an `Error`, fail (or warn). If it's `undefined`, no-op. |
| 376 |
* If the argument is an array, apply the above behavior on each of elements. |
| 377 |
*/ |
| 378 |
expectOK( |
| 379 |
error, |
| 380 |
{ mode = 'fail', niceStack } = {}) |
| 381 |
{ |
| 382 |
const handleError = (error) => { |
| 383 |
if (error instanceof Error) { |
| 384 |
if (niceStack) { |
| 385 |
error.stack = niceStack.stack; |
| 386 |
} |
| 387 |
if (mode === 'fail') { |
| 388 |
this.rec.expectationFailed(error); |
| 389 |
} else if (mode === 'warn') { |
| 390 |
this.rec.warn(error); |
| 391 |
} else { |
| 392 |
unreachable(); |
| 393 |
} |
| 394 |
} |
| 395 |
}; |
| 396 |
|
| 397 |
if (Array.isArray(error)) { |
| 398 |
for (const e of error) { |
| 399 |
handleError(e); |
| 400 |
} |
| 401 |
} else { |
| 402 |
handleError(error); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
eventualExpectOK( |
| 407 |
error, |
| 408 |
{ mode = 'fail' } = {}) |
| 409 |
{ |
| 410 |
this.eventualAsyncExpectation(async (niceStack) => { |
| 411 |
this.expectOK(await error, { mode, niceStack }); |
| 412 |
}); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
/** |
| 419 |
* FixtureClass encapsulates a constructor for fixture and a corresponding |
| 420 |
* shared state factory function. An interface version of the type is also |
| 421 |
* defined for mixin declaration use ONLY. The interface version is necessary |
| 422 |
* because mixin classes need a constructor with a single any[] rest |
| 423 |
* parameter. |
| 424 |
*/ |