Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"prepublishOnly": "npm run build",
"build": "microbundle -i src/index.ts -f es,cjs",
"test": "typed-test --typeCheck 'src/**/*.test.ts'"
"test": "typed-test --typeCheck 'src/**/*.__test__.ts'"
},
"keywords": [
"most",
Expand Down
164 changes: 164 additions & 0 deletions src/index.__test__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import {
count,
index,
indexed,
keepIndex,
withCount,
withIndex,
withIndexStart
} from "./index";
import { constant, periodic, runEffects, take, tap } from "@most/core";
import { newDefaultScheduler } from "@most/scheduler";
import { Stream } from "@most/types";
import { describe, it, given } from "@typed/test";

const TEST_EVENT_VALUE = "test";

const collect = async <A>(n: number, s: Stream<A>): Promise<A[]> => {
const xs: A[] = [];
const sa = tap(x => xs.push(x), s);
await runEffects(take(n, sa), newDefaultScheduler());
return xs;
};

const range = (start: number, n: number): number[] =>
Array.from(Array(n), (_, i) => start + i);

const randomInt = (min: number, max: number) =>
min + Math.floor(Math.random() * (max - min));

export const indexTests = describe("index", [
given("a stream", [
it("replaces events with 0-based index", async ({ equal }) => {
// Fixture setup
const s = periodic(1);
const n = randomInt(10, 20);
const expectedEvents = range(0, n);
// Exercise system
const result = index(s);
// Verify outcome
const actualEvents = await collect(n, result);
equal(expectedEvents, actualEvents);
})
])
]);

export const withIndexTests = describe("withIndex", [
given("a stream", [
it("pairs events with 0-based count", async ({ equal }) => {
// Fixture setup
const s = constant(TEST_EVENT_VALUE, periodic(1));
const n = randomInt(10, 20);
const expectedEvents = range(0, n).map<[number, string]>(n => [
n,
TEST_EVENT_VALUE
]);
// Exercise system
const result = withIndex(s);
// Verify outcome
const actualEvents = await collect(n, result);
equal(expectedEvents, actualEvents);
})
])
]);

export const countTests = describe("count", [
given("a stream", [
it("replaces events with 1-based count", async ({ equal }) => {
// Fixture setup
const s = periodic(1);
const n = randomInt(10, 20);
const expectedEvents = range(1, n);
// Exercise system
const result = count(s);
// Verify outcome
const actualEvents = await collect(n, result);
equal(expectedEvents, actualEvents);
})
])
]);

export const withCountTests = describe("withCount", [
given("a stream", [
it("pairs events with 1-based count", async ({ equal }) => {
// Fixture setup
const s = constant(TEST_EVENT_VALUE, periodic(1));
const n = randomInt(10, 20);
const expectedEvents = range(1, n).map<[number, string]>(n => [
n,
TEST_EVENT_VALUE
]);
// Exercise system
const result = withCount(s);
// Verify outcome
const actualEvents = await collect(n, result);
equal(expectedEvents, actualEvents);
})
])
]);

export const withIndexStartTests = describe("withIndexStart", [
given("a start index and a stream", [
it("pairs events with start-based index", async ({ equal }) => {
// Fixture setup
const idx = randomInt(0, 10000);
const s = constant(TEST_EVENT_VALUE, periodic(1));
const n = randomInt(10, 20);
const expectedEvents = range(idx, n).map<[number, string]>(n => [
n,
TEST_EVENT_VALUE
]);
// Exercise system
const result = withIndexStart(idx, s);
// Verify outcome
const actualEvents = await collect(n, result);
equal(expectedEvents, actualEvents);
})
])
]);

export const indexedTests = describe("indexed", [
given("a function, an initial value, and a stream", [
it("should pair events with computed index", async ({ equal }) => {
// Fixture setup
const n = randomInt(10, 20);
const arbitraryChar = "a";
const strOfLenN = Array(n)
.fill(arbitraryChar)
.join("");
const stringIndex = (s: string) => (prev: string): [string, string] => [
prev,
prev + s
];
const f = stringIndex(arbitraryChar);
const init = "";
const s = constant(TEST_EVENT_VALUE, periodic(1));
const expectedEvents = range(0, n).map<[string, string]>((_, i) => [
strOfLenN.slice(0, i),
TEST_EVENT_VALUE
]);
// Exercise system
const result = indexed(f, init, s);
// Verify outcome
const actualEvents = await collect(n, result);
equal(expectedEvents, actualEvents);
})
])
]);

export const keepIndexTests = describe("keepIndex", [
given("a stream of an [index, value] pair", [
it("should keep index and discard value", async ({ equal }) => {
// Fixture setup
const idx = randomInt(0, 10000);
const s = withIndexStart(idx, constant(TEST_EVENT_VALUE, periodic(1)));
const n = randomInt(10, 20);
const expectedEvents = range(idx, n);
// Exercise system
const result = keepIndex(s);
// Verify outcome
const actualEvents = await collect(n, result);
equal(expectedEvents, actualEvents);
})
])
]);
83 changes: 0 additions & 83 deletions src/index.test.ts

This file was deleted.

21 changes: 21 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"lib": ["es2015", "es2016", "es2017"],
"declaration": true,
"sourceMap": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"exclude": ["node_modules", "src/**/*.__test__.ts"]
}
30 changes: 3 additions & 27 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"moduleResolution": "node",
"target": "es5",
"module": "es2015",
"lib": [
"es5",
"es2015",
"es2016",
"es2017"
],
"incremental": true,
"strict": true,
"sourceMap": true,
"declaration": true,
"declarationDir": "dist",
"outDir": "dist",
"typeRoots": [
"node_modules/@types"
]
"outDir": "dist"
},
"exclude": [
"experiments/**/*.ts",
"examples/**/*.ts",
"node_modules",
"src/**/*.test.ts"
],
"include": [
"src"
]
"include": ["src/**/*.ts"]
}
9 changes: 9 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"lib": ["dom", "es2015", "es2016", "es2017"],
"outDir": "__test__"
},
"include": ["src/**/*.__test__.ts"],
"exclude": ["node_modules"]
}