58
58
import java .util .List ;
59
59
import java .util .Map ;
60
60
import java .util .Objects ;
61
+ import java .util .Optional ;
61
62
import java .util .Set ;
62
63
import java .util .function .Function ;
63
64
import org .jspecify .annotations .Nullable ;
@@ -1536,10 +1537,10 @@ private void validateSourceLocation(JSError jserror) {
1536
1537
.that (scriptNode )
1537
1538
.isNotNull ();
1538
1539
assertWithMessage ("Missing line number in warning: " + jserror )
1539
- .that (- 1 != jserror .getLineNumber () )
1540
+ .that (jserror .lineno () != - 1 )
1540
1541
.isTrue ();
1541
1542
assertWithMessage ("Missing char number in warning: " + jserror )
1542
- .that (- 1 != jserror .charno ())
1543
+ .that (jserror .charno () != - 1 )
1543
1544
.isTrue ();
1544
1545
}
1545
1546
}
@@ -1898,7 +1899,7 @@ private void testInternal(Iterable<TestPart> parts) {
1898
1899
} else if (part instanceof Postcondition postcondition ) {
1899
1900
postconditions .add (postcondition );
1900
1901
} else {
1901
- throw new IllegalStateException ("unexepected " + part .getClass ().getName ());
1902
+ throw new IllegalStateException ("unexpected " + part .getClass ().getName ());
1902
1903
}
1903
1904
}
1904
1905
if (EXPECTED_SAME .equals (expected )) {
@@ -1983,26 +1984,52 @@ protected static class Diagnostic implements TestPart {
1983
1984
final CheckLevel level ;
1984
1985
final DiagnosticType diagnostic ;
1985
1986
final NamedPredicate <String > messagePredicate ;
1987
+ final int line ;
1988
+ final int charno ;
1989
+ final int length ;
1986
1990
1987
1991
Diagnostic (
1988
1992
CheckLevel level ,
1989
1993
DiagnosticType diagnostic ,
1990
1994
@ 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 ) {
1991
2005
this .level = level ;
1992
2006
this .diagnostic = diagnostic ;
1993
2007
this .messagePredicate = messagePredicate ;
2008
+ this .line = line ;
2009
+ this .charno = charno ;
2010
+ this .length = length ;
1994
2011
}
1995
2012
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 ) {
2002
2014
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 ));
2004
2018
}
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 ();
2006
2033
}
2007
2034
2008
2035
public Diagnostic withMessage (final String expectedRaw ) {
@@ -2023,6 +2050,10 @@ public Diagnostic withMessageContaining(final String substring) {
2023
2050
message -> message .contains (substring ), "containing \" " + substring + "\" " ));
2024
2051
}
2025
2052
2053
+ public Diagnostic withLocation (int line , int charno , int length ) {
2054
+ return new Diagnostic (level , diagnostic , messagePredicate , line , charno , length );
2055
+ }
2056
+
2026
2057
@ Override
2027
2058
public String toString () {
2028
2059
return diagnostic .key + (messagePredicate != null ? " with message " + messagePredicate : "" );
@@ -2043,9 +2074,9 @@ private static class WarningDiagnostic extends Diagnostic {
2043
2074
2044
2075
private static final Correspondence <JSError , Diagnostic > DIAGNOSTIC_CORRESPONDENCE =
2045
2076
Correspondence .from (
2046
- (JSError actual , Diagnostic expected ) -> expected .matches (actual ),
2077
+ (JSError actual , Diagnostic expected ) -> expected .formatDiff (actual ). isEmpty ( ),
2047
2078
"is a JSError matching" )
2048
- .formattingDiffsUsing ((actual , expected ) -> expected .formatDiff (actual ));
2079
+ .formattingDiffsUsing ((actual , expected ) -> expected .formatDiff (actual ). get () );
2049
2080
2050
2081
private static class NamedPredicate <T > implements Predicate <T > {
2051
2082
final Predicate <T > delegate ;
0 commit comments