From cc72c2f5c7c34a52dfd7793d840ea5b0a22f3982 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 28 Feb 2024 16:10:56 -0800 Subject: [PATCH] [Tests] `Uint8Array.prototype.toHex`: add test coverage from https://github.com/tc39/test262/pull/3994 --- test/Uint8Array.prototype.toHex.js | 66 +++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/test/Uint8Array.prototype.toHex.js b/test/Uint8Array.prototype.toHex.js index 1b86ae7..538752f 100644 --- a/test/Uint8Array.prototype.toHex.js +++ b/test/Uint8Array.prototype.toHex.js @@ -1,9 +1,16 @@ 'use strict'; -var defineProperties = require('define-properties'); -var test = require('tape'); +var availableTypedArrays = require('available-typed-arrays')(); var callBind = require('call-bind'); +var defineProperties = require('define-properties'); +var DetachArrayBuffer = require('es-abstract/2023/DetachArrayBuffer'); +var forEach = require('for-each'); var inspect = require('object-inspect'); +var isCore = require('is-core-module'); +var test = require('tape'); + +/* globals postMessage: false */ +var canDetach = typeof structuredClone === 'function' || typeof postMessage === 'function' || isCore('worker_threads'); var index = require('../Uint8Array.prototype.toHex'); var impl = require('../Uint8Array.prototype.toHex/implementation'); @@ -44,6 +51,61 @@ module.exports = { inspect(array2) + ' produces expected hex string' ); + st.test('test262: test/built-ins/Uint8Array/prototype/toHex/detached-buffer.js', { skip: !canDetach }, function (s2t) { + var arr = new Uint8Array(2); + DetachArrayBuffer(arr.buffer); + s2t['throws']( + function () { method(arr); }, + TypeError + ); + + s2t.end(); + }); + + st.test('test262: test/built-ins/Uint8Array/prototype/toHex/receiver-not-uint8array.js', { + skip: !defineProperties.supportsDescriptors + }, function (s2t) { + var options = {}; + s2t.intercept(options, 'alphabet', { + get: function () { + throw new EvalError('options.alphabet accessed despite incompatible receiver'); + } + }); + + forEach(availableTypedArrays, function (taName) { + if (taName === 'Uint8Array') { return; } + var TA = global[taName]; + var sample = new TA(2); + s2t['throws']( + function () { method(sample, options); }, + TypeError, + 'throws with ' + taName + ); + }); + + s2t['throws']( + function () { method([]); }, + TypeError, + 'throws on a normal array receiver' + ); + + s2t['throws']( + function () { method(); }, + TypeError, + 'throws on no receiver' + ); + + s2t.end(); + }); + + st.equal(method(new Uint8Array([])), ''); + st.equal(method(new Uint8Array([102])), '66'); + st.equal(method(new Uint8Array([102, 111])), '666f'); + st.equal(method(new Uint8Array([102, 111, 111])), '666f6f'); + st.equal(method(new Uint8Array([102, 111, 111, 98])), '666f6f62'); + st.equal(method(new Uint8Array([102, 111, 111, 98, 97])), '666f6f6261'); + st.equal(method(new Uint8Array([102, 111, 111, 98, 97, 114])), '666f6f626172'); + st.end(); }); },