Skip to content

Commit

Permalink
Instead of calling Subject.actual(), store the actual value in a fiel…
Browse files Browse the repository at this point in the history
…d, and read that.

actual() is being removed.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=247414505
  • Loading branch information
cpovirk authored and ronshapiro committed May 15, 2019
1 parent 9ec074c commit 38f6d0c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
20 changes: 11 additions & 9 deletions src/main/java/com/google/testing/compile/CompilationSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

Expand All @@ -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()));
}
}

Expand Down Expand Up @@ -173,7 +175,7 @@ public DiagnosticInFile hadNoteContainingMatch(Pattern expectedPattern) {
private void checkDiagnosticCount(
int expectedCount, Diagnostic.Kind kind, Diagnostic.Kind... more) {
Iterable<Diagnostic<? extends JavaFileObject>> diagnostics =
actual().diagnosticsOfKind(kind, more);
actual.diagnosticsOfKind(kind, more);
int actualCount = size(diagnostics);
if (actualCount != expectedCount) {
failWithoutActual(
Expand Down Expand Up @@ -282,7 +284,7 @@ private ImmutableList<Diagnostic<? extends JavaFileObject>> findMatchingDiagnost
Diagnostic.Kind kind,
Diagnostic.Kind... more) {
ImmutableList<Diagnostic<? extends JavaFileObject>> diagnosticsOfKind =
actual().diagnosticsOfKind(kind, more);
actual.diagnosticsOfKind(kind, more);
ImmutableList<Diagnostic<? extends JavaFileObject>> diagnosticsWithMessage =
diagnosticsOfKind
.stream()
Expand Down Expand Up @@ -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. */
Expand All @@ -332,7 +334,7 @@ private JavaFileObjectSubject checkGeneratedFile(
ImmutableList.Builder<Fact> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -166,7 +169,7 @@ private void performTreeDifference(
String failureVerb,
String expectedTitle,
BiFunction<ParseResult, ParseResult, TreeDifference> 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));
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@
public final class JavaSourcesSubject
extends Subject<JavaSourcesSubject, Iterable<? extends JavaFileObject>>
implements CompileTester, ProcessedCompileTesterFactory {
private final Iterable<? extends JavaFileObject> actual;
private final List<String> options = new ArrayList<String>(Arrays.asList("-Xlint"));
@Nullable private ClassLoader classLoader;
@Nullable private ImmutableList<File> classPath;

JavaSourcesSubject(FailureMetadata failureMetadata, Iterable<? extends JavaFileObject> subject) {
super(failureMetadata, subject);
this.actual = subject;
}

@Override
Expand Down Expand Up @@ -150,13 +152,13 @@ private CompilationClause(Iterable<? extends Processor> 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<Diagnostic<? extends JavaFileObject>> errors =
actualResult.diagnosticsByKind().get(Kind.ERROR);
if (!errors.isEmpty()) {
Expand Down Expand Up @@ -334,7 +336,7 @@ private Compilation compilation() {
if (classPath != null) {
compiler = compiler.withClasspath(classPath);
}
return compiler.compile(actual());
return compiler.compile(actual);
}
}

Expand Down

0 comments on commit 38f6d0c

Please sign in to comment.