|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var fl = require('fantasy-land'); |
| 4 | +var expect = require('../spec-helper').expect; |
| 5 | +var namespace = require('../../lib/minim').namespace(); |
| 6 | + |
| 7 | +var ArrayElement = namespace.getElementClass('array'); |
| 8 | + |
| 9 | +describe('ArrayElement', function() { |
| 10 | + var array = new ArrayElement([1, 2, 3, 4]); |
| 11 | + |
| 12 | + describe('Functor', function() { |
| 13 | + it('can transform elements into new ArrayElement', function() { |
| 14 | + var result = array[fl.map](function(n) { |
| 15 | + return new namespace.elements.Number(n.toValue() * 2); |
| 16 | + }); |
| 17 | + |
| 18 | + expect(result).to.be.instanceof(ArrayElement); |
| 19 | + expect(result.toValue()).to.deep.equal([2, 4, 6, 8]); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('Semigroup', function() { |
| 24 | + it('can concatinate two array elements', function() { |
| 25 | + var result = array[fl.concat](new ArrayElement([5, 6])); |
| 26 | + |
| 27 | + expect(result).to.be.instanceof(ArrayElement); |
| 28 | + expect(result.toValue()).to.deep.equal([1, 2, 3, 4, 5, 6]); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('Monoid', function() { |
| 33 | + it('can create an empty ArrayElement', function() { |
| 34 | + var result = ArrayElement[fl.empty](); |
| 35 | + |
| 36 | + expect(result).to.be.instanceof(ArrayElement); |
| 37 | + expect(result.toValue()).to.deep.equal([]); |
| 38 | + }); |
| 39 | + |
| 40 | + it('can create an empty ArrayElement from another ArrayElement', function() { |
| 41 | + var result = array[fl.empty](); |
| 42 | + |
| 43 | + expect(result).to.be.instanceof(ArrayElement); |
| 44 | + expect(result.toValue()).to.deep.equal([]); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('Filterable', function() { |
| 49 | + it('can filter all elements into equivilent ArrayElement', function() { |
| 50 | + var result = array[fl.filter](function() { return true; }); |
| 51 | + |
| 52 | + expect(result).to.deep.equal(array); |
| 53 | + }); |
| 54 | + |
| 55 | + it('can filter into empty ArrayElement', function() { |
| 56 | + var result = array[fl.filter](function() { return false; }); |
| 57 | + |
| 58 | + expect(result).to.be.instanceof(ArrayElement); |
| 59 | + expect(result.isEmpty).to.be.true; |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('Chain', function() { |
| 64 | + it('can transform and chain results into new ArrayElement', function() { |
| 65 | + var duplicate = function(n) { return new ArrayElement([n, n]); }; |
| 66 | + var result = array[fl.chain](duplicate); |
| 67 | + |
| 68 | + expect(result).to.be.instanceof(ArrayElement); |
| 69 | + expect(result.toValue()).to.deep.equal([1, 1, 2, 2, 3, 3, 4, 4]); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('Foldable', function() { |
| 74 | + it('can reduce results into new ArrayElement', function() { |
| 75 | + var result = array[fl.reduce](function (accumulator, element) { |
| 76 | + return accumulator.concat(new ArrayElement([element.toValue(), element.toValue()])); |
| 77 | + }, new ArrayElement()); |
| 78 | + |
| 79 | + expect(result).to.be.instanceof(ArrayElement); |
| 80 | + expect(result.toValue()).to.deep.equal([1, 1, 2, 2, 3, 3, 4, 4]); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments