Skip to content

Commit

Permalink
Make some assertions more Truth-y.
Browse files Browse the repository at this point in the history
This should make the code and assertion output more readable.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=236969746
  • Loading branch information
nreid260 authored and blickly committed Mar 6, 2019
1 parent 4763f08 commit 0e110f9
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 108 deletions.
@@ -0,0 +1,53 @@
/*
* Copyright 2010 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.testing;

import com.google.common.truth.Correspondence;
import com.google.javascript.jscomp.CompilerInput;
import com.google.javascript.jscomp.DiagnosticType;
import com.google.javascript.jscomp.JSError;
import java.util.function.Function;

/** Well known {@link Correspondence} instances for use in tests. */
public final class JSCompCorrespondences {

public static final Correspondence<JSError, DiagnosticType> DIAGNOSTIC_EQUALITY =
transforming(JSError::getType, "has diagnostic type equal to");

public static final Correspondence<JSError, String> DESCRIPTION_EQUALITY =
transforming((e) -> e.description, "has description equal to");

public static final Correspondence<CompilerInput, String> INPUT_NAME_EQUALITY =
transforming(CompilerInput::getName, "has name equal to");

private static final <A, E> Correspondence<A, E> transforming(
Function<? super A, ? extends E> transformation, String description) {
return new Correspondence<A, E>() {
@Override
public boolean compare(A actual, E expected) {
return transformation.apply(actual).equals(expected);
}

@Override
public String toString() {
return description;
}
};
}

// Not instantiable.
private JSCompCorrespondences() {}
}
19 changes: 10 additions & 9 deletions test/com/google/javascript/jscomp/CompilerTest.java
Expand Up @@ -18,6 +18,7 @@
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.common.truth.Truth.assertWithMessage;
import static com.google.javascript.jscomp.CompilerTestCase.lines; import static com.google.javascript.jscomp.CompilerTestCase.lines;
import static com.google.javascript.jscomp.testing.JSCompCorrespondences.DIAGNOSTIC_EQUALITY;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


Expand Down Expand Up @@ -453,8 +454,8 @@ public void testBug2176967Default() {
// Default is warning. // Default is warning.
compiler.compile(SourceFile.fromCode("extern.js", ""), compiler.compile(SourceFile.fromCode("extern.js", ""),
SourceFile.fromCode("test.js", badJsDoc), options); SourceFile.fromCode("test.js", badJsDoc), options);
assertThat(compiler.getWarningCount()).isEqualTo(1); assertThat(compiler.getWarnings()).hasSize(1);
assertThat(compiler.getErrorCount()).isEqualTo(0); assertThat(compiler.getErrors()).isEmpty();
} }


