Skip to content

Commit

Permalink
[#561][#650][#424][#541][#663][#672] added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Apr 26, 2019
1 parent f27e48a commit 43a932b
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/test/java/picocli/HelpSubCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package picocli;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
Expand All @@ -12,6 +16,14 @@
import static picocli.PicocliTestUtil.setOf;

public class HelpSubCommandTest {
@Rule
public final ProvideSystemProperty ansiOFF = new ProvideSystemProperty("picocli.ansi", "false");

@Rule
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();

@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

@Test
public void testShowSynopsisUsageWithCommandOption() {
Expand Down Expand Up @@ -108,4 +120,36 @@ public void testCommandAliasAnnotationSubcommandUsageHelp() {
" @|bold,underline subsub|@, @|bold,underline ss|@, @|bold,underline sbsb|@ I'm like a 3rd rate command but great bang for your buck%n")).toString();
assertEquals(expected, baos.toString());
}

@Command(name = "customHelp", helpCommand = true)
static class LegacyCustomHelpCommand implements IHelpCommandInitializable, Runnable {
private CommandLine helpCommandLine;
private Help.Ansi ansi;
private PrintStream out;
private PrintStream err;

public void init(CommandLine helpCommandLine, Help.Ansi ansi, PrintStream out, PrintStream err) {
this.helpCommandLine = helpCommandLine;
this.ansi = ansi;
this.out = out;
this.err = err;
}

public void run() {
out.println("Hi, ansi is " + ansi);
err.println("Hello, ansi is " + ansi);
}
}
@Test
public void testLegacyCustomHelpCommand() {
@Command(subcommands = LegacyCustomHelpCommand.class)
class App implements Runnable {
public void run() { }
}

CommandLine cmd = new CommandLine(new App());
cmd.parseWithHandler(new RunLast().useOut(System.out).useErr(System.err).useAnsi(Help.Ansi.OFF),new String[]{"customHelp"});
assertEquals(String.format("Hi, ansi is OFF%n"), systemOutRule.getLog());
assertEquals(String.format("Hello, ansi is OFF%n"), systemErrRule.getLog());
}
}
162 changes: 162 additions & 0 deletions src/test/java/picocli/SubcommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import picocli.CommandLine.RunAll;
import picocli.CommandLine.UnmatchedArgumentException;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -970,6 +972,166 @@ class TopLevel {}
assertTrue(grandChildCount > 0);
}

@Test
public void testErr_AfterSubcommandsAdded() {
@Command
class TopLevel {}
CommandLine commandLine = new CommandLine(new TopLevel());
PrintWriter original = commandLine.getErr();
assertNotNull(original);
PrintWriter err = new PrintWriter(new StringWriter());
commandLine.setErr(err);
assertEquals(err, commandLine.getErr());

int childCount = 0;
int grandChildCount = 0;
commandLine.addSubcommand("main", createNestedCommand());
for (CommandLine sub : commandLine.getSubcommands().values()) {
childCount++;
assertNotSame("subcommand added afterwards is not impacted", err, sub.getErr().getClass());
for (CommandLine subsub : sub.getSubcommands().values()) {
grandChildCount++;
assertNotSame("subcommand added afterwards is not impacted", err, subsub.getErr().getClass());
}
}
assertTrue(childCount > 0);
assertTrue(grandChildCount > 0);
}

@Test
public void testErr_BeforeSubcommandsAdded() {
@Command
class TopLevel {}
CommandLine commandLine = new CommandLine(new TopLevel());
commandLine.addSubcommand("main", createNestedCommand());
PrintWriter original = commandLine.getErr();
assertNotNull(original);
PrintWriter err = new PrintWriter(new StringWriter());
commandLine.setErr(err);
assertEquals(err, commandLine.getErr());

int childCount = 0;
int grandChildCount = 0;
for (CommandLine sub : commandLine.getSubcommands().values()) {
childCount++;
assertSame("subcommand added before IS impacted", err, sub.getErr());
for (CommandLine subsub : sub.getSubcommands().values()) {
grandChildCount++;
assertSame("subsubcommand added before IS impacted", err, sub.getErr());
}
}
assertTrue(childCount > 0);
assertTrue(grandChildCount > 0);
}

