Debian Codegraph @

test/com/google/javascript/jscomp/parsing/JsDocInfoParserTest.java

raw symbols SCIP
1 /*
2 * Copyright 2007 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.parsing;
18
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Sets;
21 import com.google.javascript.jscomp.parsing.Config.LanguageMode;
22 import com.google.javascript.jscomp.testing.TestErrorReporter;
23 import com.google.javascript.rhino.InputId;
24 import com.google.javascript.rhino.JSDocInfo;
25 import com.google.javascript.rhino.JSDocInfo.Visibility;
26 import com.google.javascript.rhino.JSTypeExpression;
27 import com.google.javascript.rhino.Node;
28 import com.google.javascript.rhino.Token;
29 import com.google.javascript.rhino.head.CompilerEnvirons;
30 import com.google.javascript.rhino.head.Parser;
31 import com.google.javascript.rhino.head.Token.CommentType;
32 import com.google.javascript.rhino.head.ast.AstRoot;
33 import com.google.javascript.rhino.head.ast.Comment;
34 import com.google.javascript.rhino.jstype.JSType;
35 import com.google.javascript.rhino.jstype.ObjectType;
36 import com.google.javascript.rhino.jstype.SimpleSourceFile;
37 import com.google.javascript.rhino.jstype.StaticSourceFile;
38 import com.google.javascript.rhino.jstype.TemplateType;
39 import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
40
41 import java.util.Collection;
42 import java.util.List;
43 import java.util.Set;
44
45 public class JsDocInfoParserTest extends BaseJSTypeTestCase {
46
47 private Set<String> extraAnnotations;
48 private Set<String> extraSuppressions;
49 private Node.FileLevelJsDocBuilder fileLevelJsDocBuilder = null;
50
51 @Override
52 public void setUp() throws Exception {
53 super.setUp();
54 extraAnnotations =
55 Sets.newHashSet(
56 ParserRunner.createConfig(true, LanguageMode.ECMASCRIPT3, false)
57 .annotationNames.keySet());
58 extraSuppressions =
59 Sets.newHashSet(
60 ParserRunner.createConfig(true, LanguageMode.ECMASCRIPT3, false)
61 .suppressionNames);
62
63 extraSuppressions.add("x");
64 extraSuppressions.add("y");
65 extraSuppressions.add("z");
66 }
67
68 public void testParseTypeViaStatic1() throws Exception {
69 Node typeNode = parseType("null");
70 assertTypeEquals(NULL_TYPE, typeNode);
71 }
72
73 public void testParseTypeViaStatic2() throws Exception {
74 Node typeNode = parseType("string");
75 assertTypeEquals(STRING_TYPE, typeNode);
76 }
77
78 public void testParseTypeViaStatic3() throws Exception {
79 Node typeNode = parseType("!Date");
80 assertTypeEquals(DATE_TYPE, typeNode);
81 }
82
83 public void testParseTypeViaStatic4() throws Exception {
84 Node typeNode = parseType("boolean|string");
85 assertTypeEquals(createUnionType(BOOLEAN_TYPE, STRING_TYPE), typeNode);
86 }
87
88 public void testParseInvalidTypeViaStatic() throws Exception {
89 Node typeNode = parseType("sometype.<anothertype");
90 assertNull(typeNode);
91 }
92
93 public void testParseInvalidTypeViaStatic2() throws Exception {
94 Node typeNode = parseType("");
95 assertNull(typeNode);
96 }
97
98 public void testParseNamedType1() throws Exception {
99 assertNull(parse("@type null", "Unexpected end of file"));
100 }
101
102 public void testParseNamedType2() throws Exception {
103 JSDocInfo info = parse("@type null*/");
104 assertTypeEquals(NULL_TYPE, info.getType());
105 }
106
107 public void testParseNamedType3() throws Exception {
108 JSDocInfo info = parse("@type {string}*/");
109 assertTypeEquals(STRING_TYPE, info.getType());
110 }
111
112 public void testParseNamedType4() throws Exception {
113 // Multi-line @type.
114 JSDocInfo info = parse("@type \n {string}*/");
115 assertTypeEquals(STRING_TYPE, info.getType());
116 }
117
118 public void testParseNamedType5() throws Exception {
119 JSDocInfo info = parse("@type {!goog.\nBar}*/");
120 assertTypeEquals(
121 registry.createNamedType("goog.Bar", null, -1, -1),
122 info.getType());
123 }
124
125 public void testParseNamedType6() throws Exception {
126 JSDocInfo info = parse("@type {!goog.\n * Bar.\n * Baz}*/");
127 assertTypeEquals(
128 registry.createNamedType("goog.Bar.Baz", null, -1, -1),
129 info.getType());
130 }
131
132 public void testParseNamedTypeError1() throws Exception {
133 // To avoid parsing ambiguities, type names must end in a '.' to
134 // get the continuation behavior.
135 parse("@type {!goog\n * .Bar} */",
136 "Bad type annotation. expected closing }");
137 }
138
139 public void testParseNamedTypeError2() throws Exception {
140 parse("@type {!goog.\n * Bar\n * .Baz} */",
141 "Bad type annotation. expected closing }");
142 }
143
144 public void testTypedefType1() throws Exception {
145 JSDocInfo info = parse("@typedef string */");
146 assertTrue(info.hasTypedefType());
147 assertTypeEquals(STRING_TYPE, info.getTypedefType());
148 }
149
150 public void testTypedefType2() throws Exception {
151 JSDocInfo info = parse("@typedef \n {string}*/");
152 assertTrue(info.hasTypedefType());
153 assertTypeEquals(STRING_TYPE, info.getTypedefType());
154 }
155
156 public void testTypedefType3() throws Exception {
157 JSDocInfo info = parse("@typedef \n {(string|number)}*/");
158 assertTrue(info.hasTypedefType());
159 assertTypeEquals(
160 createUnionType(NUMBER_TYPE, STRING_TYPE),
161 info.getTypedefType());
162 }
163
164 public void testParseStringType1() throws Exception {
165 assertTypeEquals(STRING_TYPE, parse("@type {string}*/").getType());
166 }
167
168 public void testParseStringType2() throws Exception {
169 assertTypeEquals(STRING_OBJECT_TYPE, parse("@type {!String}*/").getType());
170 }
171
172 public void testParseBooleanType1() throws Exception {
173 assertTypeEquals(BOOLEAN_TYPE, parse("@type {boolean}*/").getType());
174 }
175
176 public void testParseBooleanType2() throws Exception {
177 assertTypeEquals(
178 BOOLEAN_OBJECT_TYPE, parse("@type {!Boolean}*/").getType());
179 }
180
181 public void testParseNumberType1() throws Exception {
182 assertTypeEquals(NUMBER_TYPE, parse("@type {number}*/").getType());
183 }
184
185 public void testParseNumberType2() throws Exception {
186 assertTypeEquals(NUMBER_OBJECT_TYPE, parse("@type {!Number}*/").getType());
187 }
188
189 public void testParseNullType1() throws Exception {
190 assertTypeEquals(NULL_TYPE, parse("@type {null}*/").getType());
191 }
192
193 public void testParseNullType2() throws Exception {
194 assertTypeEquals(NULL_TYPE, parse("@type {Null}*/").getType());
195 }
196
197 public void testParseAllType1() throws Exception {
198 testParseType("*");
199 }
200
201 public void testParseAllType2() throws Exception {
202 testParseType("*?", "*");
203 }
204
205 public void testParseObjectType() throws Exception {
206 assertTypeEquals(OBJECT_TYPE, parse("@type {!Object}*/").getType());
207 }
208
209 public void testParseDateType() throws Exception {
210 assertTypeEquals(DATE_TYPE, parse("@type {!Date}*/").getType());
211 }
212
213 public void testParseFunctionType() throws Exception {
214 assertTypeEquals(
215 createNullableType(U2U_CONSTRUCTOR_TYPE),
216 parse("@type {Function}*/").getType());
217 }
218
219 public void testParseRegExpType() throws Exception {
220 assertTypeEquals(REGEXP_TYPE, parse("@type {!RegExp}*/").getType());
221 }
222
223 public void testParseErrorTypes() throws Exception {
224 assertTypeEquals(ERROR_TYPE, parse("@type {!Error}*/").getType());
225 assertTypeEquals(URI_ERROR_TYPE, parse("@type {!URIError}*/").getType());
226 assertTypeEquals(EVAL_ERROR_TYPE, parse("@type {!EvalError}*/").getType());
227 assertTypeEquals(REFERENCE_ERROR_TYPE,
228 parse("@type {!ReferenceError}*/").getType());
229 assertTypeEquals(TYPE_ERROR_TYPE, parse("@type {!TypeError}*/").getType());
230 assertTypeEquals(
231 RANGE_ERROR_TYPE, parse("@type {!RangeError}*/").getType());
232 assertTypeEquals(
233 SYNTAX_ERROR_TYPE, parse("@type {!SyntaxError}*/").getType());
234 }
235
236 public void testParseUndefinedType1() throws Exception {
237 assertTypeEquals(VOID_TYPE, parse("@type {undefined}*/").getType());
238 }
239
240 public void testParseUndefinedType2() throws Exception {
241 assertTypeEquals(VOID_TYPE, parse("@type {Undefined}*/").getType());
242 }
243
244 public void testParseUndefinedType3() throws Exception {
245 assertTypeEquals(VOID_TYPE, parse("@type {void}*/").getType());
246 }
247
248 public void testParseTemplatizedType1() throws Exception {
249 JSDocInfo info = parse("@type !Array.<number> */");
250 assertTypeEquals(
251 createTemplatizedType(ARRAY_TYPE, NUMBER_TYPE), info.getType());
252 }
253
254 public void testParseTemplatizedType2() throws Exception {
255 JSDocInfo info = parse("@type {!Array.<number>}*/");
256 assertTypeEquals(
257 createTemplatizedType(ARRAY_TYPE, NUMBER_TYPE), info.getType());
258 }
259
260 public void testParseTemplatizedType3() throws Exception {
261 JSDocInfo info = parse("@type !Array.<(number,null)>*/");
262 assertTypeEquals(
263 createTemplatizedType(ARRAY_TYPE,
264 createUnionType(NUMBER_TYPE, NULL_TYPE)),
265 info.getType());
266 }
267
268 public void testParseTemplatizedType4() throws Exception {
269 JSDocInfo info = parse("@type {!Array.<(number|null)>}*/");
270 assertTypeEquals(
271 createTemplatizedType(ARRAY_TYPE,
272 createUnionType(NUMBER_TYPE, NULL_TYPE)),
273 info.getType());
274 }
275
276 public void testParseTemplatizedType5() throws Exception {
277 JSDocInfo info = parse("@type {!Array.<Array.<(number|null)>>}*/");
278 assertTypeEquals(
279 createTemplatizedType(ARRAY_TYPE,
280 createUnionType(NULL_TYPE,
281 createTemplatizedType(ARRAY_TYPE,
282 createUnionType(NUMBER_TYPE, NULL_TYPE)))),
283 info.getType());
284 }
285
286 public void testParseTemplatizedType6() throws Exception {
287 JSDocInfo info = parse("@type {!Array.<!Array.<(number|null)>>}*/");
288 assertTypeEquals(
289 createTemplatizedType(ARRAY_TYPE,
290 createTemplatizedType(ARRAY_TYPE,
291 createUnionType(NUMBER_TYPE, NULL_TYPE))),
292 info.getType());
293 }
294
295 public void testParseTemplatizedType7() throws Exception {
296 JSDocInfo info = parse("@type {!Array.<function():Date>}*/");
297 assertTypeEquals(
298 createTemplatizedType(ARRAY_TYPE,
299 registry.createFunctionType(
300 createUnionType(DATE_TYPE, NULL_TYPE))),
301 info.getType());
302 }
303
304 public void testParseTemplatizedType8() throws Exception {
305 JSDocInfo info = parse("@type {!Array.<function():!Date>}*/");
306 assertTypeEquals(
307 createTemplatizedType(ARRAY_TYPE,
308 registry.createFunctionType(DATE_TYPE)),
309 info.getType());
310 }
311
312 public void testParseTemplatizedType9() throws Exception {
313 JSDocInfo info = parse("@type {!Array.<Date|number>}*/");
314 assertTypeEquals(
315 createTemplatizedType(ARRAY_TYPE,
316 createUnionType(DATE_TYPE, NUMBER_TYPE, NULL_TYPE)),
317 info.getType());
318 }
319
320 public void testParseTemplatizedType10() throws Exception {
321 JSDocInfo info = parse("@type {!Array.<Date|number|boolean>}*/");
322 assertTypeEquals(
323 createTemplatizedType(ARRAY_TYPE,
324 createUnionType(DATE_TYPE, NUMBER_TYPE, BOOLEAN_TYPE, NULL_TYPE)),
325 info.getType());
326 }
327
328 public void testParseTemplatizedType11() throws Exception {
329 JSDocInfo info = parse("@type {!Object.<number>}*/");
330 assertTypeEquals(
331 createTemplatizedType(
332 OBJECT_TYPE, ImmutableList.of(UNKNOWN_TYPE, NUMBER_TYPE)),
333 info.getType());
334 assertTemplatizedTypeEquals(
335 registry.getObjectElementKey(), NUMBER_TYPE, info.getType());
336 }
337
338 public void testParseTemplatizedType12() throws Exception {
339 JSDocInfo info = parse("@type {!Object.<string,number>}*/");
340 assertTypeEquals(
341 createTemplatizedType(
342 OBJECT_TYPE, ImmutableList.of(STRING_TYPE, NUMBER_TYPE)),
343 info.getType());
344 assertTemplatizedTypeEquals(
345 registry.getObjectElementKey(), NUMBER_TYPE, info.getType());
346 assertTemplatizedTypeEquals(
347 registry.getObjectIndexKey(), STRING_TYPE, info.getType());
348 }
349
350 public void testParseTemplatizedType13() throws Exception {
351 JSDocInfo info = parse("@type !Array.<?> */");
352 assertTypeEquals(
353 createTemplatizedType(ARRAY_TYPE, UNKNOWN_TYPE), info.getType());
354 }
355
356 public void testParseUnionType1() throws Exception {
357 JSDocInfo info = parse("@type {(boolean,null)}*/");
358 assertTypeEquals(createUnionType(BOOLEAN_TYPE, NULL_TYPE), info.getType());
359 }
360
361 public void testParseUnionType2() throws Exception {
362 JSDocInfo info = parse("@type {boolean|null}*/");
363 assertTypeEquals(createUnionType(BOOLEAN_TYPE, NULL_TYPE), info.getType());
364 }
365
366 public void testParseUnionType3() throws Exception {
367 JSDocInfo info = parse("@type {boolean||null}*/");
368 assertTypeEquals(createUnionType(BOOLEAN_TYPE, NULL_TYPE), info.getType());
369 }
370
371 public void testParseUnionType4() throws Exception {
372 JSDocInfo info = parse("@type {(Array.<boolean>,null)}*/");
373 assertTypeEquals(createUnionType(
374 createTemplatizedType(
375 ARRAY_TYPE, BOOLEAN_TYPE), NULL_TYPE), info.getType());
376 }
377
378 public void testParseUnionType5() throws Exception {
379 JSDocInfo info = parse("@type {(null, Array.<boolean>)}*/");
380 assertTypeEquals(createUnionType(
381 createTemplatizedType(
382 ARRAY_TYPE, BOOLEAN_TYPE), NULL_TYPE), info.getType());
383 }
384
385 public void testParseUnionType6() throws Exception {
386 JSDocInfo info = parse("@type {Array.<boolean>|null}*/");
387 assertTypeEquals(createUnionType(
388 createTemplatizedType(
389 ARRAY_TYPE, BOOLEAN_TYPE), NULL_TYPE), info.getType());
390 }
391
392 public void testParseUnionType7() throws Exception {
393 JSDocInfo info = parse("@type {null|Array.<boolean>}*/");
394 assertTypeEquals(createUnionType(
395 createTemplatizedType(
396 ARRAY_TYPE, BOOLEAN_TYPE), NULL_TYPE), info.getType());
397 }
398
399 public void testParseUnionType8() throws Exception {
400 JSDocInfo info = parse("@type {null||Array.<boolean>}*/");
401 assertTypeEquals(createUnionType(
402 createTemplatizedType(
403 ARRAY_TYPE, BOOLEAN_TYPE), NULL_TYPE), info.getType());
404 }
405
406 public void testParseUnionType9() throws Exception {
407 JSDocInfo info = parse("@type {Array.<boolean>||null}*/");
408 assertTypeEquals(createUnionType(
409 createTemplatizedType(
410 ARRAY_TYPE, BOOLEAN_TYPE), NULL_TYPE), info.getType());
411 }
412
413 public void testParseUnionType10() throws Exception {
414 parse("@type {string|}*/",
415 "Bad type annotation. type not recognized due to syntax error");
416 }
417
418 public void testParseUnionType11() throws Exception {
419 parse("@type {(string,)}*/",
420 "Bad type annotation. type not recognized due to syntax error");
421 }
422
423 public void testParseUnionType12() throws Exception {
424 parse("@type {()}*/",
425 "Bad type annotation. type not recognized due to syntax error");
426 }
427
428 public void testParseUnionType13() throws Exception {
429 testParseType(
430 "(function(this:Date),function(this:String):number)",
431 "Function");
432 }
433
434 public void testParseUnionType14() throws Exception {
435 testParseType(
436 "(function(...[function(number):boolean]):number)|" +
437 "function(this:String, string):number",
438 "Function");
439 }
440
441 public void testParseUnionType15() throws Exception {
442 testParseType("*|number", "*");
443 }
444
445 public void testParseUnionType16() throws Exception {
446 testParseType("number|*", "*");
447 }
448
449 public void testParseUnionType17() throws Exception {
450 testParseType("string|number|*", "*");
451 }
452
453 public void testParseUnionType18() throws Exception {
454 testParseType("(string,*,number)", "*");
455 }
456
457 public void testParseUnionTypeError1() throws Exception {
458 parse("@type {(string,|number)} */",
459 "Bad type annotation. type not recognized due to syntax error");
460 }
461
462 public void testParseUnknownType1() throws Exception {
463 testParseType("?");
464 }
465
466 public void testParseUnknownType2() throws Exception {
467 testParseType("(?|number)", "?");
468 }
469
470 public void testParseUnknownType3() throws Exception {
471 testParseType("(number|?)", "?");
472 }
473
474 public void testParseFunctionalType1() throws Exception {
475 testParseType("function (): number");
476 }
477
478 public void testParseFunctionalType2() throws Exception {
479 testParseType("function (number, string): boolean");
480 }
481
482 public void testParseFunctionalType3() throws Exception {
483 testParseType(
484 "function(this:Array)", "function (this:Array): ?");
485 }
486
487 public void testParseFunctionalType4() throws Exception {
488 testParseType("function (...[number]): boolean");
489 }
490
491 public void testParseFunctionalType5() throws Exception {
492 testParseType("function (number, ...[string]): boolean");
493 }
494
495 public void testParseFunctionalType6() throws Exception {
496 testParseType(
497 "function (this:Date, number): (boolean|number|string)");
498 }
499
500 public void testParseFunctionalType7() throws Exception {
501 testParseType("function()", "function (): ?");
502 }
503
504 public void testParseFunctionalType8() throws Exception {
505 testParseType(
506 "function(this:Array,...[boolean])",
507 "function (this:Array, ...[boolean]): ?");
508 }
509
510 public void testParseFunctionalType9() throws Exception {
511 testParseType(
512 "function(this:Array,!Date,...[boolean?])",
513 "function (this:Array, Date, ...[(boolean|null)]): ?");
514 }
515
516 public void testParseFunctionalType10() throws Exception {
517 testParseType(
518 "function(...[Object?]):boolean?",
519 "function (...[(Object|null)]): (boolean|null)");
520 }
521
522 public void testParseFunctionalType11() throws Exception {
523 testParseType(
524 "function(...[[number]]):[number?]",
525 "function (...[Array]): Array");
526 }
527
528 public void testParseFunctionalType12() throws Exception {
529 testParseType(
530 "function(...)",
531 "function (...[?]): ?");
532 }
533
534 public void testParseFunctionalType13() throws Exception {
535 testParseType(
536 "function(...): void",
537 "function (...[?]): undefined");
538 }
539
540 public void testParseFunctionalType14() throws Exception {
541 testParseType("function (*, string, number): boolean");
542 }
543
544 public void testParseFunctionalType15() throws Exception {
545 testParseType("function (?, string): boolean");
546 }
547
548 public void testParseFunctionalType16() throws Exception {
549 testParseType("function (string, ?): ?");
550 }
551
552 public void testParseFunctionalType17() throws Exception {
553 testParseType("(function (?): ?|number)");
554 }
555
556 public void testParseFunctionalType18() throws Exception {
557 testParseType("function (?): (?|number)", "function (?): ?");
558 }
559
560 public void testParseFunctionalType19() throws Exception {
561 testParseType(
562 "function(...[?]): void",
563 "function (...[?]): undefined");
564 }
565
566 public void testStructuralConstructor() throws Exception {
567 JSType type = testParseType(
568 "function (new:Object)", "function (new:Object): ?");
569 assertTrue(type.isConstructor());
570 assertFalse(type.isNominalConstructor());
571 }
572
573 public void testNominalConstructor() throws Exception {
574 ObjectType type = testParseType("Array", "(Array|null)").dereference();
575 assertTrue(type.getConstructor().isNominalConstructor());
576 }
577
578 public void testBug1419535() throws Exception {
579 parse("@type {function(Object, string, *)?} */");
580 parse("@type {function(Object, string, *)|null} */");
581 }
582
583 public void testIssue477() throws Exception {
584 parse("@type function */",
585 "Bad type annotation. missing opening (");
586 }
587
588 public void testMalformedThisAnnotation() throws Exception {
589 parse("@this */",
590 "Bad type annotation. type not recognized due to syntax error");
591 }
592
593 public void testParseFunctionalTypeError1() throws Exception {
594 parse("@type {function number):string}*/",
595 "Bad type annotation. missing opening (");
596 }
597
598 public void testParseFunctionalTypeError2() throws Exception {
599 parse("@type {function( number}*/",
600 "Bad type annotation. missing closing )");
601 }
602
603 public void testParseFunctionalTypeError3() throws Exception {
604 parse("@type {function(...[number], string)}*/",
605 "Bad type annotation. variable length argument must be last");
606 }
607
608 public void testParseFunctionalTypeError4() throws Exception {
609 parse("@type {function(string, ...[number], boolean):string}*/",
610 "Bad type annotation. variable length argument must be last");
611 }
612
613 public void testParseFunctionalTypeError5() throws Exception {
614 parse("@type {function (thi:Array)}*/",
615 "Bad type annotation. missing closing )");
616 }
617
618 public void testParseFunctionalTypeError6() throws Exception {
619 resolve(parse("@type {function (this:number)}*/").getType(),
620 "this type must be an object type");
621 }
622
623 public void testParseFunctionalTypeError7() throws Exception {
624 parse("@type {function(...[number)}*/",
625 "Bad type annotation. missing closing ]");
626 }
627
628 public void testParseFunctionalTypeError8() throws Exception {
629 parse("@type {function(...number])}*/",
630 "Bad type annotation. missing opening [");
631 }
632
633 public void testParseFunctionalTypeError9() throws Exception {
634 parse("@type {function (new:Array, this:Object)} */",
635 "Bad type annotation. missing closing )");
636 }
637
638 public void testParseFunctionalTypeError10() throws Exception {
639 parse("@type {function (this:Array, new:Object)} */",
640 "Bad type annotation. missing closing )");
641 }
642
643 public void testParseFunctionalTypeError11() throws Exception {
644 parse("@type {function (Array, new:Object)} */",
645 "Bad type annotation. missing closing )");
646 }
647
648 public void testParseFunctionalTypeError12() throws Exception {
649 resolve(parse("@type {function (new:number)}*/").getType(),
650 "constructed type must be an object type");
651 }
652
653 public void testParseArrayType1() throws Exception {
654 testParseType("[number]", "Array");
655 }
656
657 public void testParseArrayType2() throws Exception {
658 testParseType("[(number,boolean,[Object?])]", "Array");
659 }
660
661 public void testParseArrayType3() throws Exception {
662 testParseType("[[number],[string]]?", "(Array|null)");
663 }
664
665 public void testParseArrayTypeError1() throws Exception {
666 parse("@type {[number}*/",
667 "Bad type annotation. missing closing ]");
668 }
669
670 public void testParseArrayTypeError2() throws Exception {
671 parse("@type {number]}*/",
672 "Bad type annotation. expected closing }");
673 }
674
675 public void testParseArrayTypeError3() throws Exception {
676 parse("@type {[(number,boolean,Object?])]}*/",
677 "Bad type annotation. missing closing )");
678 }
679
680 public void testParseArrayTypeError4() throws Exception {
681 parse("@type {(number,boolean,[Object?)]}*/",
682 "Bad type annotation. missing closing ]");
683 }
684
685 private JSType testParseType(String type) throws Exception {
686 return testParseType(type, type);
687 }
688
689 private JSType testParseType(
690 String type, String typeExpected) throws Exception {
691 JSDocInfo info = parse("@type {" + type + "}*/");
692
693 assertNotNull(info);
694 assertTrue(info.hasType());
695
696 JSType actual = resolve(info.getType());
697 assertEquals(typeExpected, actual.toString());
698 return actual;
699 }
700
701 public void testParseNullableModifiers1() throws Exception {
702 JSDocInfo info = parse("@type {string?}*/");
703 assertTypeEquals(createNullableType(STRING_TYPE), info.getType());
704 }
705
706 public void testParseNullableModifiers2() throws Exception {
707 JSDocInfo info = parse("@type {!Array.<string?>}*/");
708 assertTypeEquals(
709 createTemplatizedType(
710 ARRAY_TYPE, createUnionType(STRING_TYPE, NULL_TYPE)),
711 info.getType());
712 }
713
714 public void testParseNullableModifiers3() throws Exception {
715 JSDocInfo info = parse("@type {Array.<boolean>?}*/");
716 assertTypeEquals(
717 createNullableType(createTemplatizedType(ARRAY_TYPE, BOOLEAN_TYPE)),
718 info.getType());
719 }
720
721 public void testParseNullableModifiers4() throws Exception {
722 JSDocInfo info = parse("@type {(string,boolean)?}*/");
723 assertTypeEquals(
724 createNullableType(createUnionType(STRING_TYPE, BOOLEAN_TYPE)),
725 info.getType());
726 }
727
728 public void testParseNullableModifiers5() throws Exception {
729 JSDocInfo info = parse("@type {(string?,boolean)}*/");
730 assertTypeEquals(
731 createUnionType(createNullableType(STRING_TYPE), BOOLEAN_TYPE),
732 info.getType());
733 }
734
735 public void testParseNullableModifiers6() throws Exception {
736 JSDocInfo info = parse("@type {(string,boolean?)}*/");
737 assertTypeEquals(
738 createUnionType(STRING_TYPE, createNullableType(BOOLEAN_TYPE)),
739 info.getType());
740 }
741
742 public void testParseNullableModifiers7() throws Exception {
743 JSDocInfo info = parse("@type {string?|boolean}*/");
744 assertTypeEquals(
745 createUnionType(createNullableType(STRING_TYPE), BOOLEAN_TYPE),
746 info.getType());
747 }
748
749 public void testParseNullableModifiers8() throws Exception {
750 JSDocInfo info = parse("@type {string|boolean?}*/");
751 assertTypeEquals(
752 createUnionType(STRING_TYPE, createNullableType(BOOLEAN_TYPE)),
753 info.getType());
754 }
755
756 public void testParseNullableModifiers9() throws Exception {
757 JSDocInfo info = parse("@type {foo.Hello.World?}*/");
758 assertTypeEquals(
759 createNullableType(
760 registry.createNamedType(
761 "foo.Hello.World", null, -1, -1)),
762 info.getType());
763 }
764
765 public void testParseOptionalModifier() throws Exception {
766 JSDocInfo info = parse("@type {function(number=)}*/");
767 assertTypeEquals(
768 registry.createFunctionType(
769 UNKNOWN_TYPE, registry.createOptionalParameters(NUMBER_TYPE)),
770 info.getType());
771 }
772
773 public void testParseNewline1() throws Exception {
774 JSDocInfo info = parse("@type {string\n* }\n*/");
775 assertTypeEquals(STRING_TYPE, info.getType());
776 }
777
778 public void testParseNewline2() throws Exception {
779 JSDocInfo info = parse("@type !Array.<\n* number\n* > */");
780 assertTypeEquals(
781 createTemplatizedType(ARRAY_TYPE, NUMBER_TYPE), info.getType());
782 }
783
784 public void testParseNewline3() throws Exception {
785 JSDocInfo info = parse("@type !Array.<(number,\n* null)>*/");
786 assertTypeEquals(
787 createTemplatizedType(
788 ARRAY_TYPE, createUnionType(NUMBER_TYPE, NULL_TYPE)),
789 info.getType());
790 }
791
792 public void testParseNewline4() throws Exception {
793 JSDocInfo info = parse("@type !Array.<(number|\n* null)>*/");
794 assertTypeEquals(
795 createTemplatizedType(
796 ARRAY_TYPE, createUnionType(NUMBER_TYPE, NULL_TYPE)),
797 info.getType());
798 }
799
800 public void testParseNewline5() throws Exception {
801 JSDocInfo info = parse("@type !Array.<function(\n* )\n* :\n* Date>*/");
802 assertTypeEquals(
803 createTemplatizedType(ARRAY_TYPE,
804 registry.createFunctionType(
805 createUnionType(DATE_TYPE, NULL_TYPE))),
806 info.getType());
807 }
808
809 public void testParseReturnType1() throws Exception {
810 JSDocInfo info =
811 parse("@return {null|string|Array.<boolean>}*/");
812 assertTypeEquals(
813 createUnionType(createTemplatizedType(ARRAY_TYPE, BOOLEAN_TYPE),
814 NULL_TYPE, STRING_TYPE),
815 info.getReturnType());
816 }
817
818 public void testParseReturnType2() throws Exception {
819 JSDocInfo info =
820 parse("@returns {null|(string,Array.<boolean>)}*/");
821 assertTypeEquals(
822 createUnionType(createTemplatizedType(ARRAY_TYPE, BOOLEAN_TYPE),
823 NULL_TYPE, STRING_TYPE),
824 info.getReturnType());
825 }
826
827 public void testParseReturnType3() throws Exception {
828 JSDocInfo info =
829 parse("@return {((null||Array.<boolean>,string),boolean)}*/");
830 assertTypeEquals(
831 createUnionType(createTemplatizedType(ARRAY_TYPE, BOOLEAN_TYPE),
832 NULL_TYPE, STRING_TYPE, BOOLEAN_TYPE),
833 info.getReturnType());
834 }
835
836 public void testParseThisType1() throws Exception {
837 JSDocInfo info =
838 parse("@this {goog.foo.Bar}*/");
839 assertTypeEquals(
840 registry.createNamedType("goog.foo.Bar", null, -1, -1),
841 info.getThisType());
842 }
843
844 public void testParseThisType2() throws Exception {
845 JSDocInfo info =
846 parse("@this goog.foo.Bar*/");
847 assertTypeEquals(
848 registry.createNamedType("goog.foo.Bar", null, -1, -1),
849 info.getThisType());
850 }
851
852 public void testParseThisType3() throws Exception {
853 parse("@type {number}\n@this goog.foo.Bar*/",
854 "Bad type annotation. type annotation incompatible " +
855 "with other annotations");
856 }
857
858 public void testParseThisType4() throws Exception {
859 resolve(parse("@this number*/").getThisType(),
860 "@this must specify an object type");
861 }
862
863 public void testParseThisType5() throws Exception {
864 parse("@this {Date|Error}*/");
865 }
866
867 public void testParseThisType6() throws Exception {
868 resolve(parse("@this {Date|number}*/").getThisType(),
869 "@this must specify an object type");
870 }
871
872 public void testParseParam1() throws Exception {
873 JSDocInfo info = parse("@param {number} index*/");
874 assertEquals(1, info.getParameterCount());
875 assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
876 }
877
878 public void testParseParam2() throws Exception {
879 JSDocInfo info = parse("@param index*/");
880 assertEquals(1, info.getParameterCount());
881 assertEquals(null, info.getParameterType("index"));
882 }
883
884 public void testParseParam3() throws Exception {
885 JSDocInfo info = parse("@param {number} index useful comments*/");
886 assertEquals(1, info.getParameterCount());
887 assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
888 }
889
890 public void testParseParam4() throws Exception {
891 JSDocInfo info = parse("@param index useful comments*/");
892 assertEquals(1, info.getParameterCount());
893 assertEquals(null, info.getParameterType("index"));
894 }
895
896 public void testParseParam5() throws Exception {
897 // Test for multi-line @param.
898 JSDocInfo info = parse("@param {number} \n index */");
899 assertEquals(1, info.getParameterCount());
900 assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
901 }
902
903 public void testParseParam6() throws Exception {
904 // Test for multi-line @param.
905 JSDocInfo info = parse("@param {number} \n * index */");
906 assertEquals(1, info.getParameterCount());
907 assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
908 }
909
910 public void testParseParam7() throws Exception {
911 // Optional @param
912 JSDocInfo info = parse("@param {number=} index */");
913 assertTypeEquals(
914 registry.createOptionalType(NUMBER_TYPE),
915 info.getParameterType("index"));
916 }
917
918 public void testParseParam8() throws Exception {
919 // Var args @param
920 JSDocInfo info = parse("@param {...number} index */");
921 assertTypeEquals(
922 registry.createOptionalType(NUMBER_TYPE),
923 info.getParameterType("index"));
924 }
925
926 public void testParseParam9() throws Exception {
927 parse("@param {...number=} index */",
928 "Bad type annotation. expected closing }",
929 "Bad type annotation. expecting a variable name in a @param tag");
930 }
931
932 public void testParseParam10() throws Exception {
933 parse("@param {...number index */",
934 "Bad type annotation. expected closing }");
935 }
936
937 public void testParseParam11() throws Exception {
938 parse("@param {number= index */",
939 "Bad type annotation. expected closing }");
940 }
941
942 public void testParseParam12() throws Exception {
943 JSDocInfo info = parse("@param {...number|string} index */");
944 assertTypeEquals(
945 registry.createOptionalType(
946 registry.createUnionType(STRING_TYPE, NUMBER_TYPE)),
947 info.getParameterType("index"));
948 }
949
950 public void testParseParam13() throws Exception {
951 JSDocInfo info = parse("@param {...(number|string)} index */");
952 assertTypeEquals(
953 registry.createOptionalType(
954 registry.createUnionType(STRING_TYPE, NUMBER_TYPE)),
955 info.getParameterType("index"));
956 }
957
958 public void testParseParam14() throws Exception {
959 JSDocInfo info = parse("@param {string} [index] */");
960 assertEquals(1, info.getParameterCount());
961 assertTypeEquals(
962 registry.createOptionalType(STRING_TYPE),
963 info.getParameterType("index"));
964 }
965
966 public void testParseParam15() throws Exception {
967 JSDocInfo info = parse("@param {string} [index */",
968 "Bad type annotation. missing closing ]");
969 assertEquals(1, info.getParameterCount());
970 assertTypeEquals(STRING_TYPE, info.getParameterType("index"));
971 }
972
973 public void testParseParam16() throws Exception {
974 JSDocInfo info = parse("@param {string} index] */");
975 assertEquals(1, info.getParameterCount());
976 assertTypeEquals(STRING_TYPE, info.getParameterType("index"));
977 }
978
979 public void testParseParam17() throws Exception {
980 JSDocInfo info = parse("@param {string=} [index] */");
981 assertEquals(1, info.getParameterCount());
982 assertTypeEquals(
983 registry.createOptionalType(STRING_TYPE),
984 info.getParameterType("index"));
985 }
986
987 public void testParseParam18() throws Exception {
988 JSDocInfo info = parse("@param {...string} [index] */");
989 assertEquals(1, info.getParameterCount());
990 assertTypeEquals(
991 registry.createOptionalType(STRING_TYPE),
992 info.getParameterType("index"));
993 }
994
995 public void testParseParam19() throws Exception {
996 JSDocInfo info = parse("@param {...} [index] */");
997 assertEquals(1, info.getParameterCount());
998 assertTypeEquals(
999 registry.createOptionalType(UNKNOWN_TYPE),
1000 info.getParameterType("index"));
1001 assertTrue(info.getParameterType("index").isVarArgs());
1002 }
1003
1004 public void testParseParam20() throws Exception {
1005 JSDocInfo info = parse("@param {?=} index */");
1006 assertEquals(1, info.getParameterCount());
1007 assertTypeEquals(
1008 UNKNOWN_TYPE, info.getParameterType("index"));
1009 }
1010
1011 public void testParseParam21() throws Exception {
1012 JSDocInfo info = parse("@param {...?} index */");
1013 assertEquals(1, info.getParameterCount());
1014 assertTypeEquals(
1015 UNKNOWN_TYPE, info.getParameterType("index"));
1016 assertTrue(info.getParameterType("index").isVarArgs());
1017 }
1018
1019 public void testParseThrows1() throws Exception {
1020 JSDocInfo info = parse("@throws {number} Some number */");
1021 assertEquals(1, info.getThrownTypes().size());
1022 assertTypeEquals(NUMBER_TYPE, info.getThrownTypes().get(0));
1023 }
1024
1025 public void testParseThrows2() throws Exception {
1026 JSDocInfo info = parse("@throws {number} Some number\n "
1027 + "*@throws {String} A string */");
1028 assertEquals(2, info.getThrownTypes().size());
1029 assertTypeEquals(NUMBER_TYPE, info.getThrownTypes().get(0));
1030 }
1031
1032 public void testParseRecordType1() throws Exception {
1033 parseFull("/** @param {{x}} n\n*/");
1034 }
1035
1036 public void testParseRecordType2() throws Exception {
1037 parseFull("/** @param {{z, y}} n\n*/");
1038 }
1039
1040 public void testParseRecordType3() throws Exception {
1041 parseFull("/** @param {{z, y, x, q, hello, thisisatest}} n\n*/");
1042 }
1043
1044 public void testParseRecordType4() throws Exception {
1045 parseFull("/** @param {{a, 'a', 'hello', 2, this, do, while, for}} n\n*/");
1046 }
1047
1048 public void testParseRecordType5() throws Exception {
1049 parseFull("/** @param {{x : hello}} n\n*/");
1050 }
1051
1052 public void testParseRecordType6() throws Exception {
1053 parseFull("/** @param {{'x' : hello}} n\n*/");
1054 }
1055
1056 public void testParseRecordType7() throws Exception {
1057 parseFull("/** @param {{'x' : !hello}} n\n*/");
1058 }
1059
1060 public void testParseRecordType8() throws Exception {
1061 parseFull("/** @param {{'x' : !hello, y : bar}} n\n*/");
1062 }
1063
1064 public void testParseRecordType9() throws Exception {
1065 parseFull("/** @param {{'x' : !hello, y : {z : bar, 3 : meh}}} n\n*/");
1066 }
1067
1068 public void testParseRecordType10() throws Exception {
1069 parseFull("/** @param {{__proto__ : moo}} n\n*/");
1070 }
1071
1072 public void testParseRecordType11() throws Exception {
1073 parseFull("/** @param {{a : b} n\n*/",
1074 "Bad type annotation. expected closing }");
1075 }
1076
1077 public void testParseRecordType12() throws Exception {
1078 parseFull("/** @param {{!hello : hey}} n\n*/",
1079 "Bad type annotation. type not recognized due to syntax error");
1080 }
1081
1082 public void testParseRecordType13() throws Exception {
1083 parseFull("/** @param {{x}|number} n\n*/");
1084 }
1085
1086 public void testParseRecordType14() throws Exception {
1087 parseFull("/** @param {{x : y}|number} n\n*/");
1088 }
1089
1090 public void testParseRecordType15() throws Exception {
1091 parseFull("/** @param {{'x' : y}|number} n\n*/");
1092 }
1093
1094 public void testParseRecordType16() throws Exception {
1095 parseFull("/** @param {{x, y}|number} n\n*/");
1096 }
1097
1098 public void testParseRecordType17() throws Exception {
1099 parseFull("/** @param {{x : hello, 'y'}|number} n\n*/");
1100 }
1101
1102 public void testParseRecordType18() throws Exception {
1103 parseFull("/** @param {number|{x : hello, 'y'}} n\n*/");
1104 }
1105
1106 public void testParseRecordType19() throws Exception {
1107 parseFull("/** @param {?{x : hello, 'y'}} n\n*/");
1108 }
1109
1110 public void testParseRecordType20() throws Exception {
1111 parseFull("/** @param {!{x : hello, 'y'}} n\n*/");
1112 }
1113
1114 public void testParseRecordType21() throws Exception {
1115 parseFull("/** @param {{x : hello, 'y'}|boolean} n\n*/");
1116 }
1117
1118 public void testParseRecordType22() throws Exception {
1119 parseFull("/** @param {{x : hello, 'y'}|function()} n\n*/");
1120 }
1121
1122 public void testParseRecordType23() throws Exception {
1123 parseFull("/** @param {{x : function(), 'y'}|function()} n\n*/");
1124 }
1125
1126 public void testParseParamError1() throws Exception {
1127 parseFull("/** @param\n*/",
1128 "Bad type annotation. expecting a variable name in a @param tag");
1129 }
1130
1131 public void testParseParamError2() throws Exception {
1132 parseFull("/** @param {Number}*/",
1133 "Bad type annotation. expecting a variable name in a @param tag");
1134 }
1135
1136 public void testParseParamError3() throws Exception {
1137 parseFull("/** @param {Number}\n*/",
1138 "Bad type annotation. expecting a variable name in a @param tag");
1139 }
1140
1141 public void testParseParamError4() throws Exception {
1142 parseFull("/** @param {Number}\n* * num */",
1143 "Bad type annotation. expecting a variable name in a @param tag");
1144 }
1145
1146 public void testParseParamError5() throws Exception {
1147 parse("@param {number} x \n * @param {string} x */",
1148 "Bad type annotation. duplicate variable name \"x\"");
1149 }
1150
1151 public void testParseExtends1() throws Exception {
1152 assertTypeEquals(STRING_OBJECT_TYPE,
1153 parse("@extends String*/").getBaseType());
1154 }
1155
1156 public void testParseExtends2() throws Exception {
1157 JSDocInfo info = parse("@extends com.google.Foo.Bar.Hello.World*/");
1158 assertTypeEquals(
1159 registry.createNamedType(
1160 "com.google.Foo.Bar.Hello.World", null, -1, -1),
1161 info.getBaseType());
1162 }
1163
1164 public void testParseExtendsGenerics() throws Exception {
1165 JSDocInfo info =
1166 parse("@extends com.google.Foo.Bar.Hello.World.<Boolean,number>*/");
1167 assertTypeEquals(
1168 registry.createNamedType(
1169 "com.google.Foo.Bar.Hello.World", null, -1, -1),
1170 info.getBaseType());
1171 }
1172
1173 public void testParseImplementsGenerics() throws Exception {
1174 // For types that are not templatized, <> annotations are ignored.
1175 List<JSTypeExpression> interfaces =
1176 parse("@implements {SomeInterface.<*>} */")
1177 .getImplementedInterfaces();
1178 assertEquals(1, interfaces.size());
1179 assertTypeEquals(registry.createNamedType("SomeInterface", null, -1, -1),
1180 interfaces.get(0));
1181 }
1182
1183 public void testParseExtends4() throws Exception {
1184 assertTypeEquals(STRING_OBJECT_TYPE,
1185 parse("@extends {String}*/").getBaseType());
1186 }
1187
1188 public void testParseExtends5() throws Exception {
1189 assertTypeEquals(STRING_OBJECT_TYPE,
1190 parse("@extends {String*/",
1191 "Bad type annotation. expected closing }").getBaseType());
1192 }
1193
1194 public void testParseExtends6() throws Exception {
1195 // Multi-line extends
1196 assertTypeEquals(STRING_OBJECT_TYPE,
1197 parse("@extends \n * {String}*/").getBaseType());
1198 }
1199
1200 public void testParseExtendsInvalidName() throws Exception {
1201 // This looks bad, but for the time being it should be OK, as
1202 // we will not find a type with this name in the JS parsed tree.
1203 // If this is fixed in the future, change this test to check for a
1204 // warning/error message.
1205 assertTypeEquals(
1206 registry.createNamedType("some_++#%$%_UglyString", null, -1, -1),
1207 parse("@extends {some_++#%$%_UglyString} */").getBaseType());
1208 }
1209
1210 public void testParseExtendsNullable1() throws Exception {
1211 parse("@extends {Base?} */", "Bad type annotation. expected closing }");
1212 }
1213
1214 public void testParseExtendsNullable2() throws Exception {
1215 parse("@extends Base? */",
1216 "Bad type annotation. expected end of line or comment");
1217 }
1218
1219 public void testParseEnum1() throws Exception {
1220 assertTypeEquals(NUMBER_TYPE, parse("@enum*/").getEnumParameterType());
1221 }
1222
1223 public void testParseEnum2() throws Exception {
1224 assertTypeEquals(STRING_TYPE,
1225 parse("@enum {string}*/").getEnumParameterType());
1226 }
1227
1228 public void testParseEnum3() throws Exception {
1229 assertTypeEquals(STRING_TYPE,
1230 parse("@enum string*/").getEnumParameterType());
1231 }
1232
1233 public void testParseDesc1() throws Exception {
1234 assertEquals("hello world!",
1235 parse("@desc hello world!*/").getDescription());
1236 }
1237
1238 public void testParseDesc2() throws Exception {
1239 assertEquals("hello world!",
1240 parse("@desc hello world!\n*/").getDescription());
1241 }
1242
1243 public void testParseDesc3() throws Exception {
1244 assertEquals("", parse("@desc*/").getDescription());
1245 }
1246
1247 public void testParseDesc4() throws Exception {
1248 assertEquals("", parse("@desc\n*/").getDescription());
1249 }
1250
1251 public void testParseDesc5() throws Exception {
1252 assertEquals("hello world!",
1253 parse("@desc hello\nworld!\n*/").getDescription());
1254 }
1255
1256 public void testParseDesc6() throws Exception {
1257 assertEquals("hello world!",
1258 parse("@desc hello\n* world!\n*/").getDescription());
1259 }
1260
1261 public void testParseDesc7() throws Exception {
1262 assertEquals("a b c", parse("@desc a\n\nb\nc*/").getDescription());
1263 }
1264
1265 public void testParseDesc8() throws Exception {
1266 assertEquals("a b c d",
1267 parse("@desc a\n *b\n\n *c\n\nd*/").getDescription());
1268 }
1269
1270 public void testParseDesc9() throws Exception {
1271 String comment = "@desc\n.\n,\n{\n)\n}\n|\n.<\n>\n<\n?\n~\n+\n-\n;\n:\n*/";
1272
1273 assertEquals(". , { ) } | .< > < ? ~ + - ; :",
1274 parse(comment).getDescription());
1275 }
1276
1277 public void testParseDesc10() throws Exception {
1278 String comment = "@desc\n?\n?\n?\n?*/";
1279
1280 assertEquals("? ? ? ?", parse(comment).getDescription());
1281 }
1282
1283 public void testParseDesc11() throws Exception {
1284 String comment = "@desc :[]*/";
1285
1286 assertEquals(":[]", parse(comment).getDescription());
1287 }
1288
1289 public void testParseDesc12() throws Exception {
1290 String comment = "@desc\n:\n[\n]\n...*/";
1291
1292 assertEquals(": [ ] ...", parse(comment).getDescription());
1293 }
1294
1295 public void testParseMeaning1() throws Exception {
1296 assertEquals("tigers",
1297 parse("@meaning tigers */").getMeaning());
1298 }
1299
1300 public void testParseMeaning2() throws Exception {
1301 assertEquals("tigers and lions and bears",
1302 parse("@meaning tigers\n * and lions\n * and bears */").getMeaning());
1303 }
1304
1305 public void testParseMeaning3() throws Exception {
1306 JSDocInfo info =
1307 parse("@meaning tigers\n * and lions\n * @desc and bears */");
1308 assertEquals("tigers and lions", info.getMeaning());
1309 assertEquals("and bears", info.getDescription());
1310 }
1311
1312 public void testParseMeaning4() throws Exception {
1313 parse("@meaning tigers\n * @meaning and lions */",
1314 "extra @meaning tag");
1315 }
1316
1317 public void testParseLends1() throws Exception {
1318 JSDocInfo info = parse("@lends {name} */");
1319 assertEquals("name", info.getLendsName());
1320 }
1321
1322 public void testParseLends2() throws Exception {
1323 JSDocInfo info = parse("@lends foo.bar */");
1324 assertEquals("foo.bar", info.getLendsName());
1325 }
1326
1327 public void testParseLends3() throws Exception {
1328 parse("@lends {name */", "Bad type annotation. expected closing }");
1329 }
1330
1331 public void testParseLends4() throws Exception {
1332 parse("@lends {} */",
1333 "Bad type annotation. missing object name in @lends tag");
1334 }
1335
1336 public void testParseLends5() throws Exception {
1337 parse("@lends } */",
1338 "Bad type annotation. missing object name in @lends tag");
1339 }
1340
1341 public void testParseLends6() throws Exception {
1342 parse("@lends {string} \n * @lends {string} */",
1343 "Bad type annotation. @lends tag incompatible with other annotations");
1344 }
1345
1346 public void testParseLends7() throws Exception {
1347 parse("@type {string} \n * @lends {string} */",
1348 "Bad type annotation. @lends tag incompatible with other annotations");
1349 }
1350
1351 public void testParsePreserve() throws Exception {
1352 Node node = new Node(1);
1353 this.fileLevelJsDocBuilder = node.getJsDocBuilderForNode();
1354 String comment = "@preserve Foo\nBar\n\nBaz*/";
1355 parse(comment);
1356 assertEquals(" Foo\nBar\n\nBaz", node.getJSDocInfo().getLicense());
1357 }
1358
1359 public void testParseLicense() throws Exception {
1360 Node node = new Node(1);
1361 this.fileLevelJsDocBuilder = node.getJsDocBuilderForNode();
1362 String comment = "@license Foo\nBar\n\nBaz*/";
1363 parse(comment);
1364 assertEquals(" Foo\nBar\n\nBaz", node.getJSDocInfo().getLicense());
1365 }
1366
1367 public void testParseLicenseAscii() throws Exception {
1368 Node node = new Node(1);
1369 this.fileLevelJsDocBuilder = node.getJsDocBuilderForNode();
1370 String comment = "@license Foo\n * Bar\n\n Baz*/";
1371 parse(comment);
1372 assertEquals(" Foo\n Bar\n\n Baz", node.getJSDocInfo().getLicense());
1373 }
1374
1375 public void testParseLicenseWithAnnotation() throws Exception {
1376 Node node = new Node(1);
1377 this.fileLevelJsDocBuilder = node.getJsDocBuilderForNode();
1378 String comment = "@license Foo \n * @author Charlie Brown */";
1379 parse(comment);
1380 assertEquals(" Foo \n @author Charlie Brown ",
1381 node.getJSDocInfo().getLicense());
1382 }
1383
1384 public void testParseDefine1() throws Exception {
1385 assertTypeEquals(STRING_TYPE,
1386 parse("@define {string}*/").getType());
1387 }
1388
1389 public void testParseDefine2() throws Exception {
1390 assertTypeEquals(STRING_TYPE,
1391 parse("@define {string*/",
1392 "Bad type annotation. expected closing }").getType());
1393 }
1394
1395 public void testParseDefine3() throws Exception {
1396 JSDocInfo info = parse("@define {boolean}*/");
1397 assertTrue(info.isConstant());
1398 assertTrue(info.isDefine());
1399 assertTypeEquals(BOOLEAN_TYPE, info.getType());
1400 }
1401
1402 public void testParseDefine4() throws Exception {
1403 assertTypeEquals(NUMBER_TYPE, parse("@define {number}*/").getType());
1404 }
1405
1406 public void testParseDefine5() throws Exception {
1407 assertTypeEquals(createUnionType(NUMBER_TYPE, BOOLEAN_TYPE),
1408 parse("@define {number|boolean}*/").getType());
1409 }
1410
1411 public void testParseDefineErrors1() throws Exception {
1412 parse("@enum {string}\n @define {string} */", "conflicting @define tag");
1413 }
1414
1415 public void testParseDefineErrors2() throws Exception {
1416 parse("@define {string}\n @enum {string} */",
1417 "Bad type annotation. " +
1418 "type annotation incompatible with other annotations");
1419 }
1420
1421 public void testParseDefineErrors3() throws Exception {
1422 parse("@const\n @define {string} */", "conflicting @define tag");
1423 }
1424
1425 public void testParseDefineErrors4() throws Exception {
1426 parse("@type string \n @define {string} */", "conflicting @define tag");
1427 }
1428
1429 public void testParseDefineErrors5() throws Exception {
1430 parse("@return {string}\n @define {string} */", "conflicting @define tag");
1431 }
1432
1433 public void testParseDefineErrors7() throws Exception {
1434 parse("@define {string}\n @const */", "conflicting @const tag");
1435 }
1436
1437 public void testParseDefineErrors8() throws Exception {
1438 parse("@define {string}\n @type string */",
1439 "Bad type annotation. " +
1440 "type annotation incompatible with other annotations");
1441 }
1442
1443 public void testParseNoCheck1() throws Exception {
1444 assertTrue(parse("@notypecheck*/").isNoTypeCheck());
1445 }
1446
1447 public void testParseNoCheck2() throws Exception {
1448 parse("@notypecheck\n@notypecheck*/", "extra @notypecheck tag");
1449 }
1450
1451 public void testParseOverride1() throws Exception {
1452 assertTrue(parse("@override*/").isOverride());
1453 }
1454
1455 public void testParseOverride2() throws Exception {
1456 parse("@override\n@override*/",
1457 "Bad type annotation. extra @override/@inheritDoc tag");
1458 }
1459
1460 public void testParseInheritDoc1() throws Exception {
1461 assertTrue(parse("@inheritDoc*/").isOverride());
1462 }
1463
1464 public void testParseInheritDoc2() throws Exception {
1465 parse("@override\n@inheritDoc*/",
1466 "Bad type annotation. extra @override/@inheritDoc tag");
1467 }
1468
1469 public void testParseInheritDoc3() throws Exception {
1470 parse("@inheritDoc\n@inheritDoc*/",
1471 "Bad type annotation. extra @override/@inheritDoc tag");
1472 }
1473
1474 public void testParseNoAlias1() throws Exception {
1475 assertTrue(parse("@noalias*/").isNoAlias());
1476 }
1477
1478 public void testParseNoAlias2() throws Exception {
1479 parse("@noalias\n@noalias*/", "extra @noalias tag");
1480 }
1481
1482 public void testParseDeprecated1() throws Exception {
1483 assertTrue(parse("@deprecated*/").isDeprecated());
1484 }
1485
1486 public void testParseDeprecated2() throws Exception {
1487 parse("@deprecated\n@deprecated*/", "extra @deprecated tag");
1488 }
1489
1490 public void testParseExport1() throws Exception {
1491 assertTrue(parse("@export*/").isExport());
1492 }
1493
1494 public void testParseExport2() throws Exception {
1495 parse("@export\n@export*/", "extra @export tag");
1496 }
1497
1498 public void testParseExpose1() throws Exception {
1499 assertTrue(parse("@expose*/").isExpose());
1500 }
1501
1502 public void testParseExpose2() throws Exception {
1503 parse("@expose\n@expose*/", "extra @expose tag");
1504 }
1505
1506 public void testParseExterns1() throws Exception {
1507 assertTrue(parseFileOverview("@externs*/").isExterns());
1508 }
1509
1510 public void testParseExterns2() throws Exception {
1511 parseFileOverview("@externs\n@externs*/", "extra @externs tag");
1512 }
1513
1514 public void testParseExterns3() throws Exception {
1515 assertNull(parse("@externs*/"));
1516 }
1517
1518 public void testParseJavaDispatch1() throws Exception {
1519 assertTrue(parse("@javadispatch*/").isJavaDispatch());
1520 }
1521
1522 public void testParseJavaDispatch2() throws Exception {
1523 parse("@javadispatch\n@javadispatch*/",
1524 "extra @javadispatch tag");
1525 }
1526
1527 public void testParseJavaDispatch3() throws Exception {
1528 assertNull(parseFileOverview("@javadispatch*/"));
1529 }
1530
1531 public void testParseNoCompile1() throws Exception {
1532 assertTrue(parseFileOverview("@nocompile*/").isNoCompile());
1533 }
1534
1535 public void testParseNoCompile2() throws Exception {
1536 parseFileOverview("@nocompile\n@nocompile*/", "extra @nocompile tag");
1537 }
1538
1539 public void testBugAnnotation() throws Exception {
1540 parse("@bug */");
1541 }
1542
1543 public void testDescriptionAnnotation() throws Exception {
1544 parse("@description */");
1545 }
1546
1547 public void testRegression1() throws Exception {
1548 String comment =
1549 " * @param {number} index the index of blah\n" +
1550 " * @return {boolean} whatever\n" +
1551 " * @private\n" +
1552 " */";
1553
1554 JSDocInfo info = parse(comment);
1555 assertEquals(1, info.getParameterCount());
1556 assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
1557 assertTypeEquals(BOOLEAN_TYPE, info.getReturnType());
1558 assertEquals(Visibility.PRIVATE, info.getVisibility());
1559 }
1560
1561 public void testRegression2() throws Exception {
1562 String comment =
1563 " * @return {boolean} whatever\n" +
1564 " * but important\n" +
1565 " *\n" +
1566 " * @param {number} index the index of blah\n" +
1567 " * some more comments here\n" +
1568 " * @param name the name of the guy\n" +
1569 " *\n" +
1570 " * @protected\n" +
1571 " */";
1572
1573 JSDocInfo info = parse(comment);
1574 assertEquals(2, info.getParameterCount());
1575 assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
1576 assertEquals(null, info.getParameterType("name"));
1577 assertTypeEquals(BOOLEAN_TYPE, info.getReturnType());
1578 assertEquals(Visibility.PROTECTED, info.getVisibility());
1579 }
1580
1581 public void testRegression3() throws Exception {
1582 String comment =
1583 " * @param mediaTag this specified whether the @media tag is ....\n" +
1584 " *\n" +
1585 "\n" +
1586 "@public\n" +
1587 " *\n" +
1588 "\n" +
1589 " **********\n" +
1590 " * @final\n" +
1591 " */";
1592
1593 JSDocInfo info = parse(comment);
1594 assertEquals(1, info.getParameterCount());
1595 assertEquals(null, info.getParameterType("mediaTag"));
1596 assertEquals(Visibility.PUBLIC, info.getVisibility());
1597 assertTrue(info.isConstant());
1598 }
1599
1600 public void testRegression4() throws Exception {
1601 String comment =
1602 " * @const\n" +
1603 " * @hidden\n" +
1604 " * @preserveTry\n" +
1605 " * @constructor\n" +
1606 " */";
1607
1608 JSDocInfo info = parse(comment);
1609 assertTrue(info.isConstant());
1610 assertFalse(info.isDefine());
1611 assertTrue(info.isConstructor());
1612 assertTrue(info.isHidden());
1613 assertTrue(info.shouldPreserveTry());
1614 }
1615
1616 public void testRegression5() throws Exception {
1617 String comment = "@const\n@enum {string}\n@public*/";
1618
1619 JSDocInfo info = parse(comment);
1620 assertTrue(info.isConstant());
1621 assertFalse(info.isDefine());
1622 assertTypeEquals(STRING_TYPE, info.getEnumParameterType());
1623 assertEquals(Visibility.PUBLIC, info.getVisibility());
1624 }
1625
1626 public void testRegression6() throws Exception {
1627 String comment = "@hidden\n@enum\n@public*/";
1628
1629 JSDocInfo info = parse(comment);
1630 assertTrue(info.isHidden());
1631 assertTypeEquals(NUMBER_TYPE, info.getEnumParameterType());
1632 assertEquals(Visibility.PUBLIC, info.getVisibility());
1633 }
1634
1635 public void testRegression7() throws Exception {
1636 String comment =
1637 " * @desc description here\n" +
1638 " * @param {boolean} flag and some more description\n" +
1639 " * nicely formatted\n" +
1640 " */";
1641
1642 JSDocInfo info = parse(comment);
1643 assertEquals(1, info.getParameterCount());
1644 assertTypeEquals(BOOLEAN_TYPE, info.getParameterType("flag"));
1645 assertEquals("description here", info.getDescription());
1646 }
1647
1648 public void testRegression8() throws Exception {
1649 String comment =
1650 " * @name random tag here\n" +
1651 " * @desc description here\n" +
1652 " *\n" +
1653 " * @param {boolean} flag and some more description\n" +
1654 " * nicely formatted\n" +
1655 " */";
1656
1657 JSDocInfo info = parse(comment);
1658 assertEquals(1, info.getParameterCount());
1659 assertTypeEquals(BOOLEAN_TYPE, info.getParameterType("flag"));
1660 assertEquals("description here", info.getDescription());
1661 }
1662
1663 public void testRegression9() throws Exception {
1664 JSDocInfo jsdoc = parse(
1665 " * @param {string} p0 blah blah blah\n" +
1666 " */");
1667
1668 assertNull(jsdoc.getBaseType());
1669 assertFalse(jsdoc.isConstant());
1670 assertNull(jsdoc.getDescription());
1671 assertNull(jsdoc.getEnumParameterType());
1672 assertFalse(jsdoc.isHidden());
1673 assertEquals(1, jsdoc.getParameterCount());
1674 assertTypeEquals(STRING_TYPE, jsdoc.getParameterType("p0"));
1675 assertNull(jsdoc.getReturnType());
1676 assertNull(jsdoc.getType());
1677 assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
1678 }
1679
1680 public void testRegression10() throws Exception {
1681 JSDocInfo jsdoc = parse(
1682 " * @param {!String} p0 blah blah blah\n" +
1683 " * @param {boolean} p1 fobar\n" +
1684 " * @return {!Date} jksjkash dshad\n" +
1685 " */");
1686
1687 assertNull(jsdoc.getBaseType());
1688 assertFalse(jsdoc.isConstant());
1689 assertNull(jsdoc.getDescription());
1690 assertNull(jsdoc.getEnumParameterType());
1691 assertFalse(jsdoc.isHidden());
1692 assertEquals(2, jsdoc.getParameterCount());
1693 assertTypeEquals(STRING_OBJECT_TYPE, jsdoc.getParameterType("p0"));
1694 assertTypeEquals(BOOLEAN_TYPE, jsdoc.getParameterType("p1"));
1695 assertTypeEquals(DATE_TYPE, jsdoc.getReturnType());
1696 assertNull(jsdoc.getType());
1697 assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
1698 }
1699
1700 public void testRegression11() throws Exception {
1701 JSDocInfo jsdoc = parse(
1702 " * @constructor\n" +
1703 " */");
1704
1705 assertNull(jsdoc.getBaseType());
1706 assertFalse(jsdoc.isConstant());
1707 assertNull(jsdoc.getDescription());
1708 assertNull(jsdoc.getEnumParameterType());
1709 assertFalse(jsdoc.isHidden());
1710 assertEquals(0, jsdoc.getParameterCount());
1711 assertNull(jsdoc.getReturnType());
1712 assertNull(jsdoc.getType());
1713 assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
1714 }
1715
1716 public void testRegression12() throws Exception {
1717 JSDocInfo jsdoc = parse(
1718 " * @extends FooBar\n" +
1719 " */");
1720
1721 assertTypeEquals(registry.createNamedType("FooBar", null, 0, 0),
1722 jsdoc.getBaseType());
1723 assertFalse(jsdoc.isConstant());
1724 assertNull(jsdoc.getDescription());
1725 assertNull(jsdoc.getEnumParameterType());
1726 assertFalse(jsdoc.isHidden());
1727 assertEquals(0, jsdoc.getParameterCount());
1728 assertNull(jsdoc.getReturnType());
1729 assertNull(jsdoc.getType());
1730 assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
1731 }
1732
1733 public void testRegression13() throws Exception {
1734 JSDocInfo jsdoc = parse(
1735 " * @type {!RegExp}\n" +
1736 " * @protected\n" +
1737 " */");
1738
1739 assertNull(jsdoc.getBaseType());
1740 assertFalse(jsdoc.isConstant());
1741 assertNull(jsdoc.getDescription());
1742 assertNull(jsdoc.getEnumParameterType());
1743 assertFalse(jsdoc.isHidden());
1744 assertEquals(0, jsdoc.getParameterCount());
1745 assertNull(jsdoc.getReturnType());
1746 assertTypeEquals(REGEXP_TYPE, jsdoc.getType());
1747 assertEquals(Visibility.PROTECTED, jsdoc.getVisibility());
1748 }
1749
1750 public void testRegression14() throws Exception {
1751 JSDocInfo jsdoc = parse(
1752 " * @const\n" +
1753 " * @private\n" +
1754 " */");
1755
1756 assertNull(jsdoc.getBaseType());
1757 assertTrue(jsdoc.isConstant());
1758 assertNull(jsdoc.getDescription());
1759 assertNull(jsdoc.getEnumParameterType());
1760 assertFalse(jsdoc.isHidden());
1761 assertEquals(0, jsdoc.getParameterCount());
1762 assertNull(jsdoc.getReturnType());
1763 assertNull(jsdoc.getType());
1764 assertEquals(Visibility.PRIVATE, jsdoc.getVisibility());
1765 }
1766
1767 public void testRegression15() throws Exception {
1768 JSDocInfo jsdoc = parse(
1769 " * @desc Hello,\n" +
1770 " * World!\n" +
1771 " */");
1772
1773 assertNull(jsdoc.getBaseType());
1774 assertFalse(jsdoc.isConstant());
1775 assertEquals("Hello, World!", jsdoc.getDescription());
1776 assertNull(jsdoc.getEnumParameterType());
1777 assertFalse(jsdoc.isHidden());
1778 assertEquals(0, jsdoc.getParameterCount());
1779 assertNull(jsdoc.getReturnType());
1780 assertNull(jsdoc.getType());
1781 assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
1782 assertFalse(jsdoc.isExport());
1783 }
1784
1785 public void testRegression16() throws Exception {
1786 JSDocInfo jsdoc = parse(
1787 " Email is [email protected]\n" +
1788 " @type {string}\n" +
1789 " */");
1790
1791 assertNull(jsdoc.getBaseType());
1792 assertFalse(jsdoc.isConstant());
1793 assertTypeEquals(STRING_TYPE, jsdoc.getType());
1794 assertFalse(jsdoc.isHidden());
1795 assertEquals(0, jsdoc.getParameterCount());
1796 assertNull(jsdoc.getReturnType());
1797 assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
1798 }
1799
1800 public void testRegression17() throws Exception {
1801 // verifying that if no @desc is present the description is empty
1802 assertNull(parse("@private*/").getDescription());
1803 }
1804
1805 public void testFullRegression1() throws Exception {
1806 parseFull("/** @param (string,number) foo*/function bar(foo){}",
1807 "Bad type annotation. expecting a variable name in a @param tag");
1808 }
1809
1810 public void testFullRegression2() throws Exception {
1811 parseFull("/** @param {string,number) foo*/function bar(foo){}",
1812 "Bad type annotation. expected closing }",
1813 "Bad type annotation. expecting a variable name in a @param tag");
1814 }
1815
1816 public void testFullRegression3() throws Exception {
1817 parseFull("/**..\n*/");
1818 }
1819
1820 public void testBug907488() throws Exception {
1821 parse("@type {number,null} */",
1822 "Bad type annotation. expected closing }");
1823 }
1824
1825 public void testBug907494() throws Exception {
1826 parse("@return {Object,undefined} */",
1827 "Bad type annotation. expected closing }");
1828 }
1829
1830 public void testBug909468() throws Exception {
1831 parse("@extends {(x)}*/",
1832 "Bad type annotation. expecting a type name");
1833 }
1834
1835 public void testParseInterface() throws Exception {
1836 assertTrue(parse("@interface*/").isInterface());
1837 }
1838
1839 public void testParseImplicitCast1() throws Exception {
1840 assertTrue(parse("@type {string} \n * @implicitCast*/").isImplicitCast());
1841 }
1842
1843 public void testParseImplicitCast2() throws Exception {
1844 assertFalse(parse("@type {string}*/").isImplicitCast());
1845 }
1846
1847 public void testParseDuplicateImplicitCast() throws Exception {
1848 parse("@type {string} \n * @implicitCast \n * @implicitCast*/",
1849 "Bad type annotation. extra @implicitCast tag");
1850 }
1851
1852 public void testParseInterfaceDoubled() throws Exception {
1853 parse(
1854 "* @interface\n" +
1855 "* @interface\n" +
1856 "*/",
1857 "Bad type annotation. " +
1858 "type annotation incompatible with other annotations");
1859 }
1860
1861 public void testParseImplements() throws Exception {
1862 List<JSTypeExpression> interfaces = parse("@implements {SomeInterface}*/")
1863 .getImplementedInterfaces();
1864 assertEquals(1, interfaces.size());
1865 assertTypeEquals(registry.createNamedType("SomeInterface", null, -1, -1),
1866 interfaces.get(0));
1867 }
1868
1869 public void testParseImplementsTwo() throws Exception {
1870 List<JSTypeExpression> interfaces =
1871 parse(
1872 "* @implements {SomeInterface1}\n" +
1873 "* @implements {SomeInterface2}\n" +
1874 "*/")
1875 .getImplementedInterfaces();
1876 assertEquals(2, interfaces.size());
1877 assertTypeEquals(registry.createNamedType("SomeInterface1", null, -1, -1),
1878 interfaces.get(0));
1879 assertTypeEquals(registry.createNamedType("SomeInterface2", null, -1, -1),
1880 interfaces.get(1));
1881 }
1882
1883 public void testParseImplementsSameTwice() throws Exception {
1884 parse(
1885 "* @implements {Smth}\n" +
1886 "* @implements {Smth}\n" +
1887 "*/",
1888 "Bad type annotation. duplicate @implements tag");
1889 }
1890
1891 public void testParseImplementsNoName() throws Exception {
1892 parse("* @implements {} */",
1893 "Bad type annotation. expecting a type name");
1894 }
1895
1896 public void testParseImplementsMissingRC() throws Exception {
1897 parse("* @implements {Smth */",
1898 "Bad type annotation. expected closing }");
1899 }
1900
1901 public void testParseImplementsNullable1() throws Exception {
1902 parse("@implements {Base?} */", "Bad type annotation. expected closing }");
1903 }
1904
1905 public void testParseImplementsNullable2() throws Exception {
1906 parse("@implements Base? */",
1907 "Bad type annotation. expected end of line or comment");
1908 }
1909
1910 public void testInterfaceExtends() throws Exception {
1911 JSDocInfo jsdoc = parse(
1912 " * @interface \n" +
1913 " * @extends {Extended} */");
1914 assertTrue(jsdoc.isInterface());
1915 assertEquals(1, jsdoc.getExtendedInterfacesCount());
1916 List<JSTypeExpression> types = jsdoc.getExtendedInterfaces();
1917 assertTypeEquals(registry.createNamedType("Extended", null, -1, -1),
1918 types.get(0));
1919 }
1920
1921 public void testInterfaceMultiExtends1() throws Exception {
1922 JSDocInfo jsdoc = parse(
1923 " * @interface \n" +
1924 " * @extends {Extended1} \n" +
1925 " * @extends {Extended2} */");
1926 assertTrue(jsdoc.isInterface());
1927 assertNull(jsdoc.getBaseType());
1928 assertEquals(2, jsdoc.getExtendedInterfacesCount());
1929 List<JSTypeExpression> types = jsdoc.getExtendedInterfaces();
1930 assertTypeEquals(registry.createNamedType("Extended1", null, -1, -1),
1931 types.get(0));
1932 assertTypeEquals(registry.createNamedType("Extended2", null, -1, -1),
1933 types.get(1));
1934 }
1935
1936 public void testInterfaceMultiExtends2() throws Exception {
1937 JSDocInfo jsdoc = parse(
1938 " * @extends {Extended1} \n" +
1939 " * @interface \n" +
1940 " * @extends {Extended2} \n" +
1941 " * @extends {Extended3} */");
1942 assertTrue(jsdoc.isInterface());
1943 assertNull(jsdoc.getBaseType());
1944 assertEquals(3, jsdoc.getExtendedInterfacesCount());
1945 List<JSTypeExpression> types = jsdoc.getExtendedInterfaces();
1946 assertTypeEquals(registry.createNamedType("Extended1", null, -1, -1),
1947 types.get(0));
1948 assertTypeEquals(registry.createNamedType("Extended2", null, -1, -1),
1949 types.get(1));
1950 assertTypeEquals(registry.createNamedType("Extended3", null, -1, -1),
1951 types.get(2));
1952 }
1953
1954 public void testBadClassMultiExtends() throws Exception {
1955 parse(" * @extends {Extended1} \n" +
1956 " * @constructor \n" +
1957 " * @extends {Extended2} */",
1958 "Bad type annotation. type annotation incompatible with other " +
1959 "annotations");
1960 }
1961
1962 public void testBadExtendsWithNullable() throws Exception {
1963 JSDocInfo jsdoc = parse("@constructor\n * @extends {Object?} */",
1964 "Bad type annotation. expected closing }");
1965 assertTrue(jsdoc.isConstructor());
1966 assertTypeEquals(OBJECT_TYPE, jsdoc.getBaseType());
1967 }
1968
1969 public void testBadImplementsWithNullable() throws Exception {
1970 JSDocInfo jsdoc = parse("@implements {Disposable?}\n * @constructor */",
1971 "Bad type annotation. expected closing }");
1972 assertTrue(jsdoc.isConstructor());
1973 assertTypeEquals(registry.createNamedType("Disposable", null, -1, -1),
1974 jsdoc.getImplementedInterfaces().get(0));
1975 }
1976
1977 public void testBadTypeDefInterfaceAndConstructor1() throws Exception {
1978 JSDocInfo jsdoc = parse("@interface\n@constructor*/",
1979 "Bad type annotation. cannot be both an interface and a constructor");
1980 assertTrue(jsdoc.isInterface());
1981 }
1982
1983 public void testBadTypeDefInterfaceAndConstructor2() throws Exception {
1984 JSDocInfo jsdoc = parse("@constructor\n@interface*/",
1985 "Bad type annotation. cannot be both an interface and a constructor");
1986 assertTrue(jsdoc.isConstructor());
1987 }
1988
1989 public void testDocumentationParameter() throws Exception {
1990 JSDocInfo jsdoc
1991 = parse("@param {Number} number42 This is a description.*/", true);
1992
1993 assertTrue(jsdoc.hasDescriptionForParameter("number42"));
1994 assertEquals("This is a description.",
1995 jsdoc.getDescriptionForParameter("number42"));
1996 }
1997
1998 public void testMultilineDocumentationParameter() throws Exception {
1999 JSDocInfo jsdoc
2000 = parse("@param {Number} number42 This is a description"
2001 + "\n* on multiple \n* lines.*/", true);
2002
2003 assertTrue(jsdoc.hasDescriptionForParameter("number42"));
2004 assertEquals("This is a description on multiple lines.",
2005 jsdoc.getDescriptionForParameter("number42"));
2006
2007 }
2008
2009 public void testDocumentationMultipleParameter() throws Exception {
2010 JSDocInfo jsdoc
2011 = parse("@param {Number} number42 This is a description."
2012 + "\n* @param {Integer} number87 This is another description.*/"
2013 , true);
2014
2015 assertTrue(jsdoc.hasDescriptionForParameter("number42"));
2016 assertEquals("This is a description.",
2017 jsdoc.getDescriptionForParameter("number42"));
2018
2019 assertTrue(jsdoc.hasDescriptionForParameter("number87"));
2020 assertEquals("This is another description.",
2021 jsdoc.getDescriptionForParameter("number87"));
2022 }
2023
2024 public void testDocumentationMultipleParameter2() throws Exception {
2025 JSDocInfo jsdoc
2026 = parse("@param {number} delta = 0 results in a redraw\n" +
2027 " != 0 ..... */", true);
2028 assertTrue(jsdoc.hasDescriptionForParameter("delta"));
2029 assertEquals("= 0 results in a redraw != 0 .....",
2030 jsdoc.getDescriptionForParameter("delta"));
2031 }
2032
2033
2034 public void testAuthors() throws Exception {
2035 JSDocInfo jsdoc
2036 = parse("@param {Number} number42 This is a description."
2037 + "\n* @param {Integer} number87 This is another description."
2038 + "\n* @author [email protected] (A Person)"
2039 + "\n* @author [email protected] (B Person)"
2040 + "\n* @author [email protected] (C Person)*/"
2041 , true);
2042
2043 Collection<String> authors = jsdoc.getAuthors();
2044
2045 assertTrue(authors != null);
2046 assertTrue(authors.size() == 3);
2047
2048 assertContains(authors, "[email protected] (A Person)");
2049 assertContains(authors, "[email protected] (B Person)");
2050 assertContains(authors, "[email protected] (C Person)");
2051 }
2052
2053 public void testSuppress1() throws Exception {
2054 JSDocInfo info = parse("@suppress {x} */");
2055 assertEquals(Sets.newHashSet("x"), info.getSuppressions());
2056 }
2057
2058 public void testSuppress2() throws Exception {
2059 JSDocInfo info = parse("@suppress {x|y|x|z} */");
2060 assertEquals(Sets.newHashSet("x", "y", "z"), info.getSuppressions());
2061 }
2062
2063 public void testSuppress3() throws Exception {
2064 JSDocInfo info = parse("@suppress {x,y} */");
2065 assertEquals(Sets.newHashSet("x", "y"), info.getSuppressions());
2066 }
2067
2068 public void testBadSuppress1() throws Exception {
2069 parse("@suppress {} */", "malformed @suppress tag");
2070 }
2071
2072 public void testBadSuppress2() throws Exception {
2073 parse("@suppress {x|} */", "malformed @suppress tag");
2074 }
2075
2076 public void testBadSuppress3() throws Exception {
2077 parse("@suppress {|x} */", "malformed @suppress tag");
2078 }
2079
2080 public void testBadSuppress4() throws Exception {
2081 parse("@suppress {x|y */", "malformed @suppress tag");
2082 }
2083
2084 public void testBadSuppress6() throws Exception {
2085 parse("@suppress {x} \n * @suppress {y} */", "duplicate @suppress tag");
2086 }
2087
2088 public void testBadSuppress7() throws Exception {
2089 parse("@suppress {impossible} */",
2090 "unknown @suppress parameter: impossible");
2091 }
2092
2093 public void testModifies1() throws Exception {
2094 JSDocInfo info = parse("@modifies {this} */");
2095 assertEquals(Sets.newHashSet("this"), info.getModifies());
2096 }
2097
2098 public void testModifies2() throws Exception {
2099 JSDocInfo info = parse("@modifies {arguments} */");
2100 assertEquals(Sets.newHashSet("arguments"), info.getModifies());
2101 }
2102
2103 public void testModifies3() throws Exception {
2104 JSDocInfo info = parse("@modifies {this|arguments} */");
2105 assertEquals(Sets.newHashSet("this", "arguments"), info.getModifies());
2106 }
2107
2108 public void testModifies4() throws Exception {
2109 JSDocInfo info = parse("@param {*} x\n * @modifies {x} */");
2110 assertEquals(Sets.newHashSet("x"), info.getModifies());
2111 }
2112
2113 public void testModifies5() throws Exception {
2114 JSDocInfo info = parse(
2115 "@param {*} x\n"
2116 + " * @param {*} y\n"
2117 + " * @modifies {x} */");
2118 assertEquals(Sets.newHashSet("x"), info.getModifies());
2119 }
2120
2121 public void testModifies6() throws Exception {
2122 JSDocInfo info = parse(
2123 "@param {*} x\n"
2124 + " * @param {*} y\n"
2125 + " * @modifies {x|y} */");
2126 assertEquals(Sets.newHashSet("x", "y"), info.getModifies());
2127 }
2128
2129
2130 public void testBadModifies1() throws Exception {
2131 parse("@modifies {} */", "malformed @modifies tag");
2132 }
2133
2134 public void testBadModifies2() throws Exception {
2135 parse("@modifies {this|} */", "malformed @modifies tag");
2136 }
2137
2138 public void testBadModifies3() throws Exception {
2139 parse("@modifies {|this} */", "malformed @modifies tag");
2140 }
2141
2142 public void testBadModifies4() throws Exception {
2143 parse("@modifies {this|arguments */", "malformed @modifies tag");
2144 }
2145
2146 public void testBadModifies5() throws Exception {
2147 parse("@modifies {this,arguments} */", "malformed @modifies tag");
2148 }
2149
2150 public void testBadModifies6() throws Exception {
2151 parse("@modifies {this} \n * @modifies {this} */",
2152 "conflicting @modifies tag");
2153 }
2154
2155 public void testBadModifies7() throws Exception {
2156 parse("@modifies {impossible} */",
2157 "unknown @modifies parameter: impossible");
2158 }
2159
2160 public void testBadModifies8() throws Exception {
2161 parse("@modifies {this}\n"
2162 + "@nosideeffects */", "conflicting @nosideeffects tag");
2163 }
2164
2165 public void testBadModifies9() throws Exception {
2166 parse("@nosideeffects\n"
2167 + "@modifies {this} */", "conflicting @modifies tag");
2168 }
2169
2170 //public void testNoParseFileOverview() throws Exception {
2171 // JSDocInfo jsdoc = parseFileOverviewWithoutDoc("@fileoverview Hi mom! */");
2172 // assertNull(jsdoc.getFileOverview());
2173 // assertTrue(jsdoc.hasFileOverview());
2174 //}
2175
2176 public void testFileOverviewSingleLine() throws Exception {
2177 JSDocInfo jsdoc = parseFileOverview("@fileoverview Hi mom! */");
2178 assertEquals("Hi mom!", jsdoc.getFileOverview());
2179 }
2180
2181 public void testFileOverviewMultiLine() throws Exception {
2182 JSDocInfo jsdoc = parseFileOverview("@fileoverview Pie is \n * good! */");
2183 assertEquals("Pie is\n good!", jsdoc.getFileOverview());
2184 }
2185
2186 public void testFileOverviewDuplicate() throws Exception {
2187 parseFileOverview(
2188 "@fileoverview Pie \n * @fileoverview Cake */",
2189 "extra @fileoverview tag");
2190 }
2191
2192 public void testReferences() throws Exception {
2193 JSDocInfo jsdoc
2194 = parse("@see A cool place!"
2195 + "\n* @see The world."
2196 + "\n* @see SomeClass#SomeMember"
2197 + "\n* @see A boring test case*/"
2198 , true);
2199
2200 Collection<String> references = jsdoc.getReferences();
2201
2202 assertTrue(references != null);
2203 assertTrue(references.size() == 4);
2204
2205 assertContains(references, "A cool place!");
2206 assertContains(references, "The world.");
2207 assertContains(references, "SomeClass#SomeMember");
2208 assertContains(references, "A boring test case");
2209 }
2210
2211 public void testSingleTags() throws Exception {
2212 JSDocInfo jsdoc
2213 = parse("@version Some old version"
2214 + "\n* @deprecated In favor of the new one!"
2215 + "\n* @return {SomeType} The most important object :-)*/"
2216 , true);
2217
2218 assertTrue(jsdoc.isDeprecated());
2219 assertEquals("In favor of the new one!", jsdoc.getDeprecationReason());
2220 assertEquals("Some old version", jsdoc.getVersion());
2221 assertEquals("The most important object :-)", jsdoc.getReturnDescription());
2222 }
2223
2224 public void testSingleTagsReordered() throws Exception {
2225 JSDocInfo jsdoc
2226 = parse("@deprecated In favor of the new one!"
2227 + "\n * @return {SomeType} The most important object :-)"
2228 + "\n * @version Some old version*/"
2229 , true);
2230
2231 assertTrue(jsdoc.isDeprecated());
2232 assertEquals("In favor of the new one!", jsdoc.getDeprecationReason());
2233 assertEquals("Some old version", jsdoc.getVersion());
2234 assertEquals("The most important object :-)", jsdoc.getReturnDescription());
2235 }
2236
2237 public void testVersionDuplication() throws Exception {
2238 parse("* @version Some old version"
2239 + "\n* @version Another version*/", true,
2240 "conflicting @version tag");
2241 }
2242
2243 public void testVersionMissing() throws Exception {
2244 parse("* @version */", true,
2245 "@version tag missing version information");
2246 }
2247
2248 public void testAuthorMissing() throws Exception {
2249 parse("* @author */", true,
2250 "@author tag missing author");
2251 }
2252
2253 public void testSeeMissing() throws Exception {
2254 parse("* @see */", true,
2255 "@see tag missing description");
2256 }
2257
2258 public void testSourceName() throws Exception {
2259 JSDocInfo jsdoc = parse("@deprecated */", true);
2260 assertEquals("testcode", jsdoc.getAssociatedNode().getSourceFileName());
2261 }
2262
2263 public void testParseBlockComment() throws Exception {
2264 JSDocInfo jsdoc = parse("this is a nice comment\n "
2265 + "* that is multiline \n"
2266 + "* @author [email protected] */", true);
2267
2268 assertEquals("this is a nice comment\nthat is multiline",
2269 jsdoc.getBlockDescription());
2270
2271 assertDocumentationInMarker(
2272 assertAnnotationMarker(jsdoc, "author", 2, 2),
2273 "[email protected]", 9, 2, 23);
2274 }
2275
2276 public void testParseBlockComment2() throws Exception {
2277 JSDocInfo jsdoc = parse("this is a nice comment\n "
2278 + "* that is *** multiline \n"
2279 + "* @author [email protected] */", true);
2280
2281 assertEquals("this is a nice comment\nthat is *** multiline",
2282 jsdoc.getBlockDescription());
2283
2284 assertDocumentationInMarker(
2285 assertAnnotationMarker(jsdoc, "author", 2, 2),
2286 "[email protected]", 9, 2, 23);
2287 }
2288
2289 public void testParseBlockComment3() throws Exception {
2290 JSDocInfo jsdoc = parse("\n "
2291 + "* hello world \n"
2292 + "* @author [email protected] */", true);
2293
2294 assertEquals("hello world", jsdoc.getBlockDescription());
2295
2296 assertDocumentationInMarker(
2297 assertAnnotationMarker(jsdoc, "author", 2, 2),
2298 "[email protected]", 9, 2, 23);
2299 }
2300
2301 public void testParseWithMarkers1() throws Exception {
2302 JSDocInfo jsdoc = parse("@author [email protected] */", true);
2303
2304 assertDocumentationInMarker(
2305 assertAnnotationMarker(jsdoc, "author", 0, 0),
2306 "[email protected]", 7, 0, 21);
2307 }
2308
2309 public void testParseWithMarkers2() throws Exception {
2310 JSDocInfo jsdoc = parse("@param {Foo} somename [email protected] */", true);
2311
2312 assertDocumentationInMarker(
2313 assertAnnotationMarker(jsdoc, "param", 0, 0),
2314 "[email protected]", 21, 0, 37);
2315 }
2316
2317 public void testParseWithMarkers3() throws Exception {
2318 JSDocInfo jsdoc =
2319 parse("@return {Foo} some long \n * multiline" +
2320 " \n * description */", true);
2321
2322 JSDocInfo.Marker returnDoc =
2323 assertAnnotationMarker(jsdoc, "return", 0, 0);
2324 assertDocumentationInMarker(returnDoc,
2325 "some long multiline description", 13, 2, 15);
2326 assertEquals(8, returnDoc.getType().getPositionOnStartLine());
2327 assertEquals(12, returnDoc.getType().getPositionOnEndLine());
2328 }
2329
2330 public void testParseWithMarkers4() throws Exception {
2331 JSDocInfo jsdoc =
2332 parse("@author foobar \n * @param {Foo} somename [email protected] */",
2333 true);
2334
2335 assertAnnotationMarker(jsdoc, "author", 0, 0);
2336 assertAnnotationMarker(jsdoc, "param", 1, 3);
2337 }
2338
2339 public void testParseWithMarkers5() throws Exception {
2340 JSDocInfo jsdoc =
2341 parse("@return some long \n * multiline" +
2342 " \n * description */", true);
2343
2344 assertDocumentationInMarker(
2345 assertAnnotationMarker(jsdoc, "return", 0, 0),
2346 "some long multiline description", 8, 2, 15);
2347 }
2348
2349 public void testParseWithMarkers6() throws Exception {
2350 JSDocInfo jsdoc =
2351 parse("@param x some long \n * multiline" +
2352 " \n * description */", true);
2353
2354 assertDocumentationInMarker(
2355 assertAnnotationMarker(jsdoc, "param", 0, 0),
2356 "some long multiline description", 8, 2, 15);
2357 }
2358
2359 public void testParseWithMarkerNames1() throws Exception {
2360 JSDocInfo jsdoc = parse("@param {SomeType} name somedescription */", true);
2361
2362 assertNameInMarker(
2363 assertAnnotationMarker(jsdoc, "param", 0, 0),
2364 "name", 0, 18);
2365 }
2366
2367 public void testParseWithMarkerNames2() throws Exception {
2368 JSDocInfo jsdoc = parse("@param {SomeType} name somedescription \n" +
2369 "* @param {AnotherType} anothername des */", true);
2370
2371 assertTypeInMarker(
2372 assertNameInMarker(
2373 assertAnnotationMarker(jsdoc, "param", 0, 0, 0),
2374 "name", 0, 18),
2375 "SomeType", 0, 7, 0, 16, true);
2376
2377 assertTypeInMarker(
2378 assertNameInMarker(
2379 assertAnnotationMarker(jsdoc, "param", 1, 2, 1),
2380 "anothername", 1, 23),
2381 "AnotherType", 1, 9, 1, 21, true);
2382 }
2383
2384 public void testParseWithMarkerNames3() throws Exception {
2385 JSDocInfo jsdoc = parse(
2386 "@param {Some.Long.Type.\n * Name} name somedescription */", true);
2387
2388 assertTypeInMarker(
2389 assertNameInMarker(
2390 assertAnnotationMarker(jsdoc, "param", 0, 0, 0),
2391 "name", 1, 10),
2392 "Some.Long.Type.Name", 0, 7, 1, 8, true);
2393 }
2394
2395 @SuppressWarnings("deprecation")
2396 public void testParseWithoutMarkerName() throws Exception {
2397 JSDocInfo jsdoc = parse("@author helloworld*/", true);
2398 assertNull(assertAnnotationMarker(jsdoc, "author", 0, 0).getName());
2399 }
2400
2401 public void testParseWithMarkerType() throws Exception {
2402 JSDocInfo jsdoc = parse("@extends {FooBar}*/", true);
2403
2404 assertTypeInMarker(
2405 assertAnnotationMarker(jsdoc, "extends", 0, 0),
2406 "FooBar", 0, 9, 0, 16, true);
2407 }
2408
2409 public void testParseWithMarkerType2() throws Exception {
2410 JSDocInfo jsdoc = parse("@extends FooBar*/", true);
2411
2412 assertTypeInMarker(
2413 assertAnnotationMarker(jsdoc, "extends", 0, 0),
2414 "FooBar", 0, 9, 0, 15, false);
2415 }
2416
2417 public void testTypeTagConflict1() throws Exception {
2418 parse("@constructor \n * @constructor */",
2419 "Bad type annotation. " +
2420 "type annotation incompatible with other annotations");
2421 }
2422
2423 public void testTypeTagConflict2() throws Exception {
2424 parse("@interface \n * @interface */",
2425 "Bad type annotation. " +
2426 "type annotation incompatible with other annotations");
2427 }
2428
2429 public void testTypeTagConflict3() throws Exception {
2430 parse("@constructor \n * @interface */",
2431 "Bad type annotation. cannot be both an interface and a constructor");
2432 }
2433
2434 public void testTypeTagConflict4() throws Exception {
2435 parse("@interface \n * @constructor */",
2436 "Bad type annotation. cannot be both an interface and a constructor");
2437 }
2438
2439 public void testTypeTagConflict5() throws Exception {
2440 parse("@interface \n * @type {string} */",
2441 "Bad type annotation. " +
2442 "type annotation incompatible with other annotations");
2443 }
2444
2445 public void testTypeTagConflict6() throws Exception {
2446 parse("@typedef {string} \n * @type {string} */",
2447 "Bad type annotation. " +
2448 "type annotation incompatible with other annotations");
2449 }
2450
2451 public void testTypeTagConflict7() throws Exception {
2452 parse("@typedef {string} \n * @constructor */",
2453 "Bad type annotation. " +
2454 "type annotation incompatible with other annotations");
2455 }
2456
2457 public void testTypeTagConflict8() throws Exception {
2458 parse("@typedef {string} \n * @return {boolean} */",
2459 "Bad type annotation. " +
2460 "type annotation incompatible with other annotations");
2461 }
2462
2463 public void testTypeTagConflict9() throws Exception {
2464 parse("@enum {string} \n * @return {boolean} */",
2465 "Bad type annotation. " +
2466 "type annotation incompatible with other annotations");
2467 }
2468
2469 public void testTypeTagConflict10() throws Exception {
2470 parse("@this {Object} \n * @enum {boolean} */",
2471 "Bad type annotation. " +
2472 "type annotation incompatible with other annotations");
2473 }
2474
2475 public void testTypeTagConflict11() throws Exception {
2476 parse("@param {Object} x \n * @type {boolean} */",
2477 "Bad type annotation. " +
2478 "type annotation incompatible with other annotations");
2479 }
2480
2481 public void testTypeTagConflict12() throws Exception {
2482 parse("@typedef {boolean} \n * @param {Object} x */",
2483 "Bad type annotation. " +
2484 "type annotation incompatible with other annotations");
2485 }
2486
2487 public void testTypeTagConflict13() throws Exception {
2488 parse("@typedef {boolean} \n * @extends {Object} */",
2489 "Bad type annotation. " +
2490 "type annotation incompatible with other annotations");
2491 }
2492
2493 public void testTypeTagConflict14() throws Exception {
2494 parse("@return x \n * @return y */",
2495 "Bad type annotation. " +
2496 "type annotation incompatible with other annotations");
2497 }
2498
2499 public void testTypeTagConflict15() throws Exception {
2500 parse("/**\n" +
2501 " * @struct\n" +
2502 " * @struct\n" +
2503 " */\n" +
2504 "function StrStr() {}",
2505 "Bad type annotation. " +
2506 "type annotation incompatible with other annotations");
2507 }
2508
2509 public void testTypeTagConflict16() throws Exception {
2510 parse("/**\n" +
2511 " * @struct\n" +
2512 " * @interface\n" +
2513 " */\n" +
2514 "function StrIntf() {}",
2515 "Bad type annotation. " +
2516 "type annotation incompatible with other annotations");
2517 }
2518
2519 public void testTypeTagConflict17() throws Exception {
2520 parse("/**\n" +
2521 " * @interface\n" +
2522 " * @struct\n" +
2523 " */\n" +
2524 "function StrIntf() {}",
2525 "Bad type annotation. " +
2526 "type annotation incompatible with other annotations");
2527 }
2528
2529 public void testTypeTagConflict18() throws Exception {
2530 parse("/**\n" +
2531 " * @dict\n" +
2532 " * @dict\n" +
2533 " */\n" +
2534 "function DictDict() {}",
2535 "Bad type annotation. " +
2536 "type annotation incompatible with other annotations");
2537 }
2538
2539 public void testTypeTagConflict19() throws Exception {
2540 parse("/**\n" +
2541 " * @dict\n" +
2542 " * @interface\n" +
2543 " */\n" +
2544 "function DictDict() {}",
2545 "Bad type annotation. " +
2546 "type annotation incompatible with other annotations");
2547 }
2548
2549 public void testTypeTagConflict20() throws Exception {
2550 parse("/**\n" +
2551 " * @interface\n" +
2552 " * @dict\n" +
2553 " */\n" +
2554 "function DictDict() {}",
2555 "Bad type annotation. " +
2556 "type annotation incompatible with other annotations");
2557 }
2558
2559 public void testTypeTagConflict21() throws Exception {
2560 parse("/**\n" +
2561 " * @private {string}\n" +
2562 " * @type {number}\n" +
2563 " */\n" +
2564 "function DictDict() {}",
2565 "Bad type annotation. " +
2566 "type annotation incompatible with other annotations");
2567 }
2568
2569 public void testTypeTagConflict22() throws Exception {
2570 parse("/**\n" +
2571 " * @protected {string}\n" +
2572 " * @param {string} x\n" +
2573 " */\n" +
2574 "function DictDict(x) {}",
2575 "Bad type annotation. " +
2576 "type annotation incompatible with other annotations");
2577 }
2578
2579 public void testTypeTagConflict23() throws Exception {
2580 parse("/**\n" +
2581 " * @public {string}\n" +
2582 " * @return {string} x\n" +
2583 " */\n" +
2584 "function DictDict() {}",
2585 "Bad type annotation. " +
2586 "type annotation incompatible with other annotations");
2587 }
2588
2589 public void testTypeTagConflict24() throws Exception {
2590 parse("/**\n" +
2591 " * @const {string}\n" +
2592 " * @return {string} x\n" +
2593 " */\n" +
2594 "function DictDict() {}",
2595 "Bad type annotation. " +
2596 "type annotation incompatible with other annotations");
2597 }
2598
2599 public void testPrivateType() throws Exception {
2600 JSDocInfo jsdoc = parse("@private {string} */");
2601 assertTypeEquals(STRING_TYPE, jsdoc.getType());
2602 }
2603
2604 public void testProtectedType() throws Exception {
2605 JSDocInfo jsdoc = parse("@protected {string} */");
2606 assertTypeEquals(STRING_TYPE, jsdoc.getType());
2607 }
2608
2609 public void testPublicType() throws Exception {
2610 JSDocInfo jsdoc = parse("@public {string} */");
2611 assertTypeEquals(STRING_TYPE, jsdoc.getType());
2612 }
2613
2614 public void testConstType() throws Exception {
2615 JSDocInfo jsdoc = parse("@const {string} */");
2616 assertTypeEquals(STRING_TYPE, jsdoc.getType());
2617 }
2618
2619 public void testStableIdGeneratorConflict() throws Exception {
2620 parse("/**\n" +
2621 " * @stableIdGenerator\n" +
2622 " * @stableIdGenerator\n" +
2623 " */\n" +
2624 "function getId() {}",
2625 "extra @stableIdGenerator tag");
2626 }
2627
2628 public void testParserWithTemplateTypeNameMissing() {
2629 parse("@template */",
2630 "Bad type annotation. @template tag missing type name");
2631 }
2632
2633 public void testParserWithTemplateDuplicated() {
2634 parse("@template T\n@template V */",
2635 "Bad type annotation. @template tag at most once");
2636 }
2637
2638 public void testParserWithTwoTemplates() {
2639 parse("@template T,V */");
2640 }
2641
2642 public void testParserWithClassTemplateTypeNameMissing() {
2643 parse("@classTemplate */",
2644 "Bad type annotation. @classTemplate tag missing type name");
2645 }
2646
2647 public void testParserWithClassTemplateDuplicated() {
2648 parse("@classTemplate T\n@classTemplate V */",
2649 "Bad type annotation. @classTemplate tag at most once");
2650 }
2651
2652 public void testParserWithTwoClassTemplates() {
2653 parse("@classTemplate T,V */");
2654 }
2655
2656 public void testParserWithClassTemplatesAndTemplate() {
2657 parse("@template T\n@classTemplate T,V */");
2658 }
2659
2660 public void testWhitelistedNewAnnotations() {
2661 parse("@foobar */",
2662 "illegal use of unknown JSDoc tag \"foobar\"; ignoring it");
2663 extraAnnotations.add("foobar");
2664 parse("@foobar */");
2665 }
2666
2667 public void testWhitelistedConflictingAnnotation() {
2668 extraAnnotations.add("param");
2669 JSDocInfo info = parse("@param {number} index */");
2670 assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
2671 }
2672
2673 public void testNonIdentifierAnnotation() {
2674 // Try to whitelist an annotation that is not a valid JS identifier.
2675 // It should not work.
2676 extraAnnotations.add("123");
2677 parse("@123 */", "illegal use of unknown JSDoc tag \"\"; ignoring it");
2678 }
2679
2680 public void testUnsupportedJsDocSyntax1() {
2681 JSDocInfo info =
2682 parse("@param {string} [accessLevel=\"author\"] The user level */",
2683 true);
2684 assertEquals(1, info.getParameterCount());
2685 assertTypeEquals(
2686 registry.createOptionalType(STRING_TYPE),
2687 info.getParameterType("accessLevel"));
2688 assertEquals("The user level",
2689 info.getDescriptionForParameter("accessLevel"));
2690 }
2691
2692 public void testUnsupportedJsDocSyntax2() {
2693 JSDocInfo info =
2694 parse("@param userInfo The user info. \n" +
2695 " * @param userInfo.name The name of the user */", true);
2696 assertEquals(1, info.getParameterCount());
2697 assertEquals("The user info.",
2698 info.getDescriptionForParameter("userInfo"));
2699 }
2700
2701 public void testWhitelistedAnnotations() {
2702 parse(
2703 "* @addon \n" +
2704 "* @ngInject \n" +
2705 "* @augments \n" +
2706 "* @base \n" +
2707 "* @borrows \n" +
2708 "* @bug \n" +
2709 "* @class \n" +
2710 "* @config \n" +
2711 "* @constructs \n" +
2712 "* @default \n" +
2713 "* @description \n" +
2714 "* @event \n" +
2715 "* @example \n" +
2716 "* @exception \n" +
2717 "* @exec \n" +
2718 "* @externs \n" +
2719 "* @field \n" +
2720 "* @function \n" +
2721 "* @id \n" +
2722 "* @ignore \n" +
2723 "* @inner \n" +
2724 "* @lends {string} \n" +
2725 "* @link \n" +
2726 "* @member \n" +
2727 "* @memberOf \n" +
2728 "* @modName \n" +
2729 "* @mods \n" +
2730 "* @name \n" +
2731 "* @namespace \n" +
2732 "* @nocompile \n" +
2733 "* @property \n" +
2734 "* @requires \n" +
2735 "* @since \n" +
2736 "* @static \n" +
2737 "* @supported */");
2738 }
2739
2740 public void testGetOriginalCommentString() throws Exception {
2741 String comment = "* @desc This is a comment */";
2742 JSDocInfo info = parse(comment);
2743 assertNull(info.getOriginalCommentString());
2744 info = parse(comment, true /* parseDocumentation */);
2745 assertEquals(comment, info.getOriginalCommentString());
2746 }
2747
2748 public void testParseNgInject1() throws Exception {
2749 assertTrue(parse("@ngInject*/").isNgInject());
2750 }
2751
2752 public void testParseNgInject2() throws Exception {
2753 parse("@ngInject \n@ngInject*/", "extra @ngInject tag");
2754 }
2755
2756 public void testTextExtents() {
2757 parse("@return {@code foo} bar \n * baz. */",
2758 true, "Bad type annotation. type not recognized due to syntax error");
2759 }
2760
2761 /**
2762 * Asserts that a documentation field exists on the given marker.
2763 *
2764 * @param description The text of the documentation field expected.
2765 * @param startCharno The starting character of the text.
2766 * @param endLineno The ending line of the text.
2767 * @param endCharno The ending character of the text.
2768 * @return The marker, for chaining purposes.
2769 */
2770 private JSDocInfo.Marker assertDocumentationInMarker(JSDocInfo.Marker marker,
2771 String description,
2772 int startCharno,
2773 int endLineno,
2774 int endCharno) {
2775 assertTrue(marker.getDescription() != null);
2776 assertEquals(description, marker.getDescription().getItem());
2777
2778 // Match positional information.
2779 assertEquals(marker.getAnnotation().getStartLine(),
2780 marker.getDescription().getStartLine());
2781 assertEquals(startCharno, marker.getDescription().getPositionOnStartLine());
2782 assertEquals(endLineno, marker.getDescription().getEndLine());
2783 assertEquals(endCharno, marker.getDescription().getPositionOnEndLine());
2784
2785 return marker;
2786 }
2787
2788 /**
2789 * Asserts that a type field exists on the given marker.
2790 *
2791 * @param typeName The name of the type expected in the type field.
2792 * @param startCharno The starting character of the type declaration.
2793 * @param hasBrackets Whether the type in the type field is expected
2794 * to have brackets.
2795 * @return The marker, for chaining purposes.
2796 */
2797 private JSDocInfo.Marker assertTypeInMarker(
2798 JSDocInfo.Marker marker, String typeName,
2799 int startLineno, int startCharno, int endLineno, int endCharno,
2800 boolean hasBrackets) {
2801
2802 assertTrue(marker.getType() != null);
2803 assertTrue(marker.getType().getItem().isString());
2804
2805 // Match the name and brackets information.
2806 String foundName = marker.getType().getItem().getString();
2807
2808 assertEquals(typeName, foundName);
2809 assertEquals(hasBrackets, marker.getType().hasBrackets());
2810
2811 // Match position information.
2812 assertEquals(startCharno, marker.getType().getPositionOnStartLine());
2813 assertEquals(endCharno, marker.getType().getPositionOnEndLine());
2814 assertEquals(startLineno, marker.getType().getStartLine());
2815 assertEquals(endLineno, marker.getType().getEndLine());
2816
2817 return marker;
2818 }
2819
2820 /**
2821 * Asserts that a name field exists on the given marker.
2822 *
2823 * @param name The name expected in the name field.
2824 * @param startCharno The starting character of the text.
2825 * @return The marker, for chaining purposes.
2826 */
2827 @SuppressWarnings("deprecation")
2828 private JSDocInfo.Marker assertNameInMarker(JSDocInfo.Marker marker,
2829 String name, int startLine, int startCharno) {
2830 assertTrue(marker.getName() != null);
2831 assertEquals(name, marker.getName().getItem());
2832
2833 assertEquals(startCharno, marker.getName().getPositionOnStartLine());
2834 assertEquals(startCharno + name.length(),
2835 marker.getName().getPositionOnEndLine());
2836
2837 assertEquals(startLine, marker.getName().getStartLine());
2838 assertEquals(startLine, marker.getName().getEndLine());
2839
2840 return marker;
2841 }
2842
2843 /**
2844 * Asserts that an annotation marker of a given annotation name
2845 * is found in the given JSDocInfo.
2846 *
2847 * @param jsdoc The JSDocInfo in which to search for the annotation marker.
2848 * @param annotationName The name/type of the annotation for which to
2849 * search. Example: "author" for an "@author" annotation.
2850 * @param startLineno The expected starting line number of the marker.
2851 * @param startCharno The expected character on the starting line.
2852 * @return The marker found, for further testing.
2853 */
2854 private JSDocInfo.Marker assertAnnotationMarker(JSDocInfo jsdoc,
2855 String annotationName,
2856 int startLineno,
2857 int startCharno) {
2858 return assertAnnotationMarker(jsdoc, annotationName, startLineno,
2859 startCharno, 0);
2860 }
2861
2862 /**
2863 * Asserts that the index-th annotation marker of a given annotation name
2864 * is found in the given JSDocInfo.
2865 *
2866 * @param jsdoc The JSDocInfo in which to search for the annotation marker.
2867 * @param annotationName The name/type of the annotation for which to
2868 * search. Example: "author" for an "@author" annotation.
2869 * @param startLineno The expected starting line number of the marker.
2870 * @param startCharno The expected character on the starting line.
2871 * @param index The index of the marker.
2872 * @return The marker found, for further testing.
2873 */
2874 private JSDocInfo.Marker assertAnnotationMarker(JSDocInfo jsdoc,
2875 String annotationName,
2876 int startLineno,
2877 int startCharno,
2878 int index) {
2879
2880 Collection<JSDocInfo.Marker> markers = jsdoc.getMarkers();
2881
2882 assertTrue(markers.size() > 0);
2883
2884 int counter = 0;
2885
2886 for (JSDocInfo.Marker marker : markers) {
2887 if (marker.getAnnotation() != null) {
2888 if (annotationName.equals(marker.getAnnotation().getItem())) {
2889
2890 if (counter == index) {
2891 assertEquals(startLineno, marker.getAnnotation().getStartLine());
2892 assertEquals(startCharno,
2893 marker.getAnnotation().getPositionOnStartLine());
2894 assertEquals(startLineno, marker.getAnnotation().getEndLine());
2895 assertEquals(startCharno + annotationName.length(),
2896 marker.getAnnotation().getPositionOnEndLine());
2897
2898 return marker;
2899 }
2900
2901 counter++;
2902 }
2903 }
2904 }
2905
2906 fail("No marker found");
2907 return null;
2908 }
2909
2910 private <T> void assertContains(Collection<T> collection, T item) {
2911 assertTrue(collection.contains(item));
2912 }
2913
2914 private void parseFull(String code, String... warnings) {
2915 CompilerEnvirons environment = new CompilerEnvirons();
2916
2917 TestErrorReporter testErrorReporter = new TestErrorReporter(null, warnings);
2918 environment.setErrorReporter(testErrorReporter);
2919
2920 environment.setRecordingComments(true);
2921 environment.setRecordingLocalJsDocComments(true);
2922
2923 Parser p = new Parser(environment, testErrorReporter);
2924 AstRoot script = p.parse(code, null, 0);
2925
2926 Config config =
2927 new Config(extraAnnotations, extraSuppressions,
2928 true, LanguageMode.ECMASCRIPT3, false);
2929 for (Comment comment : script.getComments()) {
2930 JsDocInfoParser jsdocParser =
2931 new JsDocInfoParser(
2932 new JsDocTokenStream(comment.getValue().substring(3),
2933 comment.getLineno()),
2934 comment,
2935 null,
2936 config,
2937 testErrorReporter);
2938 jsdocParser.parse();
2939 jsdocParser.retrieveAndResetParsedJSDocInfo();
2940 }
2941
2942 assertTrue("some expected warnings were not reported",
2943 testErrorReporter.hasEncounteredAllWarnings());
2944 }
2945
2946 @SuppressWarnings("unused")
2947 private JSDocInfo parseFileOverviewWithoutDoc(String comment,
2948 String... warnings) {
2949 return parse(comment, false, true, warnings);
2950 }
2951
2952 private JSDocInfo parseFileOverview(String comment, String... warnings) {
2953 return parse(comment, true, true, warnings);
2954 }
2955
2956 private JSDocInfo parse(String comment, String... warnings) {
2957 return parse(comment, false, warnings);
2958 }
2959
2960 private JSDocInfo parse(String comment, boolean parseDocumentation,
2961 String... warnings) {
2962 return parse(comment, parseDocumentation, false, warnings);
2963 }
2964
2965 private JSDocInfo parse(String comment, boolean parseDocumentation,
2966 boolean parseFileOverview, String... warnings) {
2967 TestErrorReporter errorReporter = new TestErrorReporter(null, warnings);
2968
2969 Config config = new Config(extraAnnotations, extraSuppressions,
2970 parseDocumentation, LanguageMode.ECMASCRIPT3, false);
2971 StaticSourceFile file = new SimpleSourceFile("testcode", false);
2972 Node associatedNode = new Node(Token.SCRIPT);
2973 associatedNode.setInputId(new InputId(file.getName()));
2974 associatedNode.setStaticSourceFile(file);
2975 JsDocInfoParser jsdocParser = new JsDocInfoParser(
2976 stream(comment),
2977 new Comment(0, 0, CommentType.JSDOC, comment),
2978 associatedNode,
2979 config, errorReporter);
2980
2981 if (fileLevelJsDocBuilder != null) {
2982 jsdocParser.setFileLevelJsDocBuilder(fileLevelJsDocBuilder);
2983 }
2984
2985 jsdocParser.parse();
2986
2987 assertTrue("expected warnings were not reported",
2988 errorReporter.hasEncounteredAllWarnings());
2989
2990 if (parseFileOverview) {
2991 return jsdocParser.getFileOverviewJSDocInfo();
2992 } else {
2993 return jsdocParser.retrieveAndResetParsedJSDocInfo();
2994 }
2995 }
2996
2997 private Node parseType(String typeComment) {
2998 return JsDocInfoParser.parseTypeString(typeComment);
2999 }
3000
3001 private JsDocTokenStream stream(String source) {
3002 return new JsDocTokenStream(source, 0);
3003 }
3004
3005 private void assertTemplatizedTypeEquals(TemplateType key, JSType expected,
3006 JSTypeExpression te) {
3007 assertEquals(
3008 expected, resolve(te).getTemplateTypeMap().getTemplateType(key));
3009 }
3010 }