Skip to content

Commit

Permalink
[Console] Add another test and some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
battye committed May 4, 2019
1 parent 9fbfe1c commit df84a50
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
54 changes: 27 additions & 27 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Expand Up @@ -303,10 +303,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
if ($numMatches > 0 && -1 !== $ofs) {
$ret = $matches[$ofs];
// Echo out remaining chars for current match
$remainingCharacters = substr($ret, strlen(trim($this->mostRecentlyEnteredValue($fullChoice))));
$remainingCharacters = substr($ret, \strlen(trim($this->mostRecentlyEnteredValue($fullChoice))));
$output->write($remainingCharacters);
$fullChoice .= $remainingCharacters;
$i = strlen($fullChoice);
$i = \strlen($fullChoice);
}

if ("\n" === $c) {
Expand All @@ -330,9 +330,8 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu

$tempRet = $ret;

if ($question instanceof ChoiceQuestion && $question->isMultiselect())
{
$tempRet = $this->mostRecentlyEnteredValue($ret);
if ($question instanceof ChoiceQuestion && $question->isMultiselect()) {
$tempRet = $this->mostRecentlyEnteredValue($fullChoice);
}

$numMatches = 0;
Expand All @@ -353,7 +352,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
// Save cursor position
$output->write("\0337");
// Write highlighted text, complete the partially entered response
$charactersEntered = strlen(trim($this->mostRecentlyEnteredValue($ret)));
$charactersEntered = \strlen(trim($this->mostRecentlyEnteredValue($fullChoice)));
$output->write('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $charactersEntered)).'</hl>');
// Restore cursor position
$output->write("\0338");
Expand All @@ -366,27 +365,28 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
return $fullChoice;
}

/**
* Determine the most recent value the user entered
* @param $ret
* @return string
*/
private function mostRecentlyEnteredValue($ret)
{
$tempRet = $ret;

if (strpos($ret, ',') !== false)
{
$choices = explode(',', $ret);
$lastChoice = trim($choices[count($choices) - 1]);
if (strlen($lastChoice) > 0)
{
$tempRet = $lastChoice;
}
}

return $tempRet;
}
/**
* Determine the most recent value the user entered.
*
* @param $entered
*
* @return string
*/
private function mostRecentlyEnteredValue($entered)
{
$tempEntered = $entered;

if (false !== strpos($entered, ',')) {
$choices = explode(',', $entered);
$lastChoice = trim($choices[\count($choices) - 1]);

if (\strlen($lastChoice) > 0) {
$tempEntered = $lastChoice;
}
}

return $tempEntered;
}

/**
* Gets a hidden response from user.
Expand Down
Expand Up @@ -1020,15 +1020,13 @@ public function testTraversableAutocomplete()

public function testTraversableMultiselectAutocomplete()
{
// Test cases:
// 1) default; 2) Tab single; 3) Traverse and tab multiple; 4) Backspace and traverse multiple; 5) Backspace all

// <NEWLINE>
// F<TAB><NEWLINE>
// A<3x UP ARROW><TAB>,F<TAB><NEWLINE>
// F00<BACKSPACE><BACKSPACE>o<TAB>,A<DOWN ARROW>,<SPACE>SecurityBundle<NEWLINE>
// Acme<TAB> ,<SPACE>As<TAB><29x BACKSPACE>S<TAB><NEWLINE>
$inputStream = $this->getInputStream("\nF\t\nA\033[A\033[A\033[A\t,F\t\nF00\177\177o\t,A\033[B\t, SecurityBundle\nAcme\t, As\t\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177S\t\n");
// Acme<TAB>,<SPACE>As<TAB><29x BACKSPACE>S<TAB><NEWLINE>
// Ac<TAB>,As<TAB><3x BACKSPACE>d<TAB><NEWLINE>
$inputStream = $this->getInputStream("\nF\t\nA\033[A\033[A\033[A\t,F\t\nF00\177\177o\t,A\033[B\t, SecurityBundle\nAcme\t, As\t\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177S\t\nAc\t,As\t\177\177\177d\t\n");

$dialog = new QuestionHelper();
$helperSet = new HelperSet([new FormatterHelper()]);
Expand All @@ -1048,6 +1046,7 @@ public function testTraversableMultiselectAutocomplete()
$this->assertEquals(['AsseticBundle', 'FooBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
$this->assertEquals(['FooBundle', 'AsseticBundle', 'SecurityBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
$this->assertEquals(['SecurityBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
$this->assertEquals(['AcmeDemoBundle', 'AsseticBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
}

protected function getInputStream($input)
Expand Down

0 comments on commit df84a50

Please sign in to comment.