11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests interoperability between TextEncoder and TextDecoder with
41cb0ef41Sopenharmony_ci// Node.js util.inspect and Buffer APIs
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci'use strict';
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cirequire('../common');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst { customInspectSymbol: inspect } = require('internal/util');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst encoded = Buffer.from([0xef, 0xbb, 0xbf, 0x74, 0x65,
141cb0ef41Sopenharmony_ci                             0x73, 0x74, 0xe2, 0x82, 0xac]);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// Make Sure TextEncoder exists
171cb0ef41Sopenharmony_ciassert(TextEncoder);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci// Test TextEncoder
201cb0ef41Sopenharmony_ci{
211cb0ef41Sopenharmony_ci  const enc = new TextEncoder();
221cb0ef41Sopenharmony_ci  assert.strictEqual(enc.encoding, 'utf-8');
231cb0ef41Sopenharmony_ci  assert(enc);
241cb0ef41Sopenharmony_ci  const buf = enc.encode('\ufefftest€');
251cb0ef41Sopenharmony_ci  assert.strictEqual(Buffer.compare(buf, encoded), 0);
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci  const enc = new TextEncoder();
301cb0ef41Sopenharmony_ci  const buf = enc.encode();
311cb0ef41Sopenharmony_ci  assert.strictEqual(buf.length, 0);
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci{
351cb0ef41Sopenharmony_ci  const enc = new TextEncoder();
361cb0ef41Sopenharmony_ci  const buf = enc.encode(undefined);
371cb0ef41Sopenharmony_ci  assert.strictEqual(buf.length, 0);
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci{
411cb0ef41Sopenharmony_ci  const inspectFn = TextEncoder.prototype[inspect];
421cb0ef41Sopenharmony_ci  const encodeFn = TextEncoder.prototype.encode;
431cb0ef41Sopenharmony_ci  const encodingGetter =
441cb0ef41Sopenharmony_ci    Object.getOwnPropertyDescriptor(TextEncoder.prototype, 'encoding').get;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  const instance = new TextEncoder();
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  const expectedError = {
491cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
501cb0ef41Sopenharmony_ci    name: 'TypeError',
511cb0ef41Sopenharmony_ci    message: 'Value of "this" must be of type TextEncoder'
521cb0ef41Sopenharmony_ci  };
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  inspectFn.call(instance, Infinity, {});
551cb0ef41Sopenharmony_ci  encodeFn.call(instance);
561cb0ef41Sopenharmony_ci  encodingGetter.call(instance);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  const invalidThisArgs = [{}, [], true, 1, '', new TextDecoder()];
591cb0ef41Sopenharmony_ci  invalidThisArgs.forEach((i) => {
601cb0ef41Sopenharmony_ci    assert.throws(() => inspectFn.call(i, Infinity, {}), expectedError);
611cb0ef41Sopenharmony_ci    assert.throws(() => encodeFn.call(i), expectedError);
621cb0ef41Sopenharmony_ci    assert.throws(() => encodingGetter.call(i), expectedError);
631cb0ef41Sopenharmony_ci  });
641cb0ef41Sopenharmony_ci}
65