Debian Codegraph @

test/com/google/javascript/jscomp/CompilerTest.java

raw symbols SCIP
1 /*
2 * Copyright 2010 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.common.collect.ImmutableList;
20 import com.google.common.collect.Lists;
21 import com.google.javascript.rhino.Node;
22
23 import junit.framework.TestCase;
24
25 import java.util.List;
26
27 /**
28 * @author [email protected] (John Lenz)
29 */
30 public class CompilerTest extends TestCase {
31
32 // Verify the line and column information is maintained after a reset
33 public void testCodeBuilderColumnAfterReset() {
34 Compiler.CodeBuilder cb = new Compiler.CodeBuilder();
35 String js = "foo();\ngoo();";
36 cb.append(js);
37 assertEquals(js, cb.toString());
38 assertEquals(1, cb.getLineIndex());
39 assertEquals(6, cb.getColumnIndex());
40
41 cb.reset();
42
43 assertTrue(cb.toString().isEmpty());
44 assertEquals(1, cb.getLineIndex());
45 assertEquals(6, cb.getColumnIndex());
46 }
47
48 public void testCodeBuilderAppend() {
49 Compiler.CodeBuilder cb = new Compiler.CodeBuilder();
50 cb.append("foo();");
51 assertEquals(0, cb.getLineIndex());
52 assertEquals(6, cb.getColumnIndex());
53
54 cb.append("goo();");
55
56 assertEquals(0, cb.getLineIndex());
57 assertEquals(12, cb.getColumnIndex());
58
59 // newline reset the column index
60 cb.append("blah();\ngoo();");
61
62 assertEquals(1, cb.getLineIndex());
63 assertEquals(6, cb.getColumnIndex());
64 }
65
66 public void testCyclicalDependencyInInputs() {
67 List<SourceFile> inputs = Lists.newArrayList(
68 SourceFile.fromCode(
69 "gin", "goog.provide('gin'); goog.require('tonic'); var gin = {};"),
70 SourceFile.fromCode("tonic",
71 "goog.provide('tonic'); goog.require('gin'); var tonic = {};"),
72 SourceFile.fromCode(
73 "mix", "goog.require('gin'); goog.require('tonic');"));
74 CompilerOptions options = new CompilerOptions();
75 options.ideMode = true;
76 options.setManageClosureDependencies(true);
77 Compiler compiler = new Compiler();
78 compiler.init(ImmutableList.<SourceFile>of(), inputs, options);
79 compiler.parseInputs();
80 assertEquals(compiler.externAndJsRoot, compiler.jsRoot.getParent());
81 assertEquals(compiler.externAndJsRoot, compiler.externsRoot.getParent());
82 assertNotNull(compiler.externAndJsRoot);
83
84 Node jsRoot = compiler.jsRoot;
85 assertEquals(3, jsRoot.getChildCount());
86 }
87
88 public void testLocalUndefined() throws Exception {
89 // Some JavaScript libraries like to create a local instance of "undefined",
90 // to ensure that other libraries don't try to overwrite it.
91 //
92 // Most of the time, this is OK, because normalization will rename
93 // that variable to undefined$$1. But this won't happen if they don't
94 // include the default externs.
95 //
96 // This test is just to make sure that the compiler doesn't crash.
97 CompilerOptions options = new CompilerOptions();
98 CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(
99 options);
100 Compiler compiler = new Compiler();
101 SourceFile externs = SourceFile.fromCode("externs.js", "");
102 SourceFile input = SourceFile.fromCode("input.js",
103 "(function (undefined) { alert(undefined); })();");
104 compiler.compile(externs, input, options);
105 }
106
107 public void testCommonJSProvidesAndRequire() throws Exception {
108 List<SourceFile> inputs = Lists.newArrayList(
109 SourceFile.fromCode("gin.js", "require('tonic')"),
110 SourceFile.fromCode("tonic.js", ""),
111 SourceFile.fromCode("mix.js", "require('gin'); require('tonic');"));
112 List<String> entryPoints = Lists.newArrayList("module$mix");
113
114 Compiler compiler = initCompilerForCommonJS(inputs, entryPoints);
115 JSModuleGraph graph = compiler.getModuleGraph();
116 assertEquals(4, graph.getModuleCount());
117 List<CompilerInput> result = graph.manageDependencies(entryPoints,
118 compiler.getInputsForTesting());
119 assertEquals("[root]", result.get(0).getName());
120 assertEquals("[module$tonic]", result.get(1).getName());
121 assertEquals("[module$gin]", result.get(2).getName());
122 assertEquals("tonic.js", result.get(3).getName());
123 assertEquals("gin.js", result.get(4).getName());
124 assertEquals("mix.js", result.get(5).getName());
125 }
126
127 public void testCommonJSMissingRequire() throws Exception {
128 List<SourceFile> inputs = Lists.newArrayList(
129 SourceFile.fromCode("gin.js", "require('missing')"));
130 Compiler compiler = initCompilerForCommonJS(
131 inputs, ImmutableList.of("module$gin"));
132 compiler.processAMDAndCommonJSModules();
133
134 assertEquals(1, compiler.getErrorManager().getErrorCount());
135 String error = compiler.getErrorManager().getErrors()[0].toString();
136 assertTrue(
137 "Unexpected error: " + error,
138 error.contains(
139 "required entry point \"module$missing\" never provided"));
140 }
141
142 private Compiler initCompilerForCommonJS(
143 List<SourceFile> inputs, List<String> entryPoints)
144 throws Exception {
145 CompilerOptions options = new CompilerOptions();
146 options.ideMode = true;
147 options.setManageClosureDependencies(entryPoints);
148 options.closurePass = true;
149 options.processCommonJSModules = true;
150 Compiler compiler = new Compiler();
151 compiler.init(Lists.<SourceFile>newArrayList(), inputs, options);
152 compiler.parseInputs();
153 return compiler;
154 }
155
156
157 }