1// META: global=window,worker
2
3'use strict';
4
5// Verify that constructor arguments are correctly reflected in the attributes.
6
7// Mapping of the first argument to TextDecoderStream to the expected value of
8// the encoding attribute. We assume that if this subset works correctly, the
9// rest probably work too.
10const labelToName = {
11  'unicode-1-1-utf-8': 'utf-8',
12  'iso-8859-2': 'iso-8859-2',
13  'ascii': 'windows-1252',
14  'utf-16': 'utf-16le'
15};
16
17for (const label of Object.keys(labelToName)) {
18  test(() => {
19    const stream = new TextDecoderStream(label);
20    assert_equals(stream.encoding, labelToName[label], 'encoding should match');
21  }, `encoding attribute should have correct value for '${label}'`);
22}
23
24for (const falseValue of [false, 0, '', undefined, null]) {
25  test(() => {
26    const stream = new TextDecoderStream('utf-8', { fatal: falseValue });
27    assert_false(stream.fatal, 'fatal should be false');
28  }, `setting fatal to '${falseValue}' should set the attribute to false`);
29
30  test(() => {
31    const stream = new TextDecoderStream('utf-8', { ignoreBOM: falseValue });
32    assert_false(stream.ignoreBOM, 'ignoreBOM should be false');
33  }, `setting ignoreBOM to '${falseValue}' should set the attribute to false`);
34}
35
36for (const trueValue of [true, 1, {}, [], 'yes']) {
37  test(() => {
38    const stream = new TextDecoderStream('utf-8', { fatal: trueValue });
39    assert_true(stream.fatal, 'fatal should be true');
40  }, `setting fatal to '${trueValue}' should set the attribute to true`);
41
42  test(() => {
43    const stream = new TextDecoderStream('utf-8', { ignoreBOM: trueValue });
44    assert_true(stream.ignoreBOM, 'ignoreBOM should be true');
45  }, `setting ignoreBOM to '${trueValue}' should set the attribute to true`);
46}
47
48test(() => {
49  assert_throws_js(RangeError, () => new TextDecoderStream(''),
50                   'the constructor should throw');
51}, 'constructing with an invalid encoding should throw');
52
53test(() => {
54  assert_throws_js(TypeError, () => new TextDecoderStream({
55    toString() { return {}; }
56  }), 'the constructor should throw');
57}, 'constructing with a non-stringifiable encoding should throw');
58
59test(() => {
60  assert_throws_js(Error,
61                   () => new TextDecoderStream('utf-8', {
62                     get fatal() { throw new Error(); }
63                   }), 'the constructor should throw');
64}, 'a throwing fatal member should cause the constructor to throw');
65
66test(() => {
67  assert_throws_js(Error,
68                   () => new TextDecoderStream('utf-8', {
69                     get ignoreBOM() { throw new Error(); }
70                   }), 'the constructor should throw');
71}, 'a throwing ignoreBOM member should cause the constructor to throw');
72