From f68c5fff4b050ad4607ea1ef38cc30199778d5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 12 Jun 2015 01:31:26 +0200 Subject: [PATCH] Support Readline::setInput() during 'line' event --- src/Readline.php | 13 ++++++------- tests/ReadlineTest.php | 27 +++++++++++++++++++++++++++ tests/bootstrap.php | 11 +++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/Readline.php b/src/Readline.php index ac6ad58..1b22964 100644 --- a/src/Readline.php +++ b/src/Readline.php @@ -537,21 +537,20 @@ public function deleteChar($n) /** * process the current line buffer, emit event and redraw empty line + * + * @uses self::setInput() */ protected function processLine() { + // store and reset/clear/redraw current input $line = $this->linebuffer; + $this->setInput(''); - $this->emit('data', array($line)); - + // process stored input buffer if ($this->history !== null) { $this->history->addLine($line); } - - $this->linebuffer = ''; - $this->linepos = 0; - - $this->redraw(); + $this->emit('data', array($line)); } protected function strlen($str) diff --git a/tests/ReadlineTest.php b/tests/ReadlineTest.php index d6b38c6..132977c 100644 --- a/tests/ReadlineTest.php +++ b/tests/ReadlineTest.php @@ -449,6 +449,33 @@ public function testCursorCellObeysCustomEchoAsterisk(Readline $readline) $this->assertEquals(3, $readline->getCursorCell()); } + public function testEmitEmptyInputOnEnter() + { + $this->readline->on('data', $this->expectCallableOnceWith('')); + $this->readline->onKeyEnter(); + } + + public function testEmitInputOnEnterAndClearsInput() + { + $this->readline->setInput('test'); + $this->readline->on('data', $this->expectCallableOnceWith('test')); + $this->readline->onKeyEnter(); + + $this->assertEquals('', $this->readline->getInput()); + } + + public function testSetInputDuringEmitKeepsInput() + { + $readline = $this->readline; + + $readline->on('data', function ($line) use ($readline) { + $readline->setInput('test'); + }); + $readline->onKeyEnter(); + + $this->assertEquals('test', $readline->getInput()); + } + private function pushInputBytes(Readline $readline, $bytes) { foreach (str_split($bytes, 1) as $byte) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e7d3165..4f81975 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -26,6 +26,17 @@ protected function expectCallableNever() return $mock; } + protected function expectCallableOnceWith($value) + { + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->equalTo($value)); + + return $mock; + } + protected function expectCallableOnceParameter($type) { $mock = $this->createCallableMock();