-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
131 lines (120 loc) · 3.63 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
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
/* global describe, it, after */
'use strict'
const BufferHelper = require('bufferhelper')
const assert = require('assert')
const cp = require('child_process')
const fs = require('fs')
console.log('process.platform', process.platform)
let nodeCmd = 'node'
if (process.platform === 'win32') {
nodeCmd = require('which').sync('node')
}
console.log('nodeCmd', nodeCmd)
// todo: more test cases
describe('jayin', () => {
it ('x', (done) => {
const helper = new BufferHelper()
const js = cp.spawn(nodeCmd, ['./index.js', 'x.slice(2, 4)'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '[3,4]')
done()
})
js.stdout.on('data', (chunk) => {
helper.concat(chunk)
})
js.stdin.end('[1,2,3,4,5]')
})
it('-t', (done) => {
const helper = new BufferHelper()
const js = cp.spawn(nodeCmd, ['./index.js', '-t', 'x.slice(1, -1).replace(/,/g, `\n`)'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '1\n2\n3\n4\n5')
done()
})
js.stdout.on('data', (chunk) => {
helper.concat(chunk)
})
js.stdin.end('[1,2,3,4,5]')
})
it('-ti', (done) => {
const helper = new BufferHelper()
const js = cp.spawn(nodeCmd, ['./index.js', '-ti', 'a=JSON.parse(x), a.push(999), a'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '[1,2,3,4,5,999]')
done()
})
js.stdout.on('data', (chunk) => {
helper.concat(chunk)
})
js.stdin.end('[1,2,3,4,5]')
})
it('-to', (done) => {
const helper = new BufferHelper()
const js = cp.spawn(nodeCmd, ['./index.js', '-to', 'x.join(`\n`)'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '1\n2\n3\n4\n5')
done()
})
js.stdout.on('data', (chunk) => {
helper.concat(chunk)
})
js.stdin.end('[1,2,3,4,5]')
})
it('exprs', (done) => {
const helper = new BufferHelper()
const js = cp.spawn(nodeCmd, ['./index.js', 'x.filter(x => x % 2)', 'x.reverse()'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '[3,1]')
done()
})
js.stdout.on('data', (chunk) => {
helper.concat(chunk)
})
js.stdin.end('[1,2,3,4]')
})
it('no expr', (done) => {
const helper = new BufferHelper()
const js = cp.spawn(nodeCmd, ['./index.js'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '[1,2,3,4]')
done()
})
js.stdout.on('data', (chunk) => {
helper.concat(chunk)
})
js.stdin.end('[1,2,3,4]')
})
it('_, _.filter, _.reverse', (done) => {
const helper = new BufferHelper()
const js = cp.spawn(nodeCmd, ['./index.js', '_(x).filter(x => x % 2).reverse().value()'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '[3,1]')
done()
})
js.stdout.on('data', (chunk) => {
helper.concat(chunk)
})
js.stdin.end('[1,2,3,4]')
})
it ('i: x, -e, -c', (done) => {
// https://www.shell-tips.com/2010/06/14/performing-math-calculation-in-bash/
// const env = { count: 0 }
// execSync('count=0')
const helper = new BufferHelper()
const js = cp.spawn(nodeCmd, ['./index.js', '-e', '-c', "(echo ${i}: ${x}) >> tmp"])
js.stdout.once('end', () => {
// assert.equal(execSync('echo $count').toString(), '15')
// assert.equal(env.count, '15')
assert.equal(helper.toString(), '["a","b","c"]') // in chain
assert.equal(cp.execSync('cat tmp').toString().replace(/\r\n/g, '\n'), '0: a\n1: b\n2: c\n')
done()
})
js.stdout.on('data', (chunk) => {
helper.concat(chunk)
})
js.stdin.end('["a","b","c"]')
})
after(() => {
fs.unlinkSync('tmp')
})
})