UNPKG

4.2 kBJavaScriptView Raw
1var R = require('ramda');
2var assert = require('assert');
3var jsv = require('jsverify');
4var types = require('./types')(R.equals);
5
6var Either = require('..').Either;
7
8var leftArb = function(arb) {
9 return arb.smap(Either.Left, R.prop('value'), R.toString);
10};
11var rightArb = function(arb) {
12 return arb.smap(Either.Right, R.prop('value'), R.toString);
13};
14var eitherArb = function(arb) {
15 return jsv.oneof([leftArb(arb), rightArb(arb)]);
16};
17
18var eNatArb = eitherArb(jsv.nat);
19var eFnArb = eitherArb(jsv.fn(jsv.nat));
20var fnNatArb = jsv.fn(jsv.nat);
21
22describe('Either', function() {
23
24 it('is a Functor', function() {
25 var fTest = types.functor;
26 jsv.assert(jsv.forall(eNatArb, fTest.iface));
27 jsv.assert(jsv.forall(eNatArb, fTest.id));
28 jsv.assert(jsv.forall(eNatArb, fnNatArb, fnNatArb, fTest.compose));
29 });
30
31 it('is an Apply', function() {
32 var aTest = types.apply;
33 jsv.assert(jsv.forall(eFnArb, eFnArb, eNatArb, aTest.compose));
34 jsv.assert(jsv.forall(eNatArb, aTest.iface));
35 });
36
37 it('is an Applicative', function() {
38 var aTest = types.applicative;
39 jsv.assert(jsv.forall(eNatArb, aTest.iface));
40 jsv.assert(jsv.forall(eNatArb, eNatArb, aTest.id));
41 jsv.assert(jsv.forall(eNatArb, fnNatArb, jsv.nat, aTest.homomorphic));
42 jsv.assert(jsv.forall(eNatArb, eFnArb, jsv.nat, aTest.interchange));
43 });
44
45 it('is a Chain', function() {
46 var cTest = types.chain;
47 var fnEArb = jsv.fn(eNatArb);
48 jsv.assert(jsv.forall(eNatArb, cTest.iface));
49 jsv.assert(jsv.forall(eNatArb, fnEArb, fnEArb, cTest.associative));
50 });
51
52 it('is a Monad', function() {
53 jsv.assert(jsv.forall(eNatArb, types.monad.iface));
54 });
55
56 it('is an Extend', function() {
57 var eTest = types.extend;
58 jsv.assert(jsv.forall(eNatArb, eTest.iface));
59 jsv.assert(jsv.forall(eNatArb, fnNatArb, fnNatArb, eTest.associative));
60 });
61
62 describe('checking for Left | Right', function() {
63 it('should allow the user to check if the instance is a Left', function() {
64 jsv.assert(jsv.forall(leftArb(jsv.nat), rightArb(jsv.nat), function(l, r) {
65 return l.isLeft === true && r.isLeft === false;
66 }));
67 });
68
69 it('should allow the user to check if the instance is a Right', function() {
70 jsv.assert(jsv.forall(leftArb(jsv.nat), rightArb(jsv.nat), function(l, r) {
71 return l.isRight === false && r.isRight === true;
72 }));
73 });
74
75 it('can check the type statically', function() {
76 jsv.assert(jsv.forall(leftArb(jsv.nat), rightArb(jsv.nat), function(l, r) {
77 return Either.isRight(l) === false && Either.isRight(r) === true &&
78 Either.isLeft(l) === true && Either.isLeft(r) === false;
79 }));
80 });
81 });
82
83 describe('#bimap', function() {
84
85 it('maps the first function over the left value', function() {
86 jsv.assert(jsv.forall(leftArb(jsv.nat), fnNatArb, fnNatArb, function(e, f, g) {
87 return e.bimap(f, g).value === f(e.value);
88 }));
89 });
90
91 it('maps the second function over the right value', function() {
92 jsv.assert(jsv.forall(rightArb(jsv.nat), fnNatArb, fnNatArb, function(e, f, g) {
93 return e.bimap(f, g).value === g(e.value);
94 }));
95 });
96
97 });
98
99 describe('.either', function() {
100 it('returns the value of a Left after applying the first function arg', function() {
101 jsv.assert(jsv.forall(leftArb(jsv.nat), fnNatArb, fnNatArb, function(e, f, g) {
102 return Either.either(f, g, e) === f(e.value);
103 }));
104 });
105
106 it('returns the value of a Right after applying the second function arg', function() {
107 jsv.assert(jsv.forall(rightArb(jsv.nat), fnNatArb, fnNatArb, function(e, f, g) {
108 return Either.either(f, g, e) === g(e.value);
109 }));
110 });
111 });
112
113 describe('#toString', function() {
114
115 it('returns the string representation of a Left', function() {
116 assert.strictEqual(Either.Left('Cannot divide by zero').toString(),
117 'Either.Left("Cannot divide by zero")');
118 });
119
120 it('returns the string representation of a Right', function() {
121 assert.strictEqual(Either.Right([1, 2, 3]).toString(),
122 'Either.Right([1, 2, 3])');
123 });
124
125 });
126
127});