11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciclass ThrowingOptions {
51cb0ef41Sopenharmony_ci  constructor(whatShouldThrow) {
61cb0ef41Sopenharmony_ci    this.whatShouldThrow = whatShouldThrow;
71cb0ef41Sopenharmony_ci    this.touched = [];
81cb0ef41Sopenharmony_ci  }
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  get preventClose() {
111cb0ef41Sopenharmony_ci    this.maybeThrow('preventClose');
121cb0ef41Sopenharmony_ci    return false;
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  get preventAbort() {
161cb0ef41Sopenharmony_ci    this.maybeThrow('preventAbort');
171cb0ef41Sopenharmony_ci    return false;
181cb0ef41Sopenharmony_ci  }
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  get preventCancel() {
211cb0ef41Sopenharmony_ci    this.maybeThrow('preventCancel');
221cb0ef41Sopenharmony_ci    return false;
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  get signal() {
261cb0ef41Sopenharmony_ci    this.maybeThrow('signal');
271cb0ef41Sopenharmony_ci    return undefined;
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  maybeThrow(forWhat) {
311cb0ef41Sopenharmony_ci    this.touched.push(forWhat);
321cb0ef41Sopenharmony_ci    if (this.whatShouldThrow === forWhat) {
331cb0ef41Sopenharmony_ci      throw new Error(this.whatShouldThrow);
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst checkOrder = ['preventAbort', 'preventCancel', 'preventClose', 'signal'];
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cifor (let i = 0; i < checkOrder.length; ++i) {
411cb0ef41Sopenharmony_ci  const whatShouldThrow = checkOrder[i];
421cb0ef41Sopenharmony_ci  const whatShouldBeTouched = checkOrder.slice(0, i + 1);
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  promise_test(t => {
451cb0ef41Sopenharmony_ci    const options = new ThrowingOptions(whatShouldThrow);
461cb0ef41Sopenharmony_ci    return promise_rejects_js(
471cb0ef41Sopenharmony_ci               t, Error,
481cb0ef41Sopenharmony_ci               new ReadableStream().pipeTo(new WritableStream(), options),
491cb0ef41Sopenharmony_ci               'pipeTo should reject')
501cb0ef41Sopenharmony_ci        .then(() => assert_array_equals(
511cb0ef41Sopenharmony_ci            options.touched, whatShouldBeTouched,
521cb0ef41Sopenharmony_ci            'options should be touched in the right order'));
531cb0ef41Sopenharmony_ci  }, `pipeTo should stop after getting ${whatShouldThrow} throws`);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  test(() => {
561cb0ef41Sopenharmony_ci    const options = new ThrowingOptions(whatShouldThrow);
571cb0ef41Sopenharmony_ci    assert_throws_js(
581cb0ef41Sopenharmony_ci        Error,
591cb0ef41Sopenharmony_ci        () => new ReadableStream().pipeThrough(new TransformStream(), options),
601cb0ef41Sopenharmony_ci        'pipeThrough should throw');
611cb0ef41Sopenharmony_ci    assert_array_equals(
621cb0ef41Sopenharmony_ci        options.touched, whatShouldBeTouched,
631cb0ef41Sopenharmony_ci        'options should be touched in the right order');
641cb0ef41Sopenharmony_ci  }, `pipeThrough should stop after getting ${whatShouldThrow} throws`);
651cb0ef41Sopenharmony_ci}
66