2 Copyright (C) 2013 Yusuke Suzuki <utatane.tea@gmail.com>
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 /*global require describe it*/
28 var fs = require('fs'),
29 path = require('path'),
30 root = path.join(path.dirname(fs.realpathSync(__filename)), '..'),
31 doctrine = require(root),
32 assert = require('assert');
35 // tests for the stringify function.
36 // ensure that we can parse and then stringify and the results are identical
37 describe('stringify', function () {
39 function testStringify(text) {
40 it (text, function() {
41 var result = doctrine.parse("@param {" + text + "} name");
42 // console.log("Parse Tree: " + JSON.stringify(result, null, " "));
43 var stringed = doctrine.type.stringify(result.tags[0].type, {compact:true});
44 stringed.should.equal(text);
49 testStringify("String");
51 testStringify("null");
52 testStringify("undefined");
53 testStringify("void");
54 //testStringify("?="); // Failing
57 testStringify("...string");
58 testStringify("...[string]");
59 testStringify("...[[string]]");
61 // optional, nullable, nonnullable
62 testStringify("string=");
63 testStringify("?string");
64 testStringify("!string");
65 testStringify("!string=");
68 testStringify("Array.<String>");
69 testStringify("Array.<String,Number>");
73 testStringify("(String|Number)");
76 testStringify("[String]");
77 testStringify("[String,Number]");
78 testStringify("[(String|Number)]");
82 testStringify("{a:String}");
83 testStringify("{a:String,b}");
84 testStringify("{a:String,b:object}");
85 testStringify("{a:String,b:foo.bar.baz}");
86 testStringify("{a:(String|Number),b,c:Array.<String>}");
87 testStringify("...{a:(String|Number),b,c:Array.<String>}");
88 testStringify("{a:(String|Number),b,c:Array.<String>}=");
91 testStringify("function(a)");
92 testStringify("function(a):String");
93 testStringify("function(a:number):String");
94 testStringify("function(a:number,b:Array.<(String|Number|Object)>):String");
95 testStringify("function(a:number,callback:function(a:Array.<(String|Number|Object)>):boolean):String");
96 testStringify("function(a:(string|number),this:string,new:true):function():number");
97 testStringify("function(a:(string|number),this:string,new:true):function(a:function(val):result):number");
100 describe('literals', function() {
101 it('NullableLiteral', function () {
102 doctrine.type.stringify({
103 type: doctrine.Syntax.NullableLiteral
104 }).should.equal('?');
107 it('AllLiteral', function () {
108 doctrine.type.stringify({
109 type: doctrine.Syntax.AllLiteral
110 }).should.equal('*');
113 it('NullLiteral', function () {
114 doctrine.type.stringify({
115 type: doctrine.Syntax.NullLiteral
116 }).should.equal('null');
119 it('UndefinedLiteral', function () {
120 doctrine.type.stringify({
121 type: doctrine.Syntax.UndefinedLiteral
122 }).should.equal('undefined');
126 describe('Expression', function () {
127 it('NameExpression', function () {
128 doctrine.type.stringify({
129 type: doctrine.Syntax.NameExpression,
130 name: 'this.is.valid'
131 }).should.equal('this.is.valid');
133 doctrine.type.stringify({
134 type: doctrine.Syntax.NameExpression,
136 }).should.equal('String');
139 it('ArrayType', function () {
140 doctrine.type.stringify({
141 type: doctrine.Syntax.ArrayType,
143 type: doctrine.Syntax.NameExpression,
146 }).should.equal('[String]');
148 doctrine.type.stringify({
149 type: doctrine.Syntax.ArrayType,
151 type: doctrine.Syntax.NameExpression,
154 type: doctrine.Syntax.NameExpression,
157 }).should.equal('[String, Number]');
159 doctrine.type.stringify({
160 type: doctrine.Syntax.ArrayType,
162 }).should.equal('[]');
165 it('RecordType', function () {
166 doctrine.type.stringify({
167 type: doctrine.Syntax.RecordType,
169 type: doctrine.Syntax.FieldType,
173 }).should.equal('{name}');
175 doctrine.type.stringify({
176 type: doctrine.Syntax.RecordType,
178 type: doctrine.Syntax.FieldType,
181 type: doctrine.Syntax.NameExpression,
185 }).should.equal('{name: String}');
187 doctrine.type.stringify({
188 type: doctrine.Syntax.RecordType,
190 type: doctrine.Syntax.FieldType,
193 type: doctrine.Syntax.NameExpression,
197 type: doctrine.Syntax.FieldType,
200 type: doctrine.Syntax.NameExpression,
204 }).should.equal('{string: String, number: Number}');
206 doctrine.type.stringify({
207 type: doctrine.Syntax.RecordType,
209 }).should.equal('{}');
212 it('UnionType', function () {
213 doctrine.type.stringify({
214 type: doctrine.Syntax.UnionType,
216 type: doctrine.Syntax.NameExpression,
219 }).should.equal('(String)');
221 doctrine.type.stringify({
222 type: doctrine.Syntax.UnionType,
224 type: doctrine.Syntax.NameExpression,
227 type: doctrine.Syntax.NameExpression,
230 }).should.equal('(String|Number)');
232 doctrine.type.stringify({
233 type: doctrine.Syntax.UnionType,
235 type: doctrine.Syntax.NameExpression,
238 type: doctrine.Syntax.NameExpression,
241 }, { topLevel: true }).should.equal('String|Number');
244 it('RestType', function () {
245 doctrine.type.stringify({
246 type: doctrine.Syntax.RestType,
248 type: doctrine.Syntax.NameExpression,
251 }).should.equal('...String');
254 it('NonNullableType', function () {
255 doctrine.type.stringify({
256 type: doctrine.Syntax.NonNullableType,
258 type: doctrine.Syntax.NameExpression,
262 }).should.equal('!String');
264 doctrine.type.stringify({
265 type: doctrine.Syntax.NonNullableType,
267 type: doctrine.Syntax.NameExpression,
271 }).should.equal('String!');
274 it('OptionalType', function () {
275 doctrine.type.stringify({
276 type: doctrine.Syntax.OptionalType,
278 type: doctrine.Syntax.NameExpression,
281 }).should.equal('String=');
284 it('NullableType', function () {
285 doctrine.type.stringify({
286 type: doctrine.Syntax.NullableType,
288 type: doctrine.Syntax.NameExpression,
292 }).should.equal('?String');
294 doctrine.type.stringify({
295 type: doctrine.Syntax.NullableType,
297 type: doctrine.Syntax.NameExpression,
301 }).should.equal('String?');
304 it('TypeApplication', function () {
305 doctrine.type.stringify({
306 type: doctrine.Syntax.TypeApplication,
308 type: doctrine.Syntax.NameExpression,
313 type: doctrine.Syntax.NameExpression,
317 }).should.equal('Array.<String>');
319 doctrine.type.stringify({
320 type: doctrine.Syntax.TypeApplication,
322 type: doctrine.Syntax.NameExpression,
327 type: doctrine.Syntax.NameExpression,
331 type: doctrine.Syntax.AllLiteral
334 }).should.equal('Array.<String, *>');
338 describe('Complex identity', function () {
339 it('Functions', function () {
340 var data01 = 'function (): void';
341 doctrine.type.stringify(
342 doctrine.type.parseType(data01)
343 ).should.equal(data01);
345 var data02 = 'function (): String';
346 doctrine.type.stringify(
347 doctrine.type.parseType(data02)
348 ).should.equal(data02);
350 var data03 = 'function (test: string): String';
351 doctrine.type.stringify(
352 doctrine.type.parseType(data03)
353 ).should.equal(data03);
355 var data04 = 'function (this: Date, test: String): String';
356 doctrine.type.stringify(
357 doctrine.type.parseType(data04)
358 ).should.equal(data04);
360 var data05 = 'function (this: Date, a: String, b: Number): String';
361 doctrine.type.stringify(
362 doctrine.type.parseType(data05)
363 ).should.equal(data05);
365 var data06 = 'function (this: Date, a: Array.<String, Number>, b: Number): String';
366 doctrine.type.stringify(
367 doctrine.type.parseType(data06)
368 ).should.equal(data06);
370 var data07 = 'function (new: Date, a: Array.<String, Number>, b: Number): HashMap.<String, Number>';
371 doctrine.type.stringify(
372 doctrine.type.parseType(data07)
373 ).should.equal(data07);
375 var data08 = 'function (new: Date, a: Array.<String, Number>, b: (Number|String|Date)): HashMap.<String, Number>';
376 doctrine.type.stringify(
377 doctrine.type.parseType(data08)
378 ).should.equal(data08);
380 var data09 = 'function (new: Date)';
381 doctrine.type.stringify(
382 doctrine.type.parseType(data09)
383 ).should.equal(data09);
385 var data10 = 'function (this: Date)';
386 doctrine.type.stringify(
387 doctrine.type.parseType(data10)
388 ).should.equal(data10);
390 var data11 = 'function (this: Date, ...list)';
391 doctrine.type.stringify(
392 doctrine.type.parseType(data11)
393 ).should.equal(data11);
395 var data11a = 'function (this: Date, test: String=)';
396 doctrine.type.stringify(
397 doctrine.type.parseType(data11a)
398 ).should.equal(data11a);
400 // raw ... are not supported
401 // var data12 = 'function (this: Date, ...)';
402 // doctrine.type.stringify(
403 // doctrine.type.parseType(data12)
404 // ).should.equal(data12);
406 var data12a = 'function (this: Date, ?=)';
407 doctrine.type.stringify(
408 doctrine.type.parseType(data12a)
409 ).should.equal(data12a);
413 /* vim: set sw=4 ts=4 et tw=80 : */