/** /**
Expand All @@ -471,8 +472,8 @@ public void testBug2176967Off() {
DiagnosticGroups.NON_STANDARD_JSDOC, CheckLevel.OFF); DiagnosticGroups.NON_STANDARD_JSDOC, CheckLevel.OFF);
compiler.compile(SourceFile.fromCode("extern.js", ""), compiler.compile(SourceFile.fromCode("extern.js", ""),
SourceFile.fromCode("test.js", badJsDoc), options); SourceFile.fromCode("test.js", badJsDoc), options);
assertThat(compiler.getWarningCount()).isEqualTo(0); assertThat(compiler.getWarnings()).isEmpty();
assertThat(compiler.getErrorCount()).isEqualTo(0); assertThat(compiler.getErrors()).isEmpty();
} }


/** /**
Expand All @@ -490,8 +491,8 @@ public void testBug2176967Error() {
DiagnosticGroups.NON_STANDARD_JSDOC, CheckLevel.ERROR); DiagnosticGroups.NON_STANDARD_JSDOC, CheckLevel.ERROR);
compiler.compile(SourceFile.fromCode("extern.js", ""), compiler.compile(SourceFile.fromCode("extern.js", ""),
SourceFile.fromCode("test.js", badJsDoc), options); SourceFile.fromCode("test.js", badJsDoc), options);
assertThat(compiler.getWarningCount()).isEqualTo(0); assertThat(compiler.getWarnings()).isEmpty();
assertThat(compiler.getErrorCount()).isEqualTo(1); assertThat(compiler.getErrors()).hasSize(1);
} }


@Test @Test
Expand Down Expand Up @@ -2035,9 +2036,9 @@ public void testCodeReferenceToTypeImport() throws Exception {
compiler.check(); compiler.check();


assertThat(compiler.getWarnings()).isEmpty(); assertThat(compiler.getWarnings()).isEmpty();
assertThat(compiler.getErrors()).hasSize(1); assertThat(compiler.getErrors())
assertThat(compiler.getErrors().get(0).getType()) .comparingElementsUsing(DIAGNOSTIC_EQUALITY)
.isEqualTo(CheckTypeImportCodeReferences.TYPE_IMPORT_CODE_REFERENCE); .containsExactly(CheckTypeImportCodeReferences.TYPE_IMPORT_CODE_REFERENCE);
} }


@Test @Test
Expand Down
16 changes: 1 addition & 15 deletions test/com/google/javascript/jscomp/CompilerTypeTestCase.java
Expand Up @@ -18,12 +18,12 @@
package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.javascript.jscomp.testing.JSCompCorrespondences.DESCRIPTION_EQUALITY;
import static com.google.javascript.rhino.testing.TypeSubject.assertType; import static com.google.javascript.rhino.testing.TypeSubject.assertType;
import static com.google.javascript.rhino.testing.TypeSubject.types; import static com.google.javascript.rhino.testing.TypeSubject.types;


import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.truth.Correspondence;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode; import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature; import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.JSTypeExpression; import com.google.javascript.rhino.JSTypeExpression;
Expand All @@ -36,7 +36,6 @@
import com.google.javascript.rhino.jstype.RecordTypeBuilder; import com.google.javascript.rhino.jstype.RecordTypeBuilder;
import com.google.javascript.rhino.jstype.TemplatizedType; import com.google.javascript.rhino.jstype.TemplatizedType;
import com.google.javascript.rhino.testing.TestErrorReporter; import com.google.javascript.rhino.testing.TestErrorReporter;
import java.util.Objects;
import org.junit.Before; import org.junit.Before;


/** /**
Expand Down Expand Up @@ -314,17 +313,4 @@ JSType getNativeAllType() {
JSType getNativeType(JSTypeNative jsTypeNative) { JSType getNativeType(JSTypeNative jsTypeNative) {
return registry.getNativeType(jsTypeNative); return registry.getNativeType(jsTypeNative);
} }

static final Correspondence<JSError, String> DESCRIPTION_EQUALITY =
new Correspondence<JSError, String>() {
@Override
public boolean compare(JSError error, String description) {
return Objects.equals(error.description, description);
}

@Override
public String toString() {
return "has description equal to";
}
};
} }
68 changes: 35 additions & 33 deletions test/com/google/javascript/jscomp/JsMessageVisitorTest.java
Expand Up @@ -23,6 +23,8 @@
import static com.google.javascript.jscomp.JsMessageVisitor.MESSAGE_TREE_MALFORMED; import static com.google.javascript.jscomp.JsMessageVisitor.MESSAGE_TREE_MALFORMED;
import static com.google.javascript.jscomp.JsMessageVisitor.isLowerCamelCaseWithNumericSuffixes; import static com.google.javascript.jscomp.JsMessageVisitor.isLowerCamelCaseWithNumericSuffixes;
import static com.google.javascript.jscomp.JsMessageVisitor.toLowerCamelCaseWithNumericSuffixes; import static com.google.javascript.jscomp.JsMessageVisitor.toLowerCamelCaseWithNumericSuffixes;
import static com.google.javascript.jscomp.testing.JSCompCorrespondences.DESCRIPTION_EQUALITY;
import static com.google.javascript.jscomp.testing.JSCompCorrespondences.DIAGNOSTIC_EQUALITY;
import static com.google.javascript.jscomp.testing.JSErrorSubject.assertError; import static com.google.javascript.jscomp.testing.JSErrorSubject.assertError;


import com.google.common.base.Joiner; import com.google.common.base.Joiner;
Expand Down Expand Up @@ -274,23 +276,24 @@ public void testJsMessageOnRHSOfVar() {
@Test @Test
public void testOrphanedJsMessage() { public void testOrphanedJsMessage() {
extractMessagesSafely("goog.getMsg('a')"); extractMessagesSafely("goog.getMsg('a')");
assertThat(compiler.getWarnings()).hasSize(1);
assertThat(messages).isEmpty(); assertThat(messages).isEmpty();


JSError warn = compiler.getWarnings().get(0); assertThat(compiler.getWarnings())
assertError(warn).hasType(JsMessageVisitor.MESSAGE_NODE_IS_ORPHANED); .comparingElementsUsing(DIAGNOSTIC_EQUALITY)
.containsExactly(JsMessageVisitor.MESSAGE_NODE_IS_ORPHANED);
} }


@Test @Test
public void testMessageWithoutDescription() { public void testMessageWithoutDescription() {
extractMessagesSafely("var MSG_HELLO = goog.getMsg('a')"); extractMessagesSafely("var MSG_HELLO = goog.getMsg('a')");
assertThat(compiler.getWarnings()).hasSize(1);
assertThat(messages).hasSize(1);


assertThat(messages).hasSize(1);
JsMessage msg = messages.get(0); JsMessage msg = messages.get(0);
assertThat(msg.getKey()).isEqualTo("MSG_HELLO"); assertThat(msg.getKey()).isEqualTo("MSG_HELLO");


assertError(compiler.getWarnings().get(0)).hasType(JsMessageVisitor.MESSAGE_HAS_NO_DESCRIPTION); assertThat(compiler.getWarnings())
.comparingElementsUsing(DIAGNOSTIC_EQUALITY)
.containsExactly(JsMessageVisitor.MESSAGE_HAS_NO_DESCRIPTION);
} }


@Test @Test
Expand Down Expand Up @@ -505,10 +508,10 @@ public void testEmptyTextMessage() {
extractMessagesSafely("/** @desc text */ var MSG_FOO = goog.getMsg('');"); extractMessagesSafely("/** @desc text */ var MSG_FOO = goog.getMsg('');");


