diff --git a/cli/src/main/java/com/devonfw/tools/ide/completion/CompletionCandidateCollector.java b/cli/src/main/java/com/devonfw/tools/ide/completion/CompletionCandidateCollector.java index 9d1c64f1f..27fd2e709 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/completion/CompletionCandidateCollector.java +++ b/cli/src/main/java/com/devonfw/tools/ide/completion/CompletionCandidateCollector.java @@ -57,7 +57,7 @@ default int addAllMatches(String text, String[] sortedCandidates, Property pr index++; count++; } else { - index = -index; + index = -index - 1; } while ((index >= 0) && (index < sortedCandidates.length)) { if (sortedCandidates[index].startsWith(text)) { diff --git a/cli/src/test/java/com/devonfw/tools/ide/completion/CompletionCandidateCollectorDefaultTest.java b/cli/src/test/java/com/devonfw/tools/ide/completion/CompletionCandidateCollectorDefaultTest.java new file mode 100644 index 000000000..d0ed48464 --- /dev/null +++ b/cli/src/test/java/com/devonfw/tools/ide/completion/CompletionCandidateCollectorDefaultTest.java @@ -0,0 +1,34 @@ +package com.devonfw.tools.ide.completion; + +import com.devonfw.tools.ide.commandlet.Commandlet; +import com.devonfw.tools.ide.commandlet.VersionCommandlet; +import com.devonfw.tools.ide.context.AbstractIdeContextTest; +import com.devonfw.tools.ide.context.IdeContext; +import com.devonfw.tools.ide.context.IdeTestContextMock; +import com.devonfw.tools.ide.property.Property; +import com.devonfw.tools.ide.property.VersionProperty; +import org.junit.jupiter.api.Test; + +class CompletionCandidateCollectorDefaultTest extends AbstractIdeContextTest { + + /** + * Test of {@link CompletionCandidateCollectorDefault#addAllMatches(String, String[], Property, Commandlet)} + */ + @Test + public void testAddAllMatches() { + + String[] sortedCandidates = { "1", "2.0", "2.1", "3" }; + String input = "2"; + String[] expectedCandidates = { "2.0", "2.1" }; + + VersionProperty versionProperty = new VersionProperty("", false, "version"); + IdeContext context = IdeTestContextMock.get(); + CompletionCandidateCollector collector = new CompletionCandidateCollectorDefault(context); + + int matches = collector.addAllMatches(input, sortedCandidates, versionProperty, new VersionCommandlet(context)); + + assertThat(matches).isEqualTo(2); + assertThat(collector.getCandidates().stream().map(CompletionCandidate::text)).containsExactly(expectedCandidates); + + } +} \ No newline at end of file