Skip to content

Commit 092e91c

Browse files
committed
Show found generated file on comparison failure.
1 parent ea81dbb commit 092e91c

File tree

2 files changed

+70
-24
lines changed

2 files changed

+70
-24
lines changed

src/main/java/com/google/testing/compile/JavaSourcesSubject.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,35 @@
1717

1818
import static com.google.common.base.Preconditions.checkArgument;
1919

20-
import java.io.IOException;
21-
import java.util.Arrays;
22-
23-
import javax.annotation.processing.Processor;
24-
import javax.tools.Diagnostic;
25-
import javax.tools.Diagnostic.Kind;
26-
import javax.tools.FileObject;
27-
import javax.tools.JavaFileObject;
28-
29-
import org.truth0.FailureStrategy;
30-
import org.truth0.subjects.Subject;
31-
3220
import com.google.common.base.Function;
3321
import com.google.common.base.Joiner;
3422
import com.google.common.base.Optional;
3523
import com.google.common.base.Predicate;
3624
import com.google.common.collect.FluentIterable;
3725
import com.google.common.collect.ImmutableList;
26+
import com.google.common.collect.ImmutableMap;
3827
import com.google.common.collect.ImmutableSet;
3928
import com.google.common.collect.Iterables;
4029
import com.google.common.collect.Lists;
4130
import com.google.common.io.ByteStreams;
4231
import com.google.testing.compile.Compilation.Result;
32+
4333
import com.sun.source.tree.CompilationUnitTree;
4434

35+
import org.truth0.FailureStrategy;
36+
import org.truth0.subjects.Subject;
37+
38+
import java.io.IOException;
39+
import java.util.Arrays;
40+
41+
import javax.annotation.Nullable;
42+
import javax.annotation.processing.Processor;
43+
import javax.tools.Diagnostic;
44+
import javax.tools.Diagnostic.Kind;
45+
import javax.tools.FileObject;
46+
import javax.tools.JavaFileObject;
47+
import javax.tools.StandardLocation;
48+
4549
/**
4650
* A <a href="https://github.com/truth0/truth">Truth</a> {@link Subject} that evaluates the result
4751
* of a {@code javac} compilation. See {@link com.google.testing.compile} for usage examples
@@ -88,6 +92,7 @@ private CompilationClause(Iterable<? extends Processor> processors) {
8892
this.processors = ImmutableSet.copyOf(processors);
8993
}
9094

95+
@Override
9196
public SuccessfulCompilationClause compilesWithoutError() {
9297
Compilation.Result result = Compilation.compile(processors, getSubject());
9398
if (!result.successful()) {
@@ -100,6 +105,7 @@ public SuccessfulCompilationClause compilesWithoutError() {
100105
return new SuccessfulCompilationBuilder(result);
101106
}
102107

108+
@Override
103109
public UnsuccessfulCompilationClause failsToCompile() {
104110
Result result = Compilation.compile(processors, getSubject());
105111
if (result.successful()) {
@@ -264,8 +270,30 @@ public boolean apply(CompilationUnitTree input) {
264270
}
265271
});
266272
if (!found.isPresent()) {
267-
failureStrategy.fail("Did not find a source file coresponding to "
268-
+ expected.getSourceFile().getName());
273+
ImmutableMap<String, JavaFileObject> p = FluentIterable.from(generatedSources)
274+
.uniqueIndex(new Function<JavaFileObject, String>() {
275+
@Override
276+
@Nullable
277+
public String apply(@Nullable JavaFileObject input) {
278+
String path = input.toUri().getPath();
279+
// Trim "/" + StandardLocation.name() + "/" from path.
280+
// TODO(cgruber): Does this play out on Windows?
281+
return path.substring(StandardLocation.SOURCE_OUTPUT.name().length() + 2);
282+
}
283+
});
284+
JavaFileObject actual = p.get(expected.getSourceFile().toUri().getPath());
285+
String expectedPath = expected.getSourceFile().getName();
286+
if (actual != null) {
287+
CharSequence actualSource = null;
288+
try {
289+
actualSource = actual.getCharContent(true);
290+
} catch (IOException ignored) {}
291+
failureStrategy.fail("Generated file " + expectedPath
292+
+ " did not match expectation. Found:\n"
293+
+ (actualSource == null ? "no source found" : actualSource));
294+
} else {
295+
failureStrategy.fail("Did not find a source file coresponding to " + expectedPath);
296+
}
269297
}
270298
}
271299
return this;

src/test/java/com/google/testing/compile/JavaSourcesSubjectFactoryTest.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
import static org.junit.Assert.fail;
2020
import static org.truth0.Truth.ASSERT;
2121

22+
import com.google.common.collect.ImmutableSet;
23+
import com.google.common.io.Resources;
24+
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
import org.truth0.FailureStrategy;
29+
import org.truth0.TestVerb;
30+
2231
import java.io.IOException;
2332
import java.io.Writer;
2433
import java.util.Arrays;
@@ -34,15 +43,6 @@
3443
import javax.tools.Diagnostic.Kind;
3544
import javax.tools.JavaFileObject;
3645

37-
import org.junit.Test;
38-
import org.junit.runner.RunWith;
39-
import org.junit.runners.JUnit4;
40-
import org.truth0.FailureStrategy;
41-
import org.truth0.TestVerb;
42-
43-
import com.google.common.collect.ImmutableSet;
44-
import com.google.common.io.Resources;
45-
4646
/**
4747
* Tests {@link JavaSourcesSubjectFactory} (and {@link JavaSourceSubjectFactory}).
4848
*
@@ -225,6 +225,24 @@ public void generates() {
225225
GeneratingProcessor.GENERATED_SOURCE));
226226
}
227227

228+
@Test
229+
public void generatesUnexpectedSource() {
230+
String failingExpectationSource = "abstract class Blah {}";
231+
try {
232+
VERIFY.about(javaSource())
233+
.that(JavaFileObjects.forResource("HelloWorld.java"))
234+
.processedWith(new GeneratingProcessor())
235+
.compilesWithoutError()
236+
.and().generatesSources(JavaFileObjects.forSourceString(
237+
GeneratingProcessor.GENERATED_CLASS_NAME,
238+
failingExpectationSource));
239+
} catch (VerificationException expected) {
240+
ASSERT.that(expected.getMessage()).contains(
241+
"Generated file Blah.java did not match expectation. Found:");
242+
ASSERT.that(expected.getMessage()).contains(GeneratingProcessor.GENERATED_SOURCE);
243+
}
244+
}
245+
228246
@Test
229247
public void invokesMultipleProcesors() {
230248
NoOpProcessor noopProcessor1 = new NoOpProcessor();
@@ -256,7 +274,7 @@ public void invokesMultipleProcesors_asIterable() {
256274

257275
private static final class GeneratingProcessor extends AbstractProcessor {
258276
static final String GENERATED_CLASS_NAME = "Blah";
259-
static final String GENERATED_SOURCE = "final class Blah {}";
277+
static final String GENERATED_SOURCE = "final class Blah {\n String blah = \"blah\";\n}";
260278

261279
@Override
262280
public synchronized void init(ProcessingEnvironment processingEnv) {

0 commit comments

Comments
 (0)