##// END OF EJS Templates
more tests
cin -
r77:04efbdad7679 default
parent child
Show More
@@ -30,4 +30,4 First release, intorduces the following
30 - `di` - dependency injection container
30 - `di` - dependency injection container
31 - `log` - log4 style logging system
31 - `log` - log4 style logging system
32 - `text` - simple and fast text templating and formatting
32 - `text` - simple and fast text templating and formatting
33 - `Uuid` - uuid generation traits No newline at end of file
33 - `Uuid` - uuid generation traits
@@ -248,10 +248,8 export function delegate<T, K extends ke
248 }
248 }
249
249
250 export function delay(timeMs: number, ct = Cancellation.none) {
250 export function delay(timeMs: number, ct = Cancellation.none) {
251 ct.throwIfRequested();
251 return new Promise((resolve, reject) => {
252 return new Promise((resolve, reject) => {
252 if (ct.isRequested()) {
253 ct.register(reject);
254 } else {
255 const h = ct.register(e => {
253 const h = ct.register(e => {
256 clearTimeout(id);
254 clearTimeout(id);
257 reject(e);
255 reject(e);
@@ -261,7 +259,7 export function delay(timeMs: number, ct
261 h.destroy();
259 h.destroy();
262 resolve();
260 resolve();
263 }, timeMs);
261 }, timeMs);
264 }
262
265 });
263 });
266 }
264 }
267
265
@@ -1,7 +1,6
1 import * as tape from "tape";
1 import * as tape from "tape";
2 import { Cancellation } from "@implab/core/Cancellation";
2 import { Cancellation } from "@implab/core/Cancellation";
3 import { ICancellation } from "@implab/core/interfaces";
3 import { delay } from "@implab/core/safe";
4 import { delay } from "./TestTraits";
5
4
6 tape("standalone cancellation", async t => {
5 tape("standalone cancellation", async t => {
7
6
@@ -1,8 +1,8
1 import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource";
1 import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource";
2 import * as tape from "tape";
2 import * as tape from "tape";
3 import { TapeWriter, delay } from "./TestTraits";
4 import { Observable } from "@implab/core/Observable";
3 import { Observable } from "@implab/core/Observable";
5 import { IObservable } from "@implab/core/interfaces";
4 import { IObservable } from "@implab/core/interfaces";
5 import { delay } from "@implab/core/safe";
6
6
7 const trace = TraceSource.get("ObservableTests");
7 const trace = TraceSource.get("ObservableTests");
8
8
@@ -1,7 +1,6
1 import tape = require("tape");
1 import tape = require("tape");
2 import { delay } from "./TestTraits";
3 import { Cancellation } from "@implab/core/Cancellation";
2 import { Cancellation } from "@implab/core/Cancellation";
4 import { first, isPromise } from "@implab/core/safe";
3 import { first, isPromise, firstWhere, delay, nowait } from "@implab/core/safe";
5
4
6 tape("await delay test", async t => {
5 tape("await delay test", async t => {
7 // schedule delay
6 // schedule delay
@@ -33,15 +32,10 tape("await delay test", async t => {
33 t.pass("the delay is cancelled");
32 t.pass("the delay is cancelled");
34 }
33 }
35
34
36 let died = false;
35 t.throws(() => {
37
38 // try schedule delay after the cancellation is requested
36 // try schedule delay after the cancellation is requested
39 res = delay(0, ct).then(x => true, () => died = true);
37 nowait(delay(0, ct));
40
38 }, "Should throw if cancelled before start");
41 t.false(died, "The delay should be scheduled even if the cancellation is requested");
42
43 await res;
44 t.true(died, "the delay should fail when cancelled");
45
39
46 t.end();
40 t.end();
47 });
41 });
@@ -52,20 +46,39 tape("sequemce test", async t => {
52
46
53 // synchronous tests
47 // synchronous tests
54 t.equals(first(sequence), "a", "Should return the first element");
48 t.equals(first(sequence), "a", "Should return the first element");
49 t.equals(firstWhere(sequence, x => x === "b"), "b", "Should get the second element");
55
50
56 let v: string;
51 let v: string;
57 let e: Error;
52 let e: Error;
58 first(sequence, x => v = x);
53 first(sequence, x => v = x);
59 t.equal(v, "a", "The callback should be called for the first element");
54 t.equal(v, "a", "The callback should be called for the first element");
55 firstWhere(sequence, x => x === "b", x => v = x);
56 t.equal(v, "b", "The callback should be called for the second element");
60
57
61 t.throws(() => {
58 t.throws(() => {
62 first(empty);
59 first(empty);
63 }, "Should throw when the sequence is empty");
60 }, "Should throw when the sequence is empty");
64
61
65 t.throws(() => {
62 t.throws(() => {
63 firstWhere(empty, x => x === "b");
64 }, "Should throw when the sequence is empty");
65
66 t.throws(() => {
66 first(empty, x => v = x);
67 first(empty, x => v = x);
67 }, "Should throw when the sequence is empty");
68 }, "Should throw when the sequence is empty");
68
69
70 t.throws(() => {
71 firstWhere(empty, x => x === "b", x => v = x);
72 }, "Should throw when the sequence is empty");
73
74 t.throws(() => {
75 firstWhere(sequence, x => x === "z");
76 }, "Should throw when the element isn't found");
77
78 t.throws(() => {
79 firstWhere(sequence, x => x === "z", x => v = x);
80 }, "Should throw when the element isn't found");
81
69 first(empty, null, x => e = x);
82 first(empty, null, x => e = x);
70 t.true(e, "The errorback should be called for the empty sequence");
83 t.true(e, "The errorback should be called for the empty sequence");
71
84
@@ -39,29 +39,6 export class TapeWriter implements IDest
39 }
39 }
40 }
40 }
41
41
42 export async function delay(timeout: number, ct: ICancellation = Cancellation.none) {
43 let un: IDestroyable;
44
45 try {
46 await new Promise((resolve, reject) => {
47 if (ct.isRequested()) {
48 un = ct.register(reject);
49 } else {
50 const ht = setTimeout(() => {
51 resolve();
52 }, timeout);
53
54 un = ct.register(e => {
55 clearTimeout(ht);
56 reject(e);
57 });
58 }
59 });
60 } finally {
61 destroy(un);
62 }
63 }
64
65 export function test(name: string, cb: (t: tape.Test) => any) {
42 export function test(name: string, cb: (t: tape.Test) => any) {
66 tape(name, async t => {
43 tape(name, async t => {
67 const writer = new TapeWriter(t);
44 const writer = new TapeWriter(t);
General Comments 0
You need to be logged in to leave comments. Login now