11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Verify that constructor arguments are correctly reflected in the attributes.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Mapping of the first argument to TextDecoderStream to the expected value of
81cb0ef41Sopenharmony_ci// the encoding attribute. We assume that if this subset works correctly, the
91cb0ef41Sopenharmony_ci// rest probably work too.
101cb0ef41Sopenharmony_ciconst labelToName = {
111cb0ef41Sopenharmony_ci  'unicode-1-1-utf-8': 'utf-8',
121cb0ef41Sopenharmony_ci  'iso-8859-2': 'iso-8859-2',
131cb0ef41Sopenharmony_ci  'ascii': 'windows-1252',
141cb0ef41Sopenharmony_ci  'utf-16': 'utf-16le'
151cb0ef41Sopenharmony_ci};
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifor (const label of Object.keys(labelToName)) {
181cb0ef41Sopenharmony_ci  test(() => {
191cb0ef41Sopenharmony_ci    const stream = new TextDecoderStream(label);
201cb0ef41Sopenharmony_ci    assert_equals(stream.encoding, labelToName[label], 'encoding should match');
211cb0ef41Sopenharmony_ci  }, `encoding attribute should have correct value for '${label}'`);
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cifor (const falseValue of [false, 0, '', undefined, null]) {
251cb0ef41Sopenharmony_ci  test(() => {
261cb0ef41Sopenharmony_ci    const stream = new TextDecoderStream('utf-8', { fatal: falseValue });
271cb0ef41Sopenharmony_ci    assert_false(stream.fatal, 'fatal should be false');
281cb0ef41Sopenharmony_ci  }, `setting fatal to '${falseValue}' should set the attribute to false`);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  test(() => {
311cb0ef41Sopenharmony_ci    const stream = new TextDecoderStream('utf-8', { ignoreBOM: falseValue });
321cb0ef41Sopenharmony_ci    assert_false(stream.ignoreBOM, 'ignoreBOM should be false');
331cb0ef41Sopenharmony_ci  }, `setting ignoreBOM to '${falseValue}' should set the attribute to false`);
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cifor (const trueValue of [true, 1, {}, [], 'yes']) {
371cb0ef41Sopenharmony_ci  test(() => {
381cb0ef41Sopenharmony_ci    const stream = new TextDecoderStream('utf-8', { fatal: trueValue });
391cb0ef41Sopenharmony_ci    assert_true(stream.fatal, 'fatal should be true');
401cb0ef41Sopenharmony_ci  }, `setting fatal to '${trueValue}' should set the attribute to true`);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  test(() => {
431cb0ef41Sopenharmony_ci    const stream = new TextDecoderStream('utf-8', { ignoreBOM: trueValue });
441cb0ef41Sopenharmony_ci    assert_true(stream.ignoreBOM, 'ignoreBOM should be true');
451cb0ef41Sopenharmony_ci  }, `setting ignoreBOM to '${trueValue}' should set the attribute to true`);
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_citest(() => {
491cb0ef41Sopenharmony_ci  assert_throws_js(RangeError, () => new TextDecoderStream(''),
501cb0ef41Sopenharmony_ci                   'the constructor should throw');
511cb0ef41Sopenharmony_ci}, 'constructing with an invalid encoding should throw');
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_citest(() => {
541cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => new TextDecoderStream({
551cb0ef41Sopenharmony_ci    toString() { return {}; }
561cb0ef41Sopenharmony_ci  }), 'the constructor should throw');
571cb0ef41Sopenharmony_ci}, 'constructing with a non-stringifiable encoding should throw');
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_citest(() => {
601cb0ef41Sopenharmony_ci  assert_throws_js(Error,
611cb0ef41Sopenharmony_ci                   () => new TextDecoderStream('utf-8', {
621cb0ef41Sopenharmony_ci                     get fatal() { throw new Error(); }
631cb0ef41Sopenharmony_ci                   }), 'the constructor should throw');
641cb0ef41Sopenharmony_ci}, 'a throwing fatal member should cause the constructor to throw');
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_citest(() => {
671cb0ef41Sopenharmony_ci  assert_throws_js(Error,
681cb0ef41Sopenharmony_ci                   () => new TextDecoderStream('utf-8', {
691cb0ef41Sopenharmony_ci                     get ignoreBOM() { throw new Error(); }
701cb0ef41Sopenharmony_ci                   }), 'the constructor should throw');
711cb0ef41Sopenharmony_ci}, 'a throwing ignoreBOM member should cause the constructor to throw');
72