Skip to content

Commit

Permalink
Migrate Correspondence subclasses to instead call Correspondence.from.
Browse files Browse the repository at this point in the history
This makes the code shorter, and the subclassing approach is deprecated.

Note: Correspondence.from was added to Truth 0.43. (So was Correspondence.transforming, which I'm using occasionally instead.) Correspondence.formattingDiffsUsing was added in Truth 0.44.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=249880176
  • Loading branch information
cpovirk authored and tjgq committed May 25, 2019
1 parent 84e4bf6 commit d900a00
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 94 deletions.
Expand Up @@ -48,34 +48,15 @@ public final class JSCompCorrespondences {
// tests.
public static final <A, E> Correspondence<A, E> transforming(
Function<? super A, ? extends E> transformation, String description) {
return new Correspondence<A, E>() {
@Override
public boolean compare(A actual, E expected) {
return transformation.apply(actual).equals(expected);
}

@Override
public String toString() {
return description;
}
};
return Correspondence.from(
(actual, expected) -> transformation.apply(actual).equals(expected), description);
}

// TODO(nickreid): Delete this when `Correspondence::from` is available in our Maven
// tests.
public static final <A, E> Correspondence<A, E> from(
BiPredicate<? super A, ? super E> predicate, String description) {
return new Correspondence<A, E>() {
@Override
public boolean compare(A actual, E expected) {
return predicate.test(actual, expected);
}

@Override
public String toString() {
return description;
}
};
return Correspondence.from((actual, expected) -> predicate.test(actual, expected), description);
}

/** A compiler for parsing snippets of code into AST as leniently as possible. */
Expand Down
Expand Up @@ -32,9 +32,7 @@
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
* Utilities for testing RefasterJs templates.
*/
/** Utilities for testing RefasterJs templates. */
public final class RefasterJsTestUtils {

/**
Expand Down Expand Up @@ -89,7 +87,7 @@ public static void assertFileRefactoring(
.map(m -> m.get(originalFilePath))
.collect(ImmutableList.toImmutableList());
assertThat(newCode)
.comparingElementsUsing(new IgnoringWhitespaceCorrespondence())
.comparingElementsUsing(IGNORING_WHITESPACE_CORRESPONDENCE)
.containsExactlyElementsIn(expectedCode);
}

Expand All @@ -99,20 +97,13 @@ private static String slurpFile(String originalFile) throws IOException {

private RefasterJsTestUtils() {}

private static class IgnoringWhitespaceCorrespondence extends Correspondence<String, String> {

@Override
public boolean compare(String actual, String expected) {
return replaceTrailingWhitespace(actual).equals(replaceTrailingWhitespace(expected));
}

private String replaceTrailingWhitespace(String contents) {
return contents.replaceAll("[ \t]*\n", "\n");
}
private static final Correspondence<String, String> IGNORING_WHITESPACE_CORRESPONDENCE =
Correspondence.transforming(
RefasterJsTestUtils::replaceTrailingWhitespace,
RefasterJsTestUtils::replaceTrailingWhitespace,
"equals (except for whitespace)");

@Override
public String toString() {
return "equals (except for whitespace)";
}
private static String replaceTrailingWhitespace(String contents) {
return contents.replaceAll("[ \t]*\n", "\n");
}
}
28 changes: 9 additions & 19 deletions test/com/google/javascript/jscomp/CompilerTestCase.java
Expand Up @@ -69,6 +69,7 @@
*
*/
public abstract class CompilerTestCase {

protected static final Joiner LINE_JOINER = Joiner.on('\n');

/** Externs for the test */
Expand Down Expand Up @@ -1471,7 +1472,7 @@ private void testInternal(
// Might be an expected parse error.
assertWithMessage("parse errors")
.that(compiler.getErrors())
.comparingElementsUsing(new DiagnosticCorrespondence())
.comparingElementsUsing(DIAGNOSTIC_CORRESPONDENCE)
.containsExactlyElementsIn(expectedErrors);
for (Postcondition postcondition : postconditions) {
postcondition.verify(compiler);
Expand Down Expand Up @@ -1724,7 +1725,7 @@ private void testInternal(
for (int i = 0; i < numRepetitions; i++) {
assertWithMessage("compile warnings from repetition " + (i + 1))
.that(errorManagers[i].getWarnings())
.comparingElementsUsing(new DiagnosticCorrespondence())
.comparingElementsUsing(DIAGNOSTIC_CORRESPONDENCE)
.containsExactlyElementsIn(expectedWarnings);
for (JSError warning : errorManagers[i].getWarnings()) {
validateSourceLocation(warning);
Expand Down Expand Up @@ -1822,7 +1823,7 @@ private void testInternal(
} else {
assertWithMessage("compile errors")
.that(compiler.getErrors())
.comparingElementsUsing(new DiagnosticCorrespondence())
.comparingElementsUsing(DIAGNOSTIC_CORRESPONDENCE)
.containsExactlyElementsIn(expectedErrors);
for (JSError error : compiler.getErrors()) {
validateSourceLocation(error);
Expand Down Expand Up @@ -2470,22 +2471,11 @@ private static class WarningDiagnostic extends Diagnostic {
}
}

private static class DiagnosticCorrespondence extends Correspondence<JSError, Diagnostic> {
@Override
public boolean compare(JSError actual, Diagnostic expected) {
return expected.matches(actual);
}

@Override
public String formatDiff(JSError actual, Diagnostic expected) {
return expected.formatDiff(actual);
}

@Override
public String toString() {
return "is a JSError matching";
}
}
private static final Correspondence<JSError, Diagnostic> DIAGNOSTIC_CORRESPONDENCE =
Correspondence.from(
(JSError actual, Diagnostic expected) -> expected.matches(actual),
"is a JSError matching")
.formattingDiffsUsing((actual, expected) -> expected.formatDiff(actual));

private static class NamedPredicate<T> implements Predicate<T> {
final Predicate<T> delegate;
Expand Down
18 changes: 5 additions & 13 deletions test/com/google/javascript/jscomp/PhaseOptimizerTest.java
Expand Up @@ -227,7 +227,7 @@ public void testSetSkipUnsupportedPasses() {
assertPasses();

assertThat(compiler.getWarnings())
.comparingElementsUsing(new DiagnosticCorrespondence())
.comparingElementsUsing(DIAGNOSTIC_CORRESPONDENCE)
.containsExactly(FEATURES_NOT_SUPPORTED_BY_PASS);
}

Expand All @@ -239,7 +239,7 @@ public void testSetDontSkipUnsupportedPasses() {
assertPasses("testPassFactory");

assertThat(compiler.getErrors())
.comparingElementsUsing(new DiagnosticCorrespondence())
.comparingElementsUsing(DIAGNOSTIC_CORRESPONDENCE)
.containsExactly(FEATURES_NOT_SUPPORTED_BY_PASS);
}

Expand Down Expand Up @@ -304,15 +304,7 @@ private CompilerPass createPass(final String name, int numChanges) {
};
}

private static class DiagnosticCorrespondence extends Correspondence<JSError, DiagnosticType> {
@Override
public boolean compare(JSError actual, DiagnosticType expected) {
return actual.getType().equals(expected);
}

@Override
public String toString() {
return "has diagnostic";
}
}
private static final Correspondence<JSError, DiagnosticType> DIAGNOSTIC_CORRESPONDENCE =
Correspondence.from(
(actual, expected) -> actual.getType().equals(expected), "has diagnostic");
}
14 changes: 3 additions & 11 deletions test/com/google/javascript/jscomp/RecoverableJsAstTest.java
Expand Up @@ -179,15 +179,7 @@ protected Node parseExpectedJs(List<SourceFile> inputs) {
}

private static final Correspondence<JSError, String> DESCRIPTION_EQUALITY =
new Correspondence<JSError, String>() {
@Override
public boolean compare(JSError error, String description) {
return Objects.equals(error.description, description);
}

@Override
public String toString() {
return "has description equal to";
}
};
Correspondence.from(
(error, description) -> Objects.equals(error.description, description),
"has description equal to");
}
13 changes: 2 additions & 11 deletions test/com/google/javascript/jscomp/TypeCheckTestCase.java
Expand Up @@ -353,15 +353,6 @@ private TypeCheckResult(Node root, TypedScope scope) {
}

private static final Correspondence<JSError, DiagnosticType> DIAGNOSTIC_TYPE_EQUALITY =
new Correspondence<JSError, DiagnosticType>() {
@Override
public boolean compare(JSError error, DiagnosticType type) {
return Objects.equals(error.getType(), type);
}

@Override
public String toString() {
return "has diagnostic type equal to";
}
};
Correspondence.from(
(error, type) -> Objects.equals(error.getType(), type), "has diagnostic type equal to");
}

0 comments on commit d900a00

Please sign in to comment.