Skip to content

Commit 79452ab

Browse files
committed
Rewrite unit tests for List
1 parent 2753d15 commit 79452ab

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

test/units/list.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/**
2+
* lazy-linked-lists
3+
* Lazy and infinite linked lists for JavaScript.
4+
*
5+
* list.js
6+
*
7+
* Unit tests for the List class.
8+
* @license ISC
9+
*/
10+
11+
/* global describe,context, it, beforeEach */
12+
13+
import {
14+
LT,
15+
GT,
16+
EQ
17+
} from '../source';
18+
19+
import {
20+
emptyList,
21+
list,
22+
reverse
23+
} from '../source';
24+
25+
describe(`List`, function() {
26+
beforeEach(function() {
27+
let lst1 = list(1,2,3);
28+
let lst2 = list(4,5,6);
29+
let f = x => x * 10;
30+
let fs1 = list(f);
31+
let fs2 = list(f,f,f);
32+
let str = list(`a`,`b`,`c`);
33+
});
34+
context(`#[Symbol.iterator]()`, function() {
35+
it(`should be an iterable`, function() {
36+
let i = 1;
37+
for (let value of lst1) {
38+
value.should.equal(i);
39+
i += 1;
40+
}
41+
});
42+
});
43+
context(`#isEq()`, function() {
44+
it(`should compare two lists for equality`, function() {
45+
lst1.isEq(lst1).should.be.true;
46+
lst1.isEq(lst2).should.be.false;
47+
});
48+
});
49+
context(`#compare()`, function() {
50+
it(`should compare two lists and return an ordering value`, function() {
51+
emptyList.compare(emptyList).should.equal(EQ);
52+
emptyList.compare(lst1).should.equal(LT);
53+
lst1.compare(emptyList).should.equal(GT);
54+
lst1.compare(lst2).should.equal(LT);
55+
lst2.compare(lst1).should.equal(GT);
56+
lst1.compare(lst1).should.equal(EQ);
57+
});
58+
});
59+
context(`#isLessThan()`, function() {
60+
it(`should return true if the first list is less than the second`, function() {
61+
lst1.isLessThan(lst2).should.be.true;
62+
});
63+
it(`should return false if the first list is not less than the second`, function() {
64+
lst1.isLessThan(lst1).should.be.false;
65+
lst2.isLessThan(lst1).should.be.false;
66+
});
67+
});
68+
context(`#isLessThanOrEqual()`, function() {
69+
it(`should return true if the first list is less than or equal to the second`, function() {
70+
lst1.isLessThanOrEqual(lst2).should.be.true;
71+
lst1.isLessThanOrEqual(lst1).should.be.true;
72+
});
73+
it(`should return false if the first list is not less than or equal to the second`, function() {
74+
lst2.isLessThanOrEqual(lst1).should.be.false;
75+
});
76+
});
77+
context(`#isGreaterThan()`, function() {
78+
it(`should return true if the first list is greater than the second`, function() {
79+
lst2.isGreaterThan(lst1).should.be.true;
80+
});
81+
it(`should return false if the first list is not greater than the second`, function() {
82+
lst1.isGreaterThan(lst2).should.be.false;
83+
lst2.isGreaterThan(lst2).should.be.false;
84+
});
85+
});
86+
context(`#isGreaterThanOrEqual()`, function() {
87+
it(`should return true if the first list is greater than or equal to the second`, function() {
88+
lst2.isGreaterThanOrEqual(lst1).should.be.true;
89+
lst2.isGreaterThanOrEqual(lst2).should.be.true;
90+
});
91+
it(`should return false if the first list is not greater than or equal to the second`, function() {
92+
lst1.isGreaterThanOrEqual(lst2).should.be.false;
93+
});
94+
});
95+
context(`#mempty()`, function() {
96+
it(`should return an empty list (the identity of a list)`, function() {
97+
lst1.mempty().should.equal(emptyList);
98+
emptyList.mempty().should.equal(emptyList);
99+
});
100+
});
101+
context(`#mappend()`, function() {
102+
it(`should append the first list to the second, satisfying the monoid laws`, function() {
103+
lst1.mappend(emptyList).should.eql(lst1);
104+
emptyList.mappend(lst1).should.eql(lst1);
105+
lst1.mappend(lst2).should.eql(list(1,2,3,4,5,6));
106+
});
107+
});
108+
context(`#foldr()`, function() {
109+
let g = (x, y) => x * y;
110+
it(`should fold a function over a list and accumulate the value that results`, function() {
111+
lst1.foldr(g, 1).should.equal(6);
112+
reverse(lst1).foldr(f, 1).should.equal(6);
113+
});
114+
});
115+
context(`#traverse()`, function() {
116+
let g = x => list(x * 10);
117+
it(`should map a function that returns a list over a list and collect the results`, function() {
118+
lst1.traverse(f).should.eql(list(list(10,20,30)));
119+
emptyList.traverse(f).should.eql(list(emptyList));
120+
});
121+
});
122+
context(`#fmap()`, function() {
123+
it(`should map a function over a list and return a new list containing the results`, function() {
124+
lst1.fmap(f).should.eql(list(10,20,30));
125+
});
126+
});
127+
context(`#pure()`, function() {
128+
it(`should return a new list containing the argument value`, function() {
129+
lst1.pure(1).should.eql(list(1));
130+
lst1.pure(lst1).should.eql(list(lst1));
131+
emptyList.pure(emptyList).should.eql(list(list()));
132+
});
133+
});
134+
context(`#ap()`, function() {
135+
it(`should sequentially apply the function(s) contained in a list to another list of values and collect the results in a new list`, function() {
136+
fs1.ap(lst1).should.eql(list(10,20,30));
137+
fs1.ap(emptyList).should.equal(emptyList);
138+
fs2.ap(lst1).should.eql(list(10,20,30,10,20,30,10,20,30));
139+
});
140+
});
141+
context(`#flatMap()`, function() {
142+
it(`should map a function that returns a list over a list, collect the results, and flatten the list`, function() {
143+
lst1.flatMap(f).should.eql(list(10,20,30));
144+
emptyList.flatMap(f).should.equal(emptyList);
145+
});
146+
});
147+
context(`#then()`, function() {
148+
it(`should sequentially apply the elements of one list to another but ignore the values of the first list`, function() {
149+
lst1.then(lst2).should.eql(list(4,5,6,4,5,6,4,5,6));
150+
lst1.then(emptyList).should.equal(emptyList);
151+
fs1.then(lst1).should.eql(list(1,2,3,1,2,3,1,2,3));
152+
});
153+
});
154+
context(`#toString()`, function() {
155+
it(`should return '[Object List]' when cast to a string`, function() {
156+
lst1.toString().should.equal(`[Object List]`);
157+
emptyList.toString().should.equal(`[Object List]`);
158+
});
159+
});
160+
context(`#valueOf()`, function() {
161+
it(`should return a list string, e.g. '[1:2:3:[]]', as its value`, function() {
162+
lst1.valueOf().should.equal(`[1:2:3:[]]`);
163+
str.valueOf().should.equal(`[abc]`);
164+
emptyList.valueOf().should.equal(`[]`);
165+
});
166+
});
167+
context(`#typeOf()`, function() {
168+
it(`should return its type enclosed in brackets as a string`, function() {
169+
lst1.typeOf().should.equal(`[number]`);
170+
str.typeOf().should.equal(`[string]`);
171+
emptyList.typeOf().should.equal(`[]`);
172+
});
173+
});
174+
});

0 commit comments

Comments
 (0)