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