UNPKG

2.85 kBJavaScriptView Raw
1var assert = require('assert');
2var types = require('./types');
3
4var Either = require('..').Either;
5
6describe('Either', function() {
7 var e = Either('original left', 1);
8
9 function mult(a) {
10 return function(b) { return a * b; };
11 }
12
13 function add(a) {
14 return function(b) { return a + b; };
15 }
16
17 it('is a Functor', function() {
18 var fTest = types.functor;
19 assert.equal(true, fTest.iface(e));
20 assert.equal(true, fTest.id(e));
21 assert.equal(true, fTest.compose(e, mult(2), add(3)));
22 });
23
24 it('is an Apply', function() {
25 var aTest = types.apply;
26 var appA = Either('apply test fn a', mult(10));
27 var appU = Either('apply test fn u', add(5));
28 var appV = Either('apply test value v', 10);
29
30 assert.equal(true, aTest.iface(appA));
31 assert.equal(true, aTest.compose(appA, appU, appV));
32 });
33
34 it('is an Applicative', function() {
35 var aTest = types.applicative;
36 var app1 = Either('app1', 101);
37 var app2 = Either('app2', -123);
38 var appF = Either('appF', mult(3));
39
40 assert.equal(true, aTest.iface(app1));
41 assert.equal(true, aTest.id(app1, app2));
42 assert.equal(true, aTest.homomorphic(app1, add(3), 46));
43 assert.equal(true, aTest.interchange(app1, appF, 17));
44 });
45
46 it('is a Chain', function() {
47 var cTest = types.chain;
48 var f1 = function(x) {return Either('f1', (3 * x));};
49 var f2 = function(x) {return Either('f2', (5 + x));};
50
51 assert.equal(true, cTest.iface(e));
52 assert.equal(true, cTest.associative(e, f1, f2));
53 });
54
55 it('is a Monad', function() {
56 var mTest = types.monad;
57 assert.equal(true, mTest.iface(e));
58 });
59
60 it('is an Extend', function() {
61 var eTest = types.extend;
62 var r = Either(null, 1);
63 var l = Either(1, null);
64 var f = function(x) {return x + 1;};
65
66 assert.equal(true, eTest.iface(e));
67 assert.equal(true, eTest.iface(r.extend(f)));
68 assert.equal(true, eTest.iface(l.extend(f)));
69 });
70
71 describe('#bimap', function() {
72
73 it('maps the first function over the left value', function() {
74 var e = Either(1, null);
75 var result = e.bimap(add(1));
76 assert.equal(true, result.equals(Either(2, null)));
77 });
78
79 it('maps the second function over the right value', function() {
80 var e = Either(null, 1);
81 var result = e.bimap(null, add(1));
82 assert.equal(true, result.equals(Either(null, 2)));
83 });
84
85 });
86
87 describe('#toString', function() {
88
89 it('returns the string representation of a Left', function() {
90 assert.strictEqual(Either.Left('Cannot divide by zero').toString(),
91 'Either.Left("Cannot divide by zero")');
92 });
93
94 it('returns the string representation of a Right', function() {
95 assert.strictEqual(Either.Right([1, 2, 3]).toString(),
96 'Either.Right([1, 2, 3])');
97 });
98
99 });
100
101});