From 2edc2a500e070acccd3b5727b7177bbca8bdca16 Mon Sep 17 00:00:00 2001 From: tbreisacher Date: Mon, 26 Feb 2018 16:39:27 -0800 Subject: [PATCH] Disallow nulls in assertTypeEquals() ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=187101439 --- .../javascript/rhino/testing/Asserts.java | 47 ++++++++----------- ...ClosureReverseAbstractInterpreterTest.java | 19 ++++---- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/com/google/javascript/rhino/testing/Asserts.java b/src/com/google/javascript/rhino/testing/Asserts.java index 74250447451..6cc37bcc430 100644 --- a/src/com/google/javascript/rhino/testing/Asserts.java +++ b/src/com/google/javascript/rhino/testing/Asserts.java @@ -39,17 +39,15 @@ package com.google.javascript.rhino.testing; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertEquals; import com.google.common.collect.Iterables; import com.google.javascript.rhino.ErrorReporter; import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.StaticTypedScope; - -import org.junit.Assert; - import java.util.Iterator; +import org.junit.Assert; /** * Helper methods for making assertions about the validity of types. @@ -69,12 +67,10 @@ public static JSType assertValidResolve(JSType type) { } /** @return The resolved type */ - public static JSType assertValidResolve( - JSType type, StaticTypedScope scope) { + public static JSType assertValidResolve(JSType type, StaticTypedScope scope) { ErrorReporter t = TestErrorReporter.forNoExpectedReports(); JSType resolvedType = type.resolve(t, scope); - assertTypeEquals("JSType#resolve should not affect object equality", - type, resolvedType); + assertTypeEquals("JSType#resolve should not affect object equality", type, resolvedType); return resolvedType; } @@ -84,14 +80,14 @@ public static void assertTypeNotEquals(JSType a, JSType b) { public static void assertTypeNotEquals(String message, JSType a, JSType b) { Assert.assertFalse( - message + - (message.isEmpty() ? "" : "\n") + - "Type: " + b + "\n", + message + (message.isEmpty() ? "" : "\n") + + " Equals is not symmetric.\n" + + "Type: " + b + "\n", a.isEquivalentTo(b)); Assert.assertFalse( - message + - " Equals is not symmetric.\n" + - "Type: " + b + "\n", + message + (message.isEmpty() ? "" : "\n") + + " Equals is not symmetric.\n" + + "Type: " + b + "\n", b.isEquivalentTo(a)); } @@ -100,23 +96,18 @@ public static void assertTypeEquals(JSType a, JSType b) { } public static void assertTypeEquals(String message, JSType a, JSType b) { - assertEquals( - "Both types must be null, or both must be non-null " + a + "," + b, - (b == null), (a == null)); - if (a == null) { - return; - } + checkNotNull(a); + checkNotNull(b); Assert.assertTrue( - message + - (message.isEmpty() ? "" : "\n") + - "Expected: " + a + "\n" + - "Actual : " + b, + message + (message.isEmpty() ? "" : "\n") + + "Expected: " + a + "\n" + + "Actual : " + b, a.isEquivalentTo(b)); Assert.assertTrue( - message + - " Equals is not symmetric.\n" + - "Expected: " + b + "\n" + - "Actual : " + a, + message + + " Equals is not symmetric.\n" + + "Expected: " + b + "\n" + + "Actual : " + a, b.isEquivalentTo(a)); } diff --git a/test/com/google/javascript/jscomp/ClosureReverseAbstractInterpreterTest.java b/test/com/google/javascript/jscomp/ClosureReverseAbstractInterpreterTest.java index e559009c2c0..5eda5cdb3c4 100644 --- a/test/com/google/javascript/jscomp/ClosureReverseAbstractInterpreterTest.java +++ b/test/com/google/javascript/jscomp/ClosureReverseAbstractInterpreterTest.java @@ -16,6 +16,8 @@ package com.google.javascript.jscomp; +import static com.google.common.truth.Truth.assertThat; + import com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter; import com.google.javascript.jscomp.type.FlowScope; import com.google.javascript.rhino.Node; @@ -302,19 +304,20 @@ private void testClosureFunction(String function, JSType type, assertEquals(Token.NAME, name.getToken()); flowScope.inferSlotType("a", type); - ClosureReverseAbstractInterpreter rai = - new ClosureReverseAbstractInterpreter(registry); + ClosureReverseAbstractInterpreter rai = new ClosureReverseAbstractInterpreter(registry); // trueScope Asserts.assertTypeEquals( trueType, - rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, true) - .getSlot("a").getType()); + rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, true).getSlot("a").getType()); // falseScope - Asserts.assertTypeEquals( - falseType, - rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, false) - .getSlot("a").getType()); + JSType aType = rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, false) + .getSlot("a").getType(); + if (falseType == null) { + assertThat(aType).isNull(); + } else { + Asserts.assertTypeEquals(falseType, aType); + } } }