Skip to content

Commit

Permalink
Replace uses of TestCase methods with uses of Truth.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=214889701
  • Loading branch information
nreid260 authored and brad4d committed Sep 28, 2018
1 parent 3978ac5 commit 5dfd0ac
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 251 deletions.
49 changes: 25 additions & 24 deletions test/com/google/javascript/jscomp/SimpleReplaceScriptTest.java
Expand Up @@ -17,6 +17,7 @@
package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.javascript.rhino.testing.NodeSubject.assertNode; import static com.google.javascript.rhino.testing.NodeSubject.assertNode;


import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void testInfer() {
+ "obj.temp(10);\n"; + "obj.temp(10);\n";
Result result = runReplaceScript(options, Result result = runReplaceScript(options,
ImmutableList.of(source), 0, 0, source, 0, false).getResult(); ImmutableList.of(source), 0, 0, source, 0, false).getResult();
assertTrue(result.success); assertThat(result.success).isTrue();
} }


@Test @Test
Expand All @@ -65,7 +66,7 @@ public void testInferWithModules() {
SourceFile.fromCode("in", "")); SourceFile.fromCode("in", ""));


Result result = compiler.compile(EXTVAR_EXTERNS, inputs, options); Result result = compiler.compile(EXTVAR_EXTERNS, inputs, options);
assertTrue(result.success); assertThat(result.success).isTrue();


CompilerInput oldInput = compiler.getInput(new InputId("in")); CompilerInput oldInput = compiler.getInput(new InputId("in"));
JSModule myModule = oldInput.getModule(); JSModule myModule = oldInput.getModule();
Expand All @@ -91,7 +92,7 @@ public void testreplaceScript() {
List<SourceFile> inputs = ImmutableList.of( List<SourceFile> inputs = ImmutableList.of(
SourceFile.fromCode("in", source)); SourceFile.fromCode("in", source));
Result result = compiler.compile(EXTVAR_EXTERNS, inputs, options); Result result = compiler.compile(EXTVAR_EXTERNS, inputs, options);
assertTrue(result.success); assertThat(result.success).isTrue();


// Now try to re-infer with a modified version of source // Now try to re-infer with a modified version of source
// with a new variable. // with a new variable.
Expand Down Expand Up @@ -151,7 +152,7 @@ public void testUndefinedVars() {
+ "var c = cVar + 1;"; + "var c = cVar + 1;";
Result result = this.runReplaceScript(options, ImmutableList.of( Result result = this.runReplaceScript(options, ImmutableList.of(
firstSource, secondSource), 1, 0, modifiedSource, 1, true).getResult(); firstSource, secondSource), 1, 0, modifiedSource, 1, true).getResult();
assertFalse(result.success); assertThat(result.success).isFalse();
assertThat(result.errors).hasLength(2); assertThat(result.errors).hasLength(2);
int i = 2; int i = 2;
for (JSError e : result.errors) { for (JSError e : result.errors) {
Expand Down Expand Up @@ -323,7 +324,7 @@ public void testCheckRequires() {
.getResult(); .getResult();
// TODO(joeltine): Change back to asserting an error when b/28869281 // TODO(joeltine): Change back to asserting an error when b/28869281
// is fixed. // is fixed.
assertTrue(result.success); assertThat(result.success).isTrue();
} }


@Test @Test
Expand All @@ -336,7 +337,7 @@ public void testCheckRequiresWithNewVar() {
ImmutableList.of(src), 0, 0, modifiedSrc, 0, false).getResult(); ImmutableList.of(src), 0, 0, modifiedSrc, 0, false).getResult();
// TODO(joeltine): Change back to asserting an error when b/28869281 // TODO(joeltine): Change back to asserting an error when b/28869281
// is fixed. // is fixed.
assertTrue(result.success); assertThat(result.success).isTrue();
} }


