diff --git a/src/main/java/com/google/testing/compile/CompilationSubject.java b/src/main/java/com/google/testing/compile/CompilationSubject.java index fab107eb..65610af1 100644 --- a/src/main/java/com/google/testing/compile/CompilationSubject.java +++ b/src/main/java/com/google/testing/compile/CompilationSubject.java @@ -72,16 +72,18 @@ public static CompilationSubject assertThat(Compilation actual) { return assertAbout(compilations()).that(actual); } + private final Compilation actual; + CompilationSubject(FailureMetadata failureMetadata, Compilation actual) { super(failureMetadata, actual); + this.actual = actual; } /** Asserts that the compilation succeeded. */ public void succeeded() { - if (actual().status().equals(FAILURE)) { + if (actual.status().equals(FAILURE)) { failWithoutActual( - simpleFact( - actual().describeFailureDiagnostics() + actual().describeGeneratedSourceFiles())); + simpleFact(actual.describeFailureDiagnostics() + actual.describeGeneratedSourceFiles())); } } @@ -93,11 +95,11 @@ public void succeededWithoutWarnings() { /** Asserts that the compilation failed. */ public void failed() { - if (actual().status().equals(SUCCESS)) { + if (actual.status().equals(SUCCESS)) { failWithoutActual( simpleFact( "Compilation was expected to fail, but contained no errors.\n\n" - + actual().describeGeneratedSourceFiles())); + + actual.describeGeneratedSourceFiles())); } } @@ -173,7 +175,7 @@ public DiagnosticInFile hadNoteContainingMatch(Pattern expectedPattern) { private void checkDiagnosticCount( int expectedCount, Diagnostic.Kind kind, Diagnostic.Kind... more) { Iterable> diagnostics = - actual().diagnosticsOfKind(kind, more); + actual.diagnosticsOfKind(kind, more); int actualCount = size(diagnostics); if (actualCount != expectedCount) { failWithoutActual( @@ -282,7 +284,7 @@ private ImmutableList> findMatchingDiagnost Diagnostic.Kind kind, Diagnostic.Kind... more) { ImmutableList> diagnosticsOfKind = - actual().diagnosticsOfKind(kind, more); + actual.diagnosticsOfKind(kind, more); ImmutableList> diagnosticsWithMessage = diagnosticsOfKind .stream() @@ -311,7 +313,7 @@ public JavaFileObjectSubject generatedFile( /** Asserts that compilation generated a file at {@code path}. */ @CanIgnoreReturnValue public JavaFileObjectSubject generatedFile(Location location, String path) { - return checkGeneratedFile(actual().generatedFile(location, path), location, path); + return checkGeneratedFile(actual.generatedFile(location, path), location, path); } /** Asserts that compilation generated a source file for a type with a given qualified name. */ @@ -332,7 +334,7 @@ private JavaFileObjectSubject checkGeneratedFile( ImmutableList.Builder facts = ImmutableList.builder(); facts.add(fact("in location", location.getName())); facts.add(simpleFact("it generated:")); - for (JavaFileObject generated : actual().generatedFiles()) { + for (JavaFileObject generated : actual.generatedFiles()) { if (generated.toUri().getPath().contains(location.getName())) { facts.add(simpleFact(" " + generated.toUri().getPath())); } diff --git a/src/main/java/com/google/testing/compile/JavaFileObjectSubject.java b/src/main/java/com/google/testing/compile/JavaFileObjectSubject.java index 04bb0d51..ec631ad8 100644 --- a/src/main/java/com/google/testing/compile/JavaFileObjectSubject.java +++ b/src/main/java/com/google/testing/compile/JavaFileObjectSubject.java @@ -52,13 +52,16 @@ public static JavaFileObjectSubject assertThat(JavaFileObject actual) { return assertAbout(FACTORY).that(actual); } + private final JavaFileObject actual; + JavaFileObjectSubject(FailureMetadata failureMetadata, JavaFileObject actual) { super(failureMetadata, actual); + this.actual = actual; } @Override protected String actualCustomStringRepresentation() { - return actual().toUri().getPath(); + return actual.toUri().getPath(); } /** @@ -73,7 +76,7 @@ public void isEqualTo(@Nullable Object other) { JavaFileObject otherFile = (JavaFileObject) other; try { - if (!asByteSource(actual()).contentEquals(asByteSource(otherFile))) { + if (!asByteSource(actual).contentEquals(asByteSource(otherFile))) { failWithActual("expected to be equal to", other); } } catch (IOException e) { @@ -84,7 +87,7 @@ public void isEqualTo(@Nullable Object other) { /** Asserts that the actual file's contents are equal to {@code expected}. */ public void hasContents(ByteSource expected) { try { - if (!asByteSource(actual()).contentEquals(expected)) { + if (!asByteSource(actual).contentEquals(expected)) { failWithActual("expected to have contents", expected); } } catch (IOException e) { @@ -99,7 +102,7 @@ public void hasContents(ByteSource expected) { public StringSubject contentsAsString(Charset charset) { try { return check("contents()") - .that(JavaFileObjects.asByteSource(actual()).asCharSource(charset).read()); + .that(JavaFileObjects.asByteSource(actual).asCharSource(charset).read()); } catch (IOException e) { throw new RuntimeException(e); } @@ -166,7 +169,7 @@ private void performTreeDifference( String failureVerb, String expectedTitle, BiFunction differencingFunction) { - ParseResult actualResult = Parser.parse(ImmutableList.of(actual())); + ParseResult actualResult = Parser.parse(ImmutableList.of(actual)); CompilationUnitTree actualTree = getOnlyElement(actualResult.compilationUnits()); ParseResult expectedResult = Parser.parse(ImmutableList.of(expected)); @@ -181,11 +184,11 @@ private void performTreeDifference( new TreeContext(actualTree, actualResult.trees())); try { failWithoutActual( - fact("for file", actual().toUri().getPath()), + fact("for file", actual.toUri().getPath()), fact(failureVerb, expected.toUri().getPath()), fact("diff", diffReport), fact(expectedTitle, expected.getCharContent(false)), - fact("but was", actual().getCharContent(false))); + fact("but was", actual.getCharContent(false))); } catch (IOException e) { throw new IllegalStateException( "Couldn't read from JavaFileObject when it was already in memory.", e); diff --git a/src/main/java/com/google/testing/compile/JavaSourcesSubject.java b/src/main/java/com/google/testing/compile/JavaSourcesSubject.java index 07aa9bce..09d8cebf 100644 --- a/src/main/java/com/google/testing/compile/JavaSourcesSubject.java +++ b/src/main/java/com/google/testing/compile/JavaSourcesSubject.java @@ -65,12 +65,14 @@ public final class JavaSourcesSubject extends Subject> implements CompileTester, ProcessedCompileTesterFactory { + private final Iterable actual; private final List options = new ArrayList(Arrays.asList("-Xlint")); @Nullable private ClassLoader classLoader; @Nullable private ImmutableList classPath; JavaSourcesSubject(FailureMetadata failureMetadata, Iterable subject) { super(failureMetadata, subject); + this.actual = subject; } @Override @@ -150,13 +152,13 @@ private CompilationClause(Iterable processors) { @Override public void parsesAs(JavaFileObject first, JavaFileObject... rest) { - if (Iterables.isEmpty(actual())) { + if (Iterables.isEmpty(actual)) { failWithoutActual( simpleFact( "Compilation generated no additional source files, though some were expected.")); return; } - ParseResult actualResult = Parser.parse(actual()); + ParseResult actualResult = Parser.parse(actual); ImmutableList> errors = actualResult.diagnosticsByKind().get(Kind.ERROR); if (!errors.isEmpty()) { @@ -334,7 +336,7 @@ private Compilation compilation() { if (classPath != null) { compiler = compiler.withClasspath(classPath); } - return compiler.compile(actual()); + return compiler.compile(actual); } }