Skip to content

Commit 0522e7a

Browse files
committed
Move tests to test.js
1 parent 218a680 commit 0522e7a

File tree

5 files changed

+215
-233
lines changed

5 files changed

+215
-233
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"scripts": {
4545
"format": "prettier --write \"**/*.js\" && xo --fix",
46-
"test": "tape test/*.js"
46+
"test": "node test"
4747
},
4848
"prettier": {
4949
"tabWidth": 2,

test.js

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
'use strict'
2+
3+
var test = require('tape')
4+
var u = require('unist-builder')
5+
var select = require('unist-util-select')
6+
var Index = require('.')
7+
8+
test('index.add', function(t) {
9+
var ast = u('root', [u('node', {word: 'foo'}), u('node', {word: 'bar'})])
10+
var extraNode = u('node', {word: 'foo'})
11+
var $ = select.one(ast)
12+
13+
var index = new Index(ast, 'word')
14+
t.deepEqual(index.get('foo'), [$('[word=foo]')])
15+
16+
var result = index.add(extraNode)
17+
t.deepEqual(index.get('foo'), [$('[word=foo]'), extraNode])
18+
19+
t.equal(result, index, 'returns this')
20+
21+
index.add($('[word=foo]'))
22+
t.deepEqual(index.get('foo'), [$('[word=foo]'), extraNode])
23+
24+
index.add(extraNode)
25+
t.deepEqual(index.get('foo'), [$('[word=foo]'), extraNode])
26+
27+
t.end()
28+
})
29+
30+
test('index.get', function(t) {
31+
t.test('get', function(st) {
32+
var ast = u('node', {color: 'black', id: 0}, [
33+
u('node', {color: 'black', id: 1}, [
34+
u('node', {color: 'red', id: 2}, [
35+
u('node', {color: 'black', id: 3}),
36+
u('node', {color: 'black', id: 4})
37+
]),
38+
u('node', {color: 'black', id: 5})
39+
]),
40+
u('node', {color: 'red', id: 6}, [
41+
u('node', {color: 'black', id: 7}, [
42+
u('node', {color: 'black', id: 8}),
43+
u('node', {color: 'black', id: 9})
44+
]),
45+
u('node', {color: 'black', id: 10}, [
46+
u('node', {color: 'red', id: 11}, [
47+
u('node', {color: 'black', id: 12}),
48+
u('node', {color: 'black', id: 13})
49+
]),
50+
u('node', {color: 'black', id: 14})
51+
])
52+
])
53+
])
54+
var $ = select.one(ast)
55+
56+
var index = new Index(ast, 'color')
57+
58+
st.deepEqual(index.get('black'), [
59+
$('[id=0]'),
60+
$('[id=1]'),
61+
$('[id=3]'),
62+
$('[id=4]'),
63+
$('[id=5]'),
64+
$('[id=7]'),
65+
$('[id=8]'),
66+
$('[id=9]'),
67+
$('[id=10]'),
68+
$('[id=12]'),
69+
$('[id=13]'),
70+
$('[id=14]')
71+
])
72+
73+
st.deepEqual(index.get('red'), [$('[id=2]'), $('[id=6]'), $('[id=11]')])
74+
75+
st.deepEqual(index.get('yellow'), [])
76+
77+
st.end()
78+
})
79+
80+
t.test('degenerate keys', function(st) {
81+
st.test('Object.prototype keys', function(sst) {
82+
var ast = u('node', {word: '__proto__', id: 0}, [
83+
u('node', {word: 'constructor', id: 1}),
84+
u('node', {word: 'toString', id: 2})
85+
])
86+
var $$ = select(ast)
87+
var index = new Index(ast, 'word')
88+
89+
sst.deepEqual(index.get('__proto__'), $$('[id=0]'))
90+
sst.deepEqual(index.get('constructor'), $$('[id=1]'))
91+
sst.deepEqual(index.get('toString'), $$('[id=2]'))
92+
sst.end()
93+
})
94+
95+
st.test('identity keys', function(sst) {
96+
var id1 = {foo: 'bar'}
97+
var id2 = {foo: 'bar'}
98+
var ast = u('root', [
99+
u('node', {word: false, id: 0}),
100+
u('node', {word: 'false', id: 1}),
101+
u('node', {word: 1, id: 2}),
102+
u('node', {word: '1', id: 3}),
103+
u('node', {word: id1, id: 4}),
104+
u('node', {word: id2, id: 5})
105+
])
106+
var $$ = select(ast)
107+
var index = new Index(ast, 'word')
108+
109+
sst.deepEqual(index.get(false), $$('[id=0]'))
110+
sst.deepEqual(index.get('false'), $$('[id=1]'))
111+
sst.deepEqual(index.get(1), $$('[id=2]'))
112+
sst.deepEqual(index.get('1'), $$('[id=3]'))
113+
sst.deepEqual(index.get(id1), $$('[id=4]'))
114+
sst.deepEqual(index.get(id2), $$('[id=5]'))
115+
sst.deepEqual(index.get({foo: 'bar'}), [])
116+
sst.end()
117+
})
118+
119+
st.end()
120+
})
121+
122+
t.test('empty index', function(st) {
123+
st.deepEqual(new Index(null, 'foo').get('bar'), [])
124+
st.deepEqual(new Index('foo').get('bar'), [])
125+
st.end()
126+
})
127+
128+
t.test('Index filter', function(st) {
129+
var ast = u('root', [
130+
u('node', {word: 'foo'}),
131+
u('node', {word: 'bar'}),
132+
u('skip', {word: 'foo'}),
133+
u('skip', {word: 'bar'})
134+
])
135+
var $$ = select(ast)
136+
137+
st.test('type test', function(sst) {
138+
var index = new Index(ast, 'node', 'word')
139+
sst.deepEqual(index.get('foo'), $$('node[word="foo"]'))
140+
sst.deepEqual(index.get('bar'), $$('node[word="bar"]'))
141+
sst.end()
142+
})
143+
144+
st.test('function test', function(sst) {
145+
var index = new Index(ast, filter, 'word')
146+
sst.deepEqual(index.get('foo'), $$('node[word="foo"]'))
147+
sst.deepEqual(index.get('bar'), $$('node[word="bar"]'))
148+
sst.end()
149+
150+
function filter(node, index, parent) {
151+
return 'word' in node && index < 2 && parent.type === 'root'
152+
}
153+
})
154+
155+
st.end()
156+
})
157+
158+
t.test('computed keys', function(st) {
159+
var ast = u('root', {x: 0, y: 4, id: 0}, [
160+
u('node', {x: 3, y: 2, id: 1}),
161+
u('node', {x: 2, y: 2, id: 2}),
162+
u('node', {x: 3, y: 1, id: 3}),
163+
u('node', {x: 4, y: 1, id: 4})
164+
])
165+
var $ = select.one(ast)
166+
var index
167+
168+
index = new Index(ast, xPlusY)
169+
st.deepEqual(index.get(4), [$('[id=0]'), $('[id=2]'), $('[id=3]')])
170+
st.deepEqual(index.get(0), [])
171+
st.deepEqual(index.get(5), [$('[id=1]'), $('[id=4]')])
172+
173+
st.deepEqual(new Index(ast, 'node', xPlusY).get(4), [
174+
$('[id=2]'),
175+
$('[id=3]')
176+
])
177+
178+
st.end()
179+
180+
function xPlusY(node) {
181+
return node.x + node.y
182+
}
183+
})
184+
185+
t.end()
186+
})
187+
188+
test('index.remove', function(t) {
189+
var ast = u('root', [
190+
u('bad', {word: 'foo'}),
191+
u('node', {word: 'foo'}),
192+
u('node', {word: 'bar'})
193+
])
194+
var $ = select.one(ast)
195+
196+
var index = new Index(ast, 'word')
197+
t.deepEqual(index.get('foo'), [$('bad[word=foo]'), $('node[word=foo]')])
198+
199+
var result = index.remove($('bad'))
200+
t.deepEqual(index.get('foo'), [$('node[word=foo]')])
201+
202+
t.equal(result, index, 'returns this')
203+
204+
index.remove($('bad'))
205+
t.deepEqual(index.get('foo'), [$('node[word=foo]')])
206+
207+
index.remove(u('terrible', {word: 'baz'}))
208+
t.deepEqual(index.get('foo'), [$('node[word=foo]')])
209+
210+
index.remove($('node[word=foo]'))
211+
t.deepEqual(index.get('foo'), [])
212+
213+
t.end()
214+
})

test/add.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)