@Test @Test
Expand All @@ -348,7 +349,7 @@ public void testCheckProvides() {
+ "/** @constructor */ ns.Bar = function() {};"; + "/** @constructor */ ns.Bar = function() {};";
Result result = runReplaceScript(options, Result result = runReplaceScript(options,
ImmutableList.of(source0), 1, 0, source0, 0, true).getResult(); ImmutableList.of(source0), 1, 0, source0, 0, true).getResult();
assertFalse(result.success); assertThat(result.success).isFalse();


assertThat(result.errors).hasLength(1); assertThat(result.errors).hasLength(1);
assertErrorType(result.errors[0], CheckProvides.MISSING_PROVIDE_WARNING, 1); assertErrorType(result.errors[0], CheckProvides.MISSING_PROVIDE_WARNING, 1);
Expand All @@ -366,7 +367,7 @@ public void testNewTypeAdded() {
+ "var b = a * 20;"; + "var b = a * 20;";
Result result = this.runReplaceScript(options, Result result = this.runReplaceScript(options,
ImmutableList.of(src), 0, 0, modifiedSrc, 0, false).getResult(); ImmutableList.of(src), 0, 0, modifiedSrc, 0, false).getResult();
assertFalse(result.success); assertThat(result.success).isFalse();


assertThat(result.errors).hasLength(1); assertThat(result.errors).hasLength(1);
assertErrorType(result.errors[0], TypeValidator.TYPE_MISMATCH_WARNING, 4); assertErrorType(result.errors[0], TypeValidator.TYPE_MISMATCH_WARNING, 4);
Expand Down Expand Up @@ -792,7 +793,7 @@ public void testPrototypeSlotChangedOnCompile() {
type = compiler.getTypeRegistry().getGlobalType("ns.Foo"); type = compiler.getTypeRegistry().getGlobalType("ns.Foo");
fnType = type.toObjectType().getConstructor(); fnType = type.toObjectType().getConstructor();
StaticTypedSlot newSlot = fnType.getSlot("prototype"); StaticTypedSlot newSlot = fnType.getSlot("prototype");
assertNotSame(originalSlot, newSlot); assertThat(newSlot).isNotSameAs(originalSlot);
} }


/** This test will fail if global scope generation happens before closure-pass. */ /** This test will fail if global scope generation happens before closure-pass. */
Expand Down Expand Up @@ -919,8 +920,8 @@ public void testGoogScope() {


Result result = this.runReplaceScript(options, ImmutableList.of(src0, Result result = this.runReplaceScript(options, ImmutableList.of(src0,
src1), 0, 0, modifiedSrc1, 1, true).getResult(); src1), 0, 0, modifiedSrc1, 1, true).getResult();
//ImmutableList.of(src0, modifiedSrc1), 1, 0, modifiedSrc1, 1, true); // ImmutableList.of(src0, modifiedSrc1), 1, 0, modifiedSrc1, 1, true);
assertFalse(result.success); assertThat(result.success).isFalse();
assertThat(result.errors).hasLength(1); assertThat(result.errors).hasLength(1);
assertErrorType(result.errors[0], assertErrorType(result.errors[0],
CheckAccessControls.BAD_PRIVATE_PROPERTY_ACCESS, 5); CheckAccessControls.BAD_PRIVATE_PROPERTY_ACCESS, 5);
Expand Down Expand Up @@ -961,32 +962,32 @@ public void testPatchGlobalTypedScope() {
SourceFile newSource1 = SourceFile.fromCode("in1", src1); SourceFile newSource1 = SourceFile.fromCode("in1", src1);
JsAst ast = new JsAst(newSource1); JsAst ast = new JsAst(newSource1);
compiler.replaceScript(ast); compiler.replaceScript(ast);
assertTrue(compiler.getResult().success); assertThat(compiler.getResult().success).isTrue();
assertScopesSimilar(oldGlobalScope, compiler.getTopScope()); assertScopesSimilar(oldGlobalScope, compiler.getTopScope());
assertScopeAndThisForScopeSimilar(compiler.getTopScope()); assertScopeAndThisForScopeSimilar(compiler.getTopScope());




SourceFile newSource2 = SourceFile.fromCode("in2", src2); SourceFile newSource2 = SourceFile.fromCode("in2", src2);
ast = new JsAst(newSource2); ast = new JsAst(newSource2);
compiler.replaceScript(ast); compiler.replaceScript(ast);
assertTrue(compiler.getResult().success); assertThat(compiler.getResult().success).isTrue();
assertScopesSimilar(oldGlobalScope, compiler.getTopScope()); assertScopesSimilar(oldGlobalScope, compiler.getTopScope());
assertScopeAndThisForScopeSimilar(compiler.getTopScope()); assertScopeAndThisForScopeSimilar(compiler.getTopScope());


newSource2 = SourceFile.fromCode("in2", ""); newSource2 = SourceFile.fromCode("in2", "");
ast = new JsAst(newSource2); ast = new JsAst(newSource2);
compiler.replaceScript(ast); compiler.replaceScript(ast);
assertTrue(compiler.getResult().success); assertThat(compiler.getResult().success).isTrue();
assertSubsetScope(compiler.getTopScope(), oldGlobalScope, assertSubsetScope(
ImmutableSet.of("obj2", "objNoType2")); compiler.getTopScope(), oldGlobalScope, ImmutableSet.of("obj2", "objNoType2"));
assertScopeAndThisForScopeSimilar(compiler.getTopScope()); assertScopeAndThisForScopeSimilar(compiler.getTopScope());
} }


private void assertScopeAndThisForScopeSimilar(TypedScope scope) { private void assertScopeAndThisForScopeSimilar(TypedScope scope) {
ObjectType typeOfThis = scope.getTypeOfThis().toObjectType(); ObjectType typeOfThis = scope.getTypeOfThis().toObjectType();
for (TypedVar v : scope.getAllSymbols()) { for (TypedVar v : scope.getAllSymbols()) {
if (!v.getName().contains(".")) { if (!v.getName().contains(".")) {
assertEquals(v.getNameNode(), typeOfThis.getPropertyNode(v.getName())); assertThat(typeOfThis.getPropertyNode(v.getName())).isEqualTo(v.getNameNode());
} }
} }
} }
Expand All @@ -1000,10 +1001,10 @@ private void assertSubsetScope(TypedScope subScope, TypedScope scope,
for (TypedVar var1 : scope.getVarIterable()) { for (TypedVar var1 : scope.getVarIterable()) {
TypedVar var2 = subScope.getVar(var1.getName()); TypedVar var2 = subScope.getVar(var1.getName());
if (missingVars.contains(var1.getName())) { if (missingVars.contains(var1.getName())) {
assertNull(var2); assertThat(var2).isNull();
} else { } else {
assertNotNull(var2); assertThat(var2).isNotNull();
assertEquals(var1.getType(), var2.getType()); assertThat(var2.getType()).isEqualTo(var1.getType());
} }
} }
} }
Expand All @@ -1022,7 +1023,7 @@ public void testInferJsDocInfo() {
ImmutableList.of(src), 0, 0, modifiedSrc, 0, true); ImmutableList.of(src), 0, 0, modifiedSrc, 0, true);
TypedVar var = compiler.getTopScope().getVar("temp"); TypedVar var = compiler.getTopScope().getVar("temp");
ObjectType type = var.getType().toObjectType().getImplicitPrototype(); ObjectType type = var.getType().toObjectType().getImplicitPrototype();
assertNotNull(type.getOwnPropertyJSDocInfo("prop")); assertThat(type.getOwnPropertyJSDocInfo("prop")).isNotNull();
} }


