Skip to content

Commit

Permalink
picocli fish completion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
serg-v committed Jan 22, 2023
1 parent 1af8274 commit cbb3219
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ picocli.iml
/src/test/dejagnu.tests/testrun.log
/src/test/dejagnu.tests/testrun.sum
/src/test/dejagnu.tests/tmp/
/src/test/dejagnu.fishtests/log/completion.sum
/src/test/dejagnu.fishtests/log/completion.log
/picocli-legacy-tests/gradle/wrapper/dists/**/*.lck
/picocli-legacy-tests/gradle/wrapper/dists/**/*.ok
19 changes: 9 additions & 10 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,22 +554,18 @@ public static String fish(String scriptName, CommandLine commandLine) {
continue;
}
if (!descriptor.parentFunctionName.equals(parentFunction)) {
if (!currentLevelCommands.isEmpty()) {
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
rootDescriptor = null;
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
rootDescriptor = null;

currentLevel.clear();
currentLevelCommands.clear();
}
currentLevel.clear();
currentLevelCommands.clear();
parentFunction = descriptor.parentFunctionName;
}

currentLevel.add(descriptor);
currentLevelCommands.add(descriptor.commandName);
}
if (!currentLevelCommands.isEmpty()) {
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
}
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);


return result.toString();
Expand All @@ -578,7 +574,10 @@ public static String fish(String scriptName, CommandLine commandLine) {
private static void processLevel(String scriptName, StringBuilder result, List<CommandDescriptor> currentLevel,
List<String> currentLevelCommands, String levelName,
CommandDescriptor rootDescriptor) {
result.append("\n# ").append(levelName).append(" completion \n");
if (levelName.equals("")) {
levelName = "root";
}
result.append("\n# ").append(levelName).append(" completion\n");
result.append("set -l ").append(levelName).append(" ").append(String.join(" ", currentLevelCommands)).append(
"\n");
if (rootDescriptor != null) {
Expand Down
4 changes: 4 additions & 0 deletions src/test/dejagnu.fishtests/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Run
```
runtest --outdir log --tool completion
```
29 changes: 29 additions & 0 deletions src/test/dejagnu.fishtests/completion/simple.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
exp_internal 1
set timeout 1

proc run_completion_test {cmd test candidates} {
send "${cmd}\t"
expect {
-re "(\n${candidates}\u001b)" { pass $test }
timeout { fail $test }
}
puts "###Output"
puts "$expect_out(1,string)"
send "\x03"
expect ">"
}

set cmd "basicExample -"
set test "Tab should show options for '$cmd'"
set candidates "-t -u --timeout --timeUnit"
run_completion_test $cmd $test $candidates

set cmd "basicExample --"
set test "Tab should show options for '$cmd'"
set candidates "--timeout --timeUnit"
run_completion_test $cmd $test $candidates

set cmd "basicExample --timeUnit "
set test "Tab should show time unit enum values for '$cmd'"
set candidates "DAYS\\s*HOURS\\s*MICROSECONDS\\s*MILLISECONDS\\s*MINUTES\\s*NANOSECONDS\\s*SECONDS\r\n/@${cmd}"
run_completion_test $cmd $test $candidates
8 changes: 8 additions & 0 deletions src/test/dejagnu.fishtests/lib/completion.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exp_spawn fish --no-config
expect -re "(.+>)"

send "source ../resources/basic.fish\r"
expect -re "(.+>)"

send "function basicExample; echo 'do'; end\r"
expect -re "(.+>)"
8 changes: 8 additions & 0 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public void basic() throws Exception {
assertEquals(expected, script);
}

@Test
public void basicFish() throws Exception {
String script = AutoComplete.fish("basicExample", new CommandLine(new BasicExample()));
System.out.println(script);
String expected = loadTextFromClasspath("/basic.fish");
assertEquals(expected, script);
}

public static class TopLevel {
@Option(names = {"-V", "--version"}, help = true) boolean versionRequested;
@Option(names = {"-h", "--help"}, help = true) boolean helpRequested;
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/basic.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

# root completion
set -l root
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -l timeUnit -d ''
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -s u -d ''
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -l timeout -d ''
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -s t -d ''

0 comments on commit cbb3219

Please sign in to comment.