Skip to content

Commit

Permalink
[#1574][#1408][#964] add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Feb 15, 2022
1 parent 44a1453 commit 50a8df2
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/test/java/picocli/HelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,64 @@ class App {
assertEquals(expectedUsage, new CommandLine(new App()).getUsageMessage());
}

@Test
public void testSortSynopsisByOptionOrderAllowsGaps() throws Exception {
@Command(sortOptions = false, sortSynopsis = false)
class App {
@Option(names = {"-a"}, order = 9) boolean a;
@Option(names = {"-b"}, order = 8) boolean b;
@Option(names = {"-c"}, order = 7) boolean c;
@Option(names = {"-d"}, order = 6) int d;
@Option(names = {"-e"}, order = 3) String[] e;
@Option(names = {"-f"}, order = 1) String[] f;
@Option(names = {"-g"}, order = 0) boolean g;
}
OptionSpec[] fields = options(new App(), "a", "b", "c", "d", "e", "f", "g");
Arrays.sort(fields, Help.createOrderComparator());
OptionSpec[] expected = options(new App(), "g", "f", "e", "d", "c", "b", "a");
assertArrayEquals(expected, fields);

String expectedUsage = String.format("" +
"Usage: <main class> [-gcba] [-f=<f>]... [-e=<e>]... [-d=<d>]%n" +
" -g%n" +
" -f=<f>%n" +
" -e=<e>%n" +
" -d=<d>%n" +
" -c%n" +
" -b%n" +
" -a%n");
assertEquals(expectedUsage, new CommandLine(new App()).getUsageMessage());
}

@Test
public void testSortSynopsisWithoutSortingOptions() throws Exception {
@Command(sortSynopsis = false)
class App {
@Option(names = {"-a"}, order = 9) boolean a;
@Option(names = {"-b"}, order = 8) boolean b;
@Option(names = {"-c"}, order = 7) boolean c;
@Option(names = {"-d"}, order = 6) int d;
@Option(names = {"-e"}, order = 3) String[] e;
@Option(names = {"-f"}, order = 1) String[] f;
@Option(names = {"-g"}, order = 0) boolean g;
}
OptionSpec[] fields = options(new App(), "a", "b", "c", "d", "e", "f", "g");
Arrays.sort(fields, Help.createOrderComparator());
OptionSpec[] expected = options(new App(), "g", "f", "e", "d", "c", "b", "a");
assertArrayEquals(expected, fields);

String expectedUsage = String.format("" +
"Usage: <main class> [-gcba] [-f=<f>]... [-e=<e>]... [-d=<d>]%n" +
" -a%n" +
" -b%n" +
" -c%n" +
" -d=<d>%n" +
" -e=<e>%n" +
" -f=<f>%n" +
" -g%n");
assertEquals(expectedUsage, new CommandLine(new App()).getUsageMessage());
}

@Test
public void testSortByOptionOrderStableSortWhenEqualOrder() throws Exception {
@Command(sortOptions = false)
Expand Down Expand Up @@ -1368,6 +1426,35 @@ class App {
assertEquals(expectedUsage, new CommandLine(new App()).getUsageMessage());
}

@Test
public void testSortSynopsisByOptionOrderStableSortWhenEqualOrder() throws Exception {
@Command(sortOptions = false, sortSynopsis = false)
class App {
@Option(names = {"-a"}, order = 9) boolean a;
@Option(names = {"-b"}, order = 8) boolean b;
@Option(names = {"-c"}, order = 7) boolean c;
@Option(names = {"-d"}, order = 7) int d;
@Option(names = {"-e"}, order = 7) String[] e;
@Option(names = {"-f"}, order = 7) String[] f;
@Option(names = {"-g"}, order = 0) boolean g;
}
OptionSpec[] fields = options(new App(), "a", "b", "c", "d", "e", "f", "g");
Arrays.sort(fields, Help.createOrderComparator());
OptionSpec[] expected = options(new App(), "g", "c", "d", "e", "f", "b", "a");
assertArrayEquals(expected, fields);

String expectedUsage = String.format("" +
"Usage: <main class> [-gcba] [-d=<d>] [-e=<e>]... [-f=<f>]...%n" +
" -g%n" +
" -c%n" +
" -d=<d>%n" +
" -e=<e>%n" +
" -f=<f>%n" +
" -b%n" +
" -a%n");
assertEquals(expectedUsage, new CommandLine(new App()).getUsageMessage());
}

@Test
public void testSortDeclarationOrderWhenOrderAttributeOmitted() {
@Command(sortOptions = false)
Expand Down

0 comments on commit 50a8df2

Please sign in to comment.