/** Effectively this tests the clean-up of properties on un-named objects. */ /** Effectively this tests the clean-up of properties on un-named objects. */
Expand All @@ -1036,7 +1037,7 @@ public void testNoErrorOnGoogProvide() {
+ "ns.Bar.bar = function() {};\n"; + "ns.Bar.bar = function() {};\n";
Result result = this.runReplaceScript(options, Result result = this.runReplaceScript(options,
ImmutableList.of(src0, src1), 0, 0, src1, 1, false).getResult(); ImmutableList.of(src0, src1), 0, 0, src1, 1, false).getResult();
assertTrue(result.success); assertThat(result.success).isTrue();


assertThat(result.errors).isEmpty(); assertThat(result.errors).isEmpty();
assertThat(result.warnings).isEmpty(); assertThat(result.warnings).isEmpty();
Expand All @@ -1053,7 +1054,7 @@ public void testAsyncReplaceScript() {
String src0 = "async function foo() {}"; String src0 = "async function foo() {}";
Result result = Result result =
this.runReplaceScript(options, ImmutableList.of(src0), 0, 0, src0, 0, false).getResult(); this.runReplaceScript(options, ImmutableList.of(src0), 0, 0, src0, 0, false).getResult();
assertTrue(result.success); assertThat(result.success).isTrue();


assertThat(result.errors).isEmpty(); assertThat(result.errors).isEmpty();
assertThat(result.warnings).isEmpty(); assertThat(result.warnings).isEmpty();
Expand Down Expand Up @@ -1100,7 +1101,7 @@ public void testAddExistingScript() {


try { try {
doAddScript(compiler, updatedOtherSrc, 1); doAddScript(compiler, updatedOtherSrc, 1);
fail("Expected an IllegalStateException to be thrown"); assertWithMessage("Expected an IllegalStateException to be thrown").fail();
} catch (IllegalStateException expectedISE) { } catch (IllegalStateException expectedISE) {
//ignore expected exception //ignore expected exception
} }
Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;


import com.google.javascript.jscomp.SortingErrorManager.ErrorWithLevel; import com.google.javascript.jscomp.SortingErrorManager.ErrorWithLevel;
import com.google.javascript.jscomp.SortingErrorManager.LeveledJSErrorComparator; import com.google.javascript.jscomp.SortingErrorManager.LeveledJSErrorComparator;
Expand Down Expand Up @@ -47,7 +48,7 @@ public final class SortingErrorManagerTest extends TestCase {


@Test @Test
public void testOrderingBothNull() { public void testOrderingBothNull() {
assertEquals(0, comparator.compare(null, null)); assertThat(comparator.compare(null, null)).isEqualTo(0);
} }


@Test @Test
Expand Down Expand Up @@ -156,7 +157,7 @@ public void println(CheckLevel level, JSError error) {


@Override @Override
protected void printSummary() { protected void printSummary() {
assertEquals(1, printed); assertThat(printed).isEqualTo(1);
} }
}; };
manager.report(CheckLevel.ERROR, JSError.make(NULL_SOURCE, -1, -1, FOO_TYPE)); manager.report(CheckLevel.ERROR, JSError.make(NULL_SOURCE, -1, -1, FOO_TYPE));
Expand All @@ -173,8 +174,8 @@ private ErrorWithLevel warning(JSError e) {


private void assertSmaller(ErrorWithLevel p1, ErrorWithLevel p2) { private void assertSmaller(ErrorWithLevel p1, ErrorWithLevel p2) {
int p1p2 = comparator.compare(p1, p2); int p1p2 = comparator.compare(p1, p2);
assertTrue(Integer.toString(p1p2), p1p2 < 0); assertWithMessage(Integer.toString(p1p2)).that(p1p2 < 0).isTrue();
int p2p1 = comparator.compare(p2, p1); int p2p1 = comparator.compare(p2, p1);
assertTrue(Integer.toString(p2p1), p2p1 > 0); assertWithMessage(Integer.toString(p2p1)).that(p2p1 > 0).isTrue();
} }
} }
24 changes: 14 additions & 10 deletions test/com/google/javascript/jscomp/SourceMapResolverTest.java
Expand Up @@ -15,6 +15,7 @@
*/ */
package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;


import com.google.common.io.BaseEncoding; import com.google.common.io.BaseEncoding;
Expand Down Expand Up @@ -42,7 +43,7 @@ public void testResolveBase64Inline() throws Exception {
SourceFile noInline = SourceFile noInline =
SourceMapResolver.extractSourceMap( SourceMapResolver.extractSourceMap(
SourceFile.fromCode("somePath/hello.js", code), url, false); SourceFile.fromCode("somePath/hello.js", code), url, false);
assertNull(noInline); assertThat(noInline).isNull();
} }


@Test @Test
Expand All @@ -63,23 +64,26 @@ public void testResolveBase64WithCharsetInline() throws Exception {
SourceFile result = SourceFile result =
SourceMapResolver.extractSourceMap( SourceMapResolver.extractSourceMap(
SourceFile.fromCode("somePath/hello.js", charsetCode), dataURLWithBadCharset, true); SourceFile.fromCode("somePath/hello.js", charsetCode), dataURLWithBadCharset, true);
assertNull(result); assertThat(result).isNull();
} }


