Skip to content

Commit 798d448

Browse files
committed
Only generate sources for annotated elements.
1 parent affa4ef commit 798d448

29 files changed

+542
-671
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444

4545
<modules>
4646
<module>stack-source</module>
47-
<module>stack-source-test</module>
4847
<module>stack-source-junit4</module>
4948
<module>stack-source-junit5</module>
5049
<module>stack-source-processor</module>

stack-source-junit4/src/test/java/stack/source/junit4/ErrorDecoratorTest.java

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,40 @@
1010
import static java.lang.Math.min;
1111
import static java.lang.System.lineSeparator;
1212
import static org.hamcrest.CoreMatchers.is;
13-
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.*;
1414
import static org.junit.Assume.assumeThat;
1515
import static stack.source.internal.Throwables.getStackTraceAsString;
1616

1717
public final class ErrorDecoratorTest {
1818

19+
@Test
20+
public void failure() {
21+
fail("testing failure");
22+
}
23+
24+
@Test
25+
public void failByAssertEquals() {
26+
assertEquals("test message", "1", "2");
27+
}
28+
29+
@Test
30+
public void failByAssertArrayEquals() {
31+
assertArrayEquals(
32+
"test message",
33+
new String[]{"1"},
34+
new String[]{"2"});
35+
}
36+
37+
@Test
38+
public void assumeApiPassThrough() {
39+
assumeThat(false, is(true));
40+
}
41+
42+
@Test
43+
@Ignore
44+
public void ignoreApiPassThrough() {
45+
}
46+
1947
@Rule
2048
public final RuleChain r = RuleChain
2149
.outerRule(ErrorDecoratorTest::apply)
@@ -36,7 +64,7 @@ public void evaluate() throws Throwable {
3664

3765
private static void assertFailure(Throwable e, Description desc) {
3866
switch (desc.getMethodName()) {
39-
case "fail":
67+
case "failure":
4068
assertFail(e);
4169
break;
4270
case "failByAssertEquals":
@@ -55,13 +83,12 @@ private static void assertFail(Throwable e) {
5583
String expected = String.join(lineSeparator(),
5684
"java.lang.AssertionError: testing failure",
5785
"\tat org.junit.Assert.fail(Assert.java:88)",
58-
"\tat stack.source.junit4.Fail.run(Fail.java:8)",
59-
"",
60-
"\t 6 @Override",
61-
"\t 7 public void run() {",
62-
"\t-> 8 fail(\"testing failure\");",
63-
"\t 9 }",
86+
"\tat stack.source.junit4.ErrorDecoratorTest.failure(ErrorDecoratorTest.java:21)",
6487
"",
88+
"\t 19 @Test",
89+
"\t 20 public void failure() {",
90+
"\t-> 21 fail(\"testing failure\");",
91+
"\t 22 }",
6592
""
6693
);
6794
assertStackTrace(expected, e);
@@ -71,13 +98,12 @@ private static void assertFailByAssertEquals(Throwable e) {
7198
String expected = String.join(lineSeparator(),
7299
"org.junit.ComparisonFailure: test message expected:<[1]> but was:<[2]>",
73100
"\tat org.junit.Assert.assertEquals(Assert.java:115)",
74-
"\tat stack.source.junit4.FailByAssertEquals.run(FailByAssertEquals.java:8)",
75-
"",
76-
"\t 6 @Override",
77-
"\t 7 public void run() {",
78-
"\t-> 8 assertEquals(\"test message\", \"1\", \"2\");",
79-
"\t 9 }",
101+
"\tat stack.source.junit4.ErrorDecoratorTest.failByAssertEquals(ErrorDecoratorTest.java:26)",
80102
"",
103+
"\t 24 @Test",
104+
"\t 25 public void failByAssertEquals() {",
105+
"\t-> 26 assertEquals(\"test message\", \"1\", \"2\");",
106+
"\t 27 }",
81107
""
82108
);
83109
assertStackTrace(expected, e);
@@ -89,13 +115,12 @@ private static void assertFailByAssertArrayEquals(Throwable e) {
89115
"\tat org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:55)",
90116
"\tat org.junit.Assert.internalArrayEquals(Assert.java:532)",
91117
"\tat org.junit.Assert.assertArrayEquals(Assert.java:283)",
92-
"\tat stack.source.junit4.FailByAssertArrayEquals.run(FailByAssertArrayEquals.java:8)",
93-
"",
94-
"\t-> 8 assertArrayEquals(",
95-
"\t 9 \"test message\",",
96-
"\t 10 new String[]{\"1\"},",
97-
"\t 11 new String[]{\"2\"});",
118+
"\tat stack.source.junit4.ErrorDecoratorTest.failByAssertArrayEquals(ErrorDecoratorTest.java:31)",
98119
"",
120+
"\t-> 31 assertArrayEquals(",
121+
"\t 32 \"test message\",",
122+
"\t 33 new String[]{\"1\"},",
123+
"\t 34 new String[]{\"2\"});",
99124
""
100125
);
101126
assertStackTrace(expected, e);
@@ -107,28 +132,4 @@ private static void assertStackTrace(String expected, Throwable e) {
107132
assertEquals(expected, actual);
108133
}
109134

110-
@Test
111-
public void fail() {
112-
new Fail().run();
113-
}
114-
115-
@Test
116-
public void failByAssertEquals() {
117-
new FailByAssertEquals().run();
118-
}
119-
120-
@Test
121-
public void failByAssertArrayEquals() {
122-
new FailByAssertArrayEquals().run();
123-
}
124-
125-
@Test
126-
public void assumeApiPassThrough() {
127-
assumeThat(false, is(true));
128-
}
129-
130-
@Test
131-
@Ignore
132-
public void ignoreApiPassThrough() {
133-
}
134135
}

stack-source-junit4/src/test/java/stack/source/junit4/Fail.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

stack-source-junit4/src/test/java/stack/source/junit4/FailByAssertArrayEquals.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

stack-source-junit4/src/test/java/stack/source/junit4/FailByAssertEquals.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

stack-source-test/src/test/java/NoPackageTest.java renamed to stack-source-junit5/src/test/java/NoPackageTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
import org.junit.Test;
1+
2+
import org.junit.jupiter.api.Test;
3+
import org.junit.jupiter.api.extension.ExtendWith;
24
import stack.source.internal.Decorator;
3-
import stack.source.test.TestException;
5+
import stack.source.junit5.ErrorDecorator;
46

57
import static java.lang.Math.min;
68
import static java.lang.System.lineSeparator;
7-
import static org.junit.Assert.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
810

9-
public final class NoPackageTest {
11+
@ExtendWith({ErrorDecorator.class})
12+
final class NoPackageTest {
1013

1114
@Test
12-
public void noPackage() {
15+
void noPackage() {
1316
try {
14-
new NoPackage().run();
15-
} catch (TestException e) {
17+
throw new AssertionError("no package");
18+
} catch (AssertionError e) {
1619
String expected = String.join(lineSeparator(),
17-
"stack.source.test.TestException: no package",
18-
"\tat NoPackage.run(NoPackage.java:7)",
19-
"\tat NoPackageTest.noPackage(NoPackageTest.java:14)",
20-
"",
21-
"\t-> 14 new NoPackage().run();",
20+
"java.lang.AssertionError: no package",
21+
"\tat NoPackageTest.noPackage(NoPackageTest.java:17)",
2222
"",
23+
"\t-> 17 throw new AssertionError(\"no package\");",
2324
""
2425
);
2526
String actual = Decorator.print(e);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package stack.source.junit5;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
7+
8+
import static java.lang.Math.min;
9+
import static java.lang.String.join;
10+
import static java.lang.System.lineSeparator;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static stack.source.internal.Throwables.getStackTraceAsString;
13+
14+
@ExtendWith({
15+
ChainTest.AssertDecoration.class,
16+
ErrorDecorator.class
17+
})
18+
class ChainTest {
19+
20+
@Test
21+
void test() {
22+
new ChainTest()
23+
.nothing1()
24+
.nothing2()
25+
.fail("what?")
26+
.nothing3()
27+
.fail("more?")
28+
.fail("and more?")
29+
.fail("and more more?");
30+
}
31+
32+
private ChainTest fail(String message) {
33+
throw new AssertionError(message);
34+
}
35+
36+
private ChainTest nothing1() {
37+
return this;
38+
}
39+
40+
private ChainTest nothing2() {
41+
return this;
42+
}
43+
44+
private ChainTest nothing3() {
45+
return this;
46+
}
47+
48+
static class AssertDecoration implements TestExecutionExceptionHandler {
49+
50+
@Override
51+
public void handleTestExecutionException(ExtensionContext context, Throwable e) {
52+
String expected = join(lineSeparator(),
53+
"java.lang.AssertionError: what?",
54+
"\tat stack.source.junit5.ChainTest.fail(ChainTest.java:33)",
55+
"\tat stack.source.junit5.ChainTest.test(ChainTest.java:25)",
56+
"",
57+
"\t 22 new ChainTest()",
58+
"\t 23 .nothing1()",
59+
"\t 24 .nothing2()",
60+
"\t-> 25 .fail(\"what?\")",
61+
"\t 26 .nothing3()",
62+
"\t 27 .fail(\"more?\")",
63+
"\t 28 .fail(\"and more?\")",
64+
"\t 29 .fail(\"and more more?\");",
65+
""
66+
);
67+
assertEquals(DecoratedAssertionFailedError.class, e.getClass());
68+
assertStackTrace(expected, e);
69+
}
70+
}
71+
72+
private static void assertStackTrace(String expected, Throwable e) {
73+
String actual = getStackTraceAsString(e);
74+
actual = actual.substring(0, min(expected.length(), actual.length()));
75+
assertEquals(expected, actual);
76+
}
77+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package stack.source.junit5;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
7+
8+
import static java.lang.Math.min;
9+
import static java.lang.String.join;
10+
import static java.lang.System.lineSeparator;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
import static stack.source.internal.Throwables.getStackTraceAsString;
14+
15+
@ExtendWith({
16+
LambdaTest.AssertDecoration.class,
17+
ErrorDecorator.class
18+
})
19+
class LambdaTest {
20+
21+
@Test
22+
void test() {
23+
String expected = "hi";
24+
lambda(() -> {
25+
assertTrue(true);
26+
throw new AssertionError(expected);
27+
});
28+
}
29+
30+
private void lambda(Runnable code) {
31+
code.run();
32+
}
33+
34+
static class AssertDecoration implements TestExecutionExceptionHandler {
35+
36+
@Override
37+
public void handleTestExecutionException(ExtensionContext context, Throwable e) {
38+
String expected = join(lineSeparator(),
39+
"java.lang.AssertionError: hi",
40+
"\tat stack.source.junit5.LambdaTest.lambda$test$0(LambdaTest.java:26)",
41+
"",
42+
"\t 24 lambda(() -> {",
43+
"\t 25 assertTrue(true);",
44+
"\t-> 26 throw new AssertionError(expected);",
45+
"\t 27 });",
46+
"",
47+
"",
48+
"\tat stack.source.junit5.LambdaTest.lambda(LambdaTest.java:31)",
49+
"\tat stack.source.junit5.LambdaTest.test(LambdaTest.java:24)"
50+
);
51+
assertEquals(DecoratedAssertionFailedError.class, e.getClass());
52+
assertStackTrace(expected, e);
53+
}
54+
}
55+
56+
private static void assertStackTrace(String expected, Throwable e) {
57+
String actual = getStackTraceAsString(e);
58+
actual = actual.substring(0, min(expected.length(), actual.length()));
59+
assertEquals(expected, actual);
60+
}
61+
}

0 commit comments

Comments
 (0)