/
basic_tests.js
133 lines (115 loc) · 2.7 KB
/
basic_tests.js
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
var test = require('tape')
var Array = require('./index')
var spy = require('ispy')
var E = require('emmitt')
test('create', function(t){
t.equal(new Array().size(), 0)
t.equal(new Array(1).size(), 1)
t.end()
})
test('call constructor w/o new dont matter', function(t){
t.equal(Array().size(), 0)
t.equal(Array(1).size(), 1)
t.assert(Array() instanceof Array, 'is instanceof MantaRay')
t.end()
})
test('access', function(t){
var ray = new Array(1)
t.equal(ray.get(0), 1)
t.end()
})
test('add', function(t){
var ray = new Array()
ray.add('hello')
t.equal(ray.size(), 1)
ray.add('world')
t.equal(ray.size(), 2)
t.equal(ray.get(0), 'hello')
t.equal(ray.get(1), 'world')
t.end()
})
test('insert', function(t){
var ray = new Array(1, 3)
ray.insert(1, 2)
t.equal(ray.size(), 3)
t.equal(ray.get(0), 1)
t.equal(ray.get(1), 2)
t.equal(ray.get(2), 3)
t.end()
})
test('remove', function(t){
var ray = new Array(1, 2)
ray.remove(1)
t.equal(ray.get(0), 2)
t.equal(ray.size(), 1)
t.end()
})
test('contains', function(t){
var ray = new Array(1, 2)
t.assert(ray.contains(1))
t.assert(!ray.contains(3))
t.end()
})
test('clear', function(t){
var ray = new Array(1, 2, 3)
ray.clear()
t.equal(ray.size(), 0)
t.end()
})
test('indexOf', function(t){
var ray = new Array(1, 2, 3)
t.equal(ray.indexOf(2), 1)
t.end()
})
test('toArray', function(t){
var ray = new Array(1, 2, 3)
t.deepEqual(ray.toArray(), [1, 2, 3])
t.end()
})
test('fires on add', function(t){
var ray = new Array(1)
var onInsert = spy()
E.on(ray, 'insert', onInsert)
ray.add(2)
t.assert(onInsert.called, 'should have called')
t.deepEqual(onInsert.lastCall.args, [1, 2])
t.end()
})
test('fires on insert', function(t){
var ray = new Array(1, 2)
var onInsert = spy()
E.on(ray, 'insert', onInsert)
ray.insert(1, 3)
t.assert(onInsert.called, 'should have called')
t.deepEqual(onInsert.lastCall.args, [1, 3])
t.end()
})
test('fires on remove', function(t){
var ray = new Array(1, 2)
var onRemove = spy()
E.on(ray, 'remove', onRemove)
ray.remove(1)
t.assert(onRemove.called, 'should have called')
t.deepEqual(onRemove.lastCall.args, [0, 1])
t.end()
})
test('fires remove on clear', function(t){
var ray = new Array(1, 2)
var onRemove = spy()
E.on(ray, 'remove', onRemove)
ray.clear()
t.assert(onRemove.called, 'should have called')
t.deepEqual(onRemove.lastCall.args, [[0, 1], [1, 2]])
t.end()
})
// ES5 Array Methods
test('forEach', function(t){
var ray = new Array(1, 2)
var fun = spy()
ray.forEach(fun)
t.equal(fun.callCount, 2)
t.deepEqual(fun.calls[0].args, [1,0,[1,2]])
t.deepEqual(fun.calls[1].args, [2,1,[1,2]])
t.end()
})
// TODO: the other ones