Skip to content

Commit

Permalink
Merge pull request #28 from clue-labs/data
Browse files Browse the repository at this point in the history
Support Readline::setInput() during 'line' event
  • Loading branch information
clue committed Jun 19, 2015
2 parents 79cc794 + f68c5ff commit 30bd22a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/Readline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions tests/ReadlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 30bd22a

Please sign in to comment.