Debian Codegraph @

test/com/google/javascript/jscomp/LightweightMessageFormatterTest.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 package com.google.javascript.jscomp;
17
18 import static com.google.javascript.jscomp.LightweightMessageFormatter.LineNumberingFormatter;
19
20 import com.google.javascript.rhino.Node;
21
22 import junit.framework.TestCase;
23
24 public class LightweightMessageFormatterTest extends TestCase {
25 private static final DiagnosticType FOO_TYPE =
26 DiagnosticType.error("TEST_FOO", "error description here");
27
28 public void testNull() throws Exception {
29 assertNull(format(null));
30 }
31
32 public void testOneLineRegion() throws Exception {
33 assertEquals(" 5| hello world", format(region(5, 5, "hello world")));
34 }
35
36 public void testTwoLineRegion() throws Exception {
37 assertEquals(" 5| hello world\n" +
38 " 6| foo bar", format(region(5, 6, "hello world\nfoo bar")));
39 }
40
41 public void testThreeLineRegionAcrossNumberRange() throws Exception {
42 String region = format(region(9, 11, "hello world\nfoo bar\nanother one"));
43 assertEquals(" 9| hello world\n" +
44 " 10| foo bar\n" +
45 " 11| another one", region);
46 }
47
48 public void testThreeLineRegionEmptyLine() throws Exception {
49 String region = format(region(7, 9, "hello world\n\nanother one"));
50 assertEquals(" 7| hello world\n" +
51 " 8| \n" +
52 " 9| another one", region);
53 }
54
55 public void testOnlyOneEmptyLine() throws Exception {
56 assertNull(format(region(7, 7, "")));
57 }
58
59 public void testTwoEmptyLines() throws Exception {
60 assertEquals(" 7| ", format(region(7, 8, "\n")));
61 }
62
63 public void testThreeLineRemoveLastEmptyLine() throws Exception {
64 String region = format(region(7, 9, "hello world\nfoobar\n"));
65 assertEquals(" 7| hello world\n" +
66 " 8| foobar", region);
67 }
68
69 public void testFormatErrorSpaces() throws Exception {
70 JSError error = JSError.make("javascript/complex.js",
71 Node.newString("foobar", 5, 8), FOO_TYPE);
72 LightweightMessageFormatter formatter = formatter(" if (foobar) {");
73 assertEquals("javascript/complex.js:5: ERROR - error description here\n" +
74 " if (foobar) {\n" +
75 " ^\n", formatter.formatError(error));
76 }
77
78 public void testFormatErrorTabs() throws Exception {
79 JSError error = JSError.make("javascript/complex.js",
80 Node.newString("foobar", 5, 6), FOO_TYPE);
81 LightweightMessageFormatter formatter = formatter("\t\tif (foobar) {");
82 assertEquals("javascript/complex.js:5: ERROR - error description here\n" +
83 "\t\tif (foobar) {\n" +
84 "\t\t ^\n", formatter.formatError(error));
85 }
86
87 public void testFormatErrorSpaceEndOfLine1() throws Exception {
88 JSError error = JSError.make("javascript/complex.js",
89 1, 10, FOO_TYPE);
90 LightweightMessageFormatter formatter = formatter("assert (1;");
91 assertEquals("javascript/complex.js:1: ERROR - error description here\n" +
92 "assert (1;\n" +
93 " ^\n", formatter.formatError(error));
94 }
95
96 public void testFormatErrorSpaceEndOfLine2() throws Exception {
97 JSError error = JSError.make("javascript/complex.js",
98 6, 7, FOO_TYPE);
99 LightweightMessageFormatter formatter = formatter("if (foo");
100 assertEquals("javascript/complex.js:6: ERROR - error description here\n" +
101 "if (foo\n" +
102 " ^\n", formatter.formatError(error));
103 }
104
105 private LightweightMessageFormatter formatter(String string) {
106 return new LightweightMessageFormatter(source(string));
107 }
108
109 private SourceExcerptProvider source(final String source) {
110 return new SourceExcerptProvider() {
111 @Override
112 public String getSourceLine(String sourceName, int lineNumber) {
113 return source;
114 }
115 @Override
116 public Region getSourceRegion(String sourceName, int lineNumber) {
117 throw new UnsupportedOperationException();
118 }
119 };
120 }
121
122 private String format(Region region) {
123 return new LineNumberingFormatter().formatRegion(region);
124 }
125
126 private Region region(final int startLine, final int endLine,
127 final String source) {
128 return new SimpleRegion(startLine, endLine, source);
129 }
130 }