|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -var test = require('tap').test, |
4 |
| - parse = require('../../lib/parsers/javascript'), |
5 |
| - nest = require('../../lib/nest'); |
| 3 | +var test = require('tap').test; |
| 4 | +var nestTag = require('../../lib/nest').nestTag; |
6 | 5 |
|
7 |
| -function toComment(fn, filename) { |
8 |
| - return parse( |
9 |
| - { |
10 |
| - file: filename, |
11 |
| - source: fn instanceof Function ? '(' + fn.toString() + ')' : fn |
12 |
| - }, |
13 |
| - {} |
14 |
| - ).map(nest); |
15 |
| -} |
16 |
| - |
17 |
| -test('nest params - no params', function(t) { |
18 |
| - t.deepEqual( |
19 |
| - toComment(function() { |
20 |
| - /** @name foo */ |
21 |
| - })[0].params, |
22 |
| - [], |
23 |
| - 'no params' |
24 |
| - ); |
25 |
| - t.end(); |
26 |
| -}); |
27 |
| - |
28 |
| -test('nest params - no nesting', function(t) { |
29 |
| - var result = toComment(function() { |
30 |
| - /** @param {Object} foo */ |
31 |
| - }); |
32 |
| - t.equal(result[0].params.length, 1); |
33 |
| - t.equal(result[0].params[0].name, 'foo'); |
34 |
| - t.equal(result[0].params[0].properties, undefined); |
35 |
| - t.end(); |
36 |
| -}); |
| 6 | +// Print a tree of tags in a way that's easy to test. |
| 7 | +var printTree = indent => |
| 8 | + node => |
| 9 | + `${new Array(indent + 1).join(' ')}- ${node.name}\n${(node.children || []) |
| 10 | + .map(printTree(indent + 1)) |
| 11 | + .join('\n')}`; |
37 | 12 |
|
38 | 13 | test('nest params - basic', function(t) {
|
39 |
| - var result = toComment(function() { |
40 |
| - /** |
41 |
| - * @param {Object} foo |
42 |
| - * @param {string} foo.bar |
43 |
| - * @param {string} foo.baz |
44 |
| - */ |
45 |
| - }); |
46 |
| - t.equal(result[0].params.length, 1); |
47 |
| - t.equal(result[0].params[0].name, 'foo'); |
48 |
| - t.equal(result[0].params[0].properties.length, 2); |
49 |
| - t.equal(result[0].params[0].properties[0].name, 'foo.bar'); |
50 |
| - t.equal(result[0].params[0].properties[1].name, 'foo.baz'); |
51 |
| - t.end(); |
52 |
| -}); |
53 |
| - |
54 |
| -test('nest properties - basic', function(t) { |
55 |
| - var result = toComment(function() { |
56 |
| - /** |
57 |
| - * @property {Object} foo |
58 |
| - * @property {string} foo.bar |
59 |
| - * @property {string} foo.baz |
60 |
| - */ |
61 |
| - }); |
62 |
| - t.equal(result[0].properties.length, 1); |
63 |
| - t.equal(result[0].properties[0].name, 'foo'); |
64 |
| - t.equal(result[0].properties[0].properties.length, 2); |
65 |
| - t.equal(result[0].properties[0].properties[0].name, 'foo.bar'); |
66 |
| - t.equal(result[0].properties[0].properties[1].name, 'foo.baz'); |
67 |
| - t.end(); |
68 |
| -}); |
69 |
| - |
70 |
| -test('nest params - array', function(t) { |
71 |
| - var result = toComment(function() { |
72 |
| - /** |
73 |
| - * @param {Object[]} employees - The employees who are responsible for the project. |
74 |
| - * @param {string} employees[].name - The name of an employee. |
75 |
| - * @param {string} employees[].department - The employee's department. |
76 |
| - */ |
77 |
| - }); |
78 |
| - t.equal(result[0].params.length, 1); |
79 |
| - t.equal(result[0].params[0].name, 'employees'); |
80 |
| - t.equal(result[0].params[0].properties.length, 2); |
81 |
| - t.equal(result[0].params[0].properties[0].name, 'employees[].name'); |
82 |
| - t.equal(result[0].params[0].properties[1].name, 'employees[].department'); |
83 |
| - t.end(); |
84 |
| -}); |
85 |
| - |
86 |
| -test('nest params - missing parent', function(t) { |
87 |
| - var result = toComment(function() { |
88 |
| - /** @param {string} foo.bar */ |
89 |
| - }); |
90 |
| - t.equal(result[0].params.length, 1); |
91 |
| - t.deepEqual( |
92 |
| - result[0].errors[0], |
93 |
| - { |
94 |
| - message: "@param foo.bar's parent foo not found", |
95 |
| - commentLineNumber: 0 |
96 |
| - }, |
97 |
| - 'correct error message' |
| 14 | + var params = [ |
| 15 | + 'foo', |
| 16 | + 'foo.bar', |
| 17 | + 'foo.bar.third', |
| 18 | + 'foo.third', |
| 19 | + 'foo.third[].baz' |
| 20 | + ].map(name => ({ name })); |
| 21 | + var result = nestTag(params); |
| 22 | + t.equal( |
| 23 | + printTree(0)(result.children[0]), |
| 24 | + `- foo |
| 25 | + - foo.bar |
| 26 | + - foo.bar.third |
| 27 | +
|
| 28 | + - foo.third |
| 29 | + - foo.third[].baz |
| 30 | +` |
98 | 31 | );
|
99 |
| - t.equal(result[0].params[0].name, 'foo.bar'); |
100 | 32 | t.end();
|
101 | 33 | });
|
0 commit comments