Skip to content

Commit

Permalink
Ensure okta start prompt order is sorted correctly (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemers authored Feb 28, 2022
1 parent 4cfe9da commit 9559cd0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cli/src/main/java/com/okta/cli/console/DefaultPrompter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -89,7 +92,7 @@ public <T> T prompt(String message, List<PromptOption<T>> options, PromptOption<

Map<String, PromptOption<T>> choices = IntStream.range(0, options.size())
.boxed()
.collect(Collectors.toMap(index -> Integer.toString(index + 1), options::get));
.collect(orderedMap(index -> Integer.toString(index + 1), options::get));

out.write(message + "\n");
choices.forEach((key, value) -> {
Expand Down Expand Up @@ -127,4 +130,9 @@ public void close() throws IOException {
out.close();
consoleReader.close();
}

private static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> orderedMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper) {
return (Collector<T, ?, M>) Collectors.toMap(keyMapper, valueMapper, (x, y) -> y, LinkedHashMap::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,50 @@ class DefaultPrompterTest {
assertThat(outputStream.toString(), equalTo(expectedOutput))
}

@Test
void promptOptions_moreThan10() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
ConsoleOutput out = new ConsoleOutput.AnsiConsoleOutput(new PrintStream(outputStream), true)

expectInput("1")
DefaultPrompter prompter = new DefaultPrompter(out)

def options = [
new StubPromptOption("one", "one-1"),
new StubPromptOption("two", "two-2"),
new StubPromptOption("three", "three-3"),
new StubPromptOption("four", "four-4"),
new StubPromptOption("five", "five-5"),
new StubPromptOption("six", "six-6"),
new StubPromptOption("seven", "seven-7"),
new StubPromptOption("eight", "eight-8"),
new StubPromptOption("nine", "nine-9"),
new StubPromptOption("ten", "ten-10"),
new StubPromptOption("eleven", "eleven-11"),
new StubPromptOption("twelve", "twelve-12")
]
String result = prompter.prompt("hello", options, options[0])

// ansi colors
String expectedOutput = "hello\n" +
"\u001B[1m> 1: \u001B[0mone\n" +
"\u001B[1m> 2: \u001B[0mtwo\n" +
"\u001B[1m> 3: \u001B[0mthree\n" +
"\u001B[1m> 4: \u001B[0mfour\n" +
"\u001B[1m> 5: \u001B[0mfive\n" +
"\u001B[1m> 6: \u001B[0msix\n" +
"\u001B[1m> 7: \u001B[0mseven\n" +
"\u001B[1m> 8: \u001B[0meight\n" +
"\u001B[1m> 9: \u001B[0mnine\n" +
"\u001B[1m> 10: \u001B[0mten\n" +
"\u001B[1m> 11: \u001B[0meleven\n" +
"\u001B[1m> 12: \u001B[0mtwelve\n" +
"Enter your choice [one]: "

assertThat(result, equalTo("one-1"))
assertThat(outputStream.toString(), equalTo(expectedOutput))
}

@Test
void failToReadLine() {
ConsoleOutput out = mock(ConsoleOutput)
Expand Down

0 comments on commit 9559cd0

Please sign in to comment.