Skip to content

Commit

Permalink
Document cyclic union typedef issue in a test case
Browse files Browse the repository at this point in the history
Adds rhino.testing.Asserts.assertThrows since that was introduced in JUnit 4.13 but we're stuck on 4.12.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=236017746
  • Loading branch information
shicks authored and brad4d committed Feb 28, 2019
1 parent fa5eceb commit b551ab8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
21 changes: 21 additions & 0 deletions src/com/google/javascript/rhino/testing/Asserts.java
Expand Up @@ -112,4 +112,25 @@ public static void assertEquivalenceOperations(JSType a, JSType b) {
Assert.assertTrue(b.canCastTo(b));
Assert.assertTrue(b.canCastTo(a));
}

/**
* Assert that the given function throws a particular throwable. This method is inspired by a
* similar API in JUnit 4.13, but the compiler is currently pinned on 4.12, which doesn't include
* it.
*/
public static void assertThrows(
Class<? extends Throwable> exceptionClass, ThrowingRunnable runnable) {
try {
runnable.run();
assertWithMessage("Did not get expected exception: %s", exceptionClass).fail();
} catch (Throwable expectedException) {
assertThat(expectedException).isInstanceOf(exceptionClass);
}
}

/** Functional interface for use with {@link #assertThrows}. */
@FunctionalInterface
public static interface ThrowingRunnable {
void run() throws Throwable;
}
}
12 changes: 1 addition & 11 deletions test/com/google/javascript/jscomp/GlobalNamespaceTest.java
Expand Up @@ -18,8 +18,8 @@

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.javascript.jscomp.CompilerTypeTestCase.lines;
import static com.google.javascript.rhino.testing.Asserts.assertThrows;
import static com.google.javascript.rhino.testing.NodeSubject.assertNode;
import static com.google.javascript.rhino.testing.TypeSubject.assertType;

Expand Down Expand Up @@ -51,16 +51,6 @@ public final class GlobalNamespaceTest {

@Nullable private Compiler lastCompiler = null;

// TODO(b/124228379): We should be using assertThrows from JUnit4.
private static void assertThrows(Object exceptionClass, Runnable runnable) {
try {
runnable.run();
assertWithMessage("Did not get expected exception: %s", exceptionClass).fail();
} catch (Exception expectedException) {
// expected
}
}

@Test
public void firstGlobalAssignmentIsConsideredDeclaration() {
Name n = Name.createForTesting("a");
Expand Down
23 changes: 23 additions & 0 deletions test/com/google/javascript/jscomp/TypeCheckTest.java
Expand Up @@ -23,6 +23,7 @@
import static com.google.javascript.jscomp.TypeCheck.INSTANTIATE_ABSTRACT_CLASS;
import static com.google.javascript.jscomp.TypeCheck.STRICT_INEXISTENT_PROPERTY;
import static com.google.javascript.jscomp.parsing.JsDocInfoParser.BAD_TYPE_WIKI_LINK;
import static com.google.javascript.rhino.testing.Asserts.assertThrows;
import static com.google.javascript.rhino.testing.NodeSubject.assertNode;
import static com.google.javascript.rhino.testing.TypeSubject.assertType;

Expand Down Expand Up @@ -23079,6 +23080,28 @@ public void testUnionCollapse_recordWithOnlyOptionalPropertiesDoesNotSupercedeAr
"required: null"));
}

@Test
public void testCyclicUnionTypedefs() {
// TODO(b/112964849): This case should not throw anything.
assertThrows(
StackOverflowError.class,
() -> {
testTypes(
lines(
"/** @typedef {Foo|string} */",
"var Bar;",
"/** @typedef {Bar|number} */",
"var Foo;",
"var /** Foo */ foo;",
"var /** null */ x = foo;",
""),
lines(
"initializing variable", //
"found : (number|string)",
"required: null"));
});
}

private void testClosureTypes(String js, String description) {
testClosureTypesMultipleWarnings(js,
description == null ? null : ImmutableList.of(description));
Expand Down

0 comments on commit b551ab8

Please sign in to comment.