Skip to content

Commit

Permalink
Merge pull request #20 from jsr-core/support-node
Browse files Browse the repository at this point in the history
feat: add tests for supporting Node
  • Loading branch information
lambdalisue authored Aug 19, 2024
2 parents 2707e3e + 3ae3df5 commit 85fd83b
Show file tree
Hide file tree
Showing 178 changed files with 3,204 additions and 3,249 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test-node.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test (Node)

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22.x
- name: Install deps
run: |
npx jsr install
- name: Test
run: |
npx --yes tsx --test *_test.ts
npx --yes tsx --test async/*_test.ts
npx --yes tsx --test pipe/*_test.ts
npx --yes tsx --test pipe/async/*_test.ts
timeout-minutes: 5
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/docs
deno.lock
.coverage
node_modules
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@jsr:registry=https://npm.jsr.io
65 changes: 32 additions & 33 deletions README_test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { pipe } from "@core/pipe";
import * as rootMod from "@core/iterutil";
import * as asyncMod from "@core/iterutil/async";
import * as pipeMod from "@core/iterutil/pipe";
import * as pipeAsyncMod from "@core/iterutil/pipe/async";
import * as rootMod from "./mod.ts";
import * as asyncMod from "./async/mod.ts";
import * as pipeMod from "./pipe/mod.ts";
import * as pipeAsyncMod from "./pipe/async/mod.ts";

Deno.test("README", async (t) => {
await t.step("case 1", () => {
const iter = rootMod.map([1, 2, 3], (v) => v * 2);
assertEquals(Array.from(iter), [2, 4, 6]);
});
await test("README case 1", () => {
const iter = rootMod.map([1, 2, 3], (v) => v * 2);
assertEquals(Array.from(iter), [2, 4, 6]);
});

await t.step("case 2", async () => {
const iter = asyncMod.map([1, 2, 3], (v) => Promise.resolve(v * 2));
assertEquals(await Array.fromAsync(iter), [2, 4, 6]);
});
await test("README case 2", async () => {
const iter = asyncMod.map([1, 2, 3], (v) => Promise.resolve(v * 2));
assertEquals(await Array.fromAsync(iter), [2, 4, 6]);
});

await t.step("case 3", () => {
const iter = pipe(
[1, 2, 3],
pipeMod.map((v) => v * 2),
pipeMod.cycle,
pipeMod.take(10),
pipeMod.filter((v) => v % 2 === 0),
);
assertEquals(Array.from(iter), [2, 4, 6, 2, 4, 6, 2, 4, 6, 2]);
});
await test("README case 3", () => {
const iter = pipe(
[1, 2, 3],
pipeMod.map((v) => v * 2),
pipeMod.cycle,
pipeMod.take(10),
pipeMod.filter((v) => v % 2 === 0),
);
assertEquals(Array.from(iter), [2, 4, 6, 2, 4, 6, 2, 4, 6, 2]);
});

await t.step("case 4", async () => {
const iter = pipe(
[1, 2, 3],
pipeAsyncMod.map((v) => v * 2),
pipeAsyncMod.cycle,
pipeAsyncMod.take(10),
pipeAsyncMod.filter((v) => v % 2 === 0),
);
assertEquals(await Array.fromAsync(iter), [2, 4, 6, 2, 4, 6, 2, 4, 6, 2]);
});
await test("README case 4", async () => {
const iter = pipe(
[1, 2, 3],
pipeAsyncMod.map((v) => v * 2),
pipeAsyncMod.cycle,
pipeAsyncMod.take(10),
pipeAsyncMod.filter((v) => v % 2 === 0),
);
assertEquals(await Array.fromAsync(iter), [2, 4, 6, 2, 4, 6, 2, 4, 6, 2]);
});
59 changes: 29 additions & 30 deletions async/chain_test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { assertType, type IsExact } from "@std/testing/types";
import { toAsyncIterable } from "./to_async_iterable.ts";
import { chain } from "./chain.ts";

Deno.test("chain", async (t) => {
await t.step("with empty iterables", async () => {
const result = chain([] as number[], [] as string[]);
const expected = [] as (number | string)[];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number | string>>>(true);
});
await test("chain with empty iterables", async () => {
const result = chain([] as number[], [] as string[]);
const expected = [] as (number | string)[];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number | string>>>(true);
});

await t.step("with iterables", async () => {
const result = chain(
[1, 2, 3],
toAsyncIterable(["a", "b"]),
);
const expected = [1, 2, 3, "a", "b"];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number | string>>>(true);
});
await test("chain with iterables", async () => {
const result = chain(
[1, 2, 3],
toAsyncIterable(["a", "b"]),
);
const expected = [1, 2, 3, "a", "b"];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number | string>>>(true);
});

await t.step("with multiple iterables", async () => {
const result = chain(
toAsyncIterable([1, 2, 3]),
["a", "b"],
toAsyncIterable([true]),
);
const expected = [1, 2, 3, "a", "b", true];
assertEquals(await Array.fromAsync(result), expected);
assertType<
IsExact<typeof result, AsyncIterable<number | string | boolean>>
>(
true,
);
});
await test("chain with multiple iterables", async () => {
const result = chain(
toAsyncIterable([1, 2, 3]),
["a", "b"],
toAsyncIterable([true]),
);
const expected = [1, 2, 3, "a", "b", true];
assertEquals(await Array.fromAsync(result), expected);
assertType<
IsExact<typeof result, AsyncIterable<number | string | boolean>>
>(
true,
);
});
137 changes: 64 additions & 73 deletions async/chunked_test.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,79 @@
import { test } from "@cross/test";
import { assertEquals, assertThrows } from "@std/assert";
import { assertType, type IsExact } from "@std/testing/types";
import { toAsyncIterable } from "./to_async_iterable.ts";
import { chunked } from "./chunked.ts";

Deno.test("chunked", async (t) => {
await t.step("with async iterable", async (t) => {
await t.step("the length is divisible by the size", async () => {
const result = chunked(toAsyncIterable([1, 2, 3, 4, 5, 6]), 2);
const expected = [[1, 2], [3, 4], [5, 6]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});
await test("chunked with async iterable the length is divisible by the size", async () => {
const result = chunked(toAsyncIterable([1, 2, 3, 4, 5, 6]), 2);
const expected = [[1, 2], [3, 4], [5, 6]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});

await t.step("the length is not divisible by the size", async () => {
const result = chunked(toAsyncIterable([1, 2, 3, 4, 5]), 2);
const expected = [[1, 2], [3, 4], [5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});
await test("chunked with async iterable the length is not divisible by the size", async () => {
const result = chunked(toAsyncIterable([1, 2, 3, 4, 5]), 2);
const expected = [[1, 2], [3, 4], [5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});

await t.step("the length is equal to the size", async () => {
const result = chunked(toAsyncIterable([1, 2, 3, 4, 5]), 5);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});
await test("chunked with async iterable the length is equal to the size", async () => {
const result = chunked(toAsyncIterable([1, 2, 3, 4, 5]), 5);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});

await t.step("the length is less than the size", async () => {
const result = chunked(toAsyncIterable([1, 2, 3, 4, 5]), 6);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});
await test("chunked with async iterable the length is less than the size", async () => {
const result = chunked(toAsyncIterable([1, 2, 3, 4, 5]), 6);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});

await t.step("throws RangeError", async (t) => {
await t.step("if the length is not positive safe integer", () => {
assertThrows(() => chunked([], NaN), RangeError);
assertThrows(() => chunked([], Infinity), RangeError);
assertThrows(() => chunked([], -Infinity), RangeError);
assertThrows(() => chunked([], -1), RangeError);
assertThrows(() => chunked([], 1.1), RangeError);
assertThrows(() => chunked([], 0), RangeError);
});
});
});
await test("chunked with async iterable throws RangeError", () => {
assertThrows(() => chunked([], NaN), RangeError);
assertThrows(() => chunked([], Infinity), RangeError);
assertThrows(() => chunked([], -Infinity), RangeError);
assertThrows(() => chunked([], -1), RangeError);
assertThrows(() => chunked([], 1.1), RangeError);
assertThrows(() => chunked([], 0), RangeError);
});

await t.step("with iterable", async (t) => {
await t.step("the length is divisible by the size", async () => {
const result = chunked([1, 2, 3, 4, 5, 6], 2);
const expected = [[1, 2], [3, 4], [5, 6]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});
await test("chunked with iterable the length is divisible by the size", async () => {
const result = chunked([1, 2, 3, 4, 5, 6], 2);
const expected = [[1, 2], [3, 4], [5, 6]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});

await t.step("the length is not divisible by the size", async () => {
const result = chunked([1, 2, 3, 4, 5], 2);
const expected = [[1, 2], [3, 4], [5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});
await test("chunked with iterable the length is not divisible by the size", async () => {
const result = chunked([1, 2, 3, 4, 5], 2);
const expected = [[1, 2], [3, 4], [5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});

await t.step("the length is equal to the size", async () => {
const result = chunked([1, 2, 3, 4, 5], 5);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});
await test("chunked with iterable the length is equal to the size", async () => {
const result = chunked([1, 2, 3, 4, 5], 5);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});

await t.step("the length is less than the size", async () => {
const result = chunked([1, 2, 3, 4, 5], 6);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});
await test("chunked with iterable the length is less than the size", async () => {
const result = chunked([1, 2, 3, 4, 5], 6);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number[]>>>(true);
});

await t.step("throws RangeError", async (t) => {
await t.step("if the length is not positive safe integer", () => {
assertThrows(() => chunked([], NaN), RangeError);
assertThrows(() => chunked([], Infinity), RangeError);
assertThrows(() => chunked([], -Infinity), RangeError);
assertThrows(() => chunked([], -1), RangeError);
assertThrows(() => chunked([], 1.1), RangeError);
assertThrows(() => chunked([], 0), RangeError);
});
});
});
await test("chunked with iterable throws RangeError", () => {
assertThrows(() => chunked([], NaN), RangeError);
assertThrows(() => chunked([], Infinity), RangeError);
assertThrows(() => chunked([], -Infinity), RangeError);
assertThrows(() => chunked([], -1), RangeError);
assertThrows(() => chunked([], 1.1), RangeError);
assertThrows(() => chunked([], 0), RangeError);
});
Loading

0 comments on commit 85fd83b

Please sign in to comment.