Initial commit
[yaffs-website] / node_modules / yallist / test / basic.js
1 var t  = require('tap')
2 var Yallist = require('../yallist.js')
3
4 var y = new Yallist(1,2,3,4,5)
5 var z = new Yallist([1,2,3,4,5])
6 t.similar(y, z, 'build from single list or args')
7
8 function add10 (i) {
9   return i + 10
10 }
11 t.similar(y.map(add10).toArray(), [11, 12, 13, 14, 15])
12 t.similar(y.mapReverse(add10).toArray(), [15, 14, 13, 12, 11])
13
14 t.similar(y.map(add10).toArrayReverse(), [15, 14, 13, 12, 11])
15 t.isa(Yallist(1,2,3), 'Yallist')
16 t.equal(y.push(6, 7, 8), 8)
17 t.similar(y.toArray(), [1, 2, 3, 4, 5, 6, 7, 8])
18 y.pop()
19 y.shift()
20 y.unshift(100)
21
22 var expect = [100, 2, 3, 4, 5, 6, 7]
23 var expectReverse = [ 7, 6, 5, 4, 3, 2, 100 ]
24
25 t.similar(y.toArray(), expect)
26 t.equal(y.length, y.toArray().length)
27
28 t.test(function forEach (t) {
29   t.plan(y.length * 2)
30   y.forEach(function (item, i, list) {
31     t.equal(item, expect[i])
32     t.equal(list, y)
33   })
34 })
35
36 t.test(function forEach (t) {
37   t.plan(y.length * 5)
38   var n = 0
39   y.forEachReverse(function (item, i, list) {
40     t.equal(item, expectReverse[n])
41     t.equal(item, expect[i])
42     t.equal(item, y.get(i))
43     t.equal(item, y.getReverse(n))
44     n += 1
45     t.equal(list, y)
46   })
47 })
48
49 t.equal(y.getReverse(100), undefined)
50
51 t.equal(y.get(9999), undefined)
52
53
54 function sum (a, b) { return a + b }
55 t.equal(y.reduce(sum), 127)
56 t.equal(y.reduce(sum, 100), 227)
57 t.equal(y.reduceReverse(sum), 127)
58 t.equal(y.reduceReverse(sum, 100), 227)
59
60 t.equal(Yallist().pop(), undefined)
61 t.equal(Yallist().shift(), undefined)
62
63 var x = Yallist()
64 x.unshift(1)
65 t.equal(x.length, 1)
66 t.similar(x.toArray(), [1])
67
68 // verify that y.toArray() returns an array and if we create a
69 // new Yallist from that array, we get a list matching 
70 t.similar(Yallist(y.toArray()), y)
71 t.similar(Yallist.apply(null, y.toArray()), y)
72
73 t.throws(function () {
74   new Yallist().reduce(function () {})
75 }, {}, new TypeError('Reduce of empty list with no initial value'))
76 t.throws(function () {
77   new Yallist().reduceReverse(function () {})
78 }, {}, new TypeError('Reduce of empty list with no initial value'))
79
80 var z = y.reverse()
81 t.equal(z, y)
82 t.similar(y.toArray(), expectReverse)
83 y.reverse()
84 t.similar(y.toArray(), expect)
85
86 var a = Yallist(1,2,3,4,5,6)
87 var cases = [
88   [ [2, 4], [3, 4] ],
89   [ [2, -4], [] ],
90   [ [2, -2], [3, 4] ],
91   [ [1, -2], [2, 3, 4] ],
92   [ [-1, -2], [] ],
93   [ [-5, -2], [2, 3, 4] ],
94   [ [-99, 2], [1, 2] ],
95   [ [5, 99], [6] ],
96   [ [], [1,2,3,4,5,6] ]
97 ]
98 t.test('slice', function (t) {
99   t.plan(cases.length)
100   cases.forEach(function (c) {
101     t.test(JSON.stringify(c), function (t) {
102       t.similar(a.slice.apply(a, c[0]), Yallist(c[1]))
103       t.similar([].slice.apply(a.toArray(), c[0]), c[1])
104       t.end()
105     })
106   })
107 })
108
109 t.test('sliceReverse', function (t) {
110   t.plan(cases.length)
111   cases.forEach(function (c) {
112     var rev = c[1].slice().reverse()
113     t.test(JSON.stringify([c[0], rev]), function (t) {
114       t.similar(a.sliceReverse.apply(a, c[0]), Yallist(rev))
115       t.similar([].slice.apply(a.toArray(), c[0]).reverse(), rev)
116       t.end()
117     })
118   })
119 })
120
121 var inserter = Yallist(1,2,3,4,5)
122 inserter.unshiftNode(inserter.head.next)
123 t.similar(inserter.toArray(), [2,1,3,4,5])
124 inserter.unshiftNode(inserter.tail)
125 t.similar(inserter.toArray(), [5,2,1,3,4])
126 inserter.unshiftNode(inserter.head)
127 t.similar(inserter.toArray(), [5,2,1,3,4])
128
129 var single = Yallist(1)
130 single.unshiftNode(single.head)
131 t.similar(single.toArray(), [1])
132
133 inserter = Yallist(1,2,3,4,5)
134 inserter.pushNode(inserter.tail.prev)
135 t.similar(inserter.toArray(), [1,2,3,5,4])
136 inserter.pushNode(inserter.head)
137 t.similar(inserter.toArray(), [2,3,5,4,1])
138 inserter.unshiftNode(inserter.head)
139 t.similar(inserter.toArray(), [2,3,5,4,1])
140
141 single = Yallist(1)
142 single.pushNode(single.tail)
143 t.similar(single.toArray(), [1])
144
145 var swiped = Yallist(9,8,7)
146 inserter.unshiftNode(swiped.head.next)
147 t.similar(inserter.toArray(), [8,2,3,5,4,1])
148 t.similar(swiped.toArray(), [9,7])
149
150 swiped = Yallist(9,8,7)
151 inserter.pushNode(swiped.head.next)
152 t.similar(inserter.toArray(), [8,2,3,5,4,1,8])
153 t.similar(swiped.toArray(), [9,7])
154
155 swiped.unshiftNode(Yallist.Node(99))
156 t.similar(swiped.toArray(), [99,9,7])
157 swiped.pushNode(Yallist.Node(66))
158 t.similar(swiped.toArray(), [99,9,7,66])
159
160 var e = Yallist()
161 e.unshiftNode(Yallist.Node(1))
162 t.same(e.toArray(), [1])
163 e = Yallist()
164 e.pushNode(Yallist.Node(1))
165 t.same(e.toArray(), [1])
166
167 // steal them back, don't break the lists
168 swiped.unshiftNode(inserter.head)
169 t.same(swiped, Yallist(8,99,9,7,66))
170 t.same(inserter, Yallist(2,3,5,4,1,8))
171 swiped.unshiftNode(inserter.tail)
172 t.same(inserter, Yallist(2,3,5,4,1))
173 t.same(swiped, Yallist(8,8,99,9,7,66))
174
175
176 t.throws(function remove_foreign_node () {
177   e.removeNode(swiped.head)
178 }, {}, new Error('removing node which does not belong to this list'))
179 t.throws(function remove_unlisted_node () {
180   e.removeNode(Yallist.Node('nope'))
181 }, {}, new Error('removing node which does not belong to this list'))
182
183 e = Yallist(1,2)
184 e.removeNode(e.head)
185 t.same(e, Yallist(2))
186 e = Yallist(1,2)
187 e.removeNode(e.tail)
188 t.same(e, Yallist(1))