|
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 |
| -} |
| 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}${node.properties ? '\n' : ''}${(node.properties || [ |
| 10 | + ]) |
| 11 | + .map(printTree(indent + 1)) |
| 12 | + .join('\n')}`; |
16 | 13 |
|
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 |
| -}); |
| 14 | +var printNesting = params => |
| 15 | + printTree(0)({ properties: nestTag(params), name: 'root' }); |
27 | 16 |
|
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); |
| 17 | +test('nest params - basic', function(t) { |
| 18 | + var params = [ |
| 19 | + 'foo', |
| 20 | + 'foo.bar', |
| 21 | + 'foo.bar.third', |
| 22 | + 'foo.third', |
| 23 | + 'foo.third[].baz' |
| 24 | + ].map(name => ({ name })); |
| 25 | + t.equal( |
| 26 | + printNesting(params), |
| 27 | + `- root |
| 28 | + - foo |
| 29 | + - foo.bar |
| 30 | + - foo.bar.third |
| 31 | + - foo.third |
| 32 | + - foo.third[].baz` |
| 33 | + ); |
35 | 34 | t.end();
|
36 | 35 | });
|
37 | 36 |
|
38 |
| -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'); |
| 37 | +test('nest params - multiple roots', function(t) { |
| 38 | + var params = ['a', 'b', 'c'].map(name => ({ name })); |
| 39 | + t.equal( |
| 40 | + printNesting(params), |
| 41 | + `- root |
| 42 | + - a |
| 43 | + - b |
| 44 | + - c` |
| 45 | + ); |
51 | 46 | t.end();
|
52 | 47 | });
|
53 | 48 |
|
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 |
| - */ |
| 49 | +test('nest params - missing parent', function(t) { |
| 50 | + var params = ['foo', 'foo.bar.third'].map(name => ({ name })); |
| 51 | + t.throws(() => { |
| 52 | + nestTag(params); |
61 | 53 | });
|
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 | 54 | t.end();
|
68 | 55 | });
|
69 | 56 |
|
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'); |
| 57 | +test('nest params - #658', function(t) { |
| 58 | + var params = [ |
| 59 | + 'state', |
| 60 | + 'payload', |
| 61 | + 'payload.input_meter_levels', |
| 62 | + 'payload.input_meter_levels[].peak', |
| 63 | + 'payload.input_meter_levels[].rms', |
| 64 | + 'payload.output_meter_levels', |
| 65 | + 'payload.output_meter_levels[].peak', |
| 66 | + 'payload.output_meter_levels[].rms' |
| 67 | + ].map(name => ({ name })); |
| 68 | + t.equal( |
| 69 | + printNesting(params), |
| 70 | + `- root |
| 71 | + - state |
| 72 | + - payload |
| 73 | + - payload.input_meter_levels |
| 74 | + - payload.input_meter_levels[].peak |
| 75 | + - payload.input_meter_levels[].rms |
| 76 | + - payload.output_meter_levels |
| 77 | + - payload.output_meter_levels[].peak |
| 78 | + - payload.output_meter_levels[].rms` |
| 79 | + ); |
83 | 80 | t.end();
|
84 | 81 | });
|
85 | 82 |
|
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' |
| 83 | +test('nest params - #554', function(t) { |
| 84 | + var params = [ |
| 85 | + 'x', |
| 86 | + 'yIn', |
| 87 | + 'options', |
| 88 | + 'options.sgOption', |
| 89 | + 'options.minMaxRatio', |
| 90 | + 'options.broadRatio', |
| 91 | + 'options.noiseLevel', |
| 92 | + 'options.maxCriteria', |
| 93 | + 'options.smoothY', |
| 94 | + 'options.realTopDetection', |
| 95 | + 'options.heightFactor', |
| 96 | + 'options.boundaries', |
| 97 | + 'options.derivativeThreshold' |
| 98 | + ].map(name => ({ name })); |
| 99 | + t.equal( |
| 100 | + printNesting(params), |
| 101 | + `- root |
| 102 | + - x |
| 103 | + - yIn |
| 104 | + - options |
| 105 | + - options.sgOption |
| 106 | + - options.minMaxRatio |
| 107 | + - options.broadRatio |
| 108 | + - options.noiseLevel |
| 109 | + - options.maxCriteria |
| 110 | + - options.smoothY |
| 111 | + - options.realTopDetection |
| 112 | + - options.heightFactor |
| 113 | + - options.boundaries |
| 114 | + - options.derivativeThreshold` |
98 | 115 | );
|
99 |
| - t.equal(result[0].params[0].name, 'foo.bar'); |
100 | 116 | t.end();
|
101 | 117 | });
|
0 commit comments