@Test @Test
public void testAbsolute() { public void testAbsolute() {
SourceFile jsFile = SourceFile.fromCode("somePath/hello.js", "console.log(1)"); SourceFile jsFile = SourceFile.fromCode("somePath/hello.js", "console.log(1)");
// We cannot reslove absolute urls. // We cannot reslove absolute urls.
assertNull(SourceMapResolver.extractSourceMap(jsFile, "/asdf/asdf.js", true)); assertThat(SourceMapResolver.extractSourceMap(jsFile, "/asdf/asdf.js", true)).isNull();
assertNull(SourceMapResolver.extractSourceMap(jsFile, "/asdf/.././asdf.js", true)); assertThat(SourceMapResolver.extractSourceMap(jsFile, "/asdf/.././asdf.js", true)).isNull();
assertNull(SourceMapResolver.extractSourceMap(jsFile, "http://google.com/asdf/asdf.js", true)); assertThat(SourceMapResolver.extractSourceMap(jsFile, "http://google.com/asdf/asdf.js", true))
assertNull(SourceMapResolver.extractSourceMap(jsFile, "https://google.com/asdf/asdf.js", true)); .isNull();
assertThat(SourceMapResolver.extractSourceMap(jsFile, "https://google.com/asdf/asdf.js", true))
.isNull();


// We can resolve relative urls // We can resolve relative urls
assertNotNull(SourceMapResolver.extractSourceMap(jsFile, "asdf.js", true)); assertThat(SourceMapResolver.extractSourceMap(jsFile, "asdf.js", true)).isNotNull();
assertNotNull(SourceMapResolver.extractSourceMap(jsFile, "asdf/asdf.js", true)); assertThat(SourceMapResolver.extractSourceMap(jsFile, "asdf/asdf.js", true)).isNotNull();
assertNotNull(SourceMapResolver.extractSourceMap(jsFile, "asdf/.././asdf.js", true)); assertThat(SourceMapResolver.extractSourceMap(jsFile, "asdf/.././asdf.js", true)).isNotNull();
assertNotNull(SourceMapResolver.extractSourceMap(jsFile, "not/.././a/js/file.txt", true)); assertThat(SourceMapResolver.extractSourceMap(jsFile, "not/.././a/js/file.txt", true))
.isNotNull();
} }


@Test @Test
Expand Down

0 comments on commit 5dfd0ac

Please sign in to comment.