assertThat(messages).hasSize(1); assertThat(messages).hasSize(1);
assertThat(compiler.getWarnings()).hasSize(1); assertThat(compiler.getWarnings())
assertThat(compiler.getWarnings().get(0).description) .comparingElementsUsing(DESCRIPTION_EQUALITY)
.isEqualTo( .containsExactly(
"Message value of MSG_FOO is just an empty string. " + "Empty messages are forbidden."); "Message value of MSG_FOO is just an empty string. Empty messages are forbidden.");
} }


@Test @Test
Expand All @@ -517,19 +520,19 @@ public void testEmptyTextComplexMessage() {
+ "'' + '' + '' + ''\n+'');"); + "'' + '' + '' + ''\n+'');");


assertThat(messages).hasSize(1); assertThat(messages).hasSize(1);
assertThat(compiler.getWarnings()).hasSize(1); assertThat(compiler.getWarnings())
assertThat(compiler.getWarnings().get(0).description) .comparingElementsUsing(DESCRIPTION_EQUALITY)
.isEqualTo( .containsExactly(
"Message value of MSG_BAR is just an empty string. " + "Empty messages are forbidden."); "Message value of MSG_BAR is just an empty string. " + "Empty messages are forbidden.");
} }


@Test @Test
public void testMsgVarWithoutAssignment() { public void testMsgVarWithoutAssignment() {
extractMessages("var MSG_SILLY;"); extractMessages("var MSG_SILLY;");


assertThat(compiler.getErrors()).hasSize(1); assertThat(compiler.getErrors())
JSError error = compiler.getErrors().get(0); .comparingElementsUsing(DIAGNOSTIC_EQUALITY)
assertThat(error.getType()).isEqualTo(JsMessageVisitor.MESSAGE_HAS_NO_VALUE); .containsExactly(JsMessageVisitor.MESSAGE_HAS_NO_VALUE);
} }


