Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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
20 import com.google.common.base.Joiner;
21 import com.google.common.collect.Lists;
22
23 import junit.framework.TestCase;
24
25 import java.util.List;
26
27 /**
28 * Tests for {@link ExternExportsPass}.
29 *
30 */
31 public class ExternExportsPassTest extends TestCase {
32
33 private boolean runCheckTypes = true;
34
35 /**
36 * ExternExportsPass relies on type information to emit JSDoc annotations for
37 * exported externs. However, the user can disable type checking and still
38 * ask for externs to be exported. Set this flag to enable or disable checking
39 * of types during a test.
40 */
41 private void setRunCheckTypes(boolean shouldRunCheckTypes) {
42 runCheckTypes = shouldRunCheckTypes;
43 }
44
45 @Override
46 public void setUp() throws Exception {
47 super.setUp();
48
49 setRunCheckTypes(true);
50 }
51
52 public void testExportSymbol() throws Exception {
53 compileAndCheck("var a = {}; a.b = {}; a.b.c = function(d, e, f) {};" +
54 "goog.exportSymbol('foobar', a.b.c)",
55 "/**\n" +
56 " * @param {?} d\n" +
57 " * @param {?} e\n" +
58 " * @param {?} f\n" +
59 " * @return {undefined}\n" +
60 " */\n" +
61 "var foobar = function(d, e, f) {\n};\n");
62 }
63
64 public void testExportSymbolDefinedInVar() throws Exception {
65 compileAndCheck("var a = function(d, e, f) {};" +
66 "goog.exportSymbol('foobar', a)",
67 "/**\n" +
68 " * @param {?} d\n" +
69 " * @param {?} e\n" +
70 " * @param {?} f\n" +
71 " * @return {undefined}\n" +
72 " */\n" +
73 "var foobar = function(d, e, f) {\n};\n");
74 }
75
76 public void testExportProperty() throws Exception {
77 compileAndCheck("var a = {}; a.b = {}; a.b.c = function(d, e, f) {};" +
78 "goog.exportProperty(a.b, 'cprop', a.b.c)",
79 "var a;\n" +
80 "a.b;\n" +
81 "/**\n" +
82 " * @param {?} d\n" +
83 " * @param {?} e\n" +
84 " * @param {?} f\n" +
85 " * @return {undefined}\n" +
86 " */\n" +
87 "a.b.cprop = function(d, e, f) {\n};\n");
88 }
89
90 public void testExportMultiple() throws Exception {
91 compileAndCheck("var a = {}; a.b = function(p1) {}; " +
92 "a.b.c = function(d, e, f) {};" +
93 "a.b.prototype.c = function(g, h, i) {};" +
94 "goog.exportSymbol('a.b', a.b);" +
95 "goog.exportProperty(a.b, 'c', a.b.c);" +
96 "goog.exportProperty(a.b.prototype, 'c', a.b.prototype.c);",
97
98 "var a;\n" +
99 "/**\n" +
100 " * @param {?} p1\n" +
101 " * @return {undefined}\n" +
102 " */\n" +
103 "a.b = function(p1) {\n};\n" +
104 "/**\n" +
105 " * @param {?} d\n" +
106 " * @param {?} e\n" +
107 " * @param {?} f\n" +
108 " * @return {undefined}\n" +
109 " */\n" +
110 "a.b.c = function(d, e, f) {\n};\n" +
111 "/**\n" +
112 " * @param {?} g\n" +
113 " * @param {?} h\n" +
114 " * @param {?} i\n" +
115 " * @return {undefined}\n" +
116 " */\n" +
117 "a.b.prototype.c = function(g, h, i) {\n};\n");
118 }
119
120 public void testExportMultiple2() throws Exception {
121 compileAndCheck("var a = {}; a.b = function(p1) {}; " +
122 "a.b.c = function(d, e, f) {};" +
123 "a.b.prototype.c = function(g, h, i) {};" +
124 "goog.exportSymbol('hello', a);" +
125 "goog.exportProperty(a.b, 'c', a.b.c);" +
126 "goog.exportProperty(a.b.prototype, 'c', a.b.prototype.c);",
127
128 "/** @type {{b: function (?): undefined}} */\n" +
129 "var hello = {};\n" +
130 "hello.b;\n" +
131 "/**\n" +
132 " * @param {?} d\n" +
133 " * @param {?} e\n" +
134 " * @param {?} f\n" +
135 " * @return {undefined}\n" +
136 " */\n" +
137 "hello.b.c = function(d, e, f) {\n};\n" +
138 "/**\n" +
139 " * @param {?} g\n" +
140 " * @param {?} h\n" +
141 " * @param {?} i\n" +
142 " * @return {undefined}\n" +
143 " */\n" +
144 "hello.b.prototype.c = function(g, h, i) {\n};\n");
145 }
146
147 public void testExportMultiple3() throws Exception {
148 compileAndCheck("var a = {}; a.b = function(p1) {}; " +
149 "a.b.c = function(d, e, f) {};" +
150 "a.b.prototype.c = function(g, h, i) {};" +
151 "goog.exportSymbol('prefix', a.b);" +
152 "goog.exportProperty(a.b, 'c', a.b.c);",
153
154 "/**\n" +
155 " * @param {?} p1\n" +
156 " * @return {undefined}\n" +
157 " */\n" +
158 "var prefix = function(p1) {\n};\n" +
159 "/**\n" +
160 " * @param {?} d\n" +
161 " * @param {?} e\n" +
162 " * @param {?} f\n" +
163 " * @return {undefined}\n" +
164 " */\n" +
165 "prefix.c = function(d, e, f) {\n};\n");
166 }
167
168 public void testExportNonStaticSymbol() throws Exception {
169 compileAndCheck("var a = {}; a.b = {}; var d = {}; a.b.c = d;" +
170 "goog.exportSymbol('foobar', a.b.c)",
171 "var foobar;\n");
172 }
173
174 public void testExportNonStaticSymbol2() throws Exception {
175 compileAndCheck("var a = {}; a.b = {}; var d = null; a.b.c = d;" +
176 "goog.exportSymbol('foobar', a.b.c())",
177 "var foobar;\n");
178 }
179
180 public void testExportNonexistentProperty() throws Exception {
181 compileAndCheck("var a = {}; a.b = {}; a.b.c = function(d, e, f) {};" +
182 "goog.exportProperty(a.b, 'none', a.b.none)",
183 "var a;\n" +
184 "a.b;\n" +
185 "a.b.none;\n");
186 }
187
188 public void testExportSymbolWithTypeAnnotation() {
189
190 compileAndCheck("var internalName;\n" +
191 "/**\n" +
192 " * @param {string} param1\n" +
193 " * @param {number} param2\n" +
194 " * @return {string}\n" +
195 " */\n" +
196 "internalName = function(param1, param2) {" +
197 "return param1 + param2;" +
198 "};" +
199 "goog.exportSymbol('externalName', internalName)",
200 "/**\n" +
201 " * @param {string} param1\n" +
202 " * @param {number} param2\n" +
203 " * @return {string}\n" +
204 " */\n" +
205 "var externalName = function(param1, param2) {\n};\n");
206 }
207
208 public void testExportSymbolWithTemplateAnnotation() {
209
210 compileAndCheck("var internalName;\n" +
211 "/**\n" +
212 " * @param {T} param1\n" +
213 " * @return {T}\n" +
214 " * @template T\n" +
215 " */\n" +
216 "internalName = function(param1) {" +
217 "return param1;" +
218 "};" +
219 "goog.exportSymbol('externalName', internalName)",
220 "/**\n" +
221 " * @param {T} param1\n" +
222 " * @return {T}\n" +
223 " * @template T\n" +
224 " */\n" +
225 "var externalName = function(param1) {\n};\n");
226 }
227
228 public void testExportSymbolWithMultipleTemplateAnnotation() {
229
230 compileAndCheck("var internalName;\n" +
231 "/**\n" +
232 " * @param {K} param1\n" +
233 " * @return {V}\n" +
234 " * @template K,V\n" +
235 " */\n" +
236 "internalName = function(param1) {" +
237 "return /** @type {V} */ (param1);" +
238 "};" +
239 "goog.exportSymbol('externalName', internalName)",
240 "/**\n" +
241 " * @param {K} param1\n" +
242 " * @return {V}\n" +
243 " * @template K,V\n" +
244 " */\n" +
245 "var externalName = function(param1) {\n};\n");
246 }
247
248 public void testExportSymbolWithoutTypeCheck() {
249 // ExternExportsPass should not emit annotations
250 // if there is no type information available.
251 setRunCheckTypes(false);
252
253 compileAndCheck("var internalName;\n" +
254 "/**\n" +
255 " * @param {string} param1\n" +
256 " * @param {number} param2\n" +
257 " * @return {string}\n" +
258 " */\n" +
259 "internalName = function(param1, param2) {" +
260 "return param1 + param2;" +
261 "};" +
262 "goog.exportSymbol('externalName', internalName)",
263 "var externalName = function(param1, param2) {\n};\n");
264 }
265
266 public void testExportSymbolWithConstructor() {
267 compileAndCheck("var internalName;\n" +
268 "/**\n" +
269 " * @constructor\n" +
270 " */\n" +
271 "internalName = function() {" +
272 "};" +
273 "goog.exportSymbol('externalName', internalName)",
274 "/**\n" +
275 " * @return {undefined}\n" +
276 " * @constructor\n" +
277 " */\n" +
278 "var externalName = function() {\n};\n");
279 }
280
281 public void testExportSymbolWithConstructorWithoutTypeCheck() {
282 // For now, skipping type checking should prevent generating
283 // annotations of any kind, so, e.g., @constructor is not preserved.
284 // This is probably not ideal, but since JSDocInfo for functions is attached
285 // to JSTypes and not Nodes (and no JSTypes are created when checkTypes
286 // is false), we don't really have a choice.
287
288 setRunCheckTypes(false);
289
290 compileAndCheck("var internalName;\n" +
291 "/**\n" +
292 " * @constructor\n" +
293 " */\n" +
294 "internalName = function() {" +
295 "};" +
296 "goog.exportSymbol('externalName', internalName)",
297 "var externalName = function() {\n};\n");
298 }
299
300 public void testExportFunctionWithOptionalArguments1() {
301 compileAndCheck("var internalName;\n" +
302 "/**\n" +
303 " * @param {number=} a\n" +
304 " */\n" +
305 "internalName = function(a) {" +
306 "};" +
307 "goog.exportSymbol('externalName', internalName)",
308 "/**\n" +
309 " * @param {number=} a\n" +
310 " * @return {undefined}\n" +
311 " */\n" +
312 "var externalName = function(a) {\n};\n");
313 }
314
315 public void testExportFunctionWithOptionalArguments2() {
316 compileAndCheck("var internalName;\n" +
317 "/**\n" +
318 " * @param {number=} a\n" +
319 " */\n" +
320 "internalName = function(a) {" +
321 " return 6;\n" +
322 "};" +
323 "goog.exportSymbol('externalName', internalName)",
324 "/**\n" +
325 " * @param {number=} a\n" +
326 " * @return {?}\n" +
327 " */\n" +
328 "var externalName = function(a) {\n};\n");
329 }
330
331 public void testExportFunctionWithOptionalArguments3() {
332 compileAndCheck("var internalName;\n" +
333 "/**\n" +
334 " * @param {number=} a\n" +
335 " */\n" +
336 "internalName = function(a) {" +
337 " return a;\n" +
338 "};" +
339 "goog.exportSymbol('externalName', internalName)",
340 "/**\n" +
341 " * @param {number=} a\n" +
342 " * @return {?}\n" +
343 " */\n" +
344 "var externalName = function(a) {\n};\n");
345 }
346
347 public void testExportFunctionWithVariableArguments() {
348 compileAndCheck("var internalName;\n" +
349 "/**\n" +
350 " * @param {...number} a\n" +
351 " * @return {number}\n" +
352 " */\n" +
353 "internalName = function(a) {" +
354 " return 6;\n" +
355 "};" +
356 "goog.exportSymbol('externalName', internalName)",
357 "/**\n" +
358 " * @param {...number} a\n" +
359 " * @return {number}\n" +
360 " */\n" +
361 "var externalName = function(a) {\n};\n");
362 }
363
364 /**
365 * Enums are not currently handled.
366 */
367 public void testExportEnum() {
368 // We don't care what the values of the object properties are.
369 // They're ignored by the type checker, and even if they weren't, it'd
370 // be incomputable to get them correct in all cases
371 // (think complex objects).
372 compileAndCheck(
373 "/** @enum {string}\n @export */ var E = {A:8, B:9};" +
374 "goog.exportSymbol('E', E);",
375 "/** @enum {string} */\n" +
376 "var E = {A:1, B:2};\n");
377 }
378
379 /** If we export a property with "prototype" as a path component, there
380 * is no need to emit the initializer for prototype because every namespace
381 * has one automatically.
382 */
383 public void testExportDontEmitPrototypePathPrefix() {
384 compileAndCheck(
385 "/**\n" +
386 " * @constructor\n" +
387 " */\n" +
388 "var Foo = function() {};" +
389 "/**\n" +
390 " * @return {number}\n" +
391 " */\n" +
392 "Foo.prototype.m = function() {return 6;};\n" +
393 "goog.exportSymbol('Foo', Foo);\n" +
394 "goog.exportProperty(Foo.prototype, 'm', Foo.prototype.m);",
395 "/**\n" +
396 " * @return {undefined}\n" +
397 " * @constructor\n" +
398 " */\n" +
399 "var Foo = function() {\n};\n" +
400 "/**\n" +
401 " * @return {number}\n" +
402 " */\n" +
403 "Foo.prototype.m = function() {\n};\n"
404 );
405 }
406
407 /**
408 * Test the workflow of creating an externs file for a library
409 * via the export pass and then using that externs file in a client.
410 *
411 * There should be no warnings in the client if the library includes
412 * type information for the exported functions and the client uses them
413 * correctly.
414 */
415 public void testUseExportsAsExterns() {
416 String librarySource =
417 "/**\n" +
418 " * @param {number} a\n" +
419 " * @constructor\n" +
420 " */\n" +
421 "var InternalName = function(a) {" +
422 "};" +
423 "goog.exportSymbol('ExternalName', InternalName)";
424
425 String clientSource =
426 "var a = new ExternalName(6);\n" +
427 "/**\n" +
428 " * @param {ExternalName} x\n" +
429 " */\n" +
430 "var b = function(x) {};";
431
432 Result libraryCompileResult = compileAndExportExterns(librarySource);
433
434 assertEquals(0, libraryCompileResult.warnings.length);
435 assertEquals(0, libraryCompileResult.errors.length);
436
437 String generatedExterns = libraryCompileResult.externExport;
438
439 Result clientCompileResult = compileAndExportExterns(clientSource,
440 generatedExterns);
441
442 assertEquals(0, clientCompileResult.warnings.length);
443 assertEquals(0, clientCompileResult.errors.length);
444 }
445
446 public void testWarnOnExportFunctionWithUnknownReturnType() {
447 String librarySource =
448 "var InternalName = function() {" +
449 " return 6;" +
450 "};" +
451 "goog.exportSymbol('ExternalName', InternalName)";
452
453 Result libraryCompileResult = compileAndExportExterns(librarySource);
454
455 assertEquals(1, libraryCompileResult.warnings.length);
456 assertEquals(0, libraryCompileResult.errors.length);
457 }
458
459 public void testDontWarnOnExportConstructorWithUnknownReturnType() {
460 String librarySource =
461 "/**\n" +
462 " * @constructor\n" +
463 " */\n " +
464 "var InternalName = function() {" +
465 "};" +
466 "goog.exportSymbol('ExternalName', InternalName)";
467
468 Result libraryCompileResult = compileAndExportExterns(librarySource);
469
470 assertEquals(0, libraryCompileResult.warnings.length);
471 assertEquals(0, libraryCompileResult.errors.length);
472 }
473
474 public void testTypedef() {
475 compileAndCheck(
476 "/** @typedef {{x: number, y: number}} */ var Coord;\n" +
477 "/**\n" +
478 " * @param {Coord} a\n" +
479 " * @export\n" +
480 " */\n" +
481 "var fn = function(a) {};" +
482 "goog.exportSymbol('fn', fn);",
483 "/**\n" +
484 " * @param {{x: number, y: number}} a\n" +
485 " * @return {undefined}\n" +
486 " */\n" +
487 "var fn = function(a) {\n};\n");
488 }
489
490 public void testExportParamWithNull() throws Exception {
491 compileAndCheck(
492 "/** @param {string|null=} d */\n" +
493 "var f = function(d) {};\n" +
494 "goog.exportSymbol('foobar', f)\n",
495 "/**\n" +
496 " * @param {(null|string)=} d\n" +
497 " * @return {undefined}\n" +
498 " */\n" +
499 "var foobar = function(d) {\n" +
500 "};\n");
501 }
502
503 public void testExportConstructor() throws Exception {
504 compileAndCheck("/** @constructor */ var a = function() {};" +
505 "goog.exportSymbol('foobar', a)",
506 "/**\n" +
507 " * @return {undefined}\n" +
508 " * @constructor\n" +
509 " */\n" +
510 "var foobar = function() {\n};\n");
511 }
512
513 private void compileAndCheck(String js, String expected) {
514 Result result = compileAndExportExterns(js);
515
516 assertEquals(expected, result.externExport);
517 }
518
519 public void testWarnOnExportFunctionWithUnknownParameterTypes() {
520 /* This source is missing types for the b and c parameters */
521 String librarySource =
522 "/**\n" +
523 " * @param {number} a\n" +
524 " * @return {number}" +
525 " */\n " +
526 "var InternalName = function(a,b,c) {" +
527 " return 6;" +
528 "};" +
529 "goog.exportSymbol('ExternalName', InternalName)";
530
531 Result libraryCompileResult = compileAndExportExterns(librarySource);
532
533 assertEquals(2, libraryCompileResult.warnings.length);
534 assertEquals(0, libraryCompileResult.errors.length);
535 }
536
537 private Result compileAndExportExterns(String js) {
538 return compileAndExportExterns(js, "");
539 }
540
541 /**
542 * Compiles the passed in JavaScript with the passed in externs and returns
543 * the new externs exported by the this pass.
544 *
545 * @param js the source to be compiled
546 * @param externs the externs the {@code js} source needs
547 * @return the externs generated from {@code js}
548 */
549 private Result compileAndExportExterns(String js, String externs) {
550 Compiler compiler = new Compiler();
551 CompilerOptions options = new CompilerOptions();
552 options.externExportsPath = "externs.js";
553
554 // Turn on IDE mode to get rid of optimizations.
555 options.ideMode = true;
556
557 /* Check types so we can make sure our exported externs have
558 * type information.
559 */
560 options.checkSymbols = true;
561 options.checkTypes = runCheckTypes;
562
563 List<SourceFile> inputs = Lists.newArrayList(
564 SourceFile.fromCode("testcode",
565 "var goog = {};" +
566 "goog.exportSymbol = function(a, b) {}; " +
567 "goog.exportProperty = function(a, b, c) {}; " +
568 js));
569
570 List<SourceFile> externFiles = Lists.newArrayList(
571 SourceFile.fromCode("externs", externs));
572
573 Result result = compiler.compile(externFiles, inputs, options);
574
575 if (!result.success) {
576 String msg = "Errors:";
577 msg += Joiner.on("\n").join(result.errors);
578 assertTrue(msg, result.success);
579 }
580
581 return result;
582 }
583 }