1 | var tap = require('tap')
|
2 | var read = require('../lib/read.js')
|
3 |
|
4 | var CLOSE = 'close'
|
5 | if (process.version.match(/^v0\.6/)) {
|
6 | CLOSE = 'exit'
|
7 | }
|
8 |
|
9 | if (process.argv[2] === 'child') {
|
10 | return child()
|
11 | }
|
12 |
|
13 | var spawn = require('child_process').spawn
|
14 | function child () {
|
15 | read({prompt:'1'}, function (er, r1) {if (er) throw er
|
16 | read({prompt:'2'}, function (er, r2) {if (er) throw er
|
17 | read({prompt:'3'}, function (er, r3) {if (er) throw er
|
18 | read({prompt:'4'}, function (er, r4) {if (er) throw er
|
19 | read({prompt:'5'}, function (er, r5) {if (er) throw er
|
20 | read({prompt:'6'}, function (er, r6) {if (er) throw er
|
21 | read({prompt:'7'}, function (er, r7) {if (er) throw er
|
22 | read({prompt:'8'}, function (er, r8) {if (er) throw er
|
23 | read({prompt:'9'}, function (er, r9) {if (er) throw er
|
24 | read({prompt:'10'}, function (er, r10) {if (er) throw er
|
25 | read({prompt:'11'}, function (er, r11) {if (er) throw er
|
26 | read({prompt:'12'}, function (er, r12) {if (er) throw er
|
27 | read({prompt:'13'}, function (er, r13) {if (er) throw er
|
28 | read({prompt:'14'}, function (er, r14) {if (er) throw er
|
29 | read({prompt:'15'}, function (er, r15) {if (er) throw er
|
30 | read({prompt:'16'}, function (er, r16) {if (er) throw er
|
31 | read({prompt:'17'}, function (er, r17) {if (er) throw er
|
32 | read({prompt:'18'}, function (er, r18) {if (er) throw er
|
33 | console.log(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10,
|
34 | r11, r12, r13, r14, r15, r16, r17, r18)
|
35 | if (process.stdin.unref)
|
36 | process.stdin.unref()
|
37 | })})})})})})})})})})})})})})})})})})
|
38 | }
|
39 |
|
40 | tap.test('many reads', function (t) {
|
41 | var child = spawn(process.execPath, [__filename, 'child'])
|
42 | var n = 0
|
43 | var output = ''
|
44 | var expect = '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ' +
|
45 | '16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 ' +
|
46 | '13 14 15 16 17 18\n'
|
47 | var write = child.stdin.write.bind(child.stdin)
|
48 | var answers =
|
49 | [ '1\n',
|
50 | '2\n',
|
51 | '3\n',
|
52 | '4\n',
|
53 | '5\n',
|
54 | '6\n',
|
55 | '7\n',
|
56 | '8\n',
|
57 | '9\n',
|
58 | '10\n',
|
59 | '11\n',
|
60 | '12\n',
|
61 | '13\n',
|
62 | '14\n',
|
63 | '15\n',
|
64 | '16\n',
|
65 | '17\n',
|
66 | '18\n' ]
|
67 | child.stdout.on('data', function (c) {
|
68 | n++;
|
69 | output += c
|
70 | if (answers.length) {
|
71 | write(answers.shift())
|
72 | }
|
73 | })
|
74 | child.stderr.on('data', function (c) {
|
75 | output += c
|
76 | console.error('' + c)
|
77 | })
|
78 | child.on(CLOSE, function (c) {
|
79 | t.equal(output, expect)
|
80 | t.equal(n, 19)
|
81 | t.end()
|
82 | })
|
83 | })
|