fx/test.js

33 lines
877 B
JavaScript
Raw Normal View History

2018-01-26 02:52:08 +00:00
'use strict'
const test = require('ava')
const {execSync} = require('child_process')
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-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
})
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
})