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 ;
35
23
import com .google .common .base .Predicate ;
36
24
import com .google .common .collect .FluentIterable ;
37
25
import com .google .common .collect .ImmutableList ;
26
+ import com .google .common .collect .ImmutableMap ;
38
27
import com .google .common .collect .ImmutableSet ;
39
28
import com .google .common .collect .Iterables ;
40
29
import com .google .common .collect .Lists ;
41
30
import com .google .common .io .ByteStreams ;
42
31
import com .google .testing .compile .Compilation .Result ;
32
+
43
33
import com .sun .source .tree .CompilationUnitTree ;
44
34
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
+
45
49
/**
46
50
* A <a href="https://github.com/truth0/truth">Truth</a> {@link Subject} that evaluates the result
47
51
* of a {@code javac} compilation. See {@link com.google.testing.compile} for usage examples
@@ -88,6 +92,7 @@ private CompilationClause(Iterable<? extends Processor> processors) {
88
92
this .processors = ImmutableSet .copyOf (processors );
89
93
}
90
94
95
+ @ Override
91
96
public SuccessfulCompilationClause compilesWithoutError () {
92
97
Compilation .Result result = Compilation .compile (processors , getSubject ());
93
98
if (!result .successful ()) {
@@ -100,6 +105,7 @@ public SuccessfulCompilationClause compilesWithoutError() {
100
105
return new SuccessfulCompilationBuilder (result );
101
106
}
102
107
108
+ @ Override
103
109
public UnsuccessfulCompilationClause failsToCompile () {
104
110
Result result = Compilation .compile (processors , getSubject ());
105
111
if (result .successful ()) {
@@ -264,8 +270,30 @@ public boolean apply(CompilationUnitTree input) {
264
270
}
265
271
});
266
272
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
+ }
269
297
}
270
298
}
271
299
return this ;
0 commit comments