Debian Codegraph @

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

raw symbols SCIP
1 /*
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Rhino code, released
17 * May 6, 1999.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1997-1999
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * Bob Jervis
26 * Google Inc.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU General Public License Version 2 or later (the "GPL"), in which
30 * case the provisions of the GPL are applicable instead of those above. If
31 * you wish to allow use of your version of this file only under the terms of
32 * the GPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replacing
34 * them with the notice and other provisions required by the GPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the GPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 package com.google.javascript.rhino.testing;
41
42 import com.google.javascript.rhino.ErrorReporter;
43 import junit.framework.Assert;
44
45 /**
46 * <p>An error reporter for testing that verifies that messages reported to the
47 * reporter are expected.</p>
48 *
49 * <p>Sample use</p>
50 * <pre>
51 * TestErrorReporter e =
52 * new TestErrorReporter(null, new String[] { "first warning" });
53 * ...
54 * assertTrue(e.hasEncounteredAllWarnings());
55 * </pre>
56 *
57 */
58 public final class TestErrorReporter extends Assert implements ErrorReporter {
59 private String[] errors;
60 private String[] warnings;
61 private int errorsIndex = 0;
62 private int warningsIndex = 0;
63
64 public TestErrorReporter(String[] errors, String[] warnings) {
65 this.errors = errors;
66 this.warnings = warnings;
67 }
68
69 public static TestErrorReporter forNoExpectedReports() {
70 return new TestErrorReporter(null, null);
71 }
72
73 public void setErrors(String[] errors) {
74 this.errors = errors;
75 errorsIndex = 0;
76 }
77
78 public void setWarnings(String[] warnings) {
79 this.warnings = warnings;
80 warningsIndex = 0;
81 }
82
83 @Override
84 public void error(String message, String sourceName, int line,
85 int lineOffset) {
86 if (errors != null && errorsIndex < errors.length) {
87 assertEquals(errors[errorsIndex++], message);
88 } else {
89 fail("extra error: " + message);
90 }
91 }
92
93 @Override
94 public void warning(String message, String sourceName, int line,
95 int lineOffset) {
96 if (warnings != null && warningsIndex < warnings.length) {
97 assertEquals(warnings[warningsIndex++], message);
98 } else {
99 fail("extra warning: " + message);
100 }
101 }
102
103 /**
104 * Returns whether all warnings were reported to this reporter.
105 */
106 public boolean hasEncounteredAllWarnings() {
107 return (warnings == null) ?
108 warningsIndex == 0 :
109 warnings.length == warningsIndex;
110 }
111
112 /**
113 * Returns whether all errors were reported to this reporter.
114 */
115 public boolean hasEncounteredAllErrors() {
116 return (errors == null) ?
117 errorsIndex == 0 :
118 errors.length == errorsIndex;
119 }
120 }