@Test
public void testOut_AfterSubcommandsAdded() {
@Command
class TopLevel {}
CommandLine commandLine = new CommandLine(new TopLevel());
PrintWriter original = commandLine.getOut();
assertNotNull(original);
PrintWriter out = new PrintWriter(new StringWriter());
commandLine.setOut(out);
assertEquals(out, commandLine.getOut());

int childCount = 0;
int grandChildCount = 0;
commandLine.addSubcommand("main", createNestedCommand());
for (CommandLine sub : commandLine.getSubcommands().values()) {
childCount++;
assertNotSame("subcommand added afterwards is not impacted", out, sub.getOut().getClass());
for (CommandLine subsub : sub.getSubcommands().values()) {
grandChildCount++;
assertNotSame("subcommand added afterwards is not impacted", out, subsub.getOut().getClass());
}
}
assertTrue(childCount > 0);
assertTrue(grandChildCount > 0);
}

@Test
public void testOut_BeforeSubcommandsAdded() {
@Command
class TopLevel {}
CommandLine commandLine = new CommandLine(new TopLevel());
commandLine.addSubcommand("main", createNestedCommand());
PrintWriter original = commandLine.getOut();
assertNotNull(original);
PrintWriter out = new PrintWriter(new StringWriter());
commandLine.setOut(out);
assertEquals(out, commandLine.getOut());

int childCount = 0;
int grandChildCount = 0;
for (CommandLine sub : commandLine.getSubcommands().values()) {
childCount++;
assertSame("subcommand added before IS impacted", out, sub.getOut());
for (CommandLine subsub : sub.getSubcommands().values()) {
grandChildCount++;
assertSame("subsubcommand added before IS impacted", out, sub.getOut());
}
}
assertTrue(childCount > 0);
assertTrue(grandChildCount > 0);
}

@Test
public void testColorScheme_AfterSubcommandsAdded() {
@Command
class TopLevel {}
CommandLine commandLine = new CommandLine(new TopLevel());
CommandLine.Help.ColorScheme original = commandLine.getColorScheme();
assertEquals(Arrays.asList(CommandLine.Help.Ansi.Style.bold), original.commandStyles);

CommandLine.Help.ColorScheme scheme = new CommandLine.Help.ColorScheme();
scheme.commands(CommandLine.Help.Ansi.Style.fg_black, CommandLine.Help.Ansi.Style.bg_cyan);
commandLine.setColorScheme(scheme);
assertEquals(scheme, commandLine.getColorScheme());

int childCount = 0;
int grandChildCount = 0;
commandLine.addSubcommand("main", createNestedCommand());
for (CommandLine sub : commandLine.getSubcommands().values()) {
childCount++;
assertNotSame("subcommand added afterwards is not impacted", scheme, sub.getColorScheme().getClass());
for (CommandLine subsub : sub.getSubcommands().values()) {
grandChildCount++;
assertNotSame("subcommand added afterwards is not impacted", scheme, subsub.getColorScheme().getClass());
}
}
assertTrue(childCount > 0);
assertTrue(grandChildCount > 0);
}

@Test
public void testColorScheme_BeforeSubcommandsAdded() {
@Command
class TopLevel {}
CommandLine commandLine = new CommandLine(new TopLevel());
commandLine.addSubcommand("main", createNestedCommand());
CommandLine.Help.ColorScheme original = commandLine.getColorScheme();
assertEquals(Arrays.asList(CommandLine.Help.Ansi.Style.bold), original.commandStyles);

CommandLine.Help.ColorScheme scheme = new CommandLine.Help.ColorScheme();
scheme.commands(CommandLine.Help.Ansi.Style.fg_black, CommandLine.Help.Ansi.Style.bg_cyan);
commandLine.setColorScheme(scheme);
assertEquals(scheme, commandLine.getColorScheme());

int childCount = 0;
int grandChildCount = 0;
for (CommandLine sub : commandLine.getSubcommands().values()) {
childCount++;
assertSame("subcommand added before IS impacted", scheme, sub.getColorScheme());
for (CommandLine subsub : sub.getSubcommands().values()) {
grandChildCount++;
assertSame("subsubcommand added before IS impacted", scheme, sub.getColorScheme());
}
}
assertTrue(childCount > 0);
assertTrue(grandChildCount > 0);
}

@Test
public void testParserToggleBooleanFlags_BeforeSubcommandsAdded() {
@Command
Expand Down

0 comments on commit 43a932b

Please sign in to comment.