!1864 Add promise tests

Merge pull request !1864 from udav/ptests
This commit is contained in:
openharmony_ci 2024-10-09 23:28:14 +00:00 committed by Gitee
commit cf5a4060fd
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
80 changed files with 2954 additions and 60 deletions

View File

@ -139,6 +139,7 @@ static_core/plugins/ets/stdlib/std/debug/concurrency/ @k
static_core/plugins/ets/tests/interop_js/tests/checked/ @igelhaus @semenovaleksandr @ignatenkooleg @igorlegalov @Prof1983
static_core/plugins/ets/tests/interop_js/tests/compiler/ @igelhaus @semenovaleksandr @ignatenkooleg @igorlegalov @Prof1983
static_core/plugins/ets/tests/interop_js/tests/promise/ @udav
static_core/plugins/ets/tests/sts_ts_subset/std/core/Promise* @ivan-tyulyandin @anton-sysoev @dmitriitr @konstanting @udav
/abc2program/ @ctw-ian
/assembler/ @ctw-ian

View File

@ -394,11 +394,11 @@ export class taskpool {
* @param group The task group for execution
* @returns Promise for array of results of executed tasks from the group
*/
static execute(group: taskpoolTaskGroup /*, priority?: Priority */): Promise<NullishType[]> {
static execute(group: taskpoolTaskGroup /*, priority?: Priority */): Promise<Array<NullishType>> {
let tasksCount: long = group.tasks.length as long;
if (tasksCount == 0) {
group.isSubmitted = true;
return Promise.resolve(new NullishType[0]);
return Promise.resolve(new Array<NullishType>());
}
taskpool.taskGroupSubmitted(group.id, tasksCount);
let promises = new Array<Promise<NullishType>>();

View File

@ -15,22 +15,31 @@
package std.core;
export interface PromiseLike<T> {
then<U>(onFulfilled: () => U|PromiseLike<U> throws): PromiseLike<U>;
then<U>(onFulfilled: (value: T) => U|PromiseLike<U> throws): PromiseLike<U>;
// NOTE(audovichenko): Mark parameters by '?' instead of add '|undefined'. Issue #17204
then<U, E = never>(onFulfilled: ((value: T) => U|PromiseLike<U> throws)|undefined,
onRejected: ((error: NullishType) => E|PromiseLike<E> throws)|undefined): PromiseLike<U|E>;
/**
* Interface for objects with 'then' method.
*/
export interface PromiseLike<out T> {
then<U, E = never>(onFulfilled?: (value: T) => U|PromiseLike<U>,
onRejected?: (error: NullishType) => E|PromiseLike<E>): PromiseLike<U|E>;
}
/**
* Base class for Promise.allSettled return type.
*/
class PromiseSettledResultBase {
status: string = "";
}
/**
* The class describes fulfillment result of Promise.allSettled's argument.
*/
export class PromiseFulfilledResult<T> extends PromiseSettledResultBase {
value: T;
}
/**
* The class describes rejection result of Promise.allSettled's argument.
*/
export class PromiseRejectedResult extends PromiseSettledResultBase {
reason: NullishType;
}
@ -44,7 +53,7 @@ export final class Promise<out T> implements PromiseLike<T> {
private constructor() {
}
constructor(callback: (resolve: (value: T|PromiseLike<T>) => void) => void throws) {
constructor(callback: (resolve: (value: T|PromiseLike<T>) => void) => void) {
try {
callback((value: T|PromiseLike<T>): void => {
this.doResolve<T>(value);
@ -54,12 +63,8 @@ export final class Promise<out T> implements PromiseLike<T> {
}
}
// NOTE(audovichenko): add constructor #17147
// constructor(callback: (resolve: (value: T|PromiseLike<T>) => void throws,
// reject: () => void) => void throws)
constructor(callback: (resolve: (value: T|PromiseLike<T>) => void throws,
reject: (error: NullishType) => void) => void throws) {
constructor(callback: (resolve: (value: T|PromiseLike<T>) => void,
reject: (error: NullishType) => void) => void) {
try {
callback((value: T|PromiseLike<T>): void => {
this.doResolve<T>(value);
@ -71,7 +76,7 @@ export final class Promise<out T> implements PromiseLike<T> {
}
}
then<U>(onFulfilled: () => U|PromiseLike<U> throws): Promise<U> {
then<U>(onFulfilled: () => U|PromiseLike<U>): Promise<U> {
let promise = new Promise<U>();
let fn: () => void = (): void => {
try {
@ -88,7 +93,7 @@ export final class Promise<out T> implements PromiseLike<T> {
return promise;
}
then<U>(onFulfilled: (value: T) => U|PromiseLike<U> throws): Promise<U> {
then<U>(onFulfilled: (value: T) => U|PromiseLike<U>): Promise<U> {
let promise = new Promise<U>();
let fn: () => void = (): void => {
try {
@ -105,9 +110,8 @@ export final class Promise<out T> implements PromiseLike<T> {
return promise;
}
// NOTE(audovichenko): Mark parameters by '?' instead of add '|undefined'. Issue #17204
then<U, E = never>(onFulfilled: ((value: T) => U|PromiseLike<U> throws)|undefined,
onRejected: ((error: NullishType) => E|PromiseLike<E> throws)|undefined): Promise<U|E> {
then<U, E = never>(onFulfilled?: (value: T) => U|PromiseLike<U>,
onRejected?: (error: NullishType) => E|PromiseLike<E>): Promise<U|E> {
let promise = new Promise<U|E>();
let fn: () => void = (): void => {
try {
@ -132,7 +136,26 @@ export final class Promise<out T> implements PromiseLike<T> {
return promise;
}
catch<U = never>(onRejected?: (error: NullishType) => U|PromiseLike<U> throws): Promise<T|U> {
// NOTE(audovichenko): Add '?' to onRejection param. Issue #20001
catch<U = never>(onRejected: () => U|PromiseLike<U>): Promise<T|U> {
let promise = new Promise<T|U>();
let fn: () => void = (): void => {
try {
if (this.state == Promise.STATE_REJECTED) {
let res: U|PromiseLike<U> = onRejected();
promise.doResolve<U>(res);
} else {
promise.doResolve<T>(this.value!);
}
} catch (e) {
promise.rejectImpl(e);
}
}
this.submitCallback(fn as Object);
return promise;
}
catch<U = never>(onRejected?: (error: NullishType) => U|PromiseLike<U>): Promise<T|U> {
let promise = new Promise<T|U>();
let fn: () => void = (): void => {
try {
@ -154,7 +177,7 @@ export final class Promise<out T> implements PromiseLike<T> {
return promise;
}
finally(onFinally?: () => void throws): Promise<T> {
finally(onFinally?: () => void): Promise<T> {
let promise = new Promise<T>();
let fn: () => void = (): void => {
try {
@ -185,6 +208,9 @@ export final class Promise<out T> implements PromiseLike<T> {
}
static resolve<U>(value: U|PromiseLike<U>): Promise<U> {
if (value instanceof Promise) {
return value as Promise<U>;
}
let p = new Promise<U>();
p.doResolve(value);
return p;
@ -196,21 +222,24 @@ export final class Promise<out T> implements PromiseLike<T> {
return p;
}
static reject<U = never>(value: U): Promise<U> {
static reject<U = never>(value: NullishType): Promise<U> {
let p = new Promise<U>();
p.rejectImpl(value);
return p;
}
static all<U>(promises: (U|PromiseLike<U>)[]): Promise<U[]> {
return new Promise<U[]>((resolve: (value: U[]) => void, reject: (error: NullishType) => void): void => {
// This temp object is needed because es2panda cannot change captured primitives
static all<U>(promises: (U|PromiseLike<U>)[]): Promise<Array<U>> {
return new Promise<Array<U>>((resolve: (value: Array<U>) => void, reject: (error: NullishType) => void): void => {
new AllPromiseConcurrency<U>(resolve, reject).all(promises);
});
}
static all<U>(promises: Iterable<U|PromiseLike<U>>): Promise<U[]> {
return Promise.all<U>(Promise.toArray(promises));
static all<U>(promises: Iterable<U|PromiseLike<U>>): Promise<Array<U>> {
try {
return Promise.all<U>(Promise.toArray(promises));
} catch (e: Error) {
return Promise.reject<Array<U>>(e);
}
}
static allSettled<U>(promises: (U|PromiseLike<U>)[]): Promise<PromiseSettledResult<U>[]> {
@ -221,7 +250,11 @@ export final class Promise<out T> implements PromiseLike<T> {
}
static allSettled<U>(promises: Iterable<U|PromiseLike<U>>): Promise<PromiseSettledResult<U>[]> {
return Promise.allSettled<U>(Promise.toArray(promises));
try {
return Promise.allSettled<U>(Promise.toArray(promises));
} catch (e: Error) {
return Promise.reject<PromiseSettledResult<U>[]>(e);
}
}
static any<U>(promises: (U|PromiseLike<U>)[]): Promise<U> {
@ -232,7 +265,11 @@ export final class Promise<out T> implements PromiseLike<T> {
}
static any<U>(promises: Iterable<U|PromiseLike<U>>): Promise<U> {
return Promise.any<U>(Promise.toArray(promises));
try {
return Promise.any<U>(Promise.toArray(promises));
} catch (e: Error) {
return Promise.reject<U>(e);
}
}
static race<U>(promises: (U|PromiseLike<U>)[]): Promise<U> {
@ -243,7 +280,11 @@ export final class Promise<out T> implements PromiseLike<T> {
}
static race<U>(promises: Iterable<U|PromiseLike<U>>): Promise<U> {
return Promise.race<U>(Promise.toArray(promises));
try {
return Promise.race<U>(Promise.toArray(promises));
} catch (e: Error) {
return Promise.reject<U>(e);
}
}
private static toArray<U>(values: Iterable<U>): U[] {
@ -345,16 +386,17 @@ class PromiseConcurrencyBase<T> {
}
class AllPromiseConcurrency<T> extends PromiseConcurrencyBase<T> {
constructor(resolve: (value: T[]) => void, reject: (error: NullishType) => void) {
constructor(resolve: (value: Array<T>) => void, reject: (error: NullishType) => void) {
this.resolve = resolve;
this.reject = reject;
}
all(promises: (T|PromiseLike<T>)[]): void {
this.values = new T[promises.length];
if (promises.length == 0) {
this.resolve(this.values!);
this.resolve(this.values);
return;
} else {
this.values = new Array<T>(promises.length);
}
for (let i = 0; i < promises.length; ++i) {
let idx = i;
@ -367,7 +409,7 @@ class AllPromiseConcurrency<T> extends PromiseConcurrencyBase<T> {
}
private resolveImpl(value: T, idx: int): void {
this.values![idx] = value;
this.values[idx] = value;
++this.resolvedCnt;
if (this.resolvedCnt == this.values!.length) {
this.resolve(this.values!);
@ -376,8 +418,8 @@ class AllPromiseConcurrency<T> extends PromiseConcurrencyBase<T> {
private resolvedCnt = 0;
private rejectedCnt = 0;
private values: T[]|null = null;
private resolve: (value: T[]) => void;
private values: Array<T> = new Array<T>();
private resolve: (value: Array<T>) => void;
private reject: (error: NullishType) => void;
}

View File

@ -448,8 +448,7 @@ async function testAsyncVoidNothing() {}
function testPromiseAllEmpty(): void {
globalTest = new Test();
let promises: Promise<Object>[] = [];
Promise.all(promises).then<void>((v: Object): void => {
let values: Object[] = v as Object[];
Promise.all(promises).then<void>((values: Array<Object>): void => {
if (values == null || values.length != 0) {
globalTest!.fail();
} else {
@ -466,7 +465,7 @@ function testPromiseAllResolved(): void {
Promise.all<Object>([
p1, p2, p3
]).then<void>((values: Object[]): void => {
]).then<void>((values: Array<Object>): void => {
if (values.length != 3) {
globalTest!.fail();
}
@ -509,7 +508,7 @@ function testPromiseAllRawValues(): void {
Promise.all<Object>([
p1, p2, p3
]).then<void>((values: Object[]): void => {
]).then<void>((values: Array<Object>): void => {
if (values.length != 3) {
globalTest!.fail();
}
@ -533,7 +532,7 @@ function testPromiseAllIterable(): void {
array.push("def");
array.push(Promise.resolve<Object>("xyz"));
Promise.all<Object>(array).then<void>((values: Object[]): void => {
Promise.all<Object>(array).then<void>((values: Array<Object>): void => {
if (values.length != 3) {
globalTest!.fail();
}

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let reject: ((err: Object)=>void)|null = null;
let error = new Error();
let p1 = Promise.resolve<number>(3);
let p2 = new Promise<number>((_: (value:number)=>void, _reject: (err: Object)=>void) => {
reject = _reject;
});
Promise.all([p1, p2]).then<void, void>((values: Array<number>): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != error) {
console.log("Test failed. The promise should be rejected by the specified value.");
return;
}
console.log("Test passed.");
});
reject!(error);

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,20 +13,13 @@
* limitations under the License.
*/
function main() {
let p = new Promise<number>(
(resolve: (v: number)=>void, _: (error: Object)=>void)=>{resolve(7);}
);
p.then<void, void>((value:number):void => {
if(value != 7) {
console.log("Test failed.");
return;
let arg: Object[] = [];
Promise.all(arg).then<void, void>((value: Array<Object>): void => {
if (value.length != 0) {
console.log("Test failed. The array should be empty.");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed.");
});
return 0;
}
main();
}, (err: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PoisonedIterable<T> implements Iterable<T> {
[Symbol.iterator](): Iterator<T> {
throw new Error("error");
}
}
Promise.all<Object>(new PoisonedIterable<Object>()).then<void, void>((value: Array<Object>): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (!(err instanceof Error)) {
console.log("Test failed. The promise should be rejected by the Error.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PoisonedIterator<T> implements Iterator<T> {
constructor(err: Error) {
this.error = err;
}
next(): IteratorResult<T> {
throw new Error("error");
}
error: Error;
}
class MyIterable<T> implements Iterable<T> {
constructor(err: Error) {
this.error = err;
}
[Symbol.iterator](): Iterator<T> {
return new PoisonedIterator<T>(this.error);
}
error: Error;
}
let error = new Error();
Promise.all<Object>(new MyIterable<Object>(error)).then<void, void>((value: Array<Object>): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != error) {
console.log("Test failed. The promise should be rejected by the Error.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
then<U, E>(_?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve<E>(onRejected!("abc"));
}
};
let thenable = new Thenable();
Promise.all([thenable]).then<void, void>((value: Array<string>): void => {
console.log("Test failed. The promise should not be resolved.");
}, (error: Object): void => {
if (error != "abc") {
console.log("Test failed. The promise should be rejected by the provided value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let reject: ((err: Object)=>void)|null = null;
let error = new Error();
let p1 = Promise.resolve<number>(3);
let p2 = new Promise<number>((_: (value:number)=>void, _reject: (err: Object)=>void) => {
reject = _reject;
});
Promise.all([p1, p2]).then<void, void>((values: Array<number>): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != error) {
console.log("Test failed. The promise should be rejected by the specified value.");
return;
}
console.log("Test passed.");
});
reject!(error);

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Fulfill implements PromiseLike<string> {
then<U, E>(onFulfill?: (value: string) => U|PromiseLike<U>, _?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<U>((): U|PromiseLike<U> => {
return onFulfill!("abc");
});
}
}
class Reject implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<E>((): E|PromiseLike<E> => {
onFulfilled!("def");
return onRejected!("xyz");
});
}
}
let thenable: PromiseLike<string>[] = [new Fulfill(), new Reject()];
Promise.all(thenable).then<void, void>((values: Array<string>): void => {
if (values.length != 2) {
console.log("Test failed. Expected a string array of length 2 but got length " + values.length + ".");
return;
}
if (values[0] != "abc" || values[1] != "def") {
console.log("Test failed. The promise should be resolved by the specified values.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let v1 = new Object();
let v2 = new Object();
let v3 = new Object();
Promise.all([v1, v2, v3]).then<void, void>((values: Array<Object>): void => {
if (values.length != 3) {
console.log("Test failed. Expected object array of length 3 but get length " + values.length + ".");
return;
}
if (values[0] != v1) {
console.log("Test failed. Unexpected value at index 0.");
return;
}
if (values[1] != v2) {
console.log("Test failed. Unexpected value at index 0.");
return;
}
if (values[2] != v3) {
console.log("Test failed. Unexpected value at index 0.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let p1 = Promise.resolve<number>(1);
let p2 = Promise.resolve<number>(2);
sequence.push(1);
p1.then<void>((): void => {
sequence.push(3);
checkSequence(sequence, 3);
});
Promise.all<number>([p1, p2]).then<void>((): void => {
sequence.push(5);
checkSequence(sequence, 5);
console.log("Test passed.");
})
p2.then<void>((): void => {
sequence.push(4);
checkSequence(sequence, 4);
});
sequence.push(2);

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let array: Promise<string>[] = [];
Promise.allSettled(array).then<void, void>((values: PromiseSettledResult<string>[]): void => {
if (values.length != 0) {
console.log("Test failed. Expected an empty string array but got length " + values.length + ".");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let r1 = Promise.reject<string>("rejected 0")
let f1 = Promise.resolve<string>("fulfilled 1");
let r2 = Promise.reject<string>("rejected 2");
let f2 = Promise.resolve<string>("fulfilled 3");
let r3 = Promise.reject<string>("rejected 4");
let f3 = Promise.resolve<string>("fulfilled 5");
Promise.allSettled([r1, f1, r2, f2, r3, f3]).then<void, void>((values: PromiseSettledResult<string>[]): void => {
if (values.length != 6) {
console.log("Test failed. Expected a string array of length 6 but got length " + values.length + ".");
return;
}
if (values[0].status != "rejected" || values[2].status != "rejected" || values[4].status != "rejected") {
console.log("Test failed. Expected elements 0, 2, 4 have status 'rejected'.");
return;
}
if (values[1].status != "fulfilled" || values[3].status != "fulfilled" || values[5].status != "fulfilled") {
console.log("Test failed. Expected elements 1, 3, 5 have status 'fulfilled'.");
return;
}
if ((values[0] as PromiseRejectedResult).reason != "rejected 0") {
console.log("Test failed. Unexpected value of the element 0.");
return;
}
if ((values[1] as PromiseFulfilledResult<string>).value != "fulfilled 1") {
console.log("Test failed. Unexpected value of the element 1.");
return;
}
if ((values[2] as PromiseRejectedResult).reason != "rejected 2") {
console.log("Test failed. Unexpected value of the element 2.");
return;
}
if ((values[3] as PromiseFulfilledResult<string>).value != "fulfilled 3") {
console.log("Test failed. Unexpected value of the element 3.");
return;
}
if ((values[4] as PromiseRejectedResult).reason != "rejected 4") {
console.log("Test failed. Unexpected value of the element 4.");
return;
}
if ((values[5] as PromiseFulfilledResult<string>).value != "fulfilled 5") {
console.log("Test failed. Unexpected value of the element 5.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PoisonedIterable<T> implements Iterable<T> {
constructor(error: Error) {
this.error = error;
}
[Symbol.iterator](): Iterator<T> {
throw this.error;
}
private error: Error;
}
let error = new Error();
Promise.allSettled<Object>(new PoisonedIterable<Object>(error)).then<void, void>((value: PromiseSettledResult<Object>[]): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != error) {
console.log("Test failed. The promise should be rejected by the provided error.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PoisonedIterator<T> implements Iterator<T> {
constructor(error: Error) {
this.error = error;
}
next(): IteratorResult<T> {
throw this.error;
}
private error: Error;
}
class MyIterable<T> implements Iterable<T> {
constructor(error: Error) {
this.error = error;
}
[Symbol.iterator](): Iterator<T> {
return new PoisonedIterator<T>(this.error);
}
private error: Error;
}
let error = new Error();
Promise.allSettled<Object>(new MyIterable<Object>(error)).then<void, void>((value: PromiseSettledResult<Object>[]): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != error) {
console.log("Test failed. The promise should be rejected by the provided error.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
constructor(error: Error) {
this.error = error;
}
then<U, E>(_?: (value: string) => U|PromiseLike<U>, __?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
throw this.error;
}
private error: Error;
};
let error = new Error();
let thenable = new Thenable(error);
Promise.allSettled<string>([thenable]).then<void, void>((result: PromiseSettledResult<string>[]): void => {
if (result.length != 1) {
console.log("Test failed. Expected array of length 1");
return;
}
if (result[0].status != "rejected") {
console.log("Test failed. Expected status 'rejected', but got ' + result[0].status + '.");
return;
}
if ((result[0] as PromiseRejectedResult).reason != error) {
console.log("Test failed. Unexpected value.");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
then<U, E>(_?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.reject<E>(onRejected!("xyz") as Object);
}
}
let thenable = new Thenable();
Promise.allSettled([thenable]).then<void, void>((values: PromiseSettledResult<string>[]): void => {
if (values.length != 1) {
console.log("Test failed. Expected a string array of length 1 but got length " + values.length + ".");
return;
}
if (values[0].status != "rejected") {
console.log("Test failed. Expected all elements has status 'rejected'.");
return;
}
if ((values[0] as PromiseRejectedResult).reason != "abc") {
console.log("Test failed. Unexpected reason of the first elements.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let r1 = Promise.reject<string>("rejected 0")
let r2 = Promise.reject<string>("rejected 1");
let r3 = Promise.reject<string>("rejected 2");
Promise.allSettled([r1, r2, r3]).then<void, void>((values: PromiseSettledResult<string>[]): void => {
if (values.length != 3) {
console.log("Test failed. Expected a string array of length 3 but got length " + values.length + ".");
return;
}
if (values[0].status != "rejected" || values[1].status != "rejected" || values[2].status != "rejected") {
console.log("Test failed. Expected elements 0, 2, 4 have status 'rejected'.");
return;
}
if ((values[0] as PromiseRejectedResult).reason != "rejected 0") {
console.log("Test failed. Unexpected value of the element 0.");
return;
}
if ((values[1] as PromiseRejectedResult).reason != "rejected 1") {
console.log("Test failed. Unexpected value of the element 1.");
return;
}
if ((values[2] as PromiseRejectedResult).reason != "rejected 2") {
console.log("Test failed. Unexpected value of the element 2.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
then<U, E>(_?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<E>((): E|PromiseLike<E> => {
return onRejected!("abc");
});
}
};
let thenable = new Thenable();
Promise.allSettled([thenable]).then<void, void>((values: PromiseSettledResult<string>[]): void => {
if (values.length != 1) {
console.log("Test failed. Expected an array of length 1 but got length " + values.length + ".");
return;
}
if (values[0].status != "rejected") {
console.log("Test failed. Expected the first element has status 'rejected' but got '" + values[0].status + "'.");
return;
}
if ((values[0] as PromiseRejectedResult).reason != "abc") {
console.log("Test failed. The promise should be rejected by the provided value.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Fulfill implements PromiseLike<string> {
then<U, E>(onFulfill?: (value: string) => U|PromiseLike<U>, _?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve<U>(onFulfill!("abc"));
}
}
class Reject implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
onFulfilled!("def");
return Promise.reject<E>(onRejected!("xyz") as Object);
}
}
let thenable: PromiseLike<string>[] = [new Fulfill(), new Reject()];
Promise.allSettled(thenable).then<void, void>((values: PromiseSettledResult<string>[]): void => {
if (values.length != 2) {
console.log("Test failed. Expected a string array of length 2 but got length " + values.length + ".");
return;
}
if (values[0].status != "fulfilled" || values[1].status != "fulfilled") {
console.log("Test failed. Expected all elements has status 'fulfilled'.");
return;
}
let v: string = (values[0] as PromiseFulfilledResult<string>).value;
if (v != "abc") {
console.log("Test failed. Expected the first elements has value 'abc' but got ' + v + '.");
return;
}
v = (values[1] as PromiseFulfilledResult<string>).value;
if (v != "def") {
console.log("Test failed. Expected the first elements has value 'abc' but got ' + v + '.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Fulfill implements PromiseLike<string> {
then<U, E>(onFulfill?: (value: string) => U|PromiseLike<U>, _?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<U>((): U|PromiseLike<U> => {
return onFulfill!("abc");
});
}
}
class Reject implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<E>((): E|PromiseLike<E> => {
onFulfilled!("def");
return onRejected!("xyz");
});
}
}
let thenable: PromiseLike<string>[] = [new Fulfill(), new Reject()];
Promise.allSettled(thenable).then<void, void>((values: PromiseSettledResult<string>[]): void => {
if (values.length != 2) {
console.log("Test failed. Expected a string array of length 2 but got length " + values.length + ".");
return;
}
if (values[0].status != "fulfilled" || values[1].status != "fulfilled") {
console.log("Test failed. Expected all elements has status 'fulfilled'.");
return;
}
let v: string = (values[0] as PromiseFulfilledResult<string>).value;
if (v != "abc") {
console.log("Test failed. Expected the first elements has value 'abc' but got ' + v + '.");
return;
}
v = (values[1] as PromiseFulfilledResult<string>).value;
if (v != "def") {
console.log("Test failed. Expected the first elements has value 'abc' but got ' + v + '.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let p1 = Promise.resolve<string>("abc");
let p2 = Promise.resolve<string>("def");
let p3 = Promise.resolve<string>("xyz");
Promise.allSettled([p1, p2, p3]).then<void, void>((values: PromiseSettledResult<string>[]): void => {
if (values.length != 3) {
console.log("Test failed. Expected a string array of length 3 but got length " + values.length + ".");
return;
}
if (values[0].status != "fulfilled" || values[1].status != "fulfilled" || values[2].status != "fulfilled") {
console.log("Test failed. Expected all elements have status 'fulfilled'.");
return;
}
if ((values[0] as PromiseFulfilledResult<string>).value != "abc") {
console.log("Test failed. Unexpected value of the element 0.");
return;
}
if ((values[1] as PromiseFulfilledResult<string>).value != "def") {
console.log("Test failed. Unexpected value of the element 1.");
return;
}
if ((values[2] as PromiseFulfilledResult<string>).value != "xyz") {
console.log("Test failed. Unexpected value of the element 2.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let array: Promise<string>[] = [];
Promise.any(array).then<void, void>((value: string): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: AggregateError): void => {
if (err.errors.length != 0) {
console.log("Test failed. Expected errors count 0 but got " + err.errors.length + ".");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PoisonedIterable<T> implements Iterable<T> {
constructor(err: Error) {
this.error = err;
}
[Symbol.iterator](): Iterator<T> {
throw this.error;
}
private error: Error;
}
let error = new Error();
Promise.any<Object>(new PoisonedIterable<Object>(error)).then<void, void>((value: Object): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != error) {
console.log("Test failed. The promise should be rejected by the specified value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PoisonedIterator<T> implements Iterator<T> {
constructor(err: Error) {
this.error = err;
}
next(): IteratorResult<T> {
throw this.error;
}
private error: Error;
}
class MyIterable<T> implements Iterable<T> {
constructor(err: Error) {
this.error = err;
}
[Symbol.iterator](): Iterator<T> {
return new PoisonedIterator<T>(this.error);
}
private error: Error;
}
let error = new Error();
Promise.any<Object>(new MyIterable<Object>(error)).then<void, void>((value: Object): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Error): void => {
if (err != error) {
console.log("Test failed. The promise should be rejected by the Error.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
constructor(error: Error) {
this.error = error;
}
then<U, E>(_?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
throw this.error;
}
private error: Error;
};
let error = new Error();
let thenable = new Thenable(error);
Promise.any([thenable]).then<void, void>((value: string): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: AggregateError): void => {
if (err.errors.length != 1) {
console.log("Test failed. Expected errors count 1 but got " + err.errors.length + ".");
return;
}
if (err.errors[0] != error) {
console.log("Test failed. The promise should be rejected by the provided value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let error = new Error();
Promise.any<Error>([Promise.reject<Error>(error)]).then<void, void>((err: Error): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: AggregateError): void => {
if (err.errors.length != 1) {
console.log("Test failed. Expected errors count 1 but got " + err.errors.length + ".");
return;
}
if (err.errors[0] != error) {
console.log("Test failed. The error should contain the specified value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let error = new Error();
class Thenable implements PromiseLike<Error> {
then<U, E>(_?: (value: Error) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<E>((): E|PromiseLike<E> => {
return onRejected!(error);
});
}
};
Promise.any([new Thenable()]).then<void, void>((values: Error): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: AggregateError): void => {
if (err.errors.length != 1) {
console.log("Test failed. Expected errors count 1 but got " + err.errors.length + ".");
return;
}
if (err.errors[0] != error) {
console.log("Test failed. The promise should be rejected by the specified value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Fulfill implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, _?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return new Promise<U>((resolve: (value: U|PromiseLike<U>) => void) => {
resolve(onFulfilled!("abc"));
});
}
}
class Reject implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return new Promise<U>((resolve: (value: U|PromiseLike<U>) => void) => {
let v = onFulfilled!("def");
onRejected!("xyz");
resolve(v);
});
}
}
let thenable: PromiseLike<string>[] = [new Fulfill(), new Reject()];
Promise.any(thenable).then<void, void>((value: string): void => {
if (value != "abc") {
console.log("Test failed. The promise should be resolved by the specified value.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Fulfill implements PromiseLike<string> {
then<U, E>(onFulfill?: (value: string) => U|PromiseLike<U>, _?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<U>((): U|PromiseLike<U> => {
return onFulfill!("abc");
});
}
}
class Reject implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<E>((): E|PromiseLike<E> => {
onFulfilled!("def");
return onRejected!("xyz");
});
}
}
let fulfill = new Fulfill();
let reject = new Reject();
let thenable: PromiseLike<string>[] = [new Fulfill(), new Reject()];
Promise.any(thenable).then<void, void>((value: string): void => {
if (value != "abc") {
console.log("Test failed. The promise should be resolved by the specified value.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let error = new Error();
class Thenable implements PromiseLike<Error> {
then<U, E>(_?: (value: Error) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve<E>(onRejected!(error));
}
}
Promise.any([new Thenable()]).then<void, void>((value: Error): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: AggregateError): void => {
if (err.errors.length != 1) {
console.log("Test failed. Expected errors count 1 but got " + err.errors.length + ".");
return;
}
if (err.errors[0] != error) {
console.log("Test failed. The promise should be rejected by the specified value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Promise.any(["a", "b", "c"]).then<void, void>((value: string): void => {
if (value != "a") {
console.log("Test failed. Expected value 'a' but got '" + value + "'.");
return;
}
console.log("Test passed.");
}, (err: AggregateError): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Resolve implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, _?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<U>((): U|PromiseLike<U> => {
return onFulfilled!("abc");
})
}
}
class LateReject implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<U>((): U|PromiseLike<U> => {
let v = onFulfilled!("def");
onRejected!("xyz");
return v;
});
}
}
let thenable: PromiseLike<string>[] = [new Resolve(), new LateReject()];
Promise.any(thenable).then<void, void>((value: string): void => {
if (value != "abc") {
console.log("Test failed. The promise should be resolve by the specified value.");
return;
}
console.log("Test passed.");
}, (err: AggregateError): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let p1 = Promise.resolve<string>("abc");
let p2 = Promise.resolve<string>("def");
sequence.push(1);
p1.then<void>((): void => {
sequence.push(3);
checkSequence(sequence, 3);
});
Promise.any([p1, p2]).then<void>((): void => {
sequence.push(5);
checkSequence(sequence, 5);
console.log("Test passed.");
});
p2.then<void>((): void => {
sequence.push(4);
checkSequence(sequence, 4);
});
sequence.push(2);

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let p1 = Promise.reject<number>(1);
let p2 = Promise.resolve<number>(2);
let p3 = Promise.reject<number>(3);
sequence.push(1);
p1.catch<void>((err: Object): void => {
sequence.push(3);
checkSequence(sequence, 3);
});
Promise.any([p1, p2, p3]).then<void>((): void => {
sequence.push(6);
checkSequence(sequence, 6);
console.log("Test passed.");
});
p2.then<void>((): void => {
sequence.push(4);
checkSequence(sequence, 4);
});
sequence.push(2);
p3.catch<void>((err: Object): void => {
sequence.push(5);
checkSequence(sequence, 5);
});

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let p = Promise.resolve<number>(1);
sequence.push(1);
Promise.any([p]).then<void>((): void => {
sequence.push(4);
checkSequence(sequence, 4);
});
p.then<void>((): void => {
sequence.push(3);
checkSequence(sequence, 3);
}).then<void>((): void => {
sequence.push(5);
checkSequence(sequence, 5);
console.log("Test passed.");
});
sequence.push(2);

View File

@ -0,0 +1,67 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let p1Err = new Error("abc");
let p2Err = new Error("def");
let p1 = Promise.reject<Error>(p1Err);
let p2 = Promise.reject<Error>(p2Err);
sequence.push(1);
p1.catch<void>((): void => {
sequence.push(3);
checkSequence(sequence, 3);
});
Promise.any<Error>([p1, p2]).then<number>((): number => {
sequence.push(5);
checkSequence(sequence, 5);
return 0;
}).then<void, void>((value: number): void => {}, (err: AggregateError): void => {
if (err.errors.length != 2) {
console.log("Test failed. Expected number of errors 2, but got " + err.errors.length);
throw new Error("Test failed");
}
if (err.errors[0] != p1Err) {
console.log("Test failed. Expected the first error is 'abc', but got " + err.errors[0]);
throw new Error("Test failed");
}
if (err.errors[1] != p2Err) {
console.log("Test failed. Expected the first error is 'def', but got " + err.errors[1]);
throw new Error("Test failed");
}
console.log("Test passed.");
});
p2.catch<void>((): void => {
sequence.push(4);
checkSequence(sequence, 4);
});
sequence.push(2);

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve(onFulfilled!("abc"));
}
};
function executor(resolve: (value: string|PromiseLike<string>) => void): void {
resolve(new Thenable());
throw new Error("ignored exception");
}
new Promise<string>(executor).then<void, void>((value: string): void => {
if (value != "abc") {
console.log("Test failed. Expected value 'abc' but got " + value + ".");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let array: Object[] = [];
Promise.race<Object>(array).then<void, void>((value: Object): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Error): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PoisonedIterable<T> implements Iterable<T> {
constructor(err: Error) {
this.error = err;
}
[Symbol.iterator](): Iterator<T> {
throw this.error;
}
private error: Error;
}
let error = new Error();
Promise.race<Object>(new PoisonedIterable<Object>(error)).then<void, void>((value: Object): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != error) {
console.log('Test failed. The promise should be rejected by the specified value.');
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PoisonedIterator<T> implements Iterator<T> {
constructor(err: Error) {
this.error = err;
}
next(): IteratorResult<T> {
throw this.error;
}
private error: Error;
}
class MyIterable<T> implements Iterable<T> {
constructor(err: Error) {
this.error = err;
}
[Symbol.iterator](): Iterator<T> {
return new PoisonedIterator<T>(this.error);
}
private error: Error;
}
let error = new Error();
Promise.race<Object>(new MyIterable<Object>(error)).then<void, void>((value: Object): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Error): void => {
if (err != error) {
console.log('Test failed. The promise should be rejected by the Error.');
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
constructor(error: Error) {
this.error = error;
}
then<U, E>(_?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
throw this.error;
}
private error: Error;
};
let error = new Error();
let thenable = new Thenable(error);
Promise.race([thenable]).then<void, void>((value: string): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != error) {
console.log("Test failed. The promise should be rejected by the provided value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
then<U, E>(_?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<E>((): E|PromiseLike<E> => {
return onRejected!("abc");
});
}
};
Promise.race([new Thenable()]).then<void, void>((values: string): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != "abc") {
console.log("Test failed. The promise should be rejected by the specified value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Fulfill implements PromiseLike<string> {
then<U, E>(onFulfill?: (value: string) => U|PromiseLike<U>, _?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<U>((): U|PromiseLike<U> => {
return onFulfill!("abc");
});
}
}
class Reject implements PromiseLike<string> {
then<U, E>(_?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve().then<E>((): E|PromiseLike<E> => {
return onRejected!("xyz");
});
}
}
let thenable: PromiseLike<string>[] = [new Fulfill(), new Reject()];
Promise.race(thenable).then<void, void>((value: string): void => {
if (value != "abc") {
console.log("Test failed. The promise should be resolved by the specified value.");
return;
}
console.log("Test passed.");
}, (error: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
then<U, E>(_?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve<E>(onRejected!("abc"));
}
}
Promise.race([new Thenable()]).then<void, void>((value: string): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != "abc") {
console.log("Test failed. The promise should be rejected by the specified value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let resolve: ((value: string) => void)|null = null;
let reject: ((err: Object) => void)|null = null;
let p1 = new Promise<string>((_resolve: (v: string)=>void): void => {
resolve = _resolve;
});
let p2 = new Promise<string>((_: (v: string)=>void, _reject: (err: Object)=>void): void => {
reject = _reject;
});
reject!(new Error());
resolve!("abc");
Promise.race<string>([p1, p2]).then<void, void>((value: Object): void => {
if (value != "abc") {
console.log("Test failed. The promise should be resolved by the specified value.");
return;
}
console.log("Test passed.");
}, (err: Error): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let resolve: ((value: string) => void)|null = null;
let reject: ((err: Object) => void)|null = null;
let p1 = new Promise<string>((_resolve: (v: string)=>void): void => {
resolve = _resolve;
});
let p2 = new Promise<string>((_: (v: string)=>void, _reject: (err: Object)=>void): void => {
reject = _reject;
});
Promise.race<string>([p1, p2]).then<void, void>((value: Object): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != "xyz") {
console.log("Test failed. The promise should be rejected by the specified value.");
return;
}
console.log("Test passed.");
});
reject!("xyz");
resolve!("abc");

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
constructor(err: Error) {
this.error = err;
}
then<U, E>(_?: (value: string) => U|PromiseLike<U>, __?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
throw this.error;
}
private error: Error;
};
let error = new Error();
let thenable = new Thenable(error);
Promise.race([thenable]).then<void, void>((value: string): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err != error) {
console.log("Test failed. The promise should be rejected by the provided value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, _?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return new Promise<U>((resolve: (val: U|PromiseLike<U>) => void): void => {
resolve(onFulfilled!("abc"));
})
}
};
let thenable = new Thenable();
Promise.race([thenable]).then<void, void>((value: string): void => {
if (value != "abc") {
console.log("Test failed. The promise should be resolved by the provided value.");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let p1 = new Promise<string>((resolve: (value: string) => void, reject: (err: Object) => void): void => {});
let p2 = Promise.resolve<string>("abc");
let p = Promise.race([p1, p2]);
sequence.push(1);
p.then<void>((value: string): void => {
if (value != "abc") {
console.log("Test failed. Expected value 'abc', but got '" + value + "'");
return;
}
sequence.push(4);
checkSequence(sequence, 4);
});
Promise.resolve().then<void>((): void => {
sequence.push(3);
checkSequence(sequence, 3);
}).then<void>((): void => {
sequence.push(5);
checkSequence(sequence, 5);
console.log("Test passed.");
});
sequence.push(2);

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let error = new Error();
let p = new Promise<string>((resolve: (value: string) => void, reject: (error: Object) => void) => {
throw error;
});
p.then<void, void>((value: string): void => {
console.log("Test failed. The promise should not be fulfilled.");
}, (err: Object): void => {
if (err !== error) {
console.log("Test failed. The promise should be rejected with the resolution value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let p = Promise.reject<number>(5);
p.then<void, void>((value: number): void => {
console.log('Test failed. The promise should not be fulfilled.');
}, (err: Object): void => {
if (!(err instanceof Number) || (err as Number) != 5) {
console.log('Test failed. The promise should be rejected with the provided value.');
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let thenable = Promise.resolve();
let resolve: ((value: string) => void)|null = null;
let reject: ((err: Object) => void)|null = null;
let p = new Promise<string>((_resolve: (value: string) => void, _reject: (err: Object) => void): void => {
resolve = _resolve;
reject = _reject;
});
p.then<void, void>((value: string): void => {
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});
resolve!("abc");
reject!(thenable);

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let thenable = Promise.resolve();
let p = new Promise<string>((resolve: (value: string) => void, reject: (err: Object) => void): void => {
resolve("abc");
reject(thenable);
});
p.then<void, void>((value: string): void => {
if (value != "abc") {
console.log("Test failed. Expected value 'abc' but got " + value + ".");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise should not be rejected.");
});

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let error = new Object();
let reject: ((err: Object) => void)|null = null;
let p1 = new Promise<string>((_: (value: string) => void, _reject: (err: Object) => void): void => {
reject = _reject;
});
let p2 = Promise.resolve<Object>(p1);
if (p1 !== p2) {
throw new Error("Test failed. Expected the same object.");
}
p2.then<void, void>((value: Object): void => {
console.log("Test failed. Expected the promise p2 to be rejected.");
}, (err: Object): void => {
if (err !== error) {
console.log("Test failed. Expetec the promise is rejected by the provided value.");
} else {
console.log("Test passed.");
}
});
reject!(error);

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let error = Promise.resolve();
let p = new Promise<string>((_: (v: string) => void, reject: (error: Object) => void) => {
reject(error);
});
p.then<void, void>((value: string): void => {
console.log("Test failed. The promise should not be fulfilled.");
}, (err: Object): void => {
if (err !== error) {
console.log("Test failed. The promise should be rejected with the resolution value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let error = new Error();
let reject: ((err: Object) => void)|null = null;
let p = new Promise<string>((_: (v: string) => void, _reject: (error: Object) => void) => {
reject = _reject;
});
p.then<void, void>((value: string): void => {
console.log("Test failed. The promise should not be fulfilled.");
}, (err: Object): void => {
if (err !== error) {
console.log("Test failed. The promise should be rejected with the resolution value.");
return;
}
console.log("Test passed.");
});
reject!(error);

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let p = new Promise<number>((resolve: (v: number) => void, _: (error: Object) => void) => {
resolve(7);
});
p.then<void, void>((value: number): void => {
if (value != 7) {
console.log("Test failed. The promise shoule be fulfilled by 7 but got " + value + ".");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise shouldn`t be rejected.");
});

View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let p = Promise.resolve<number>(7).then<void, void>((value: number): void => {
if (value != 7) {
console.log("Test failed. The promise shoule be fulfilled by 7 but got " + value + ".");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise shouldn`t be rejected.");
});

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let error = Promise.resolve();
let p = new Promise<string>((_: (v: string) => void, reject: (error: Object) => void) => {
reject(error);
});
p.then<void, void>((value: string): void => {
console.log("Test failed. The promise should not be fulfilled.");
}, (err: Object): void => {
if (err !== error) {
console.log("Test failed. The promise should be rejected with the resolution value.");
return;
}
console.log("Test passed.");
});

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let resolve: ((value: Object) => void)|null = null;
let p = new Promise<Object>((_resolve: (v: Object) => void, _: (error: Object) => void) => {
resolve = _resolve;
});
p.then<void, void>((value: Object): void => {
console.log("Test failed. The promise should not be resolved.");
}, (err: Object): void => {
if (err instanceof TypeError) {
console.log("Test passed.");
return;
}
console.log("Test failed. The promise should be rejected with a TypeError.");
});
resolve!(p);

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let value = new Object();
let thenable = Promise.resolve<Object>(value);
let p = new Promise<Object>((resolve: (v: Object) => void, _: (error: Object) => void) => {
resolve(thenable);
});
p.then<void, void>((v: Object): void => {
if (v !== value) {
console.log("Test failed. The promise should be resolved by the provided value.");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise should be rejected.");
});

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Thenable implements PromiseLike<string> {
then<U, E>(onFulfilled?: (value: string) => U|PromiseLike<U>, onRejected?: (error: Object) => E|PromiseLike<E>): PromiseLike<U|E> {
return Promise.resolve(onFulfilled!("resolved")) as PromiseLike<U|E>;
}
};
let thenable = new Thenable();
let p = Promise.resolve<string>(thenable);
p.then<void, void>((value: string): void => {
if (value == "resolved") {
console.log("Test passed.");
} else {
console.log("Test failed. The promise should be resolved by the provided value.");
}
}, (error: Object): void => {
console.log("Test failed. The promise should be resolved.");
});

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let resolve: ((value: Object) => void)|null = null;
let value = new Object();
let thenable = Promise.resolve<Object>(value);
let p = new Promise<Object>((_resolve: (v: Object) => void, _: (error: Object) => void) => {
resolve = _resolve;
});
p.then<void, void>((v: Object): void => {
if (v !== value) {
console.log("Test failed. The promise should be resolved by the provided value.");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise should be rejected.");
});
resolve!(thenable);

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let resolve: ((value: Object) => void)|null = null;
let value = new Object();
let p1 = new Promise<Object>((_resolve: (v: Object) => void, _: (error: Object) => void) => {
resolve = _resolve;
});
let p2 = Promise.resolve<Object>(p1);
if (p1 !== p2) {
throw new Error("Test failed. Expected the same promise.");
}
p2.then<void, void>((v: Object): void => {
if (v !== value) {
console.log("Test failed. The promise should be resolved by the provided value.");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise should be rejected.");
});
resolve!(value);

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let resolve: ((value: number) => void)|null = null;
let p = new Promise<number>((_resolve: (v: number) => void, _: (error: Object) => void) => {
resolve = _resolve;
});
p.then<void, void>((value: number): void => {
if (value != 7) {
console.log("Test failed. The promise shoule be fulfilled by 7 but got " + value + ".");
return;
}
console.log("Test passed.");
}, (err: Object): void => {
console.log("Test failed. The promise shouldn`t be rejected.");
});
resolve!(7);

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let resolve: ((value: number) => void)|null = null;
let p = new Promise<number>((_resolve: (v: number) => void, _: (error: Object) => void) => {
resolve = _resolve;
});
sequence.push(1);
resolve!(7);
p.then<void>((): void => {
sequence.push(3);
checkSequence(sequence, 3);
});
Promise.resolve().then<void>((): void => {
// enqueue another then-handler
p.then<void>((): void => {
sequence.push(5);
checkSequence(sequence, 5);
console.log("Test passed.");
});
sequence.push(4);
checkSequence(sequence, 4);
});
sequence.push(2);

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let resolve: ((value: number) => void)|null = null;
let reject: ((err: Object) => void)|null = null;
let p1 = new Promise<number>((_resolve: (v: number) => void, _: (error: Object) => void) => {
resolve = _resolve;
});
let p2 = new Promise<number>((_: (v: number) => void, _reject: (error: Object) => void) => {
reject = _reject;
});
reject!("abc");
resolve!(7);
p1.then<void>((): void => {
sequence.push(2);
});
p2.catch<void>((): void => {
sequence.push(3);
}).then<void>((): void => {
sequence.push(4);
checkSequence(sequence, 4);
console.log("Test passed.");
});
sequence.push(1);

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let p = new Promise<number>((resolve: (v: number) => void, _: (error: Object) => void) => {
sequence.push(1);
resolve(0);
});
p.then<void>((): void => {
sequence.push(3);
}).then<void>((): void => {
sequence.push(5);
}).then<void>((): void => {
sequence.push(7);
});
p.then<void>((): void => {
sequence.push(4);
}).then<void>((): void => {
sequence.push(6);
}).then<void>((): void => {
sequence.push(8);
}).then<void>((): void => {
checkSequence(sequence, 8);
console.log("Test passed.");
});
sequence.push(2);

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let resolve: ((value: number) => void)|null = null;
let reject: ((err: Object) => void)|null = null;
new Promise<number>((_resolve: (v: number) => void, _: (error: Object) => void) => {
resolve = _resolve;
}).then<void>((): void => {
sequence.push(3)
}).then<void>((): void => {
checkSequence(sequence, 3);
console.log("Test passed.");
});
new Promise<number>((_: (v: number) => void, _reject: (error: Object) => void) => {
reject = _reject;
}).catch<void>((): void => {
sequence.push(2);
});
reject!("abc");
resolve!(7);
sequence.push(1);

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let resolve: ((value: number) => void)|null = null;
let reject: ((err: Object) => void)|null = null;
let p1 = new Promise<number>((_resolve: (v: number) => void, _: (error: Object) => void) => {
resolve = _resolve;
});
let p2 = new Promise<number>((_: (v: number) => void, _reject: (error: Object) => void) => {
reject = _reject;
});
reject!("abc");
resolve!(7);
Promise.resolve().then<void>((): void => {
p1.then<void>((): void => {
sequence.push(2);
});
p2.catch<void>((): void => {
sequence.push(3);
}).then<void>((): void => {
checkSequence(sequence, 3);
console.log("Test passed.");
});
});
sequence.push(1);

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let reject: ((err: Object) => void)|null = null;
let p = new Promise<number>((_: (v: number) => void, _reject: (error: Object) => void) => {
reject = _reject;
});
sequence.push(1);
reject!("abc");
p.then<void, void>((valule: number): void => {
console.log("Test failed. The promise should not be resolved.")
throw new Error("Test failed");
}, (err: Object): void => {
sequence.push(3);
checkSequence(sequence, 3);
});
Promise.resolve().then<void>((): void => {
p.then<void, void>((valule: number): void => {
console.log("Test failed. The promise should not be resolved.")
throw new Error("Test failed");
}, (err: Object): void => {
sequence.push(5);
checkSequence(sequence, 5);
console.log("Test passed.");
});
sequence.push(4);
checkSequence(sequence, 4);
});
sequence.push(2);

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let resolve: ((value: number) => void)|null = null;
let p = new Promise<number>((_resolve: (v: number) => void, _: (error: Object) => void) => {
resolve = _resolve;
});
sequence.push(1);
p.then<void>((): void => {
sequence.push(3);
checkSequence(sequence, 3);
});
Promise.resolve().then<void>((): void => {
p.then<void>((): void => {
sequence.push(4);
checkSequence(sequence, 4);
console.log("Test passed.");
});
sequence.push(2);
checkSequence(sequence, 2);
resolve!(2);
});

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let err = new Object();
let p = Promise.reject<number>(err);
p.finally((): void => {
sequence.push(1);
}).then<void>((): void => {
console.log("Test failed. The promise should be rejected.");
throw new Error("Test failed");
}).catch<void>((err: Object): void => {
sequence.push(2);
}).then<void>((): void => {
checkSequence(sequence, 2);
console.log("Test passed.");
});

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function checkSequence(actual: Array<number>, expectedLength: number): void {
if (actual.length != expectedLength) {
console.log("Test failed. Expected sequence length " + expectedLength + ", but got " + actual.length);
throw new Error("Test failed");
}
for (let i = 0; i < expectedLength; ++i) {
if (actual[i] != i + 1) {
console.log("Test failed. Expected element at " + i + " to be " + i + ", but got " + actual[i]);
throw new Error("Test failed");
}
}
}
let sequence = new Array<number>();
let p = Promise.resolve<number>(5);
p.finally((): void => {
sequence.push(1);
}).then<void>((): void => {
sequence.push(2);
}).then<void>((): void => {
checkSequence(sequence, 2);
console.log("Test passed.");
});

View File

@ -3,4 +3,4 @@ spec/nullables/nullable-lambda-default-param_1.sts
spec/nullables/nullable-lambda-default-param_3.sts
spec/nullables/nullable-lambda-default-param_4.sts
spec/nullables/nullable-lambda-default-param_5.sts
spec/nullables/nullable-lambda-default-param_6.sts
spec/nullables/nullable-lambda-default-param_6.sts

View File

@ -0,0 +1,19 @@
### Issue 17754 begin
std/core/PromiseAllSettledRejectAll.sts
std/core/PromiseAllSettledMixed.sts
std/core/PromiseAllSettledResolve.sts
std/core/PromiseAllSettledReject.sts
std/core/PromiseAllSettledRejectDeferred.sts
std/core/PromiseAllSettledRejectIgnored.sts
std/core/PromiseAllSettledRejectIgnoredDeferred.sts
std/core/PromiseAllSettledPoisonedThen.sts
### Issue 17754 end
### Issue 19835 begin
std/core/PromiseResolve2.sts
std/core/PromiseReject.sts
### Issue 19835 end
### Issue 19841 begin
std/core/PromiseSequence7.sts
### Issue 19841 end

View File

@ -1 +1,4 @@
test.sts
### Issue 19842 begin
std/core/PromiseResolveBySelf.sts
std/core/PromiseAnyPoisonedThen.sts
### Issue 19842 end