SafeTests.ts
95 lines
| 2.8 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r76 | import { Cancellation } from "@implab/core/Cancellation"; | ||
|
|
r77 | import { first, isPromise, firstWhere, delay, nowait } from "@implab/core/safe"; | ||
|
|
r82 | import { test } from "./TestTraits"; | ||
|
|
r76 | |||
|
|
r82 | test("await delay test", async t => { | ||
|
|
r76 | // schedule delay | ||
| let resolved = false; | ||||
| let res = delay(0).then(() => resolved = true); | ||||
| t.false(resolved, "the delay should be async"); | ||||
| await res; | ||||
| t.pass("await delay"); | ||||
| // create cancellation token | ||||
| let cancel: (e?: any) => void; | ||||
| const ct = new Cancellation(c => cancel = c); | ||||
| // schedule delay | ||||
| resolved = false; | ||||
| res = delay(0, ct).then(() => resolved = true); | ||||
| t.false(resolved, "created delay with ct"); | ||||
| // cancel | ||||
| cancel(); | ||||
| try { | ||||
| await res; | ||||
| t.fail("the delay should fail when it is cancelled"); | ||||
| } catch { | ||||
| t.pass("the delay is cancelled"); | ||||
| } | ||||
|
|
r77 | t.throws(() => { | ||
| // try schedule delay after the cancellation is requested | ||||
| nowait(delay(0, ct)); | ||||
| }, "Should throw if cancelled before start"); | ||||
|
|
r76 | }); | ||
|
|
r82 | test("sequemce test", async t => { | ||
|
|
r76 | const sequence = ["a", "b", "c"]; | ||
| const empty = []; | ||||
| // synchronous tests | ||||
| t.equals(first(sequence), "a", "Should return the first element"); | ||||
|
|
r77 | t.equals(firstWhere(sequence, x => x === "b"), "b", "Should get the second element"); | ||
|
|
r76 | |||
| let v: string; | ||||
| let e: Error; | ||||
| first(sequence, x => v = x); | ||||
| t.equal(v, "a", "The callback should be called for the first element"); | ||||
|
|
r77 | firstWhere(sequence, x => x === "b", x => v = x); | ||
| t.equal(v, "b", "The callback should be called for the second element"); | ||||
|
|
r76 | |||
| t.throws(() => { | ||||
| first(empty); | ||||
| }, "Should throw when the sequence is empty"); | ||||
| t.throws(() => { | ||||
|
|
r77 | firstWhere(empty, x => x === "b"); | ||
| }, "Should throw when the sequence is empty"); | ||||
| t.throws(() => { | ||||
|
|
r76 | first(empty, x => v = x); | ||
| }, "Should throw when the sequence is empty"); | ||||
|
|
r77 | t.throws(() => { | ||
| firstWhere(empty, x => x === "b", x => v = x); | ||||
| }, "Should throw when the sequence is empty"); | ||||
| t.throws(() => { | ||||
| firstWhere(sequence, x => x === "z"); | ||||
| }, "Should throw when the element isn't found"); | ||||
| t.throws(() => { | ||||
| firstWhere(sequence, x => x === "z", x => v = x); | ||||
| }, "Should throw when the element isn't found"); | ||||
|
|
r76 | first(empty, null, x => e = x); | ||
| t.true(e, "The errorback should be called for the empty sequence"); | ||||
| // async tests | ||||
| const asyncSequence = Promise.resolve(sequence); | ||||
| const asyncEmptySequence = Promise.resolve(empty); | ||||
| const promise = first(asyncSequence); | ||||
| t.true(isPromise(promise), "Should return promise"); | ||||
| v = await promise; | ||||
| t.equal(v, "a", "Should return the first element"); | ||||
| v = await new Promise(resolve => first(asyncSequence, resolve)); | ||||
| t.equal(v, "a", "The callback should be called for the first element"); | ||||
| }); | ||||
