diff --git a/src/com/google/javascript/jscomp/testing/JSCompCorrespondences.java b/src/com/google/javascript/jscomp/testing/JSCompCorrespondences.java index 6bd00b84632..92fdaf4fab5 100644 --- a/src/com/google/javascript/jscomp/testing/JSCompCorrespondences.java +++ b/src/com/google/javascript/jscomp/testing/JSCompCorrespondences.java @@ -48,34 +48,15 @@ public final class JSCompCorrespondences { // tests. public static final Correspondence transforming( Function transformation, String description) { - return new Correspondence() { - @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 Correspondence from( BiPredicate predicate, String description) { - return new Correspondence() { - @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. */ diff --git a/src/com/google/javascript/refactoring/testing/RefasterJsTestUtils.java b/src/com/google/javascript/refactoring/testing/RefasterJsTestUtils.java index 64999505414..107405d137f 100644 --- a/src/com/google/javascript/refactoring/testing/RefasterJsTestUtils.java +++ b/src/com/google/javascript/refactoring/testing/RefasterJsTestUtils.java @@ -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 { /** @@ -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); } @@ -99,20 +97,13 @@ private static String slurpFile(String originalFile) throws IOException { private RefasterJsTestUtils() {} - private static class IgnoringWhitespaceCorrespondence extends Correspondence { - - @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 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"); } } diff --git a/test/com/google/javascript/jscomp/CompilerTestCase.java b/test/com/google/javascript/jscomp/CompilerTestCase.java index 745c464af3e..89b779df0e5 100644 --- a/test/com/google/javascript/jscomp/CompilerTestCase.java +++ b/test/com/google/javascript/jscomp/CompilerTestCase.java @@ -69,6 +69,7 @@ * */ public abstract class CompilerTestCase { + protected static final Joiner LINE_JOINER = Joiner.on('\n'); /** Externs for the test */ @@ -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); @@ -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); @@ -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); @@ -2470,22 +2471,11 @@ private static class WarningDiagnostic extends Diagnostic { } } - private static class DiagnosticCorrespondence extends Correspondence { - @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 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 implements Predicate { final Predicate delegate; diff --git a/test/com/google/javascript/jscomp/PhaseOptimizerTest.java b/test/com/google/javascript/jscomp/PhaseOptimizerTest.java index 856992d66e6..e14032d12a7 100644 --- a/test/com/google/javascript/jscomp/PhaseOptimizerTest.java +++ b/test/com/google/javascript/jscomp/PhaseOptimizerTest.java @@ -227,7 +227,7 @@ public void testSetSkipUnsupportedPasses() { assertPasses(); assertThat(compiler.getWarnings()) - .comparingElementsUsing(new DiagnosticCorrespondence()) + .comparingElementsUsing(DIAGNOSTIC_CORRESPONDENCE) .containsExactly(FEATURES_NOT_SUPPORTED_BY_PASS); } @@ -239,7 +239,7 @@ public void testSetDontSkipUnsupportedPasses() { assertPasses("testPassFactory"); assertThat(compiler.getErrors()) - .comparingElementsUsing(new DiagnosticCorrespondence()) + .comparingElementsUsing(DIAGNOSTIC_CORRESPONDENCE) .containsExactly(FEATURES_NOT_SUPPORTED_BY_PASS); } @@ -304,15 +304,7 @@ private CompilerPass createPass(final String name, int numChanges) { }; } - private static class DiagnosticCorrespondence extends Correspondence { - @Override - public boolean compare(JSError actual, DiagnosticType expected) { - return actual.getType().equals(expected); - } - - @Override - public String toString() { - return "has diagnostic"; - } - } + private static final Correspondence DIAGNOSTIC_CORRESPONDENCE = + Correspondence.from( + (actual, expected) -> actual.getType().equals(expected), "has diagnostic"); } diff --git a/test/com/google/javascript/jscomp/RecoverableJsAstTest.java b/test/com/google/javascript/jscomp/RecoverableJsAstTest.java index 99d6c3d0541..b7f5b2e255b 100644 --- a/test/com/google/javascript/jscomp/RecoverableJsAstTest.java +++ b/test/com/google/javascript/jscomp/RecoverableJsAstTest.java @@ -179,15 +179,7 @@ protected Node parseExpectedJs(List inputs) { } private static final Correspondence DESCRIPTION_EQUALITY = - new Correspondence() { - @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"); } diff --git a/test/com/google/javascript/jscomp/TypeCheckTestCase.java b/test/com/google/javascript/jscomp/TypeCheckTestCase.java index 7da8cc1d4c1..ab61f588ad0 100644 --- a/test/com/google/javascript/jscomp/TypeCheckTestCase.java +++ b/test/com/google/javascript/jscomp/TypeCheckTestCase.java @@ -353,15 +353,6 @@ private TypeCheckResult(Node root, TypedScope scope) { } private static final Correspondence DIAGNOSTIC_TYPE_EQUALITY = - new Correspondence() { - @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"); }