Skip to content

Commit

Permalink
Migrate users from the old, deprecated Subject.fail* methods to the n…
Browse files Browse the repository at this point in the history
…ew Subject.fail* methods or, in some cases, to Subject.check.

Most of the changes in this CL were made manually.

I've tried to preserve all information, but the format and grammar of the messages does change.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=247216731
  • Loading branch information
cpovirk committed May 8, 2019
1 parent 6b32901 commit 9ec074c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 56 deletions.
29 changes: 13 additions & 16 deletions src/main/java/com/google/testing/compile/CompilationSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.Iterables.size;
import static com.google.common.collect.Streams.mapWithIndex;
import static com.google.common.truth.Fact.fact;
import static com.google.common.truth.Fact.simpleFact;
import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.Compilation.Status.FAILURE;
Expand All @@ -36,6 +37,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.truth.Fact;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import com.google.common.truth.Truth;
Expand Down Expand Up @@ -302,14 +304,8 @@ private ImmutableList<Diagnostic<? extends JavaFileObject>> findMatchingDiagnost
@CanIgnoreReturnValue
public JavaFileObjectSubject generatedFile(
Location location, String packageName, String fileName) {
return checkGeneratedFile(
actual().generatedFile(location, packageName, fileName),
location,
"named \"%s\" in %s",
fileName,
packageName.isEmpty()
? "the default package"
: String.format("package \"%s\"", packageName));
String path = packageName.isEmpty() ? fileName : packageName.replace('.', '/') + '/' + fileName;
return generatedFile(location, path);
}

/** Asserts that compilation generated a file at {@code path}. */
Expand All @@ -330,21 +326,22 @@ public JavaFileObjectSubject generatedSourceFile(String qualifiedName) {
"compile.Failure", "package compile;", "", "final class Failure {}");

private JavaFileObjectSubject checkGeneratedFile(
Optional<JavaFileObject> generatedFile, Location location, String format, Object... args) {
String name = args.length == 0 ? format : String.format(format, args);
Optional<JavaFileObject> generatedFile, Location location, String path) {
if (!generatedFile.isPresent()) {
StringBuilder builder = new StringBuilder("generated the file ");
builder.append(name);
builder.append("; it generated:\n");
// TODO(b/132162475): Use Facts if it becomes public API.
ImmutableList.Builder<Fact> facts = ImmutableList.builder();
facts.add(fact("in location", location.getName()));
facts.add(simpleFact("it generated:"));
for (JavaFileObject generated : actual().generatedFiles()) {
if (generated.toUri().getPath().contains(location.getName())) {
builder.append(" ").append(generated.toUri().getPath()).append('\n');
facts.add(simpleFact(" " + generated.toUri().getPath()));
}
}
fail(builder.toString());
failWithoutActual(
fact("expected to generate file", "/" + path), facts.build().toArray(new Fact[0]));
return ignoreCheck().about(javaFileObjects()).that(ALREADY_FAILED);
}
return check("generatedFile(%s)", name).about(javaFileObjects()).that(generatedFile.get());
return check("generatedFile(/%s)", path).about(javaFileObjects()).that(generatedFile.get());
}

