Debian Codegraph @

src/com/google/javascript/jscomp/testing/TestErrorReporter.java

raw symbols SCIP
1 /*
2 * Copyright 2007 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp.testing;
18
19 import com.google.javascript.rhino.head.ErrorReporter;
20 import com.google.javascript.rhino.head.EvaluatorException;
21
22 import junit.framework.Assert;
23
24 /**
25 * <p>An error reporter for testing that verifies that messages reported to the
26 * reporter are expected.</p>
27 *
28 * <p>Sample use</p>
29 * <pre>
30 * TestErrorReporter e =
31 * new TestErrorReporter(null, new String[] { "first warning" });
32 * ...
33 * assertTrue(e.hasEncounteredAllWarnings());
34 * </pre>
35 *
36 */
37 public final class TestErrorReporter extends Assert implements ErrorReporter {
38 private final String[] errors;
39 private final String[] warnings;
40 private int errorsIndex = 0;
41 private int warningsIndex = 0;
42
43 public TestErrorReporter(String[] errors, String[] warnings) {
44 this.errors = errors;
45 this.warnings = warnings;
46 }
47
48 @Override
49 public void error(String message, String sourceName, int line,
50 String lineSource, int lineOffset) {
51 if (errors != null && errorsIndex < errors.length) {
52 assertEquals(errors[errorsIndex++], message);
53 } else {
54 fail("extra error: " + message);
55 }
56 }
57
58 @Override
59 public void warning(String message, String sourceName, int line,
60 String lineSource, int lineOffset) {
61 if (warnings != null && warningsIndex < warnings.length) {
62 assertEquals(warnings[warningsIndex++], message);
63 } else {
64 fail("extra warning: " + message);
65 }
66 }
67
68 @Override
69 public EvaluatorException runtimeError(String message, String sourceName,
70 int line, String lineSource, int lineOffset) {
71 return new EvaluatorException("JSCompiler test code: " + message);
72 }
73
74 /**
75 * Returns whether all warnings were reported to this reporter.
76 */
77 public boolean hasEncounteredAllWarnings() {
78 return (warnings == null) ?
79 warningsIndex == 0 :
80 warnings.length == warningsIndex;
81 }
82
83 /**
84 * Returns whether all errors were reported to this reporter.
85 */
86 public boolean hasEncounteredAllErrors() {
87 return (errors == null) ?
88 errorsIndex == 0 :
89 errors.length == errorsIndex;
90 }
91 }