Debian Codegraph @

test/com/google/javascript/jscomp/BasicErrorManagerTest.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;
18
19 import com.google.javascript.jscomp.BasicErrorManager.ErrorWithLevel;
20 import com.google.javascript.jscomp.BasicErrorManager.LeveledJSErrorComparator;
21 import com.google.javascript.jscomp.CheckLevel;
22
23 import junit.framework.TestCase;
24
25 /**
26 * Tests {@link BasicErrorManager}.
27 *
28 */
29 public class BasicErrorManagerTest extends TestCase {
30 private static final String NULL_SOURCE = null;
31
32 private LeveledJSErrorComparator comparator = new LeveledJSErrorComparator();
33
34 static final CheckLevel E = CheckLevel.ERROR;
35
36 private static final DiagnosticType FOO_TYPE =
37 DiagnosticType.error("TEST_FOO", "Foo");
38
39 private static final DiagnosticType JOO_TYPE =
40 DiagnosticType.error("TEST_JOO", "Joo");
41
42 public void testOrderingBothNull() throws Exception {
43 assertEquals(0, comparator.compare(null, null));
44 }
45
46 public void testOrderingSourceName1() throws Exception {
47 JSError e1 = JSError.make(NULL_SOURCE, -1, -1, FOO_TYPE);
48 JSError e2 = JSError.make("a", -1, -1, FOO_TYPE);
49
50 assertSmaller(error(e1), error(e2));
51 }
52
53 public void testOrderingSourceName2() throws Exception {
54 JSError e1 = JSError.make("a", -1, -1, FOO_TYPE);
55 JSError e2 = JSError.make("b", -1, -1, FOO_TYPE);
56
57 assertSmaller(error(e1), error(e2));
58 }
59
60 public void testOrderingLineno1() throws Exception {
61 JSError e1 = JSError.make(NULL_SOURCE, -1, -1, FOO_TYPE);
62 JSError e2 = JSError.make(NULL_SOURCE, 2, -1, FOO_TYPE);
63
64 assertSmaller(error(e1), error(e2));
65 }
66
67 public void testOrderingLineno2() throws Exception {
68 JSError e1 = JSError.make(NULL_SOURCE, 8, -1, FOO_TYPE);
69 JSError e2 = JSError.make(NULL_SOURCE, 56, -1, FOO_TYPE);
70 assertSmaller(error(e1), error(e2));
71 }
72
73 public void testOrderingCheckLevel() throws Exception {
74 JSError e1 = JSError.make(NULL_SOURCE, -1, -1, FOO_TYPE);
75 JSError e2 = JSError.make(NULL_SOURCE, -1, -1, FOO_TYPE);
76
77 assertSmaller(warning(e1), error(e2));
78 }
79
80 public void testOrderingCharno1() throws Exception {
81 JSError e1 = JSError.make(NULL_SOURCE, 5, -1, FOO_TYPE);
82 JSError e2 = JSError.make(NULL_SOURCE, 5, 2, FOO_TYPE);
83
84 assertSmaller(error(e1), error(e2));
85 // CheckLevel preempts charno comparison
86 assertSmaller(warning(e1), error(e2));
87 }
88
89 public void testOrderingCharno2() throws Exception {
90 JSError e1 = JSError.make(NULL_SOURCE, 8, 7, FOO_TYPE);
91 JSError e2 = JSError.make(NULL_SOURCE, 8, 5, FOO_TYPE);
92
93 assertSmaller(error(e2), error(e1));
94 // CheckLevel preempts charno comparison
95 assertSmaller(warning(e2), error(e1));
96 }
97
98 public void testOrderingDescription() throws Exception {
99 JSError e1 = JSError.make(NULL_SOURCE, -1, -1, FOO_TYPE);
100 JSError e2 = JSError.make(NULL_SOURCE, -1, -1, JOO_TYPE);
101
102 assertSmaller(error(e1), error(e2));
103 }
104
105 private ErrorWithLevel error(JSError e) {
106 return new ErrorWithLevel(e, CheckLevel.ERROR);
107 }
108
109 private ErrorWithLevel warning(JSError e) {
110 return new ErrorWithLevel(e, CheckLevel.WARNING);
111 }
112
113 private void assertSmaller(ErrorWithLevel p1, ErrorWithLevel p2) {
114 int p1p2 = comparator.compare(p1, p2);
115 assertTrue(Integer.toString(p1p2), p1p2 < 0);
116 int p2p1 = comparator.compare(p2, p1);
117 assertTrue(Integer.toString(p2p1), p2p1 > 0);
118 }
119 }