11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// utf8, ucs2, ascii, latin1, utf16le
71cb0ef41Sopenharmony_ciconst encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1',
81cb0ef41Sopenharmony_ci                   'binary', 'utf16le', 'utf-16le'];
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciencodings
111cb0ef41Sopenharmony_ci  .reduce((es, e) => es.concat(e, e.toUpperCase()), [])
121cb0ef41Sopenharmony_ci  .forEach((encoding) => {
131cb0ef41Sopenharmony_ci    assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo');
141cb0ef41Sopenharmony_ci  });
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// base64
171cb0ef41Sopenharmony_ci['base64', 'BASE64'].forEach((encoding) => {
181cb0ef41Sopenharmony_ci  assert.strictEqual(Buffer.from('Zm9v', encoding).toString(encoding), 'Zm9v');
191cb0ef41Sopenharmony_ci});
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// hex
221cb0ef41Sopenharmony_ci['hex', 'HEX'].forEach((encoding) => {
231cb0ef41Sopenharmony_ci  assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding),
241cb0ef41Sopenharmony_ci                     '666f6f');
251cb0ef41Sopenharmony_ci});
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci// Invalid encodings
281cb0ef41Sopenharmony_cifor (let i = 1; i < 10; i++) {
291cb0ef41Sopenharmony_ci  const encoding = String(i).repeat(i);
301cb0ef41Sopenharmony_ci  const error = common.expectsError({
311cb0ef41Sopenharmony_ci    code: 'ERR_UNKNOWN_ENCODING',
321cb0ef41Sopenharmony_ci    name: 'TypeError',
331cb0ef41Sopenharmony_ci    message: `Unknown encoding: ${encoding}`
341cb0ef41Sopenharmony_ci  });
351cb0ef41Sopenharmony_ci  assert.ok(!Buffer.isEncoding(encoding));
361cb0ef41Sopenharmony_ci  assert.throws(() => Buffer.from('foo').toString(encoding), error);
371cb0ef41Sopenharmony_ci}
38