Skip to content

Commit

Permalink
remove expect from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed May 6, 2019
1 parent 6556ab9 commit b2d7334
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 83 deletions.
2 changes: 2 additions & 0 deletions docs/modules/TaskOption.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions docs/modules/TaskValidation.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/TaskOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export const tryCatch = <A>(f: Lazy<Promise<A>>): TaskOption<A> =>
* 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 = <A>(fa: TaskOption<A>, onTimeout: Option<A>, millis: number): TaskOption<A> => {
return new TaskOption(withTimeoutTask(fa.value, onTimeout, millis))
Expand Down
2 changes: 2 additions & 0 deletions src/TaskValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const map = <L, A, B>(fa: TaskValidation<L, A>, 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 = <L, A>(
fa: TaskValidation<L, A>,
Expand Down
15 changes: 9 additions & 6 deletions test/Task.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
})
15 changes: 9 additions & 6 deletions test/TaskEither.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
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'
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'))
})
})
})
94 changes: 48 additions & 46 deletions test/TaskOption.ts
Original file line number Diff line number Diff line change
@@ -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<string>('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<number>(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<number>(1).getOrElse(v)
const te2 = fromOption<number>(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'))
})
})
})
47 changes: 22 additions & 25 deletions test/TaskValidation.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -6,50 +7,46 @@ import { getSemigroup, NonEmptyArray, make } from 'fp-ts/lib/NonEmptyArray2v'
describe('TaskValidation', () => {
const taskValidationNelApplicative = getApplicative(getSemigroup<string>())

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<string>): boolean => s.length < 2
const g = (n: number): boolean => n > 2
const te1 = taskValidationNelApplicative.of<number>(1).fold(f, g)
const te2 = new TaskValidation<NonEmptyArray<string>, number>(
new Task(() => Promise.resolve(failure(make<string>('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))
})
})
})

0 comments on commit b2d7334

Please sign in to comment.