-
Notifications
You must be signed in to change notification settings - Fork 13
/
maybe.spec.ts
67 lines (57 loc) · 2.03 KB
/
maybe.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as test from 'tape';
import * as Maybe from './maybe';
import {compose} from '../utils';
test('1st monad law', assert => {
assert.plan(1);
const fn = (x: number) => Maybe.of(x);
Maybe.Do.of(1).fmap(fn).map(a => Maybe.Do.of(fn(1)).map(b => assert.equal(a, b)));
});
test('2nd monad law', assert => {
assert.plan(1);
Maybe.Do.of(2).fmap(Maybe.of).map(x => assert.equal(x, 2));
});
test('3rd monad law', assert => {
assert.plan(1);
const fn = (x: number) => Maybe.of(x);
const gn = (x: number) => Maybe.of(x + 1);
Maybe.Do.of(3).fmap(fn).fmap(gn).map(a => {
Maybe.Do.of(3).fmap(x => Maybe.Do.of(fn(x)).fmap(gn).value).map(b => assert.equal(a, b));
});
});
test('it apply to another object of the same type', assert => {
assert.plan(1);
const add = (a: number) => (b: number) => a + b;
const three = compose(Maybe.ap, Maybe.map(add))(Maybe.of(1))(Maybe.of(2));
Maybe.map(three => assert.equal(three, 3))(three);
});
test('it should map 1 argument', assert => {
assert.plan(1);
const plus1 = Maybe.map((x: number) => x + 1);
Maybe.map(two => assert.equal(two, 2))(plus1(Maybe.of(1)));
});
test('it should fmap 1 argument', assert => {
assert.plan(1);
const plus1 = Maybe.fmap((x: number) => Maybe.of(x + 1));
Maybe.map(two => assert.equal(two, 2))(plus1(Maybe.of(1)));
});
test('it should ap 1 argument', assert => {
assert.plan(1);
const plus1 = Maybe.ap(Maybe.of((x: number) => x + 1));
Maybe.map(two => assert.equal(two, 2))(plus1(Maybe.of(1)));
});
test('it should orElse 1 argument', assert => {
assert.plan(1);
const plus1 = Maybe.orElse(Maybe.of('one'));
Maybe.map(one => assert.equal(one, 'one'))(plus1(Maybe.Nothing));
});
test('it should orElse 1 argument', assert => {
assert.plan(1);
const plus1 = Maybe.orSome('one');
assert.equal(plus1(Maybe.Nothing), 'one');
});
test('it not apply to another object', assert => {
assert.plan(1);
const nil = Maybe.lift2<number, number, number>((a, b) => a + b, Maybe.Nothing, Maybe.of(2));
Maybe.map(n => assert.fail(`${n}`))(nil);
assert.pass();
});