[Streams API] Import the web-platform-tests directly from the spec
[WebKit-https.git] / LayoutTests / streams / reference-implementation / brand-checks.html
1 <!DOCTYPE html>
2 <script src='../../resources/testharness.js'></script>
3 <script src='../../resources/testharnessreport.js'></script>
4 <script src='resources/streams-utils.js'></script>
5 <script>
6 // This is updated till https://github.com/whatwg/streams/commit/ec5ffa036308d9f6350d2946560d48cdbf090939
7
8 function fakeWritableStream() {
9   return {
10     get closed() { return Promise.resolve(); },
11     get ready() { return Promise.resolve(); },
12     get state() { return 'closed' },
13     abort(reason) { return Promise.resolve(); },
14     close() { return Promise.resolve(); },
15     write(chunk) { return Promise.resolve(); }
16   };
17 }
18
19 function realReadableStream() {
20     return new ReadableStream();
21 }
22
23 function getterRejects(test, obj, getterName, target, endTest) {
24     var getter = Object.getOwnPropertyDescriptor(obj, getterName).get;
25
26     getter.call(target).then(
27         test.step_func(function() { assert_unreached(getterName + ' should not fulfill'); }),
28         test.step_func(function(e) {
29             assert_throws(new TypeError(), function() { throw e; }, getterName + ' should reject with a TypeError');
30             if (endTest === true) {
31                 test.done();
32             }
33         }));
34 }
35
36 function methodRejects(test, obj, methodName, target, endTest) {
37     var method = obj[methodName];
38
39     method.call(target).then(
40         test.step_func(function() { assert_unreached(methodName + ' should not fulfill'); }),
41         test.step_func(function(e) {
42             assert_throws(new TypeError(), function() { throw e; }, methodName + ' should reject with a TypeError');
43             if (endTest === true) {
44                 test.done();
45             }
46         }));
47 }
48
49 function getterThrows(obj, getterName, target) {
50   var getter = Object.getOwnPropertyDescriptor(obj, getterName).get;
51
52     assert_throws(new TypeError(), function() { getter.call(target); }, getterName + ' should throw a TypeError');
53 }
54
55 var test6 = async_test('WritableStream.prototype.closed enforces a brand check');
56 test6.step(function() {
57     getterRejects(test6, WritableStream.prototype, 'closed', fakeWritableStream());
58     getterRejects(test6, WritableStream.prototype, 'closed', realReadableStream(), true);
59 });
60
61 var test7 = async_test('WritableStream.prototype.ready enforces a brand check');
62 test7.step(function() {
63     getterRejects(test7, WritableStream.prototype, 'ready', fakeWritableStream());
64     getterRejects(test7, WritableStream.prototype, 'ready', realReadableStream(), true);
65 });
66
67 test(function() {
68     getterThrows(WritableStream.prototype, 'state', fakeWritableStream());
69     getterThrows(WritableStream.prototype, 'state', realReadableStream());
70 }, 'WritableStream.prototype.state enforces a brand check');
71
72 var test8 = async_test('WritableStream.prototype.abort enforces a brand check');
73 test8.step(function() {
74     methodRejects(test8, WritableStream.prototype, 'abort', fakeWritableStream());
75     methodRejects(test8, WritableStream.prototype, 'abort', realReadableStream(), true);
76 });
77
78 var test9 = async_test('WritableStream.prototype.write enforces a brand check');
79 test9.step(function() {
80     methodRejects(test9, WritableStream.prototype, 'write', fakeWritableStream());
81     methodRejects(test9, WritableStream.prototype, 'write', realReadableStream(), true);
82 });
83
84 var test10 = async_test('WritableStream.prototype.close enforces a brand check');
85 test10.step(function() {
86     methodRejects(test10, WritableStream.prototype, 'close', fakeWritableStream());
87     methodRejects(test10, WritableStream.prototype, 'close', realReadableStream(), true);
88 });
89 </script>