[Console] Don't go past exact matches when autocompleting

This commit is contained in:
Nicolas Grekas 2018-04-09 12:07:56 -05:00
parent 16edba5d99
commit adba79a6b0
2 changed files with 25 additions and 1 deletions

View File

@ -284,7 +284,7 @@ class QuestionHelper extends Helper
foreach ($autocomplete as $value) {
// If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle)
if (0 === strpos($value, $ret) && $i !== strlen($value)) {
if (0 === strpos($value, $ret)) {
$matches[$numMatches++] = $value;
}
}

View File

@ -160,6 +160,30 @@ class QuestionHelperTest extends TestCase
$this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
}
public function testAskWithAutocompleteWithExactMatch()
{
if (!$this->hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}
$inputStream = $this->getInputStream("b\n");
$possibleChoices = array(
'a' => 'berlin',
'b' => 'copenhagen',
'c' => 'amsterdam',
);
$dialog = new QuestionHelper();
$dialog->setInputStream($inputStream);
$dialog->setHelperSet(new HelperSet(array(new FormatterHelper())));
$question = new ChoiceQuestion('Please select a city', $possibleChoices);
$question->setMaxAttempts(1);
$this->assertSame('b', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
}
public function testAutocompleteWithTrailingBackslash()
{
if (!$this->hasSttyAvailable()) {