Skip to content

Commit

Permalink
[Tests] Uint8Array.prototype.toHex: add test coverage from tc39/tes…
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 29, 2024
1 parent 97ea97f commit cc72c2f
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions test/Uint8Array.prototype.toHex.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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();
});
},
Expand Down

0 comments on commit cc72c2f

Please sign in to comment.