UNPKG

3.63 kBJavaScriptView Raw
1var R = require('ramda');
2var assert = require('assert');
3var types = require('./types');
4var jsv = require('jsverify');
5
6var Maybe = require('..').Maybe;
7
8var MaybeGen = R.curry(function(a, n) {
9 return n % 2 === 0 ? Maybe.Just(a.generator(n)) : Maybe.Nothing();
10});
11
12var MaybeShow = R.curry(function(a, m) {
13 return (Maybe.isJust(m)) ?
14 'Just(' + a.show(m.value) + ')' :
15 'Nothing';
16});
17
18var MaybeShrink = R.curry(function(a, m) {
19 return (Maybe.isJust(m)) ?
20 [Maybe.Nothing()].concat(a.shrink(m.value).map(Maybe.Just)) :
21 [];
22});
23
24var MaybeArb = function(a) {
25 return {
26 generator: jsv.generator.bless(MaybeGen(a)),
27 show: MaybeShow(a),
28 shrink: jsv.shrink.bless(MaybeShrink(a))
29 };
30};
31
32describe('Maybe', function() {
33 var m = MaybeArb(jsv.nat);
34 var env = {Maybe: MaybeArb};
35 var appF = 'Maybe (nat -> nat)';
36 var appN = 'Maybe nat';
37
38 it('has an arbitrary', function() {
39 var arb = jsv.forall(m, function(m) {
40 return m instanceof Maybe;
41 });
42 jsv.assert(arb);
43 });
44
45 it('is a Functor', function() {
46 var fTest = types.functor;
47
48 jsv.assert(jsv.forall(m, fTest.iface));
49 jsv.assert(jsv.forall(m, fTest.id));
50 jsv.assert(jsv.forall(m, 'nat -> nat', 'nat -> nat', fTest.compose));
51 });
52
53 it('is an Apply', function() {
54 var aTest = types.apply;
55
56 jsv.assert(jsv.forall(m, aTest.iface));
57 jsv.assert(jsv.forall(appF, appF, appN, env, aTest.compose));
58 });
59
60 it('is an Applicative', function() {
61 var aTest = types.applicative;
62
63 jsv.assert(jsv.forall(m, aTest.iface));
64 jsv.assert(jsv.forall(appN, appN, env, aTest.id));
65 jsv.assert(jsv.forall(appN, 'nat -> nat', 'nat', env, aTest.homomorphic));
66 jsv.assert(jsv.forall(appN, appF, 'nat', env, aTest.interchange));
67 });
68
69 it('is a Chain', function() {
70 var cTest = types.chain;
71 var f = 'nat -> Maybe nat';
72
73 jsv.assert(jsv.forall(m, cTest.iface));
74 jsv.assert(jsv.forall(m, f, f, env, cTest.associative));
75 });
76
77 it('is a Monad', function() {
78 var mTest = types.monad;
79
80 jsv.assert(jsv.forall(m, mTest.iface));
81 });
82
83});
84
85describe('Maybe usage', function() {
86
87 describe('checking for Just | Nothing', function() {
88 it('should allow the user to check if the instance is a Nothing', function() {
89 assert.equal(true, Maybe(null).isNothing());
90 assert.equal(false, Maybe(42).isNothing());
91 });
92
93 it('should allow the user to check if the instance is a Just', function() {
94 assert.equal(true, Maybe(42).isJust());
95 assert.equal(false, Maybe(null).isJust());
96 });
97
98 it('can check the type statically', function() {
99 var nada = Maybe.Nothing();
100 var just1 = Maybe.Just(1);
101 assert.equal(Maybe.isJust(nada), false);
102 assert.equal(Maybe.isNothing(nada), true);
103 assert.equal(Maybe.isJust(just1), true);
104 assert.equal(Maybe.isNothing(just1), false);
105 });
106 });
107
108 describe('#getOrElse', function() {
109
110 it('should return the contained value for if the instance is a Just', function() {
111 assert.equal(42, Maybe(42).getOrElse(24));
112 });
113
114 it('should return the input value if the instance is a Nothing', function() {
115 assert.equal(24, Maybe(null).getOrElse(24));
116 });
117
118 });
119
120 describe('#toString', function() {
121
122 it('returns the string representation of a Just', function() {
123 assert.strictEqual(Maybe.Just([1, 2, 3]).toString(),
124 'Maybe.Just([1, 2, 3])');
125 });
126
127 it('returns the string representation of a Nothing', function() {
128 assert.strictEqual(Maybe.Nothing().toString(),
129 'Maybe.Nothing()');
130 });
131
132 });
133
134});