UNPKG

2.57 kBJavaScriptView Raw
1var assert = require('assert');
2var types = require('./types');
3
4var Reader = require('..').Reader;
5
6function add(a) {
7 return function(b) { return a + b; };
8}
9
10function always(x) {
11 return function() { return x; };
12}
13
14function mult(a) {
15 return function(b) { return a * b; };
16}
17
18function identity(x) { return x; }
19
20describe('Reader properties', function() {
21
22 var f1 = function(x) { return x + '1 '; };
23 var f2 = function(x) { return x + '2 '; };
24 var f3 = function(x) { return x + '3 '; };
25 var r1 = Reader(f1);
26 var r2 = Reader(f2);
27
28 it('is a Functor', function() {
29 var fTest = types.functor;
30 assert.ok(fTest.iface(r1));
31 assert.ok(fTest.id(r1));
32 assert.ok(fTest.compose(r1, f2, f3));
33 });
34
35 it('is an Apply', function() {
36 var aTest = types.apply;
37 var a = Reader(function() { return add(1); });
38 var b = Reader(function() { return always(2); });
39 var c = Reader(always(4));
40
41 assert.equal(true, aTest.iface(r1));
42 assert.equal(true, aTest.compose(a, b, c));
43 });
44
45 it('is an Applicative', function() {
46 var aTest = types.applicative;
47
48 assert.equal(true, aTest.iface(r1));
49 assert.equal(true, aTest.id(Reader, r2));
50 assert.equal(true, aTest.homomorphic(r1, add(3), 46));
51 assert.equal(true, aTest.interchange(
52 Reader(function() { return mult(20); }),
53 Reader(function() { return mult(0.5); }),
54 73
55 ));
56 });
57
58 it('is a Chain', function() {
59 var cTest = types.chain;
60 var c = Reader(function() {
61 return Reader(function() {
62 return Reader(function() {
63 return 3;
64 });
65 });
66 });
67 assert.equal(true, cTest.iface(r1));
68 assert.equal(true, cTest.associative(c, identity, identity));
69 });
70
71 it('is a Monad', function() {
72 var mTest = types.monad;
73 assert.equal(true, mTest.iface(r1));
74 });
75
76 describe('#toString', function() {
77
78 it('returns the string representation of a Reader', function() {
79 assert.strictEqual(Reader(function(x) { void x; }).toString(),
80 'Reader(function (x) { void x; })');
81 });
82
83 });
84
85});
86
87describe('Reader examples', function() {
88 it('should write name of options object', function() {
89
90 var options = {name: 'header'};
91 var Printer = {};
92 Printer.write = function(x) {
93 return '/** ' + x + ' */';
94 };
95
96 function getOptionsName(opts) {
97 return Reader(function(printer) {
98 return printer.write(opts.name);
99 });
100 }
101
102 var nameReader = getOptionsName(options);
103
104 assert.equal(nameReader.run(Printer), '/** header */');
105 });
106});