File size: 6,689 Bytes
5fae594 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
describe("Lists", function(){
"use strict";
var listA = cons('a', emptyList);
var listBA = cons('b', listA);
var listCBA = cons('c', listBA);
it("can use cons, head and tail", function() {
expect(head(listCBA)).toBe('c');
expect(head(tail(listCBA))).toBe('b');
expect(head(tail(tail(listCBA)))).toBe('a');
});
/*
While desirable, freezing has been disabled because it
runs too slowly on current JS engines.
if( !Platform.isInternetExplorer || Platform.isInternetExplorer >= 9 ) {
it("freezes lists on supporting browsers", function() {
expect( listA ).toBeFrozen();
expect( listBA ).toBeFrozen();
expect( listCBA ).toBeFrozen();
})
} else {
console.warn('platform does not support object freezing');
}
*/
it("can convert to an array", function() {
var listCBA = cons('c', cons('b', cons('a', emptyList)));
expect( listAsArray(listCBA) ).toEqual( ['c','b','a'] );
});
it("can convert empty list to an array", function(){
expect( listAsArray(emptyList) ).toEqual([]);
});
it("can reverse the order of a list", function() {
var listCBA = cons('c', cons('b', cons('a', emptyList)));
expect( listAsArray(reverseList(listCBA)) ).toEqual( ['a','b','c'] );
});
it("can map over a list", function() {
var naturals = cons(1, cons(2, cons(3, emptyList)));
var evens = cons(2, cons(4, cons(6, emptyList)));
function doubleit(n){return n * 2}
expect( map(doubleit, naturals) ).toEqual( evens );
});
describe('without', function(){
function equalTo(val){
return function(candidate){
return candidate == val;
}
}
it("can remove from the middle of a list", function() {
var naturals = list(1, 2, 3);
expect( without(naturals, equalTo(2)) ).toEqual( list(1, 3) );
});
it("can remove from the end of a list", function() {
var naturals = list(1, 2, 3);
expect( without(naturals, equalTo(3)) ).toEqual( list(1, 2) );
});
it("can not remove", function() {
var naturals = list(1, 2, 3);
expect( without(naturals, equalTo(4)) ).toEqual( list(1, 2, 3) );
});
it("works with the empty list", function() {
expect( without(emptyList, equalTo(4)) ).toEqual( emptyList );
});
it("can give removed item to a callback", function() {
var callback = jasmine.createSpy();
var naturals = list(1, 2, 3);
without(naturals, equalTo(2), callback)
expect( callback ).toHaveBeenCalledWith( 2 );
});
});
it("can convert non empty list to an array", function(){
expect( listAsArray( list('a','b','c') ) ).toEqual( ['a','b','c'] );
});
it("can convert empty array to list", function() {
expect( listAsArray( arrayAsList([]) ) ).toEqual( [] );
});
it("can assert every xitem in a list holds for a given test", function(){
var l = list(1,2,3,4,5,6,7,8,9,10);
function isANumber(n) {
return typeof n == 'number';
}
function isOdd(n) {
return n % 2 == 0;
}
function isLessThanTen(n) {
return n < 10;
}
function isLessThanOrEqualToTen(n) {
return n <= 10;
}
expect( all(isANumber, l )).toBe(true);
expect( all(isOdd, l )).toBe(false);
expect( all(isLessThanTen, l )).toBe(false);
expect( all(isLessThanOrEqualToTen, l )).toBe(true);
});
describe('foldR', function(){
it("can fold list where order doesnt matter", function(){
function add(n, m){ return n+m }
var sum = foldR(add, 0, list(1,2,3,4));
expect( sum ).toEqual( 1 + 2 + 3 + 4 );
});
it("can fold list where start value matters", function(){
function divide(n, m){ return n / m }
var result = foldR(divide, 100, list(2, 2));
expect( result ).toBe( 25 ); // (100/2) / 2 = 25
});
it("can fold list in the correct order", function(){
function functionString(param, fnName){ return fnName + '(' + param + ')' }
var functionStringResult = foldR(functionString, 'x', list('a', 'b', 'c'));
// if order were wrong, might give c(b(a(x)))
expect( functionStringResult ).toEqual( 'a(b(c(x)))' );
});
});
describe('foldR1', function(){
it("can fold list where order doesnt matter", function(){
function add(n, m){ return n+m }
var sum = foldR1(add, list(1,2,3,4));
expect( sum ).toEqual( 1 + 2 + 3 + 4 );
});
it("can fold list in the correct order", function(){
function functionString(param, fnName){ return fnName + '(' + param + ')' }
var functionStringResult = foldR1(functionString, list('a', 'b', 'c'));
// if order were wrong, might give c(b(a))
expect( functionStringResult ).toEqual( 'a(b(c))' );
});
});
it('may apply a function with side effects to each item of a list', function(){
var callback1 = jasmine.createSpy(),
callback2 = jasmine.createSpy(),
callback3 = jasmine.createSpy(),
callback4 = jasmine.createSpy(),
functions = list(callback1, callback2, callback3, callback4);
applyEach( functions, ['a','b','c','d'] );
expect(callback1).toHaveBeenCalledWith('a','b','c','d');
expect(callback2).toHaveBeenCalledWith('a','b','c','d');
expect(callback3).toHaveBeenCalledWith('a','b','c','d');
expect(callback4).toHaveBeenCalledWith('a','b','c','d');
});
it('may apply a list of zero functions with side effects', function(){
expect(function(){
applyEach( list(), ['a','b','c','d'] );
}).not.toThrow();
});
beforeEach(function(){
this.addMatchers({
toBeFrozen: function(){
var obj = this.actual;
return Object.isFrozen(obj);
}
});
});
});
|