Skip to content

Commit 2f34849

Browse files
committed
Merge pull request google#37 from cgruber/showunmatchedfile
Show found generated file on comparison failure.
2 parents ea81dbb + 2e6c158 commit 2f34849

File tree

2 files changed

+66
-27
lines changed

2 files changed

+66
-27
lines changed

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@
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;
@@ -40,8 +28,21 @@
4028
import com.google.common.collect.Lists;
4129
import com.google.common.io.ByteStreams;
4230
import com.google.testing.compile.Compilation.Result;
31+
4332
import com.sun.source.tree.CompilationUnitTree;
4433

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

92+
@Override
9193
public SuccessfulCompilationClause compilesWithoutError() {
9294
Compilation.Result result = Compilation.compile(processors, getSubject());
9395
if (!result.successful()) {
@@ -100,6 +102,7 @@ public SuccessfulCompilationClause compilesWithoutError() {
100102
return new SuccessfulCompilationBuilder(result);
101103
}
102104

105+
@Override
103106
public UnsuccessfulCompilationClause failsToCompile() {
104107
Result result = Compilation.compile(processors, getSubject());
105108
if (result.successful()) {
@@ -255,17 +258,35 @@ public SuccessfulCompilationClause generatesSources(JavaFileObject first,
255258
Iterable<? extends CompilationUnitTree> actualCompilationUnits =
256259
Compilation.parse(generatedSources);
257260
final EqualityScanner scanner = new EqualityScanner();
258-
for (final CompilationUnitTree expected : Compilation.parse(Lists.asList(first, rest))) {
261+
for (final CompilationUnitTree expectedTree : Compilation.parse(Lists.asList(first, rest))) {
259262
Optional<? extends CompilationUnitTree> found =
260263
Iterables.tryFind(actualCompilationUnits, new Predicate<CompilationUnitTree>() {
261264
@Override
262-
public boolean apply(CompilationUnitTree input) {
263-
return scanner.visitCompilationUnit(expected, input);
265+
public boolean apply(CompilationUnitTree actualTree) {
266+
return scanner.visitCompilationUnit(expectedTree, actualTree);
264267
}
265268
});
266269
if (!found.isPresent()) {
267-
failureStrategy.fail("Did not find a source file coresponding to "
268-
+ expected.getSourceFile().getName());
270+
final JavaFileObject expected = expectedTree.getSourceFile();
271+
Optional<JavaFileObject> actual =
272+
FluentIterable.from(generatedSources).firstMatch(new Predicate<JavaFileObject>() {
273+
@Override public boolean apply(JavaFileObject generatedFile) {
274+
return generatedFile.toUri().getPath().endsWith(expected.toUri().getPath());
275+
}
276+
});
277+
if (actual.isPresent()) {
278+
CharSequence actualSource = null;
279+
try {
280+
actualSource = actual.get().getCharContent(false);
281+
} catch (IOException e) {
282+
throw new RuntimeException("Exception reading source content.", e);
283+
}
284+
failureStrategy.fail("Generated file " + expected.getName()
285+
+ " did not match expectation. Found:\n"
286+
+ (actualSource == null ? "no source found" : actualSource));
287+
} else {
288+
failureStrategy.fail("Did not find a source file named " + expected.getName());
289+
}
269290
}
270291
}
271292
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)