Skip to content

Commit

Permalink
Migrates "test/com/google/javascript/jscomp/**.java" to use JUnit4 ru…
Browse files Browse the repository at this point in the history
…nner and annotations.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=213944759
  • Loading branch information
nreid260 authored and tjgq committed Sep 22, 2018
1 parent 772f0f0 commit 5180a3a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@
import java.net.URI;
import java.net.URISyntaxException;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/** Tests for {@link BaseTranspiler}. */

@RunWith(JUnit4.class)
public final class BaseTranspilerTest extends TestCase {

private Transpiler transpiler;
Expand All @@ -48,6 +53,7 @@ public final class BaseTranspilerTest extends TestCase {
}

@Override
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
transpiler = new BaseTranspiler(mockCompiler, "es6_runtime");
Expand All @@ -56,27 +62,31 @@ public void setUp() {

// Tests for BaseTranspiler

@Test
public void testTranspiler_transpile() {
when(mockCompiler.compile(FOO_JS, "bar"))
.thenReturn(new BaseTranspiler.CompileResult("result", true, "srcmap"));
assertThat(transpiler.transpile(FOO_JS, "bar"))
.isEqualTo(new TranspileResult(FOO_JS, "bar", "result", "srcmap"));
}

@Test
public void testTranspiler_noTranspilation() {
when(mockCompiler.compile(FOO_JS, "bar"))
.thenReturn(new BaseTranspiler.CompileResult("result", false, "srcmap"));
assertThat(transpiler.transpile(FOO_JS, "bar"))
.isEqualTo(new TranspileResult(FOO_JS, "bar", "bar", ""));
}

@Test
public void testTranspiler_runtime() {
when(mockCompiler.runtime("es6_runtime")).thenReturn("$jscomp.es6();");
assertThat(transpiler.runtime()).isEqualTo("$jscomp.es6();");
}

// Tests for CompilerSupplier

@Test
public void testCompilerSupplier_compileChanged() {
BaseTranspiler.CompileResult result = compiler.compile(SOURCE_JS, "const x = () => 42;");
assertThat(result.source).isEqualTo("var x = function() {\n return 42;\n};\n");
Expand All @@ -85,20 +95,23 @@ public void testCompilerSupplier_compileChanged() {
.contains("\"mappings\":\"AAAA,IAAMA,IAAIA,QAAA,EAAM;AAAA,SAAA,EAAA;AAAA,CAAhB;;\"");
}

@Test
public void testCompilerSupplier_compileNoChange() {
BaseTranspiler.CompileResult result = compiler.compile(SOURCE_JS, "var x = 42;");
assertThat(result.source).isEqualTo("var x = 42;\n");
assertThat(result.transpiled).isFalse();
assertThat(result.sourceMap).isEmpty();
}

@Test
public void testCompilerSupplier_error() {
try {
compiler.compile(SOURCE_JS, "cons x = () => 42;");
fail("Expected an exception.");
} catch (TranspilationException expected) {}
}

@Test
public void testCompilerSupplier_runtime() {
String runtime = compiler.runtime("es6_runtime");
assertThat(runtime).contains("$jscomp.polyfill(\"Map\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@
import java.net.URI;
import java.net.URISyntaxException;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/** Tests for {@link CachingTranspiler}. */
@RunWith(JUnit4.class)
public final class CachingTranspilerTest extends TestCase {

private Transpiler transpiler;
Expand Down Expand Up @@ -57,23 +62,27 @@ public final class CachingTranspilerTest extends TestCase {
}

@Override
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
transpiler = new CachingTranspiler(delegate, CacheBuilder.newBuilder());
}

@Test
public void testTranspileDelegates() {
when(delegate.transpile(FOO_JS, "bar")).thenReturn(RESULT1);
assertThat(transpiler.transpile(FOO_JS, "bar")).isSameAs(RESULT1);
}

@Test
public void testTranspileCaches() {
when(delegate.transpile(FOO_JS, "bar")).thenReturn(RESULT1);
assertThat(transpiler.transpile(FOO_JS, "bar")).isSameAs(RESULT1);
assertThat(transpiler.transpile(FOO_JS, "bar")).isSameAs(RESULT1);
verify(delegate, times(1)).transpile(FOO_JS, "bar");
}

@Test
public void testTranspileDependsOnBothPathAndCode() {
when(delegate.transpile(FOO_JS, "bar")).thenReturn(RESULT1);
when(delegate.transpile(BAR_JS, "bar")).thenReturn(RESULT2);
Expand All @@ -83,11 +92,13 @@ public void testTranspileDependsOnBothPathAndCode() {
assertThat(transpiler.transpile(FOO_JS, "bard")).isSameAs(RESULT3);
}

@Test
public void testRuntimeDelegates() {
when(delegate.runtime()).thenReturn("xyzzy");
assertThat(transpiler.runtime()).isSameAs("xyzzy");
}

@Test
public void testRuntimeCaches() {
when(delegate.runtime()).thenReturn("xyzzy");
assertThat(transpiler.runtime()).isSameAs("xyzzy");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
import com.google.common.testing.EqualsTester;
import java.net.URI;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link TranspileResult}. */

@RunWith(JUnit4.class)
public final class TranspileResultTest extends TestCase {

@Test
public void testEquals() throws Exception {
new EqualsTester()
.addEqualityGroup(
Expand All @@ -38,12 +43,14 @@ public void testEquals() throws Exception {
.testEquals();
}

@Test
public void testEmbedSourceMap_noSourceMap() throws Exception {
TranspileResult result = new TranspileResult(new URI("a"), "b", "c", "");
assertThat(result.embedSourcemap()).isSameAs(result);
assertThat(result.embedSourcemapUrl("foo")).isSameAs(result);
}

@Test
public void testEmbedSourceMap() throws Exception {
TranspileResult result = new TranspileResult(new URI("a"), "b", "c", "{\"version\": 3}");
assertThat(result.embedSourcemap())
Expand Down

0 comments on commit 5180a3a

Please sign in to comment.