Skip to content

Commit cbb3219

Browse files
author
serg-v
committed
picocli fish completion tests
1 parent 1af8274 commit cbb3219

File tree

7 files changed

+67
-10
lines changed

7 files changed

+67
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ picocli.iml
2121
/src/test/dejagnu.tests/testrun.log
2222
/src/test/dejagnu.tests/testrun.sum
2323
/src/test/dejagnu.tests/tmp/
24+
/src/test/dejagnu.fishtests/log/completion.sum
25+
/src/test/dejagnu.fishtests/log/completion.log
2426
/picocli-legacy-tests/gradle/wrapper/dists/**/*.lck
2527
/picocli-legacy-tests/gradle/wrapper/dists/**/*.ok

src/main/java/picocli/AutoComplete.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -554,22 +554,18 @@ public static String fish(String scriptName, CommandLine commandLine) {
554554
continue;
555555
}
556556
if (!descriptor.parentFunctionName.equals(parentFunction)) {
557-
if (!currentLevelCommands.isEmpty()) {
558-
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
559-
rootDescriptor = null;
557+
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
558+
rootDescriptor = null;
560559

561-
currentLevel.clear();
562-
currentLevelCommands.clear();
563-
}
560+
currentLevel.clear();
561+
currentLevelCommands.clear();
564562
parentFunction = descriptor.parentFunctionName;
565563
}
566564

567565
currentLevel.add(descriptor);
568566
currentLevelCommands.add(descriptor.commandName);
569567
}
570-
if (!currentLevelCommands.isEmpty()) {
571-
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
572-
}
568+
processLevel(scriptName, result, currentLevel, currentLevelCommands, parentFunction, rootDescriptor);
573569

574570

575571
return result.toString();
@@ -578,7 +574,10 @@ public static String fish(String scriptName, CommandLine commandLine) {
578574
private static void processLevel(String scriptName, StringBuilder result, List<CommandDescriptor> currentLevel,
579575
List<String> currentLevelCommands, String levelName,
580576
CommandDescriptor rootDescriptor) {
581-
result.append("\n# ").append(levelName).append(" completion \n");
577+
if (levelName.equals("")) {
578+
levelName = "root";
579+
}
580+
result.append("\n# ").append(levelName).append(" completion\n");
582581
result.append("set -l ").append(levelName).append(" ").append(String.join(" ", currentLevelCommands)).append(
583582
"\n");
584583
if (rootDescriptor != null) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Run
2+
```
3+
runtest --outdir log --tool completion
4+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
exp_internal 1
2+
set timeout 1
3+
4+
proc run_completion_test {cmd test candidates} {
5+
send "${cmd}\t"
6+
expect {
7+
-re "(\n${candidates}\u001b)" { pass $test }
8+
timeout { fail $test }
9+
}
10+
puts "###Output"
11+
puts "$expect_out(1,string)"
12+
send "\x03"
13+
expect ">"
14+
}
15+
16+
set cmd "basicExample -"
17+
set test "Tab should show options for '$cmd'"
18+
set candidates "-t -u --timeout --timeUnit"
19+
run_completion_test $cmd $test $candidates
20+
21+
set cmd "basicExample --"
22+
set test "Tab should show options for '$cmd'"
23+
set candidates "--timeout --timeUnit"
24+
run_completion_test $cmd $test $candidates
25+
26+
set cmd "basicExample --timeUnit "
27+
set test "Tab should show time unit enum values for '$cmd'"
28+
set candidates "DAYS\\s*HOURS\\s*MICROSECONDS\\s*MILLISECONDS\\s*MINUTES\\s*NANOSECONDS\\s*SECONDS\r\n/@${cmd}"
29+
run_completion_test $cmd $test $candidates
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exp_spawn fish --no-config
2+
expect -re "(.+>)"
3+
4+
send "source ../resources/basic.fish\r"
5+
expect -re "(.+>)"
6+
7+
send "function basicExample; echo 'do'; end\r"
8+
expect -re "(.+>)"

src/test/java/picocli/AutoCompleteTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ public void basic() throws Exception {
102102
assertEquals(expected, script);
103103
}
104104

105+
@Test
106+
public void basicFish() throws Exception {
107+
String script = AutoComplete.fish("basicExample", new CommandLine(new BasicExample()));
108+
System.out.println(script);
109+
String expected = loadTextFromClasspath("/basic.fish");
110+
assertEquals(expected, script);
111+
}
112+
105113
public static class TopLevel {
106114
@Option(names = {"-V", "--version"}, help = true) boolean versionRequested;
107115
@Option(names = {"-h", "--help"}, help = true) boolean helpRequested;

src/test/resources/basic.fish

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
# root completion
3+
set -l root
4+
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -l timeUnit -d ''
5+
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -s u -d ''
6+
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -l timeout -d ''
7+
complete -c basicExample -n "not __fish_seen_subcommand_from $root" -s t -d ''

0 commit comments

Comments
 (0)