2018-01-26 02:52:08 +00:00
|
|
|
'use strict'
|
|
|
|
const test = require('ava')
|
|
|
|
const {execSync} = require('child_process')
|
2019-02-25 19:06:12 +00:00
|
|
|
const stream = require('./stream')
|
2018-01-26 02:52:08 +00:00
|
|
|
|
|
|
|
function fx(json, code = '') {
|
2018-08-30 15:09:12 +00:00
|
|
|
return execSync(`echo '${JSON.stringify(json)}' | node index.js ${code}`).toString('utf8')
|
2018-01-26 02:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test('pass', t => {
|
2018-08-30 15:09:12 +00:00
|
|
|
const r = fx([{"greeting": "hello world"}])
|
|
|
|
t.deepEqual(JSON.parse(r), [{"greeting": "hello world"}])
|
2018-01-26 02:52:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('anon func', t => {
|
2018-12-07 16:33:55 +00:00
|
|
|
const r = fx({"key": "value"}, "'function (x) { return x.key }'")
|
|
|
|
t.deepEqual(r, 'value\n')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('arrow func', t => {
|
2018-08-30 15:09:12 +00:00
|
|
|
const r = fx({"key": "value"}, "'x => x.key'")
|
|
|
|
t.deepEqual(r, 'value\n')
|
2018-01-26 02:52:08 +00:00
|
|
|
})
|
|
|
|
|
2018-12-07 16:33:55 +00:00
|
|
|
test('arrow func ()', t => {
|
|
|
|
const r = fx({"key": "value"}, "'(x) => x.key'")
|
|
|
|
t.deepEqual(r, 'value\n')
|
|
|
|
})
|
|
|
|
|
2018-01-26 02:52:08 +00:00
|
|
|
test('this bind', t => {
|
2018-08-30 15:09:12 +00:00
|
|
|
const r = fx([1, 2, 3, 4, 5], "'this.map(x => x * this.length)'")
|
|
|
|
t.deepEqual(JSON.parse(r), [5, 10, 15, 20, 25])
|
2018-01-26 02:52:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('generator', t => {
|
2018-08-30 15:09:12 +00:00
|
|
|
const r = fx([1, 2, 3, 4, 5], "'for (let i of this) if (i % 2 == 0) yield i'")
|
|
|
|
t.deepEqual(JSON.parse(r), [2, 4])
|
2018-01-26 02:52:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('chain', t => {
|
2018-08-30 15:09:12 +00:00
|
|
|
const r = fx({"items": ["foo", "bar"]}, "'this.items' 'yield* this' 'x => x[1]'")
|
|
|
|
t.deepEqual(r, 'bar\n')
|
2018-01-26 02:52:08 +00:00
|
|
|
})
|
2019-02-24 14:22:55 +00:00
|
|
|
|
|
|
|
test('file argument', t => {
|
|
|
|
const r = execSync(`node index.js package.json .name`).toString('utf8')
|
|
|
|
t.deepEqual(r, 'fx\n')
|
|
|
|
})
|
2019-02-25 19:06:12 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|