@@ -33,4 +33,217 @@ public function testCtorArgsWillBeReturnedByGetters()
3333 $ this ->assertSame ($ output , $ stdio ->getOutput ());
3434 $ this ->assertSame ($ readline , $ stdio ->getReadline ());
3535 }
36+
37+ public function testWriteWillClearReadlineWriteOutputAndRestoreReadline ()
38+ {
39+ $ input = $ this ->getMock ('React\Stream\ReadableStreamInterface ' );
40+ $ output = $ this ->getMock ('React\Stream\WritableStreamInterface ' );
41+
42+ //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
43+ $ readline = new Readline ($ input , $ output );
44+ $ readline ->setPrompt ('> ' );
45+ $ readline ->setInput ('input ' );
46+
47+ $ stdio = new Stdio ($ this ->loop , $ input , $ output , $ readline );
48+
49+ $ buffer = '' ;
50+ $ output ->expects ($ this ->any ())->method ('write ' )->will ($ this ->returnCallback (function ($ data ) use (&$ buffer ) {
51+ $ buffer .= $ data ;
52+ }));
53+
54+ $ stdio ->write ('test ' );
55+
56+ $ this ->assertEquals ("\r\033[K " . "test \n" . "\r\033[K " . "> input " , $ buffer );
57+ }
58+
59+ public function testWriteAgainWillClearReadlineMoveToPreviousLineWriteOutputAndRestoreReadline ()
60+ {
61+ $ input = $ this ->getMock ('React\Stream\ReadableStreamInterface ' );
62+ $ output = $ this ->getMock ('React\Stream\WritableStreamInterface ' );
63+
64+ //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
65+ $ readline = new Readline ($ input , $ output );
66+ $ readline ->setPrompt ('> ' );
67+ $ readline ->setInput ('input ' );
68+
69+ $ stdio = new Stdio ($ this ->loop , $ input , $ output , $ readline );
70+
71+ $ stdio ->write ('hello ' );
72+
73+ $ buffer = '' ;
74+ $ output ->expects ($ this ->any ())->method ('write ' )->will ($ this ->returnCallback (function ($ data ) use (&$ buffer ) {
75+ $ buffer .= $ data ;
76+ }));
77+
78+ $ stdio ->write ('world ' );
79+
80+ $ this ->assertEquals ("\r\033[K " . "\033[A " . "\r\033[5C " . "world \n" . "\r\033[K " . "> input " , $ buffer );
81+ }
82+
83+ public function testWriteAgainWithBackspaceWillClearReadlineMoveToPreviousLineWriteOutputAndRestoreReadline ()
84+ {
85+ $ input = $ this ->getMock ('React\Stream\ReadableStreamInterface ' );
86+ $ output = $ this ->getMock ('React\Stream\WritableStreamInterface ' );
87+
88+ //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
89+ $ readline = new Readline ($ input , $ output );
90+ $ readline ->setPrompt ('> ' );
91+ $ readline ->setInput ('input ' );
92+
93+ $ stdio = new Stdio ($ this ->loop , $ input , $ output , $ readline );
94+
95+ $ stdio ->write ('hello! ' );
96+
97+ $ buffer = '' ;
98+ $ output ->expects ($ this ->any ())->method ('write ' )->will ($ this ->returnCallback (function ($ data ) use (&$ buffer ) {
99+ $ buffer .= $ data ;
100+ }));
101+
102+ $ stdio ->write ("\x08 world! " );
103+
104+ $ this ->assertEquals ("\r\033[K " . "\033[A " . "\r\033[6C " . "\x08 world! \n" . "\r\033[K " . "> input " , $ buffer );
105+ }
106+
107+ public function testWriteAgainWithNewlinesWillClearReadlineMoveToPreviousLineWriteOutputAndRestoreReadline ()
108+ {
109+ $ input = $ this ->getMock ('React\Stream\ReadableStreamInterface ' );
110+ $ output = $ this ->getMock ('React\Stream\WritableStreamInterface ' );
111+
112+ //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
113+ $ readline = new Readline ($ input , $ output );
114+ $ readline ->setPrompt ('> ' );
115+ $ readline ->setInput ('input ' );
116+
117+ $ stdio = new Stdio ($ this ->loop , $ input , $ output , $ readline );
118+
119+ $ stdio ->write ("first " . "\n" . "sec " );
120+
121+ $ buffer = '' ;
122+ $ output ->expects ($ this ->any ())->method ('write ' )->will ($ this ->returnCallback (function ($ data ) use (&$ buffer ) {
123+ $ buffer .= $ data ;
124+ }));
125+
126+ $ stdio ->write ("ond " . "\n" . "third " );
127+
128+ $ this ->assertEquals ("\r\033[K " . "\033[A " . "\r\033[3C " . "ond \nthird \n" . "\r\033[K " . "> input " , $ buffer );
129+ }
130+
131+ public function testWriteAfterReadlineInputWillClearReadlineWriteOutputAndRestoreReadline ()
132+ {
133+ $ input = $ this ->getMock ('React\Stream\ReadableStreamInterface ' );
134+ $ output = $ this ->getMock ('React\Stream\WritableStreamInterface ' );
135+
136+ //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
137+ $ readline = new Readline ($ input , $ output );
138+ $ readline ->setPrompt ('> ' );
139+
140+ $ stdio = new Stdio ($ this ->loop , $ input , $ output , $ readline );
141+
142+ $ stdio ->write ('incomplete ' );
143+
144+ $ readline ->emit ('data ' , array ('test ' ));
145+ $ readline ->setInput ('input ' );
146+
147+ $ buffer = '' ;
148+ $ output ->expects ($ this ->any ())->method ('write ' )->will ($ this ->returnCallback (function ($ data ) use (&$ buffer ) {
149+ $ buffer .= $ data ;
150+ }));
151+
152+ $ stdio ->writeln ('test ' );
153+
154+ $ this ->assertEquals ("\r\033[K " . "test \n" . "\r\033[K " . "> input " , $ buffer );
155+ }
156+
157+ public function testOverwriteWillClearReadlineMoveToPreviousLineWriteOutputAndRestoreReadline ()
158+ {
159+ $ input = $ this ->getMock ('React\Stream\ReadableStreamInterface ' );
160+ $ output = $ this ->getMock ('React\Stream\WritableStreamInterface ' );
161+
162+ //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
163+ $ readline = new Readline ($ input , $ output );
164+ $ readline ->setPrompt ('> ' );
165+ $ readline ->setInput ('input ' );
166+
167+ $ stdio = new Stdio ($ this ->loop , $ input , $ output , $ readline );
168+
169+ $ stdio ->write ('first ' );
170+
171+ $ buffer = '' ;
172+ $ output ->expects ($ this ->any ())->method ('write ' )->will ($ this ->returnCallback (function ($ data ) use (&$ buffer ) {
173+ $ buffer .= $ data ;
174+ }));
175+
176+ $ stdio ->overwrite ('overwrite ' );
177+
178+ $ this ->assertEquals ("\r\033[K " . "\033[A " . "\r\033[K " . "overwrite \n" . "\r\033[K " . "> input " , $ buffer );
179+ }
180+
181+ public function testOverwriteAfterNewlineWillClearReadlineAndWriteOutputAndRestoreReadline ()
182+ {
183+ $ input = $ this ->getMock ('React\Stream\ReadableStreamInterface ' );
184+ $ output = $ this ->getMock ('React\Stream\WritableStreamInterface ' );
185+
186+ //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
187+ $ readline = new Readline ($ input , $ output );
188+ $ readline ->setPrompt ('> ' );
189+ $ readline ->setInput ('input ' );
190+
191+ $ stdio = new Stdio ($ this ->loop , $ input , $ output , $ readline );
192+
193+ $ stdio ->write ("first \n" );
194+
195+ $ buffer = '' ;
196+ $ output ->expects ($ this ->any ())->method ('write ' )->will ($ this ->returnCallback (function ($ data ) use (&$ buffer ) {
197+ $ buffer .= $ data ;
198+ }));
199+
200+ $ stdio ->overwrite ('overwrite ' );
201+
202+ $ this ->assertEquals ("\r\033[K " . "overwrite \n" . "\r\033[K " . "> input " , $ buffer );
203+ }
204+
205+ public function testWriteLineWillClearReadlineWriteOutputAndRestoreReadline ()
206+ {
207+ $ input = $ this ->getMock ('React\Stream\ReadableStreamInterface ' );
208+ $ output = $ this ->getMock ('React\Stream\WritableStreamInterface ' );
209+
210+ //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
211+ $ readline = new Readline ($ input , $ output );
212+ $ readline ->setPrompt ('> ' );
213+ $ readline ->setInput ('input ' );
214+
215+ $ stdio = new Stdio ($ this ->loop , $ input , $ output , $ readline );
216+
217+ $ buffer = '' ;
218+ $ output ->expects ($ this ->any ())->method ('write ' )->will ($ this ->returnCallback (function ($ data ) use (&$ buffer ) {
219+ $ buffer .= $ data ;
220+ }));
221+
222+ $ stdio ->writeln ('test ' );
223+
224+ $ this ->assertEquals ("\r\033[K " . "test \n" . "\r\033[K " . "> input " , $ buffer );
225+ }
226+
227+ public function testWriteTwoLinesWillClearReadlineWriteOutputAndRestoreReadline ()
228+ {
229+ $ input = $ this ->getMock ('React\Stream\ReadableStreamInterface ' );
230+ $ output = $ this ->getMock ('React\Stream\WritableStreamInterface ' );
231+
232+ //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
233+ $ readline = new Readline ($ input , $ output );
234+ $ readline ->setPrompt ('> ' );
235+ $ readline ->setInput ('input ' );
236+
237+ $ stdio = new Stdio ($ this ->loop , $ input , $ output , $ readline );
238+
239+ $ buffer = '' ;
240+ $ output ->expects ($ this ->any ())->method ('write ' )->will ($ this ->returnCallback (function ($ data ) use (&$ buffer ) {
241+ $ buffer .= $ data ;
242+ }));
243+
244+ $ stdio ->writeln ('hello ' );
245+ $ stdio ->writeln ('world ' );
246+
247+ $ this ->assertEquals ("\r\033[K " . "hello \n" . "\r\033[K " . "> input " . "\r\033[K " . "world \n" . "\r\033[K " . "> input " , $ buffer );
248+ }
36249}
0 commit comments