forked from VimCommando/fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
75 lines (62 loc) · 1.72 KB
/
test.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
'use strict'
const test = require('ava')
const {execSync} = require('child_process')
const stream = require('./stream')
function fx(json, code = '') {
return execSync(`echo '${JSON.stringify(json)}' | node index.js ${code}`).toString('utf8')
}
test('pass', t => {
const r = fx([{"greeting": "hello world"}])
t.deepEqual(JSON.parse(r), [{"greeting": "hello world"}])
})
test('anon func', t => {
const r = fx({"key": "value"}, "'function (x) { return x.key }'")
t.is(r, 'value\n')
})
test('arrow func', t => {
const r = fx({"key": "value"}, "'x => x.key'")
t.is(r, 'value\n')
})
test('arrow func ()', t => {
const r = fx({"key": "value"}, "'(x) => x.key'")
t.is(r, 'value\n')
})
test('this bind', t => {
const r = fx([1, 2, 3, 4, 5], "'this.map(x => x * this.length)'")
t.deepEqual(JSON.parse(r), [5, 10, 15, 20, 25])
})
test('chain', t => {
const r = fx({"items": ["foo", "bar"]}, "'this.items' '.' 'x => x[1]'")
t.is(r, 'bar\n')
})
test('file argument', t => {
const r = execSync(`node index.js package.json .name`).toString('utf8')
t.is(r, 'fx\n')
})
test('stream', t => {
const input = `
{"index": 0} {"index": 1}
{"index": 2, "quote": "\\""}
{"index": 3} "Hello" "world"
{"index": 6, "key": "one \\"two\\" three"}
`
t.plan(7 * (input.length - 1))
for (let i = 0; i < input.length; i++) {
const parts = [input.substring(0, i), input.substring(i)]
const reader = stream(
{
read() {
return parts.shift()
}
},
json => {
t.pass()
}
)
reader.read()
}
})
test('lossless number', t => {
const r = execSync(`echo '{"long": 123456789012345678901}' | node index.js .long`).toString('utf8')
t.is(r, '123456789012345678901\n')
})