@@ -100,7 +100,7 @@ describe('PythonShell', function () {
100
100
var pyshell = new PythonShell ( 'conversation.py' , {
101
101
mode : 'json'
102
102
} ) ;
103
- var output = '' ;
103
+ var outputs = [ ] ;
104
104
105
105
function makeKnockKnockMessage ( message ) {
106
106
return {
@@ -109,21 +109,31 @@ describe('PythonShell', function () {
109
109
} ;
110
110
}
111
111
112
+ var outgoingMessages = [
113
+ "Knock, knock." ,
114
+ "Orange." ,
115
+ "Orange you glad I didn't say, 'banana'?"
116
+ ] ;
117
+
118
+ var incomingMessages = [
119
+ "Who's there?" ,
120
+ "Orange who?" ,
121
+ "Ha ha."
122
+ ] ;
123
+
112
124
var makeKnockKnockReply = makeKnockKnockMessage ;
113
125
114
126
function handleReply ( reply ) {
115
127
switch ( reply . message ) {
116
- case "Who's there?" :
128
+ case incomingMessages [ 0 ] :
117
129
// awaitReply();
118
- pyshell . send ( makeKnockKnockMessage ( 'Orange.' ) ) ;
119
- flushStdIn ( ) ;
130
+ pyshell . send ( makeKnockKnockMessage ( outgoingMessages [ 1 ] ) ) ;
120
131
break ;
121
- case "Orange who?" :
132
+ case incomingMessages [ 1 ] :
122
133
// awaitReply();
123
- pyshell . send ( makeKnockKnockMessage ( "Orange you glad I didn't say, 'banana'?" ) ) ;
124
- flushStdIn ( ) ;
134
+ pyshell . send ( makeKnockKnockMessage ( outgoingMessages [ 2 ] ) ) ;
125
135
break ;
126
- case "Ha ha." :
136
+ case incomingMessages [ 2 ] :
127
137
endAndAssert ( ) ;
128
138
break ;
129
139
}
@@ -132,11 +142,11 @@ describe('PythonShell', function () {
132
142
}
133
143
134
144
// function awaitReply() {
135
- pyshell . stdout . on ( 'data' , function ( data ) {
136
- output += '' + data ;
137
-
145
+ pyshell . on ( 'message' , function ( data ) {
138
146
console . log ( "Data to stdout: " , data ) ;
139
147
148
+ outputs . push ( data ) ;
149
+
140
150
switch ( data . action ) {
141
151
case 'knockknockjoke' :
142
152
handleReply ( data ) ;
@@ -146,7 +156,7 @@ describe('PythonShell', function () {
146
156
}
147
157
} ) ;
148
158
149
- pyshell . stdout . resume ( ) ;
159
+ // pyshell.stdout.resume();
150
160
// }
151
161
152
162
// function awaitReply() {
@@ -165,9 +175,9 @@ describe('PythonShell', function () {
165
175
// });
166
176
// }
167
177
168
- pyshell . stderr . on ( 'data' , function ( err ) {
169
- console . error ( err ) ;
170
- } ) ;
178
+ // pyshell.stderr.on('data', function(err) {
179
+ // console.error(err);
180
+ // });
171
181
172
182
pyshell . on ( 'close' , function ( err ) {
173
183
if ( err ) {
@@ -176,44 +186,25 @@ describe('PythonShell', function () {
176
186
return done ( 'Unexpectedly closed.' ) ;
177
187
} ) ;
178
188
// awaitReply();
179
- pyshell . send ( makeKnockKnockMessage ( 'Knock, knock.' ) ) ;
180
- flushStdIn ( ) ;
181
-
182
- function flushStdIn ( ) {
183
- // pyshell.stdin.write(os.EOF);
184
- pyshell . pauseInput ( ) ;
185
- // pyshell.flushInput();
186
- }
189
+ pyshell . send ( makeKnockKnockMessage ( outgoingMessages [ 0 ] ) ) ;
187
190
188
191
function endAndAssert ( ) {
189
192
pyshell . end ( function ( err ) {
190
193
if ( err ) {
191
194
return done ( err ) ;
192
195
}
193
- var outputs = output . split ( "\n" ) ;
194
-
195
- var parsedOutputs = [ ] ;
196
- outputs
197
- . slice ( 0 , 3 )
198
- . forEach ( function ( outputRaw ) {
199
- try {
200
- var parsed = JSON . parse ( outputRaw ) ;
201
- parsedOutputs . push ( parsed ) ;
202
- } catch ( err ) {
203
- done ( err ) ;
204
- }
205
- } ) ;
196
+ console . log ( outputs ) ;
206
197
207
- should ( parsedOutputs [ 0 ] )
208
- . eql ( makeKnockKnockReply ( "Who's there?" ) ,
198
+ should ( outputs [ 0 ] )
199
+ . eql ( makeKnockKnockReply ( incomingMessages [ 0 ] ) ,
209
200
"Correct knock-knock reply received." ) ;
210
201
211
- should ( parsedOutputs [ 1 ] )
212
- . eql ( makeKnockKnockReply ( "Orange who?" ) ,
202
+ should ( outputs [ 1 ] )
203
+ . eql ( makeKnockKnockReply ( incomingMessages [ 1 ] ) ,
213
204
"Correct knock-knock reply received." ) ;
214
205
215
- should ( parsedOutputs [ 2 ] )
216
- . eql ( makeKnockKnockReply ( "Ha ha." ) ,
206
+ should ( outputs [ 2 ] )
207
+ . eql ( makeKnockKnockReply ( incomingMessages [ 1 ] ) ,
217
208
"Correct knock-knock reply received." ) ;
218
209
219
210
done ( ) ;
0 commit comments