Debian Codegraph @

test/com/google/javascript/jscomp/deps/JsFileParserTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 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.deps;
18
19 import com.google.common.collect.ImmutableList;
20 import com.google.javascript.jscomp.ErrorManager;
21 import com.google.javascript.jscomp.PrintStreamErrorManager;
22
23 import junit.framework.TestCase;
24
25 import java.util.Collections;
26
27 /**
28 * Tests for {@link JsFileParser}.
29 *
30 * @author [email protected] (Andrew Grieve)
31 */
32 public class JsFileParserTest extends TestCase {
33
34 JsFileParser parser;
35 private ErrorManager errorManager;
36
37 private static final String SRC_PATH = "a";
38 private static final String CLOSURE_PATH = "b";
39
40 @Override
41 public void setUp() {
42 errorManager = new PrintStreamErrorManager(System.err);
43 parser = new JsFileParser(errorManager);
44 parser.setShortcutMode(true);
45 }
46
47 /**
48 * Tests:
49 * -Parsing of comments,
50 * -Parsing of different styles of quotes,
51 * -Correct recording of what was parsed.
52 */
53 public void testParseFile() {
54 String contents = "/*"
55 + "goog.provide('no1');*//*\n"
56 + "goog.provide('no2');\n"
57 + "*/goog.provide('yes1');\n"
58 + "/* blah */goog.provide(\"yes2\")/* blah*/\n"
59 + "goog.require('yes3'); // goog.provide('no3');\n"
60 + "// goog.provide('no4');\n"
61 + "goog.require(\"bar.data.SuperstarAddStarThreadActionRequestDelegate\"); "
62 + "//no new line at EOF";
63
64 DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH,
65 ImmutableList.of("yes1", "yes2"),
66 ImmutableList.of("yes3", "bar.data.SuperstarAddStarThreadActionRequestDelegate"));
67
68 DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents);
69
70 assertDeps(expected, result);
71 }
72
73 public void testMultiplePerLine() {
74 String contents = "goog.provide('yes1');goog.provide('yes2');/*"
75 + "goog.provide('no1');*/goog.provide('yes3');//goog.provide('no2');";
76
77 DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH,
78 ImmutableList.of("yes1", "yes2", "yes3"), Collections.<String>emptyList());
79
80 DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents);
81
82 assertDeps(expected, result);
83 }
84
85 public void testShortcutMode1() {
86 // For efficiency reasons, we stop reading after the ctor.
87 String contents = " // hi ! \n /* this is a comment */ "
88 + "goog.provide('yes1');\n /* and another comment */ \n"
89 + "goog.provide('yes2'); // include this\n"
90 + "function foo() {}\n"
91 + "goog.provide('no1');";
92
93 DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH,
94 ImmutableList.of("yes1", "yes2"), Collections.<String>emptyList());
95 DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents);
96
97 assertDeps(expected, result);
98 }
99
100 public void testShortcutMode2() {
101 String contents = "/** goog.provide('no1'); \n" +
102 " * goog.provide('no2');\n */\n"
103 + "goog.provide('yes1');\n";
104
105 DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH,
106 ImmutableList.of("yes1"), Collections.<String>emptyList());
107 DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents);
108
109 assertDeps(expected, result);
110 }
111
112 public void testShortcutMode3() {
113 String contents = "/**\n" +
114 " * goog.provide('no1');\n */\n"
115 + "goog.provide('yes1');\n";
116
117 DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH,
118 ImmutableList.of("yes1"), Collections.<String>emptyList());
119 DependencyInfo result = parser.parseFile(SRC_PATH, CLOSURE_PATH, contents);
120
121 assertDeps(expected, result);
122 }
123
124 public void testIncludeGoog1() {
125 String contents = "/**\n" +
126 " * the first constant in base.js\n" +
127 " */\n" +
128 "var COMPILED = false;\n";
129
130 DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH,
131 ImmutableList.of("goog"), Collections.<String>emptyList());
132 DependencyInfo result = parser.setIncludeGoogBase(true).parseFile(
133 SRC_PATH, CLOSURE_PATH, contents);
134 assertDeps(expected, result);
135 }
136
137 public void testIncludeGoog2() {
138 String contents = "goog.require('bar');";
139
140 DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH,
141 ImmutableList.<String>of(), ImmutableList.of("goog", "bar"));
142 DependencyInfo result = parser.setIncludeGoogBase(true).parseFile(
143 SRC_PATH, CLOSURE_PATH, contents);
144 assertDeps(expected, result);
145 }
146
147 public void testIncludeGoog3() {
148 // This guy is pretending to provide goog, but he really doesn't.
149 String contents = "goog.provide('x');\n" +
150 "/**\n" +
151 " * the first constant in base.js\n" +
152 " */\n" +
153 "var COMPILED = false;\n";
154
155 DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH,
156 ImmutableList.of("x"), ImmutableList.of("goog"));
157 DependencyInfo result = parser.setIncludeGoogBase(true).parseFile(
158 SRC_PATH, CLOSURE_PATH, contents);
159 assertDeps(expected, result);
160 }
161
162 public void testIncludeGoog4() {
163 String contents = "goog.addDependency('foo', [], []);\n";
164
165 DependencyInfo expected = new SimpleDependencyInfo(CLOSURE_PATH, SRC_PATH,
166 ImmutableList.<String>of(), ImmutableList.of("goog"));
167 DependencyInfo result = parser.setIncludeGoogBase(true).parseFile(
168 SRC_PATH, CLOSURE_PATH, contents);
169 assertDeps(expected, result);
170 }
171
172 /** Asserts the deps match without errors */
173 private void assertDeps(DependencyInfo expected, DependencyInfo actual) {
174 assertEquals(expected, actual);
175 assertEquals(0, errorManager.getErrorCount());
176 assertEquals(0, errorManager.getWarningCount());
177 }
178 }