1'use strict'; 2 3self.getterRejects = (t, obj, getterName, target) => { 4 const getter = Object.getOwnPropertyDescriptor(obj, getterName).get; 5 6 return promise_rejects_js(t, TypeError, getter.call(target), getterName + ' should reject with a TypeError'); 7}; 8 9self.getterRejectsForAll = (t, obj, getterName, targets) => { 10 return Promise.all(targets.map(target => self.getterRejects(t, obj, getterName, target))); 11}; 12 13self.methodRejects = (t, obj, methodName, target, args) => { 14 const method = obj[methodName]; 15 16 return promise_rejects_js(t, TypeError, method.apply(target, args), 17 methodName + ' should reject with a TypeError'); 18}; 19 20self.methodRejectsForAll = (t, obj, methodName, targets, args) => { 21 return Promise.all(targets.map(target => self.methodRejects(t, obj, methodName, target, args))); 22}; 23 24self.getterThrows = (obj, getterName, target) => { 25 const getter = Object.getOwnPropertyDescriptor(obj, getterName).get; 26 27 assert_throws_js(TypeError, () => getter.call(target), getterName + ' should throw a TypeError'); 28}; 29 30self.getterThrowsForAll = (obj, getterName, targets) => { 31 targets.forEach(target => self.getterThrows(obj, getterName, target)); 32}; 33 34self.methodThrows = (obj, methodName, target, args) => { 35 const method = obj[methodName]; 36 assert_equals(typeof method, 'function', methodName + ' should exist'); 37 38 assert_throws_js(TypeError, () => method.apply(target, args), methodName + ' should throw a TypeError'); 39}; 40 41self.methodThrowsForAll = (obj, methodName, targets, args) => { 42 targets.forEach(target => self.methodThrows(obj, methodName, target, args)); 43}; 44 45self.constructorThrowsForAll = (constructor, firstArgs) => { 46 firstArgs.forEach(firstArg => assert_throws_js(TypeError, () => new constructor(firstArg), 47 'constructor should throw a TypeError')); 48}; 49 50self.garbageCollect = async () => { 51 if (self.TestUtils?.gc) { 52 // https://testutils.spec.whatwg.org/#the-testutils-namespace 53 await TestUtils.gc(); 54 } else if (self.gc) { 55 // Use --expose_gc for V8 (and Node.js) 56 // to pass this flag at chrome launch use: --js-flags="--expose-gc" 57 // Exposed in SpiderMonkey shell as well 58 self.gc(); 59 } else if (self.GCController) { 60 // Present in some WebKit development environments 61 GCController.collect(); 62 } else { 63 /* eslint-disable no-console */ 64 console.warn('Tests are running without the ability to do manual garbage collection. They will still work, but ' + 65 'coverage will be suboptimal.'); 66 /* eslint-enable no-console */ 67 } 68}; 69 70self.delay = ms => new Promise(resolve => step_timeout(resolve, ms)); 71 72// For tests which verify that the implementation doesn't do something it shouldn't, it's better not to use a 73// timeout. Instead, assume that any reasonable implementation is going to finish work after 2 times around the event 74// loop, and use flushAsyncEvents().then(() => assert_array_equals(...)); 75// Some tests include promise resolutions which may mean the test code takes a couple of event loop visits itself. So go 76// around an extra 2 times to avoid complicating those tests. 77self.flushAsyncEvents = () => delay(0).then(() => delay(0)).then(() => delay(0)).then(() => delay(0)); 78 79self.assert_typed_array_equals = (actual, expected, message) => { 80 const prefix = message === undefined ? '' : `${message} `; 81 assert_equals(typeof actual, 'object', `${prefix}type is object`); 82 assert_equals(actual.constructor, expected.constructor, `${prefix}constructor`); 83 assert_equals(actual.byteOffset, expected.byteOffset, `${prefix}byteOffset`); 84 assert_equals(actual.byteLength, expected.byteLength, `${prefix}byteLength`); 85 assert_equals(actual.buffer.byteLength, expected.buffer.byteLength, `${prefix}buffer.byteLength`); 86 assert_array_equals([...actual], [...expected], `${prefix}contents`); 87 assert_array_equals([...new Uint8Array(actual.buffer)], [...new Uint8Array(expected.buffer)], `${prefix}buffer contents`); 88}; 89 90self.makePromiseAndResolveFunc = () => { 91 let resolve; 92 const promise = new Promise(r => { resolve = r; }); 93 return [promise, resolve]; 94}; 95