Debian Codegraph @

test/com/google/javascript/jscomp/JSCompilerSourceExcerptProviderTest.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
19 import com.google.common.collect.ImmutableList;
20
21 import junit.framework.TestCase;
22
23 /**
24 */
25 public class JSCompilerSourceExcerptProviderTest extends TestCase {
26 private SourceExcerptProvider provider;
27
28 @Override
29 protected void setUp() throws Exception {
30 SourceFile foo = SourceFile.fromCode("foo",
31 "foo:first line\nfoo:second line\nfoo:third line\n");
32 SourceFile bar = SourceFile.fromCode("bar",
33 "bar:first line\nbar:second line\nbar:third line\nbar:fourth line\n");
34 SourceFile foo2 = SourceFile.fromCode("foo2",
35 "foo2:first line\nfoo2:second line\nfoo2:third line");
36 Compiler compiler = new Compiler();
37 CompilerOptions options = new CompilerOptions();
38 compiler.init(
39 ImmutableList.<SourceFile>of(),
40 ImmutableList.of(foo, bar, foo2),
41 options);
42 this.provider = compiler;
43 }
44
45 public void testExcerptOneLine() throws Exception {
46 assertEquals("foo:first line", provider.getSourceLine("foo", 1));
47 assertEquals("foo:second line", provider.getSourceLine("foo", 2));
48 assertEquals("foo:third line", provider.getSourceLine("foo", 3));
49 assertEquals("bar:first line", provider.getSourceLine("bar", 1));
50 assertEquals("bar:second line", provider.getSourceLine("bar", 2));
51 assertEquals("bar:third line", provider.getSourceLine("bar", 3));
52 assertEquals("bar:fourth line", provider.getSourceLine("bar", 4));
53 }
54
55 public void testExcerptLineFromInexistantSource() throws Exception {
56 assertEquals(null, provider.getSourceLine("inexistant", 1));
57 assertEquals(null, provider.getSourceLine("inexistant", 7));
58 assertEquals(null, provider.getSourceLine("inexistant", 90));
59 }
60
61 public void testExcerptInexistantLine() throws Exception {
62 assertEquals(null, provider.getSourceLine("foo", 0));
63 assertEquals(null, provider.getSourceLine("foo", 4));
64 assertEquals(null, provider.getSourceLine("bar", 0));
65 assertEquals(null, provider.getSourceLine("bar", 5));
66 }
67
68 public void testExceptNoNewLine() throws Exception {
69 assertEquals("foo2:first line", provider.getSourceLine("foo2", 1));
70 assertEquals("foo2:second line", provider.getSourceLine("foo2", 2));
71 assertEquals("foo2:third line", provider.getSourceLine("foo2", 3));
72 assertEquals(null, provider.getSourceLine("foo2", 4));
73 }
74
75 public void testExcerptRegion() throws Exception {
76 assertRegionWellFormed("foo", 1);
77 assertRegionWellFormed("foo", 2);
78 assertRegionWellFormed("foo", 3);
79 assertRegionWellFormed("bar", 1);
80 assertRegionWellFormed("bar", 2);
81 assertRegionWellFormed("bar", 3);
82 assertRegionWellFormed("bar", 4);
83 }
84
85 public void testExcerptRegionFromInexistantSource() throws Exception {
86 assertEquals(null, provider.getSourceRegion("inexistant", 0));
87 assertEquals(null, provider.getSourceRegion("inexistant", 6));
88 assertEquals(null, provider.getSourceRegion("inexistant", 90));
89 }
90
91 public void testExcerptInexistantRegion() throws Exception {
92 assertEquals(null, provider.getSourceRegion("foo", 0));
93 assertEquals(null, provider.getSourceRegion("foo", 4));
94 assertEquals(null, provider.getSourceRegion("bar", 0));
95 assertEquals(null, provider.getSourceRegion("bar", 5));
96 }
97
98 /**
99 * Asserts that a region is 'well formed': it must not be an empty and
100 * cannot start or finish by a carriage return. In addition, it must
101 * contain the line whose region we are taking.
102 */
103 private void assertRegionWellFormed(String sourceName, int lineNumber) {
104 Region region = provider.getSourceRegion(sourceName, lineNumber);
105 assertNotNull(region);
106 String sourceRegion = region.getSourceExcerpt();
107 assertNotNull(sourceRegion);
108 if (lineNumber == 1) {
109 assertEquals(1, region.getBeginningLineNumber());
110 } else {
111 assertTrue(region.getBeginningLineNumber() <= lineNumber);
112 }
113 assertTrue(lineNumber <= region.getEndingLineNumber());
114 assertNotSame(sourceRegion, 0, sourceRegion.length());
115 assertNotSame(sourceRegion, '\n', sourceRegion.charAt(0));
116 assertNotSame(sourceRegion,
117 '\n', sourceRegion.charAt(sourceRegion.length() - 1));
118 String line = provider.getSourceLine(sourceName, lineNumber);
119 assertTrue(sourceRegion, sourceRegion.contains(line));
120 }
121 }