private static <T> Collector<T, ?, ImmutableList<T>> toImmutableList() {
Expand Down
35 changes: 11 additions & 24 deletions src/main/java/com/google/testing/compile/JavaFileObjectSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package com.google.testing.compile;

import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Fact.fact;
import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaFileObjects.asByteSource;
import static com.google.testing.compile.TreeDiffer.diffCompilationUnits;
import static com.google.testing.compile.TreeDiffer.matchCompilationUnits;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteSource;
import com.google.common.truth.FailureMetadata;
Expand Down Expand Up @@ -121,8 +121,8 @@ public StringSubject contentsAsUtf8String() {
public void hasSourceEquivalentTo(JavaFileObject expectedSource) {
performTreeDifference(
expectedSource,
"is equivalent to",
"Expected Source",
"expected to be equivalent to",
"expected",
(expectedResult, actualResult) ->
diffCompilationUnits(
getOnlyElement(expectedResult.compilationUnits()),
Expand Down Expand Up @@ -151,8 +151,8 @@ public void hasSourceEquivalentTo(JavaFileObject expectedSource) {
public void containsElementsIn(JavaFileObject expectedPattern) {
performTreeDifference(
expectedPattern,
"contains elements in",
"Expected Pattern",
"expected to contain elements in",
"expected pattern",
(expectedResult, actualResult) ->
matchCompilationUnits(
getOnlyElement(expectedResult.compilationUnits()),
Expand Down Expand Up @@ -180,25 +180,12 @@ private void performTreeDifference(
new TreeContext(expectedTree, expectedResult.trees()),
new TreeContext(actualTree, actualResult.trees()));
try {
fail(
Joiner.on('\n')
.join(
String.format("%s <%s>.", failureVerb, expected.toUri().getPath()),
"",
"Diffs:",
"======",
"",
diffReport,
"",
expectedTitle + ":",
"================",
"",
expected.getCharContent(false),
"",
"Actual Source:",
"==============",
"",
actual().getCharContent(false)));
failWithoutActual(
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)));
} 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 @@ -15,6 +15,7 @@
*/
package com.google.testing.compile;

import static com.google.common.truth.ExpectFailure.assertThat;
import static com.google.common.truth.Truth.assertThat;
import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.CompilationSubject.compilations;
Expand Down Expand Up @@ -796,7 +797,9 @@ public void generatedSourceFile_fail() {
.that(compilerWithGenerator().compile(HELLO_WORLD_RESOURCE))
.generatedSourceFile("ThisIsNotTheRightFile");
AssertionError expected = expectFailure.getFailure();
assertThat(expected.getMessage()).contains("generated the file ThisIsNotTheRightFile.java");
assertThat(expected)
.factValue("expected to generate file")
.isEqualTo("/ThisIsNotTheRightFile.java");
assertThat(expected.getMessage()).contains(GeneratingProcessor.GENERATED_CLASS_NAME);
}

Expand All @@ -815,8 +818,9 @@ public void generatedFilePath_fail() {
.that(compilerWithGenerator().compile(HELLO_WORLD_RESOURCE))
.generatedFile(CLASS_OUTPUT, "com/google/testing/compile/Bogus.class");
AssertionError expected = expectFailure.getFailure();
assertThat(expected.getMessage())
.contains("generated the file com/google/testing/compile/Bogus.class");
assertThat(expected)
.factValue("expected to generate file")
.isEqualTo("/com/google/testing/compile/Bogus.class");
}

@Test
Expand All @@ -834,10 +838,9 @@ public void generatedFilePackageFile_fail() {
.that(compilerWithGenerator().compile(HELLO_WORLD_RESOURCE))
.generatedFile(CLASS_OUTPUT, "com.google.testing.compile", "Bogus.class");
AssertionError expected = expectFailure.getFailure();
assertThat(expected.getMessage())
.contains(
"generated the file named \"Bogus.class\" "
+ "in package \"com.google.testing.compile\"");
assertThat(expected)
.factValue("expected to generate file")
.isEqualTo("/com/google/testing/compile/Bogus.class");
}

@Test
Expand All @@ -848,8 +851,7 @@ public void generatedFileDefaultPackageFile_fail() {
.that(compilerWithGenerator().compile(HELLO_WORLD_RESOURCE))
.generatedFile(CLASS_OUTPUT, "", "File.java");
AssertionError expected = expectFailure.getFailure();
assertThat(expected.getMessage())
.contains("generated the file named \"File.java\" in the default package");
assertThat(expected).factValue("expected to generate file").isEqualTo("/File.java");
assertThat(expected.getMessage()).contains(GeneratingProcessor.GENERATED_CLASS_NAME);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ public void hasSourceEquivalentTo_failOnDifferences() throws IOException {
.that(CLASS)
.hasSourceEquivalentTo(DIFFERENT_NAME);
AssertionError expected = expectFailure.getFailure();
assertThat(expected.getMessage()).contains("is equivalent to");
assertThat(expected).factKeys().contains("expected to be equivalent to");
assertThat(expected.getMessage()).contains(CLASS.getName());
assertThat(expected.getMessage()).contains(CLASS.getCharContent(false));
assertThat(expected).factValue("but was").isEqualTo(CLASS.getCharContent(false));
}

@Test
Expand All @@ -132,10 +132,10 @@ public void hasSourceEquivalentTo_failOnExtraInExpected() throws IOException {
.that(CLASS)
.hasSourceEquivalentTo(CLASS_WITH_FIELD);
AssertionError expected = expectFailure.getFailure();
assertThat(expected.getMessage()).contains("is equivalent to");
assertThat(expected).factKeys().contains("expected to be equivalent to");
assertThat(expected.getMessage()).contains("unmatched nodes in the expected tree");
assertThat(expected.getMessage()).contains(CLASS.getName());
assertThat(expected.getMessage()).contains(CLASS.getCharContent(false));
assertThat(expected).factValue("but was").isEqualTo(CLASS.getCharContent(false));
}

@Test
Expand All @@ -146,10 +146,10 @@ public void hasSourceEquivalentTo_failOnExtraInActual() throws IOException {
.that(CLASS_WITH_FIELD)
.hasSourceEquivalentTo(CLASS);
AssertionError expected = expectFailure.getFailure();
assertThat(expected.getMessage()).contains("is equivalent to");
assertThat(expected).factKeys().contains("expected to be equivalent to");
assertThat(expected.getMessage()).contains("unmatched nodes in the actual tree");
assertThat(expected.getMessage()).contains(CLASS_WITH_FIELD.getName());
assertThat(expected.getMessage()).contains(CLASS_WITH_FIELD.getCharContent(false));
assertThat(expected).factValue("but was").isEqualTo(CLASS_WITH_FIELD.getCharContent(false));
}

private static final JavaFileObject SAMPLE_ACTUAL_FILE_FOR_MATCHING =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.testing.compile;

import static com.google.common.truth.ExpectFailure.assertThat;
import static com.google.common.truth.Truth.assertAbout;
import static com.google.common.truth.Truth.assertThat;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
Expand Down Expand Up @@ -868,7 +869,9 @@ public void generatesFileNamed_failOnFileExistence() {
.generatesFileNamed(CLASS_OUTPUT, "com.google.testing.compile", "Bogus")
.withContents(ByteSource.wrap("Bar".getBytes(UTF_8)));
AssertionError expected = expectFailure.getFailure();
assertThat(expected.getMessage()).contains("generated the file named \"Bogus\"");
assertThat(expected)
.factValue("expected to generate file")
.isEqualTo("/com/google/testing/compile/Bogus");
assertThat(expected.getMessage()).contains(GeneratingProcessor.GENERATED_RESOURCE_NAME);
}

Expand Down

0 comments on commit 9ec074c

Please sign in to comment.