mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-26 01:35:08 +02:00
LibJS: Move tests to /Tests/LibJS
This commit is contained in:
committed by
Tim Flynn
parent
c059c6a2f5
commit
e3faa9b5ad
Notes:
github-actions[bot]
2026-02-06 11:17:48 +00:00
Author: https://github.com/gmta Commit: https://github.com/LadybirdBrowser/ladybird/commit/e3faa9b5add Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/7572 Reviewed-by: https://github.com/trflynn89 ✅
136
Tests/LibJS/Runtime/builtins/Array/Array.fromAsync.js
Normal file
136
Tests/LibJS/Runtime/builtins/Array/Array.fromAsync.js
Normal file
@@ -0,0 +1,136 @@
|
||||
test("length is 1", () => {
|
||||
expect(Array.fromAsync).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe("normal behavior", () => {
|
||||
function checkResult(promise, type = Array) {
|
||||
expect(promise).toBeInstanceOf(Promise);
|
||||
let error = null;
|
||||
let passed = false;
|
||||
promise
|
||||
.then(value => {
|
||||
expect(value instanceof type).toBeTrue();
|
||||
expect(value[0]).toBe(0);
|
||||
expect(value[1]).toBe(2);
|
||||
expect(value[2]).toBe(4);
|
||||
passed = true;
|
||||
})
|
||||
.catch(value => {
|
||||
error = value;
|
||||
});
|
||||
runQueuedPromiseJobs();
|
||||
expect(error).toBeNull();
|
||||
expect(passed).toBeTrue();
|
||||
}
|
||||
|
||||
test("async from sync iterable no mapfn", () => {
|
||||
const input = [0, Promise.resolve(2), Promise.resolve(4)].values();
|
||||
const promise = Array.fromAsync(input);
|
||||
checkResult(promise);
|
||||
});
|
||||
|
||||
test("from object of promises no mapfn", () => {
|
||||
let promise = Array.fromAsync({
|
||||
length: 3,
|
||||
0: Promise.resolve(0),
|
||||
1: Promise.resolve(2),
|
||||
2: Promise.resolve(4),
|
||||
});
|
||||
checkResult(promise);
|
||||
});
|
||||
|
||||
test("async from sync iterable with mapfn", () => {
|
||||
const input = [Promise.resolve(0), 1, Promise.resolve(2)].values();
|
||||
const promise = Array.fromAsync(input, async element => element * 2);
|
||||
checkResult(promise);
|
||||
});
|
||||
|
||||
test("from object of promises with mapfn", () => {
|
||||
let promise = Array.fromAsync(
|
||||
{
|
||||
length: 3,
|
||||
0: Promise.resolve(0),
|
||||
1: Promise.resolve(1),
|
||||
2: Promise.resolve(2),
|
||||
},
|
||||
async element => element * 2
|
||||
);
|
||||
checkResult(promise);
|
||||
});
|
||||
|
||||
test("does not double construct from array like object", () => {
|
||||
let callCount = 0;
|
||||
|
||||
class TestArray {
|
||||
constructor() {
|
||||
callCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let promise = Array.fromAsync.call(TestArray, {
|
||||
length: 3,
|
||||
0: Promise.resolve(0),
|
||||
1: Promise.resolve(2),
|
||||
2: Promise.resolve(4),
|
||||
});
|
||||
|
||||
checkResult(promise, TestArray);
|
||||
expect(callCount).toBe(1);
|
||||
});
|
||||
|
||||
asyncTest("sync iterable is closed upon rejection", async () => {
|
||||
const thenable = {
|
||||
then(resolve, reject) {
|
||||
reject();
|
||||
},
|
||||
};
|
||||
|
||||
let counter = 0;
|
||||
|
||||
function* iterator() {
|
||||
try {
|
||||
yield thenable;
|
||||
} finally {
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await Array.fromAsync(iterator());
|
||||
} catch (e) {}
|
||||
|
||||
expect(counter).toBe(1);
|
||||
});
|
||||
|
||||
asyncTest("mapper exception takes priority over iterator closure exception", async () => {
|
||||
let exceptionOrder = [];
|
||||
|
||||
async function* iterator() {
|
||||
try {
|
||||
yield 1;
|
||||
yield 2;
|
||||
} finally {
|
||||
exceptionOrder.push("iterator");
|
||||
throw "iterator exception";
|
||||
}
|
||||
}
|
||||
|
||||
let exception = null;
|
||||
|
||||
try {
|
||||
await Array.fromAsync(iterator(), value => {
|
||||
if (value === 1) {
|
||||
exceptionOrder.push("mapper");
|
||||
throw "mapper exception";
|
||||
}
|
||||
|
||||
return value;
|
||||
});
|
||||
} catch (e) {
|
||||
exception = e;
|
||||
}
|
||||
|
||||
expect(exceptionOrder).toEqual(["mapper", "iterator"]);
|
||||
expect(exception).toBe("mapper exception");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user