From b2d73349fa596e3a926d7e6733da48ac2e800645 Mon Sep 17 00:00:00 2001 From: gcanti Date: Mon, 6 May 2019 15:55:17 +0200 Subject: [PATCH] remove expect from tests --- docs/modules/TaskOption.ts.md | 2 + docs/modules/TaskValidation.ts.md | 2 + src/TaskOption.ts | 2 + src/TaskValidation.ts | 2 + test/Task.ts | 15 +++-- test/TaskEither.ts | 15 +++-- test/TaskOption.ts | 94 ++++++++++++++++--------------- test/TaskValidation.ts | 47 ++++++++-------- 8 files changed, 96 insertions(+), 83 deletions(-) diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index ba0ec3e..93ee9d9 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -183,3 +183,5 @@ withTimeout(completeAfter2s, some('timeout'), 3000).run() // Promise(some('resul withTimeout(completeAfter2s, none, 1000).run() // Promise(none) withTimeout(completeAfter2s, some('timeout'), 1000).run() // Promise(some('timeout')) ``` + +Added in v0.0.6 diff --git a/docs/modules/TaskValidation.ts.md b/docs/modules/TaskValidation.ts.md index 4a6e51d..ffb1b8f 100644 --- a/docs/modules/TaskValidation.ts.md +++ b/docs/modules/TaskValidation.ts.md @@ -105,3 +105,5 @@ withTimeout(completeAfter2s, failure('timeout'), 3000).value.run() // Promise(su withTimeout(completeAfter2s, failure('timeout'), 1000).value.run() // Promise(failure('timeout')) withTimeout(completeAfter2s, success('timeout'), 1000).value.run() // Promise(success('timeout')) ``` + +Added in v0.0.6 diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 68e3870..c568172 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -77,6 +77,8 @@ export const tryCatch = (f: Lazy>): TaskOption => * withTimeout(completeAfter2s, some('timeout'), 3000).run() // Promise(some('result')) * withTimeout(completeAfter2s, none, 1000).run() // Promise(none) * withTimeout(completeAfter2s, some('timeout'), 1000).run() // Promise(some('timeout')) + * + * @since 0.0.6 */ export const withTimeout = (fa: TaskOption, onTimeout: Option, millis: number): TaskOption => { return new TaskOption(withTimeoutTask(fa.value, onTimeout, millis)) diff --git a/src/TaskValidation.ts b/src/TaskValidation.ts index 67c1d5d..05e2521 100644 --- a/src/TaskValidation.ts +++ b/src/TaskValidation.ts @@ -48,6 +48,8 @@ const map = (fa: TaskValidation, f: (a: A) => B): TaskValidation< * withTimeout(completeAfter2s, failure('timeout'), 3000).value.run() // Promise(success('result')) * withTimeout(completeAfter2s, failure('timeout'), 1000).value.run() // Promise(failure('timeout')) * withTimeout(completeAfter2s, success('timeout'), 1000).value.run() // Promise(success('timeout')) + * + * @since 0.0.6 */ export const withTimeout = ( fa: TaskValidation, diff --git a/test/Task.ts b/test/Task.ts index 9f44988..bfe48e0 100644 --- a/test/Task.ts +++ b/test/Task.ts @@ -1,16 +1,19 @@ +import * as assert from 'assert' import { withTimeout } from '../src/Task/withTimeout' import { delay } from 'fp-ts/lib/Task' describe('Task', () => { describe('withTimeout', () => { - it('should return successfully withing the timeout', () => { - const t = withTimeout(delay(100, 'value'), 'fallback', 500) - return t.run().then(v => expect(v).toEqual('value')) + it('should return successfully withing the timeout', async () => { + const t = withTimeout(delay(10, 'value'), 'fallback', 50) + const v = await t.run() + assert.strictEqual(v, 'value') }) - it('should return the fallback value on timeout', () => { - const t = withTimeout(delay(500, 'value'), 'fallback', 100) - return t.run().then(v => expect(v).toEqual('fallback')) + it('should return the fallback value on timeout', async () => { + const t = withTimeout(delay(50, 'value'), 'fallback', 10) + const v = await t.run() + assert.strictEqual(v, 'fallback') }) }) }) diff --git a/test/TaskEither.ts b/test/TaskEither.ts index 140053c..038c0dc 100644 --- a/test/TaskEither.ts +++ b/test/TaskEither.ts @@ -1,3 +1,4 @@ +import * as assert from 'assert' import { withTimeout } from '../src/TaskEither/withTimeout' import { delay } from 'fp-ts/lib/Task' import { TaskEither } from 'fp-ts/lib/TaskEither' @@ -5,14 +6,16 @@ import { right, left } from 'fp-ts/lib/Either' describe('TaskEither', () => { describe('withTimeout', () => { - it('should return successfully within the timeout', () => { - const t = withTimeout(new TaskEither(delay(100, right('value'))), right('fallback'), 500) - return t.run().then(v => expect(v).toEqual(right('value'))) + it('should return successfully within the timeout', async () => { + const t = withTimeout(new TaskEither(delay(10, right('value'))), right('fallback'), 50) + const e = await t.run() + assert.deepStrictEqual(e, right('value')) }) - it('should return the fallback value on timeout', () => { - const t = withTimeout(new TaskEither(delay(500, right('value'))), left(new Error('timeout')), 100) - return t.run().then(v => expect(v).toEqual(left(new Error('timeout')))) + it('should return the fallback value on timeout', async () => { + const t = withTimeout(new TaskEither(delay(50, right('value'))), left('timeout'), 10) + const e = await t.run() + assert.deepStrictEqual(e, left('timeout')) }) }) }) diff --git a/test/TaskOption.ts b/test/TaskOption.ts index c826b51..3e23515 100644 --- a/test/TaskOption.ts +++ b/test/TaskOption.ts @@ -1,87 +1,89 @@ +import * as assert from 'assert' +import { none, some } from 'fp-ts/lib/Option' +import { delay, task } from 'fp-ts/lib/Task' import { - TaskOption, - taskOption, - none as taskOptionNone, fromOption, fromTask, + none as taskOptionNone, + TaskOption, + taskOption, tryCatch, withTimeout } from '../src/TaskOption' -import { delay, Task } from 'fp-ts/lib/Task' -import { some, none } from 'fp-ts/lib/Option' describe('TaskOption', () => { - it('map', () => { + it('map', async () => { const double = (n: number): number => n * 2 - return taskOption - .map(taskOption.of(1), double) - .run() - .then(e => expect(e).toEqual(some(2))) + const e = await taskOption.map(taskOption.of(1), double).run() + assert.deepStrictEqual(e, some(2)) }) - it('ap', () => { + it('ap', async () => { const double = (n: number): number => n * 2 - const fab = taskOption.of(double) - const fa = taskOption.of(1) - return Promise.all([fa.ap(fab).run(), fab.ap_(fa).run(), taskOption.ap(fab, fa).run()]).then(([e1, e2, e3]) => { - expect(e1).toEqual(some(2)) - expect(e1).toEqual(e2) - expect(e1).toEqual(e3) - }) + const mab = taskOption.of(double) + const ma = taskOption.of(1) + const e1 = await ma.ap(mab).run() + const e2 = await mab.ap_(ma).run() + const e3 = await taskOption.ap(mab, ma).run() + assert.deepStrictEqual(e1, some(2)) + assert.deepStrictEqual(e1, e2) + assert.deepStrictEqual(e1, e3) }) - it('chain', () => { + it('chain', async () => { const te1 = taskOption.chain(taskOption.of('foo'), a => (a.length > 2 ? taskOption.of(a.length) : taskOptionNone)) const te2 = taskOption.chain(taskOption.of('a'), a => a.length > 2 ? taskOption.of(a.length) : taskOptionNone ) - return Promise.all([te1.run(), te2.run()]).then(([e1, e2]) => { - expect(e1).toEqual(some(3)) - expect(e2).toEqual(none) - }) + const e1 = await te1.run() + const e2 = await te2.run() + assert.deepStrictEqual(e1, some(3)) + assert.deepStrictEqual(e2, none) }) - it('fold', () => { + it('fold', async () => { const g = (n: number): boolean => n > 2 const te1 = taskOption.of(1).fold(false, g) const te2 = taskOptionNone.fold(true, g) - return Promise.all([te1.run(), te2.run()]).then(([b1, b2]) => { - expect(b1).toEqual(false) - expect(b2).toEqual(true) - }) + const b1 = await te1.run() + const b2 = await te2.run() + assert.deepStrictEqual(b1, false) + assert.deepStrictEqual(b2, true) }) - it('getOrElse', () => { + it('getOrElse', async () => { const v: number = 42 const te1 = taskOption.of(1).getOrElse(v) const te2 = fromOption(none).getOrElse(v) - return Promise.all([te1.run(), te2.run()]).then(([b1, b2]) => { - expect(b1).toEqual(1) - expect(b2).toEqual(42) - }) + const n1 = await te1.run() + const n2 = await te2.run() + assert.deepStrictEqual(n1, 1) + assert.deepStrictEqual(n2, 42) }) - it('fromTask', () => { - const to1 = fromTask(new Task(() => Promise.resolve(none))) - return Promise.all([to1.run, taskOptionNone.run]).then(([b1, b2]) => { - expect(b1).toEqual(b2) - }) + it('fromTask', async () => { + const ma = fromTask(task.of(1)) + const n = await ma.run() + assert.deepStrictEqual(n, some(1)) }) - it('tryCatch', () => { + it('tryCatch', async () => { const to1 = tryCatch(() => Promise.reject()) - return to1.run().then(b1 => expect(b1).toEqual(none)) + const ma = await to1.run() + assert.deepStrictEqual(ma, none) }) describe('withTimeout', () => { - it('should return successfully within the timeout', () => { - const t = withTimeout(new TaskOption(delay(100, some('value'))), some('fallback'), 500) - return t.run().then(v => expect(v).toEqual(some('value'))) + it('should return successfully within the timeout', async () => { + const t = withTimeout(new TaskOption(delay(10, some('value'))), some('fallback'), 50) + const v = await t.run() + assert.deepStrictEqual(v, some('value')) }) - it('should return the fallback value on timeout', () => { - const t = withTimeout(new TaskOption(delay(500, some('value'))), some('fallback'), 100) - return t.run().then(v => expect(v).toEqual(some('fallback'))) + it('should return the fallback value on timeout', async () => { + const t = withTimeout(new TaskOption(delay(50, some('value'))), some('fallback'), 10) + const v = await t.run() + assert.deepStrictEqual(v, some('fallback')) }) }) }) diff --git a/test/TaskValidation.ts b/test/TaskValidation.ts index 1d740dd..734b2a3 100644 --- a/test/TaskValidation.ts +++ b/test/TaskValidation.ts @@ -1,3 +1,4 @@ +import * as assert from 'assert' import { TaskValidation, taskValidation, getApplicative, withTimeout } from '../src/TaskValidation' import { delay, Task } from 'fp-ts/lib/Task' import { success, failure } from 'fp-ts/lib/Validation' @@ -6,50 +7,46 @@ import { getSemigroup, NonEmptyArray, make } from 'fp-ts/lib/NonEmptyArray2v' describe('TaskValidation', () => { const taskValidationNelApplicative = getApplicative(getSemigroup()) - it('map', () => { + it('map', async () => { const double = (n: number): number => n * 2 - return taskValidation - .map(taskValidationNelApplicative.of(1), double) - .value.run() - .then(e => expect(e).toEqual(success(2))) + const e = await taskValidation.map(taskValidationNelApplicative.of(1), double).value.run() + assert.deepStrictEqual(e, success(2)) }) - it('fold', () => { + it('fold', async () => { const f = (s: NonEmptyArray): boolean => s.length < 2 const g = (n: number): boolean => n > 2 const te1 = taskValidationNelApplicative.of(1).fold(f, g) const te2 = new TaskValidation, number>( new Task(() => Promise.resolve(failure(make('failure', [])))) ).fold(f, g) - return Promise.all([te1.run(), te2.run()]).then(([b1, b2]) => { - expect(b1).toEqual(false) - expect(b2).toEqual(true) - }) + const b1 = await te1.run() + const b2 = await te2.run() + assert.deepStrictEqual(b1, false) + assert.deepStrictEqual(b2, true) }) describe('withTimeout', () => { - it('should return successfully within the timeout', () => { - const t = withTimeout(new TaskValidation(delay(100, success('value'))), failure('fallback'), 500) - return t.value.run().then(v => expect(v).toEqual(success('value'))) + it('should return successfully within the timeout', async () => { + const t = withTimeout(new TaskValidation(delay(10, success('value'))), failure('fallback'), 50) + const v = await t.value.run() + assert.deepStrictEqual(v, success('value')) }) - it('should return the fallback value on timeout', () => { - const t = withTimeout(new TaskValidation(delay(500, success('value'))), failure('fallback'), 100) - return t.value.run().then(v => expect(v).toEqual(failure('fallback'))) + it('should return the fallback value on timeout', async () => { + const t = withTimeout(new TaskValidation(delay(50, success('value'))), failure('fallback'), 10) + const v = await t.value.run() + assert.deepStrictEqual(v, failure('fallback')) }) }) describe('getApplicative', () => { - it('ap', () => { + it('ap', async () => { const double = (n: number): number => n * 2 - const fa = taskValidationNelApplicative.of(1) - const fab = taskValidationNelApplicative.of(double) - return taskValidationNelApplicative - .ap(fab, fa) - .value.run() - .then(e1 => { - expect(e1).toEqual(success(2)) - }) + const mab = taskValidationNelApplicative.of(double) + const ma = taskValidationNelApplicative.of(1) + const s = await taskValidationNelApplicative.ap(mab, ma).value.run() + assert.deepStrictEqual(s, success(2)) }) }) })