Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/testing/TestErrorReporter.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.javascript.testing;
6
7 import org.mozilla.javascript.ErrorReporter;
8 import org.mozilla.javascript.EvaluatorException;
9
10 import junit.framework.Assert;
11
12 /**
13 * <p>An error reporter for testing that verifies that messages reported to the
14 * reporter are expected.</p>
15 *
16 * <p>Sample use</p>
17 * <pre>
18 * TestErrorReporter e =
19 * new TestErrorReporter(null, new String[] { "first warning" });
20 * ...
21 * assertTrue(e.hasEncounteredAllWarnings());
22 * </pre>
23 *
24 * @author Pascal-Louis Perez
25 */
26 public class TestErrorReporter extends Assert implements ErrorReporter {
27 private final String[] errors;
28 private final String[] warnings;
29 private int errorsIndex = 0;
30 private int warningsIndex = 0;
31
32 public TestErrorReporter(String[] errors, String[] warnings) {
33 this.errors = errors;
34 this.warnings = warnings;
35 }
36
37 public void error(String message, String sourceName, int line,
38 String lineSource, int lineOffset) {
39 if (errors != null && errorsIndex < errors.length) {
40 assertEquals(errors[errorsIndex++], message);
41 } else {
42 fail("extra error: " + message);
43 }
44 }
45
46 public void warning(String message, String sourceName, int line,
47 String lineSource, int lineOffset) {
48 if (warnings != null && warningsIndex < warnings.length) {
49 assertEquals(warnings[warningsIndex++], message);
50 } else {
51 fail("extra warning: " + message);
52 }
53 }
54
55 public EvaluatorException runtimeError(
56 String message, String sourceName, int line, String lineSource,
57 int lineOffset) {
58 throw new UnsupportedOperationException();
59 }
60
61 /**
62 * Returns whether all warnings were reported to this reporter.
63 */
64 public boolean hasEncounteredAllWarnings() {
65 return (warnings == null) ?
66 warningsIndex == 0 :
67 warnings.length == warningsIndex;
68 }
69
70 /**
71 * Returns whether all errors were reported to this reporter.
72 */
73 public boolean hasEncounteredAllErrors() {
74 return (errors == null) ?
75 errorsIndex == 0 :
76 errors.length == errorsIndex;
77 }
78 }