Skip to content

Commit 8668700

Browse files
concavelenzcopybara-github
authored andcommitted
Improve diagnostic testing in CompilerTestCase by adding support for checking error location and length.
PiperOrigin-RevId: 790860369
1 parent 98f0bcf commit 8668700

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

test/com/google/javascript/jscomp/CompilerTestCase.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.List;
5959
import java.util.Map;
6060
import java.util.Objects;
61+
import java.util.Optional;
6162
import java.util.Set;
6263
import java.util.function.Function;
6364
import org.jspecify.annotations.Nullable;
@@ -1536,10 +1537,10 @@ private void validateSourceLocation(JSError jserror) {
15361537
.that(scriptNode)
15371538
.isNotNull();
15381539
assertWithMessage("Missing line number in warning: " + jserror)
1539-
.that(-1 != jserror.getLineNumber())
1540+
.that(jserror.lineno() != -1)
15401541
.isTrue();
15411542
assertWithMessage("Missing char number in warning: " + jserror)
1542-
.that(-1 != jserror.charno())
1543+
.that(jserror.charno() != -1)
15431544
.isTrue();
15441545
}
15451546
}
@@ -1898,7 +1899,7 @@ private void testInternal(Iterable<TestPart> parts) {
18981899
} else if (part instanceof Postcondition postcondition) {
18991900
postconditions.add(postcondition);
19001901
} else {
1901-
throw new IllegalStateException("unexepected " + part.getClass().getName());
1902+
throw new IllegalStateException("unexpected " + part.getClass().getName());
19021903
}
19031904
}
19041905
if (EXPECTED_SAME.equals(expected)) {
@@ -1983,26 +1984,52 @@ protected static class Diagnostic implements TestPart {
19831984
final CheckLevel level;
19841985
final DiagnosticType diagnostic;
19851986
final NamedPredicate<String> messagePredicate;
1987+
final int line;
1988+
final int charno;
1989+
final int length;
19861990

19871991
Diagnostic(
19881992
CheckLevel level,
19891993
DiagnosticType diagnostic,
19901994
@Nullable NamedPredicate<String> messagePredicate) {
1995+
this(level, diagnostic, messagePredicate, -1, -1, -1);
1996+
}
1997+
1998+
Diagnostic(
1999+
CheckLevel level,
2000+
DiagnosticType diagnostic,
2001+
@Nullable NamedPredicate<String> messagePredicate,
2002+
int line,
2003+
int charno,
2004+
int length) {
19912005
this.level = level;
19922006
this.diagnostic = diagnostic;
19932007
this.messagePredicate = messagePredicate;
2008+
this.line = line;
2009+
this.charno = charno;
2010+
this.length = length;
19942011
}
19952012

1996-
private boolean matches(JSError error) {
1997-
return Objects.equals(diagnostic, error.type())
1998-
&& (messagePredicate == null || messagePredicate.apply(error.description()));
1999-
}
2000-
2001-
private String formatDiff(JSError error) {
2013+
private Optional<String> formatDiff(JSError error) {
20022014
if (!Objects.equals(diagnostic, error.type())) {
2003-
return "diagnostic type " + error.type().key + " did not match";
2015+
return Optional.of(
2016+
String.format(
2017+
"diagnostic type <%s> did not match <%s>", error.type().key, diagnostic.key));
20042018
}
2005-
return "message \"" + error.description() + "\" was not " + messagePredicate;
2019+
if (messagePredicate != null && !messagePredicate.apply(error.description())) {
2020+
return Optional.of(
2021+
String.format("message <%s> was not <%s>", error.description(), messagePredicate));
2022+
}
2023+
if (line != -1 && error.lineno() != line) {
2024+
return Optional.of(String.format("line <%d> did not match <%d>", error.lineno(), line));
2025+
}
2026+
if (charno != -1 && error.charno() != charno) {
2027+
return Optional.of(String.format("charno <%d> did not match <%d>", error.charno(), charno));
2028+
}
2029+
if (length != -1 && error.length() != length) {
2030+
return Optional.of(String.format("length <%d> did not match <%d>", error.length(), length));
2031+
}
2032+
return Optional.empty();
20062033
}
20072034

20082035
public Diagnostic withMessage(final String expectedRaw) {
@@ -2023,6 +2050,10 @@ public Diagnostic withMessageContaining(final String substring) {
20232050
message -> message.contains(substring), "containing \"" + substring + "\""));
20242051
}
20252052

2053+
public Diagnostic withLocation(int line, int charno, int length) {
2054+
return new Diagnostic(level, diagnostic, messagePredicate, line, charno, length);
2055+
}
2056+
20262057
@Override
20272058
public String toString() {
20282059
return diagnostic.key + (messagePredicate != null ? " with message " + messagePredicate : "");
@@ -2043,9 +2074,9 @@ private static class WarningDiagnostic extends Diagnostic {
20432074

20442075
private static final Correspondence<JSError, Diagnostic> DIAGNOSTIC_CORRESPONDENCE =
20452076
Correspondence.from(
2046-
(JSError actual, Diagnostic expected) -> expected.matches(actual),
2077+
(JSError actual, Diagnostic expected) -> expected.formatDiff(actual).isEmpty(),
20472078
"is a JSError matching")
2048-
.formattingDiffsUsing((actual, expected) -> expected.formatDiff(actual));
2079+
.formattingDiffsUsing((actual, expected) -> expected.formatDiff(actual).get());
20492080

20502081
private static class NamedPredicate<T> implements Predicate<T> {
20512082
final Predicate<T> delegate;

0 commit comments

Comments
 (0)