[Streams API] Synced bad strategy test with reference implementation
[WebKit-https.git] / LayoutTests / streams / reference-implementation / bad-strategies.html
1 <!DOCTYPE html>
2 <script src='../../resources/testharness.js'></script>
3 <script src='../../resources/testharnessreport.js'></script>
4 <script>
5 test(function() {
6     var theError = new Error('a unique string');
7
8     assert_throws(theError, function() {
9         new ReadableStream({}, {
10             get size() {
11                 throw theError;
12             },
13             highWaterMark: 5
14         });
15     }, 'construction should re-throw the error');
16 }, 'Readable stream: throwing strategy.size getter');
17
18 var test1 = async_test('Readable stream: throwing strategy.size method');
19 test1.step(function() {
20     var theError = new Error('a unique string');
21     var rs = new ReadableStream(
22         {
23             start: function(c) {
24                 assert_throws(theError, function() { c.enqueue('a'); }, 'enqueue should throw the error');
25             }
26         },
27         {
28             size: function() {
29                 throw theError;
30             },
31             highWaterMark: 5
32         }
33     );
34
35     rs.getReader().closed.catch(test1.step_func(function(e) {
36         assert_equals(e, theError, 'closed should reject with the error');
37         test1.done();
38     }))
39 });
40
41 test(function() {
42     var theError = new Error('a unique string');
43
44     assert_throws(theError, function() {
45         new ReadableStream({}, {
46             size: function() {
47                 return 1;
48             },
49             get highWaterMark() {
50                 throw theError;
51             }
52         });
53     }, 'construction should re-throw the error');
54 }, 'Readable stream: throwing strategy.highWaterMark getter');
55
56 test(function() {
57     for (var highWaterMark of [-1, -Infinity]) {
58         assert_throws(new RangeError(), function() {
59             new ReadableStream({}, {
60                 size: function() {
61                     return 1;
62                 },
63                 highWaterMark
64             });
65         }, 'construction should throw a RangeError for ' + highWaterMark);
66     }
67
68     for (var highWaterMark of [NaN, 'foo', {}]) {
69         assert_throws(new TypeError(), function() {
70             new ReadableStream({}, {
71                 size: function() {
72                     return 1;
73                 },
74                 highWaterMark
75             });
76         }, 'construction should throw a TypeError for ' + highWaterMark);
77     }
78 }, 'Readable stream: invalid strategy.highWaterMark');
79
80 test(function() {
81     assert_throws(new RangeError(), function() {
82         new ReadableStream({}, {
83             size: function() {
84                 return 1;
85             },
86             highWaterMark: -1
87         });
88     }, 'construction should throw a RangeError');
89 }, 'Readable stream: negative strategy.highWaterMark');
90
91 var test2 = async_test('Readable stream: invalid strategy.size return value');
92 test2.step(function() {
93     var numberOfCalls = 0;
94     var elements = [NaN, -Infinity, +Infinity, -1];
95     var theError = [];
96     for (var i = 0; i < elements.length; i++) {
97         var rs = new ReadableStream({
98             start: function(c) {
99                 try {
100                     c.enqueue('hi');
101                     assert_unreached('enqueue didn\'t throw');
102                 } catch (error) {
103                     assert_throws(new RangeError(), function() { throw error; }, 'enqueue should throw a RangeError for ' + elements[i]);
104                     theError[i] = error;
105                 }
106             }
107         },
108         {
109             size: function() {
110                 return elements[i];
111             },
112             highWaterMark: 5
113         });
114
115         var catchFunction = function(i, e) {
116             assert_equals(e, theError[i], 'closed should reject with the error for ' + elements[i]);
117             if (++numberOfCalls, elements.length) {
118                 test2.done();
119             }
120         };
121
122         rs.getReader().closed.catch(test2.step_func(catchFunction.bind(this, i)));
123     }
124 });
125 </script>