Debian Codegraph @

test/com/google/javascript/jscomp/CheckRequiresForConstructorsTest.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;
18
19 import static com.google.javascript.jscomp.CheckRequiresForConstructors.MISSING_REQUIRE_WARNING;
20
21 import com.google.common.collect.ImmutableList;
22 import com.google.javascript.jscomp.CheckLevel;
23 import com.google.javascript.jscomp.Result;
24
25 /**
26 * Tests for {@link CheckRequiresForConstructors}.
27 *
28 */
29 public class CheckRequiresForConstructorsTest extends CompilerTestCase {
30 @Override
31 protected CompilerPass getProcessor(Compiler compiler) {
32 return new CheckRequiresForConstructors(compiler, CheckLevel.WARNING);
33 }
34
35 public void testPassWithNoNewNodes() {
36 String js = "var str = 'g4'; /* does not use new */";
37 testSame(js);
38 }
39
40 public void testPassWithOneNew() {
41 String js =
42 "var goog = {};" +
43 "goog.require('foo.bar.goo'); var bar = new foo.bar.goo();";
44 testSame(js);
45 }
46
47 public void testPassWithOneNewOuterClass() {
48 String js =
49 "var goog = {};" +
50 "goog.require('goog.foo.Bar'); var bar = new goog.foo.Bar.Baz();";
51 testSame(js);
52 }
53
54 public void testPassWithOneNewOuterClassWithUpperPrefix() {
55 String js =
56 "var goog = {};" +
57 "goog.require('goog.foo.IDBar'); var bar = new goog.foo.IDBar.Baz();";
58 testSame(js);
59 }
60
61 public void testFailWithOneNew() {
62 String[] js = new String[] {"var foo = {}; var bar = new foo.bar();"};
63 String warning = "'foo.bar' used but not goog.require'd";
64 test(js, js, null, MISSING_REQUIRE_WARNING, warning);
65 }
66
67 public void testPassWithTwoNewNodes() {
68 String js =
69 "var goog = {};" +
70 "goog.require('goog.foo.Bar');goog.require('goog.foo.Baz');" +
71 "var str = new goog.foo.Bar('g4'), num = new goog.foo.Baz(5); ";
72 testSame(js);
73 }
74
75 public void testPassWithNestedNewNodes() {
76 String js =
77 "var goog = {}; goog.require('goog.foo.Bar'); " +
78 "var str = new goog.foo.Bar(new goog.foo.Bar('5')); ";
79 testSame(js);
80 }
81
82 public void testFailWithNestedNewNodes() {
83 String[] js =
84 new String[] {"var goog = {}; goog.require('goog.foo.Bar'); "
85 + "var str = new goog.foo.Bar(new goog.foo.Baz('5')); "};
86 String warning = "'goog.foo.Baz' used but not goog.require'd";
87 test(js, js, null, MISSING_REQUIRE_WARNING, warning);
88 }
89
90 public void testPassWithLocalFunctions() {
91 String js =
92 "/** @constructor */ function tempCtor() {}; var foo = new tempCtor();";
93 testSame(js);
94 }
95
96 public void testPassWithLocalVariables() {
97 String js =
98 "/** @constructor */ var nodeCreator = function() {};"
99 + "var newNode = new nodeCreator();";
100 testSame(js);
101 }
102
103 public void testFailWithLocalVariableInMoreThanOneFile() {
104 // there should be a warning for the 2nd script because it is only declared
105 // in the 1st script
106 String localVar =
107 "/** @constructor */ function tempCtor() {}" +
108 "function baz(){" + " /** @constructor */ function tempCtor() {}; "
109 + "var foo = new tempCtor();}";
110 String[] js = new String[] {localVar, " var foo = new tempCtor();"};
111 String warning = "'tempCtor' used but not goog.require'd";
112 test(js, js, null, MISSING_REQUIRE_WARNING, warning);
113 }
114
115 public void testNewNodesMetaTraditionalFunctionForm() {
116 // the class in this script creates an instance of itself
117 // there should be no warning because the class should not have to
118 // goog.require itself .
119 String js =
120 "/** @constructor */ function Bar(){}; "
121 + "Bar.prototype.bar = function(){ return new Bar();};";
122 testSame(js);
123 }
124
125 public void testNewNodesMeta() {
126 String js =
127 "var goog = {};" +
128 "/** @constructor */goog.ui.Option = function(){};"
129 + "goog.ui.Option.optionDecorator = function(){"
130 + " return new goog.ui.Option(); };";
131 testSame(js);
132 }
133
134 public void testShouldWarnWhenInstantiatingObjectsDefinedInGlobalScope() {
135 // there should be a warning for the 2nd script because
136 // Bar was declared in the 1st file, not the 2nd
137 String good =
138 "/** @constructor */ function Bar(){}; "
139 + "Bar.prototype.bar = function(){return new Bar();};";
140 String bad = "/** @constructor */ function Foo(){ var bar = new Bar();}";
141 String[] js = new String[] {good, bad};
142 String warning = "'Bar' used but not goog.require'd";
143 test(js, js, null, MISSING_REQUIRE_WARNING, warning);
144 }
145
146 public void testShouldWarnWhenInstantiatingGlobalClassesFromGlobalScope() {
147 // there should be a warning for the 2nd script because Baz
148 // was declared in the first file, not the 2nd
149 String good =
150 "/** @constructor */ function Baz(){}; "
151 + "Baz.prototype.bar = function(){return new Baz();};";
152 String bad = "var baz = new Baz()";
153 String[] js = new String[] {good, bad};
154 String warning = "'Baz' used but not goog.require'd";
155 test(js, js, null, MISSING_REQUIRE_WARNING, warning);
156 }
157
158 public void testIgnoresNativeObject() {
159 String externs = "/** @constructor */ function String(val) {}";
160 String js = "var str = new String('4');";
161 test(externs, js, js, null, null);
162 }
163
164 public void testNewNodesWithMoreThanOneFile() {
165 // Bar is created, and goog.require()ed, but in different files.
166 String[] js = new String[] {
167 "var goog = {};" +
168 "/** @constructor */ function Bar() {}" +
169 "goog.require('Bar');",
170 "var bar = new Bar();"};
171 String warning = "'Bar' used but not goog.require'd";
172 test(js, js, null, MISSING_REQUIRE_WARNING, warning);
173 }
174
175 public void testPassWithoutWarningsAndMultipleFiles() {
176 String[] js = new String[] {
177 "var goog = {};" +
178 "goog.require('Foo'); var foo = new Foo();",
179 "goog.require('Bar'); var bar = new Bar();"};
180 testSame(js);
181 }
182
183 public void testFailWithWarningsAndMultipleFiles() {
184 /* goog.require is in the code base, but not in the correct file */
185 String[] js = new String[] {
186 "var goog = {};" +
187 "/** @constructor */ function Bar() {}" +
188 "goog.require('Bar');",
189 "var bar = new Bar();"};
190 String warning = "'Bar' used but not goog.require'd";
191 test(js, js, null, MISSING_REQUIRE_WARNING, warning);
192 }
193
194 public void testCanStillCallNumberWithoutNewOperator() {
195 String externs = "/** @constructor */ function Number(opt_value) {}";
196 String js = "var n = Number('42');";
197 test(externs, js, js, null, null);
198 js = "var n = Number();";
199 test(externs, js, js, null, null);
200 }
201
202 public void testRequiresAreCaughtBeforeProcessed() {
203 String js = "var foo = {}; var bar = new foo.bar.goo();";
204 SourceFile input = SourceFile.fromCode("foo.js", js);
205 Compiler compiler = new Compiler();
206 CompilerOptions opts = new CompilerOptions();
207 opts.checkRequires = CheckLevel.WARNING;
208 opts.closurePass = true;
209
210 Result result = compiler.compile(ImmutableList.<SourceFile>of(),
211 ImmutableList.of(input), opts);
212 JSError[] warnings = result.warnings;
213 assertNotNull(warnings);
214 assertTrue(warnings.length > 0);
215
216 String expectation = "'foo.bar.goo' used but not goog.require'd";
217
218 for (JSError warning : warnings) {
219 if (expectation.equals(warning.description)) {
220 return;
221 }
222 }
223
224 fail("Could not find the following warning:" + expectation);
225 }
226
227 public void testNoWarningsForThisConstructor() {
228 String js =
229 "var goog = {};" +
230 "/** @constructor */goog.Foo = function() {};" +
231 "goog.Foo.bar = function() {" +
232 " return new this.constructor; " +
233 "};";
234 testSame(js);
235 }
236
237 public void testBug2062487() {
238 testSame(
239 "var goog = {};" +
240 "/** @constructor */goog.Foo = function() {" +
241 " /** @constructor */ this.x_ = function() {};" +
242 " this.y_ = new this.x_();" +
243 "};");
244 }
245
246 public void testIgnoreDuplicateWarningsForSingleClasses(){
247 // no use telling them the same thing twice
248 String[] js = new String[]{
249 "var goog = {};" +
250 "/** @constructor */goog.Foo = function() {};" +
251 "goog.Foo.bar = function(){" +
252 " var first = new goog.Forgot();" +
253 " var second = new goog.Forgot();" +
254 "};"};
255 String warning = "'goog.Forgot' used but not goog.require'd";
256 test(js, js, null, MISSING_REQUIRE_WARNING, warning);
257 }
258 }