17
17
18
18
import static com .google .common .base .Preconditions .checkArgument ;
19
19
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
-
32
20
import com .google .common .base .Function ;
33
21
import com .google .common .base .Joiner ;
34
22
import com .google .common .base .Optional ;
40
28
import com .google .common .collect .Lists ;
41
29
import com .google .common .io .ByteStreams ;
42
30
import com .google .testing .compile .Compilation .Result ;
31
+
43
32
import com .sun .source .tree .CompilationUnitTree ;
44
33
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
+
45
46
/**
46
47
* A <a href="https://github.com/truth0/truth">Truth</a> {@link Subject} that evaluates the result
47
48
* of a {@code javac} compilation. See {@link com.google.testing.compile} for usage examples
@@ -88,6 +89,7 @@ private CompilationClause(Iterable<? extends Processor> processors) {
88
89
this .processors = ImmutableSet .copyOf (processors );
89
90
}
90
91
92
+ @ Override
91
93
public SuccessfulCompilationClause compilesWithoutError () {
92
94
Compilation .Result result = Compilation .compile (processors , getSubject ());
93
95
if (!result .successful ()) {
@@ -100,6 +102,7 @@ public SuccessfulCompilationClause compilesWithoutError() {
100
102
return new SuccessfulCompilationBuilder (result );
101
103
}
102
104
105
+ @ Override
103
106
public UnsuccessfulCompilationClause failsToCompile () {
104
107
Result result = Compilation .compile (processors , getSubject ());
105
108
if (result .successful ()) {
@@ -255,17 +258,35 @@ public SuccessfulCompilationClause generatesSources(JavaFileObject first,
255
258
Iterable <? extends CompilationUnitTree > actualCompilationUnits =
256
259
Compilation .parse (generatedSources );
257
260
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 ))) {
259
262
Optional <? extends CompilationUnitTree > found =
260
263
Iterables .tryFind (actualCompilationUnits , new Predicate <CompilationUnitTree >() {
261
264
@ 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 );
264
267
}
265
268
});
266
269
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
+ }
269
290
}
270
291
}
271
292
return this ;
0 commit comments