@Test @Test
Expand All @@ -544,30 +547,29 @@ public void testRegularVarWithoutAssignment() {
public void testMsgPropertyWithoutAssignment() { public void testMsgPropertyWithoutAssignment() {
extractMessages("goog.message.MSG_SILLY_PROP;"); extractMessages("goog.message.MSG_SILLY_PROP;");


assertThat(compiler.getErrors()).hasSize(1); assertThat(compiler.getErrors())
JSError error = compiler.getErrors().get(0); .comparingElementsUsing(DESCRIPTION_EQUALITY)
assertThat(error.description).isEqualTo("Message MSG_SILLY_PROP has no value"); .containsExactly("Message MSG_SILLY_PROP has no value");
} }


@Test @Test
public void testMsgVarWithIncorrectRightSide() { public void testMsgVarWithIncorrectRightSide() {
extractMessages("var MSG_SILLY = 0;"); extractMessages("var MSG_SILLY = 0;");


assertThat(compiler.getErrors()).hasSize(1); assertThat(compiler.getErrors())
JSError error = compiler.getErrors().get(0); .comparingElementsUsing(DESCRIPTION_EQUALITY)
assertThat(error.description) .containsExactly(
.isEqualTo("Message parse tree malformed. Cannot parse value of " + "message MSG_SILLY"); "Message parse tree malformed. Cannot parse value of " + "message MSG_SILLY");
} }


@Test @Test
public void testIncorrectMessage() { public void testIncorrectMessage() {
extractMessages("DP_DatePicker.MSG_DATE_SELECTION = {};"); extractMessages("DP_DatePicker.MSG_DATE_SELECTION = {};");


assertThat(messages).isEmpty(); assertThat(messages).isEmpty();
assertThat(compiler.getErrors()).hasSize(1); assertThat(compiler.getErrors())
JSError error = compiler.getErrors().get(0); .comparingElementsUsing(DESCRIPTION_EQUALITY)
assertThat(error.description) .containsExactly(
.isEqualTo(
"Message parse tree malformed." "Message parse tree malformed."
+ " Message must be initialized using goog.getMsg function."); + " Message must be initialized using goog.getMsg function.");
} }
Expand All @@ -578,10 +580,9 @@ public void testUnrecognizedFunction() {
extractMessages("DP_DatePicker.MSG_DATE_SELECTION = somefunc('a')"); extractMessages("DP_DatePicker.MSG_DATE_SELECTION = somefunc('a')");


assertThat(messages).isEmpty(); assertThat(messages).isEmpty();
assertThat(compiler.getErrors()).hasSize(1); assertThat(compiler.getErrors())
JSError error = compiler.getErrors().get(0); .comparingElementsUsing(DESCRIPTION_EQUALITY)
assertThat(error.description) .containsExactly(
.isEqualTo(
"Message parse tree malformed. Message initialized using unrecognized function. " "Message parse tree malformed. Message initialized using unrecognized function. "
+ "Please use goog.getMsg() instead."); + "Please use goog.getMsg() instead.");
} }
Expand Down Expand Up @@ -890,8 +891,9 @@ private void assertNoErrors() {
} }


private void assertOneError(DiagnosticType type) { private void assertOneError(DiagnosticType type) {
assertThat(compiler.getErrors()).hasSize(1); assertThat(compiler.getErrors())
assertError(compiler.getErrors().get(0)).hasType(type); .comparingElementsUsing(DIAGNOSTIC_EQUALITY)
.containsExactly(type);
} }


private void extractMessagesSafely(String input) { private void extractMessagesSafely(String input) {
Expand Down
1 change: 1 addition & 0 deletions test/com/google/javascript/jscomp/TypeCheckTestCase.java
Expand Up @@ -18,6 +18,7 @@


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.common.truth.Truth.assertWithMessage;
import static com.google.javascript.jscomp.testing.JSCompCorrespondences.DESCRIPTION_EQUALITY;


import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.truth.Correspondence; import com.google.common.truth.Correspondence;
Expand Down

0 comments on commit 0e110f9

Please sign in to comment.