Skip to content

Commit

Permalink
printStackTrace for mapper failure
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed May 25, 2019
1 parent a00e777 commit 726712a
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 64 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/net/jbock/compiler/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ private CodeBlock parseMethodTryBlock(

private CodeBlock parseMethodCatchBlock(ParameterSpec e) {
CodeBlock.Builder spec = CodeBlock.builder();
spec.addStatement("$N.printStackTrace($N.out)", e, err);
if (context.addHelp) {
spec.addStatement("$N.println($S)", err, "Usage:");
spec.addStatement("$N.incrementIndent()", err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void optionalPresent() {

@Test
void wrongNumber() {
f.assertThat("-a", "2").failsWithLine4(
f.assertThat("-a", "2").failsWithUsageMessage(
"For input string: \"-a\"");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void fail() {
"--bigChar", "A",
"--charOpt", "X",
"--charList", "b",
"--charList", "c").failsWithLine4(
"--charList", "c").failsWithUsageMessage(
"Not a character: <abc>");
}
}
16 changes: 8 additions & 8 deletions examples/src/test/java/net/jbock/examples/CpArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class CpArgumentsTest {

@Test
void errorMissingSource() {
f.assertThat().failsWithLine4("Missing parameter: <SOURCE>");
f.assertThat("-r").failsWithLine4("Missing parameter: <SOURCE>");
f.assertThat().failsWithUsageMessage("Missing parameter: <SOURCE>");
f.assertThat("-r").failsWithUsageMessage("Missing parameter: <SOURCE>");
}

@Test
void errorMissingDest() {
f.assertThat("a").failsWithLine4("Missing parameter: <DEST>");
f.assertThat("a", "-r").failsWithLine4("Missing parameter: <DEST>");
f.assertThat("-r", "a").failsWithLine4("Missing parameter: <DEST>");
f.assertThat("a").failsWithUsageMessage("Missing parameter: <DEST>");
f.assertThat("a", "-r").failsWithUsageMessage("Missing parameter: <DEST>");
f.assertThat("-r", "a").failsWithUsageMessage("Missing parameter: <DEST>");
}

@Test
Expand All @@ -44,17 +44,17 @@ void minimal() {

@Test
void dashNotIgnored() {
f.assertThat("-a", "b").failsWithLine4("Invalid option: -a");
f.assertThat("-a", "b").failsWithUsageMessage("Invalid option: -a");
}

@Test
void tooMany() {
f.assertThat("a", "b", "c").failsWithLine4("Invalid option: c");
f.assertThat("a", "b", "c").failsWithUsageMessage("Invalid option: c");
}

@Test
void tooManyAndFlag() {
f.assertThat("-r", "a", "b", "c").failsWithLine4("Invalid option: c");
f.assertThat("-r", "a", "b", "c").failsWithUsageMessage("Invalid option: c");
}

@Test
Expand Down
18 changes: 9 additions & 9 deletions examples/src/test/java/net/jbock/examples/CurlArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,50 +158,50 @@ void variousTests() {

@Test
void errorInvalidGrouping() {
f.assertThat("-vH1").failsWithLine4("Invalid option: -vH1");
f.assertThat("-vH1").failsWithUsageMessage("Invalid option: -vH1");
}

@Test
void errorInvalidGroupingLong() {
f.assertThat("-vXPOST").failsWithLine4("Invalid option: -vXPOST");
f.assertThat("-vXPOST").failsWithUsageMessage("Invalid option: -vXPOST");
}

@Test
void errorGroupingDuplicateFlag() {
f.assertThat("-v", "-vH'Content-Type: application/xml'").failsWithLine4(
f.assertThat("-v", "-vH'Content-Type: application/xml'").failsWithUsageMessage(
"Invalid option: -vH'Content-Type: application/xml'");
}

@Test
void errorMissingRepeatable() {
f.assertThat("-H").failsWithLine4("Missing value after token: -H");
f.assertThat("-H").failsWithUsageMessage("Missing value after token: -H");
}

@Test
void errorMissingNonRepeatable() {
f.assertThat("--request").failsWithLine4("Missing value after token: --request");
f.assertThat("--request").failsWithUsageMessage("Missing value after token: --request");
}

@Test
void errorDuplicateNonRepeatableLong() {
f.assertThat("--request", "GET", "--request", "POST").failsWithLine4(
f.assertThat("--request", "GET", "--request", "POST").failsWithUsageMessage(
"Option METHOD (-X, --request) is not repeatable");
}

@Test
void errorDuplicateNonRepeatableShort() {
f.assertThat("-X1", "-X2").failsWithLine4("Option METHOD (-X, --request) is not repeatable");
f.assertThat("-X1", "-X2").failsWithUsageMessage("Option METHOD (-X, --request) is not repeatable");
}

@Test
void errorDuplicateNonRepeatableLongDetachedShortAttached() {
f.assertThat("--request", "1", "-X2").failsWithLine4(
f.assertThat("--request", "1", "-X2").failsWithUsageMessage(
"Option METHOD (-X, --request) is not repeatable");
}

@Test
void errorDuplicateNonRepeatableLongAttachedShortDetached() {
f.assertThat("--request=1", "-X", "2").failsWithLine4(
f.assertThat("--request=1", "-X", "2").failsWithUsageMessage(
"Option METHOD (-X, --request) is not repeatable");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ void success() {

@Test
void invalidOptions() {
f.assertThat("--date", "FooBar").failsWithLine4("For input string: \"FooBar\"");
f.assertThat().failsWithLine4("Missing required option: DATE (--date)");
f.assertThat("--date", "FooBar").failsWithUsageMessage("For input string: \"FooBar\"");
f.assertThat().failsWithUsageMessage("Missing required option: DATE (--date)");
}

@Test
Expand Down
30 changes: 15 additions & 15 deletions examples/src/test/java/net/jbock/examples/GradleArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,39 @@ class GradleArgumentsTest {

@Test
void errorShortLongConflict() {
f.assertThat("-m", "hello", "--message=goodbye").failsWithLine4(
f.assertThat("-m", "hello", "--message=goodbye").failsWithUsageMessage(
"Option MESSAGE (-m, --message) is not repeatable");
}

@Test
void errorMissingValue() {
// there's nothing after -m
f.assertThat("-m").failsWithLine4("Missing value after token: -m");
f.assertThat("-m").failsWithUsageMessage("Missing value after token: -m");
}

@Test
void errorLongShortConflict() {
f.assertThat("--message=hello", "-m", "goodbye").failsWithLine4(
f.assertThat("--message=hello", "-m", "goodbye").failsWithUsageMessage(
"Option MESSAGE (-m, --message) is not repeatable");
}

@Test
void errorLongLongConflict() {
f.assertThat("--message=hello", "--message=goodbye").failsWithLine4(
f.assertThat("--message=hello", "--message=goodbye").failsWithUsageMessage(
"Option MESSAGE (-m, --message) is not repeatable");
}

@Test
void errorInvalidOption() {
f.assertThat("-c1").failsWithLine4("Invalid option: -c1");
f.assertThat("-c-v").failsWithLine4("Invalid option: -c-v");
f.assertThat("-c-").failsWithLine4("Invalid option: -c-");
f.assertThat("-c=v").failsWithLine4("Invalid option: -c=v");
f.assertThat("-c=").failsWithLine4("Invalid option: -c=");
f.assertThat("-cX=1").failsWithLine4("Invalid option: -cX=1");
f.assertThat("-cvv").failsWithLine4("Invalid option: -cvv");
f.assertThat("-cvx").failsWithLine4("Invalid option: -cvx");
f.assertThat("-cvm").failsWithLine4("Invalid option: -cvm");
f.assertThat("-c1").failsWithUsageMessage("Invalid option: -c1");
f.assertThat("-c-v").failsWithUsageMessage("Invalid option: -c-v");
f.assertThat("-c-").failsWithUsageMessage("Invalid option: -c-");
f.assertThat("-c=v").failsWithUsageMessage("Invalid option: -c=v");
f.assertThat("-c=").failsWithUsageMessage("Invalid option: -c=");
f.assertThat("-cX=1").failsWithUsageMessage("Invalid option: -cX=1");
f.assertThat("-cvv").failsWithUsageMessage("Invalid option: -cvv");
f.assertThat("-cvx").failsWithUsageMessage("Invalid option: -cvx");
f.assertThat("-cvm").failsWithUsageMessage("Invalid option: -cvm");
}

@Test
Expand Down Expand Up @@ -141,7 +141,7 @@ void testRepeatableShortAttached() {
@Test
void testLongSuppressed() {
// Long option --cmos is suppressed
f.assertThat("--cmos").failsWithLine4("Invalid option: --cmos");
f.assertThat("--cmos").failsWithUsageMessage("Invalid option: --cmos");
}

@Test
Expand Down Expand Up @@ -179,7 +179,7 @@ void twoFlags() {

@Test
void errorSuspiciousInput() {
f.assertThat("-cvm", "hello").failsWithLine4("Invalid option: -cvm");
f.assertThat("-cvm", "hello").failsWithUsageMessage("Invalid option: -cvm");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void errorNoArguments() {
TestOutputStream out = new TestOutputStream();
HelplessArguments_Parser.create().withErrorStream(out.out).parse(new String[]{});
String message = out.toString();
assertTrue(message.startsWith(String.join("\n", fullUsage)));
assertTrue(message.contains(String.join("\n", fullUsage)));
assertTrue(message.contains("Missing parameter: <REQUIRED>"));
}

Expand All @@ -67,7 +67,7 @@ void errorInvalidOption() {
TestOutputStream out = new TestOutputStream();
HelplessArguments_Parser.create().withErrorStream(out.out).parse(new String[]{"-p"});
String message = out.toString();
assertTrue(message.startsWith(String.join("\n", fullUsage)));
assertTrue(message.contains(String.join("\n", fullUsage)));
assertTrue(message.contains("Invalid option: -p"));
}
}
12 changes: 6 additions & 6 deletions examples/src/test/java/net/jbock/examples/MvArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ class MvArgumentsTest {

@Test
void notEnoughArguments() {
f.assertThat().failsWithLine4("Missing parameter: <SOURCE>");
f.assertThat("a").failsWithLine4("Missing parameter: <DEST>");
f.assertThat().failsWithUsageMessage("Missing parameter: <SOURCE>");
f.assertThat("a").failsWithUsageMessage("Missing parameter: <DEST>");
}

@Test
void invalidOption() {
f.assertThat("-aa", "b").failsWithLine4("Invalid option: -aa");
f.assertThat("-aa", "b").failsWithUsageMessage("Invalid option: -aa");
}

@Test
void excessOption() {
f.assertThat("a", "b", "c").failsWithLine4("Invalid option: c");
f.assertThat("a", "b", "c").failsWithUsageMessage("Invalid option: c");
}

@Test
void invalidOptionEscapeSequenceSecond() {
f.assertThat("a", "--").failsWithLine4("Invalid option: --");
f.assertThat("a", "--").failsWithUsageMessage("Invalid option: --");
}

@Test
void invalidOptionEscapeSequenceThird() {
f.assertThat("a", "b", "--", "c").failsWithLine4("Invalid option: --");
f.assertThat("a", "b", "--", "c").failsWithUsageMessage("Invalid option: --");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ void testOptionalInt() {

@Test
void errorMissingInt() {
f.assertThat("--cmos").failsWithLine4("Missing required option: NUMBER (-n, --number)");
f.assertThat("--cmos").failsWithUsageMessage("Missing required option: NUMBER (-n, --number)");
}

@Test
void errorUnknownToken() {
f.assertThat("blabla").failsWithLine4("Invalid option: blabla");
f.assertThat("blabla").failsWithUsageMessage("Invalid option: blabla");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class PositionalArgumentsTest {

@Test
void errorMissingParameters() {
f.assertThat().failsWithLine4("Missing parameter: <SOURCE>");
f.assertThat("a").failsWithLine4("Missing parameter: <DEST>");
f.assertThat("a", "b").failsWithLine4("Missing parameter: <ANOTHER_INT>");
f.assertThat().failsWithUsageMessage("Missing parameter: <SOURCE>");
f.assertThat("a").failsWithUsageMessage("Missing parameter: <DEST>");
f.assertThat("a", "b").failsWithUsageMessage("Missing parameter: <ANOTHER_INT>");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ void success() {

@Test
void errorDirMissing() {
f.assertThat().failsWithLine4("Missing required option: DIR (--dir)");
f.assertThat().failsWithUsageMessage("Missing required option: DIR (--dir)");
}

@Test
void errorRepeatedArgument() {
f.assertThat("--dir", "A", "--dir", "B").failsWithLine4(
f.assertThat("--dir", "A", "--dir", "B").failsWithUsageMessage(
"Option DIR (--dir) is not repeatable");
f.assertThat("--dir=A", "--dir", "B").failsWithLine4(
f.assertThat("--dir=A", "--dir", "B").failsWithUsageMessage(
"Option DIR (--dir) is not repeatable");
f.assertThat("--dir=A", "--dir=B").failsWithLine4(
f.assertThat("--dir=A", "--dir=B").failsWithUsageMessage(
"Option DIR (--dir) is not repeatable");
f.assertThat("--dir", "A", "--dir=B").failsWithLine4(
f.assertThat("--dir", "A", "--dir=B").failsWithUsageMessage(
"Option DIR (--dir) is not repeatable");
}

@Test
void errorDetachedAttached() {
f.assertThat("--dir", "A", "--dir=B").failsWithLine4("Option DIR (--dir) is not repeatable");
f.assertThat("--dir", "A", "--dir=B").failsWithUsageMessage("Option DIR (--dir) is not repeatable");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class SimpleArgumentsTest {

@Test
void invalidOptions() {
f.assertThat("xf", "1").failsWithLine4("Invalid option: xf");
f.assertThat("-xf", "1").failsWithLine4("Invalid option: -xf");
f.assertThat("xf", "1").failsWithUsageMessage("Invalid option: xf");
f.assertThat("-xf", "1").failsWithUsageMessage("Invalid option: -xf");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void testExtract() {

@Test
void noGrouping() {
f.assertThat("-v", "xf", "foo.tar").failsWithLine4("Invalid option: xf");
f.assertThat("-v", "-xf", "foo.tar").failsWithLine4("Invalid option: -xf");
f.assertThat("-v", "xf", "foo.tar").failsWithUsageMessage("Invalid option: xf");
f.assertThat("-v", "-xf", "foo.tar").failsWithUsageMessage("Invalid option: -xf");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -188,15 +189,22 @@ private JsonAssert(Optional<E> parsed, String stdout, String stderr) {
this.stderr = stderr;
}

public void failsWithLine4(String expectedMessage) {
public void failsWithUsageMessage(String expectedMessage) {
if (parsed.isPresent()) {
fail("Expecting a failure" +
" but parsing was successful");
}
assertTrue(stdout.isEmpty());
assertTrue(stderr.startsWith("Usage:"));
String actualMessage = stderr.split("\\r?\\n", -1)[4].trim();
assertEquals(expectedMessage, actualMessage);
String[] tokens = stderr.split("\\r?\\n", -1);
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i];
if (token.startsWith("Usage:")) {
String actualMessage = tokens[i + 4].trim();
assertEquals(expectedMessage, actualMessage);
return;
}
}
failsWithLines("Usage line not found");
}

public void failsWithLines(String... expected) {
Expand All @@ -206,7 +214,16 @@ public void failsWithLines(String... expected) {
}
assertTrue(stdout.isEmpty());
String[] actualMessage = stderr.split("\\r?\\n", -1);
compareArrays(expected, actualMessage);
for (int i = 0; i < actualMessage.length; i++) {
String token = actualMessage[i];
if (token.startsWith("Usage:")) {
String[] copy = new String[actualMessage.length - i];
System.arraycopy(actualMessage, i, copy, 0, copy.length);
compareArrays(expected, copy);
return;
}
}
failsWithLines("Usage line not found");
}

public void satisfies(Predicate<E> predicate) {
Expand Down

0 comments on commit 726712a

Please sign in to comment.