Skip to content

Commit

Permalink
Disallow nulls in assertTypeEquals()
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=187101439
  • Loading branch information
tbreisacher committed Feb 27, 2018
1 parent 5001deb commit 2edc2a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
47 changes: 19 additions & 28 deletions src/com/google/javascript/rhino/testing/Asserts.java
Expand Up @@ -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.
Expand All @@ -69,12 +67,10 @@ public static JSType assertValidResolve(JSType type) {
}

/** @return The resolved type */
public static JSType assertValidResolve(
JSType type, StaticTypedScope<JSType> scope) {
public static JSType assertValidResolve(JSType type, StaticTypedScope<JSType> 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;
}

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

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

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 2edc2a5

Please sign in to comment.