44
44
import com .google .javascript .rhino .ErrorReporter ;
45
45
import java .util .ArrayList ;
46
46
import java .util .Collections ;
47
+ import java .util .LinkedHashMap ;
47
48
48
49
/**
49
50
* An error reporter for testing that verifies that messages reported to the reporter are expected.
50
51
*/
51
52
public final class TestErrorReporter implements ErrorReporter {
52
53
private final ArrayList <String > expectedErrors = new ArrayList <>();
53
54
private final ArrayList <String > expectedWarnings = new ArrayList <>();
54
- private final ArrayList <String > seenErrors = new ArrayList <>();
55
- private final ArrayList <String > seenWarnings = new ArrayList <>();
55
+
56
+ // Seen diagnostics are stored in a LinkedHashMap to deduplicate identical diagnostics.
57
+ // The default com.google.javascript.jscomp.SortingErrorManager behaves in this way.
58
+ private final LinkedHashMap <String , String > seenErrors = new LinkedHashMap <>();
59
+ private final LinkedHashMap <String , String > seenWarnings = new LinkedHashMap <>();
56
60
57
61
@ Override
58
62
public void error (String message , String sourceName , int line , int lineOffset ) {
59
- this . seenErrors .add ( message );
63
+ seenErrors .put ( fmtDiagnosticKey ( message , sourceName , line , lineOffset ), message );
60
64
}
61
65
62
66
@ Override
63
67
public void warning (String message , String sourceName , int line , int lineOffset ) {
64
- this . seenWarnings .add ( message );
68
+ seenWarnings .put ( fmtDiagnosticKey ( message , sourceName , line , lineOffset ), message );
65
69
}
66
70
67
71
public TestErrorReporter expectAllErrors (String ... errors ) {
@@ -75,7 +79,11 @@ public TestErrorReporter expectAllWarnings(String... warnings) {
75
79
}
76
80
77
81
public void verifyHasEncounteredAllWarningsAndErrors () {
78
- assertThat (seenWarnings ).containsExactlyElementsIn (expectedWarnings ).inOrder ();
79
- assertThat (seenErrors ).containsExactlyElementsIn (expectedErrors ).inOrder ();
82
+ assertThat (seenWarnings .values ()).containsExactlyElementsIn (expectedWarnings ).inOrder ();
83
+ assertThat (seenErrors .values ()).containsExactlyElementsIn (expectedErrors ).inOrder ();
84
+ }
85
+
86
+ private String fmtDiagnosticKey (String message , String sourceName , int line , int lineOffset ) {
87
+ return message + ":" + sourceName + ":" + line + ":" + lineOffset ;
80
88
}
81
89
}
0 commit comments