11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciself.getterRejects = (t, obj, getterName, target) => {
41cb0ef41Sopenharmony_ci  const getter = Object.getOwnPropertyDescriptor(obj, getterName).get;
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci  return promise_rejects_js(t, TypeError, getter.call(target), getterName + ' should reject with a TypeError');
71cb0ef41Sopenharmony_ci};
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciself.getterRejectsForAll = (t, obj, getterName, targets) => {
101cb0ef41Sopenharmony_ci  return Promise.all(targets.map(target => self.getterRejects(t, obj, getterName, target)));
111cb0ef41Sopenharmony_ci};
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciself.methodRejects = (t, obj, methodName, target, args) => {
141cb0ef41Sopenharmony_ci  const method = obj[methodName];
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  return promise_rejects_js(t, TypeError, method.apply(target, args),
171cb0ef41Sopenharmony_ci                         methodName + ' should reject with a TypeError');
181cb0ef41Sopenharmony_ci};
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciself.methodRejectsForAll = (t, obj, methodName, targets, args) => {
211cb0ef41Sopenharmony_ci  return Promise.all(targets.map(target => self.methodRejects(t, obj, methodName, target, args)));
221cb0ef41Sopenharmony_ci};
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciself.getterThrows = (obj, getterName, target) => {
251cb0ef41Sopenharmony_ci  const getter = Object.getOwnPropertyDescriptor(obj, getterName).get;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => getter.call(target), getterName + ' should throw a TypeError');
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciself.getterThrowsForAll = (obj, getterName, targets) => {
311cb0ef41Sopenharmony_ci  targets.forEach(target => self.getterThrows(obj, getterName, target));
321cb0ef41Sopenharmony_ci};
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciself.methodThrows = (obj, methodName, target, args) => {
351cb0ef41Sopenharmony_ci  const method = obj[methodName];
361cb0ef41Sopenharmony_ci  assert_equals(typeof method, 'function', methodName + ' should exist');
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => method.apply(target, args), methodName + ' should throw a TypeError');
391cb0ef41Sopenharmony_ci};
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciself.methodThrowsForAll = (obj, methodName, targets, args) => {
421cb0ef41Sopenharmony_ci  targets.forEach(target => self.methodThrows(obj, methodName, target, args));
431cb0ef41Sopenharmony_ci};
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciself.constructorThrowsForAll = (constructor, firstArgs) => {
461cb0ef41Sopenharmony_ci  firstArgs.forEach(firstArg => assert_throws_js(TypeError, () => new constructor(firstArg),
471cb0ef41Sopenharmony_ci                                                 'constructor should throw a TypeError'));
481cb0ef41Sopenharmony_ci};
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciself.garbageCollect = async () => {
511cb0ef41Sopenharmony_ci  if (self.TestUtils?.gc) {
521cb0ef41Sopenharmony_ci    // https://testutils.spec.whatwg.org/#the-testutils-namespace
531cb0ef41Sopenharmony_ci    await TestUtils.gc();
541cb0ef41Sopenharmony_ci  } else if (self.gc) {
551cb0ef41Sopenharmony_ci    // Use --expose_gc for V8 (and Node.js)
561cb0ef41Sopenharmony_ci    // to pass this flag at chrome launch use: --js-flags="--expose-gc"
571cb0ef41Sopenharmony_ci    // Exposed in SpiderMonkey shell as well
581cb0ef41Sopenharmony_ci    self.gc();
591cb0ef41Sopenharmony_ci  } else if (self.GCController) {
601cb0ef41Sopenharmony_ci    // Present in some WebKit development environments
611cb0ef41Sopenharmony_ci    GCController.collect();
621cb0ef41Sopenharmony_ci  } else {
631cb0ef41Sopenharmony_ci    /* eslint-disable no-console */
641cb0ef41Sopenharmony_ci    console.warn('Tests are running without the ability to do manual garbage collection. They will still work, but ' +
651cb0ef41Sopenharmony_ci      'coverage will be suboptimal.');
661cb0ef41Sopenharmony_ci    /* eslint-enable no-console */
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci};
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciself.delay = ms => new Promise(resolve => step_timeout(resolve, ms));
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci// For tests which verify that the implementation doesn't do something it shouldn't, it's better not to use a
731cb0ef41Sopenharmony_ci// timeout. Instead, assume that any reasonable implementation is going to finish work after 2 times around the event
741cb0ef41Sopenharmony_ci// loop, and use flushAsyncEvents().then(() => assert_array_equals(...));
751cb0ef41Sopenharmony_ci// Some tests include promise resolutions which may mean the test code takes a couple of event loop visits itself. So go
761cb0ef41Sopenharmony_ci// around an extra 2 times to avoid complicating those tests.
771cb0ef41Sopenharmony_ciself.flushAsyncEvents = () => delay(0).then(() => delay(0)).then(() => delay(0)).then(() => delay(0));
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciself.assert_typed_array_equals = (actual, expected, message) => {
801cb0ef41Sopenharmony_ci  const prefix = message === undefined ? '' : `${message} `;
811cb0ef41Sopenharmony_ci  assert_equals(typeof actual, 'object', `${prefix}type is object`);
821cb0ef41Sopenharmony_ci  assert_equals(actual.constructor, expected.constructor, `${prefix}constructor`);
831cb0ef41Sopenharmony_ci  assert_equals(actual.byteOffset, expected.byteOffset, `${prefix}byteOffset`);
841cb0ef41Sopenharmony_ci  assert_equals(actual.byteLength, expected.byteLength, `${prefix}byteLength`);
851cb0ef41Sopenharmony_ci  assert_equals(actual.buffer.byteLength, expected.buffer.byteLength, `${prefix}buffer.byteLength`);
861cb0ef41Sopenharmony_ci  assert_array_equals([...actual], [...expected], `${prefix}contents`);
871cb0ef41Sopenharmony_ci  assert_array_equals([...new Uint8Array(actual.buffer)], [...new Uint8Array(expected.buffer)], `${prefix}buffer contents`);
881cb0ef41Sopenharmony_ci};
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ciself.makePromiseAndResolveFunc = () => {
911cb0ef41Sopenharmony_ci  let resolve;
921cb0ef41Sopenharmony_ci  const promise = new Promise(r => { resolve = r; });
931cb0ef41Sopenharmony_ci  return [promise, resolve];
941cb0ef41Sopenharmony_ci};
95