-
Notifications
You must be signed in to change notification settings - Fork 486
/
Copy pathhierarchy.js
202 lines (180 loc) · 3.97 KB
/
hierarchy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const parse = require('../../src/parsers/javascript');
const hierarchy = require('../../src/hierarchy');
function toComments(fn, filename) {
return parse(
{
file: filename || 'test.js',
source: fn instanceof Function ? '(' + fn.toString() + ')' : fn
},
{}
);
}
function evaluate(fn, callback) {
return hierarchy(toComments(fn, callback));
}
function map(arr, prop) {
return arr.map(function(item) {
return item[prop];
});
}
test('hierarchy', function() {
const comments = evaluate(function() {
/**
* @name Class
* @class
*/
/**
* @name getFoo
* @memberof Class
* @instance
*/
/**
* @name isClass
* @memberof Class
* @static
*/
/**
* @name MAGIC_NUMBER
* @memberof Class
*/
/**
* @name event
* @memberof Class
* @kind event
*/
});
expect(map(comments, 'name')).toEqual(['Class']);
const classMembers = comments[0].members;
expect(map(classMembers.static, 'name')).toEqual(['isClass', 'MAGIC_NUMBER']);
expect(map(classMembers.instance, 'name')).toEqual(['getFoo']);
expect(map(classMembers.static[0].path, 'name')).toEqual([
'Class',
'isClass'
]);
expect(map(classMembers.instance[0].path, 'name')).toEqual([
'Class',
'getFoo'
]);
expect(map(classMembers.events[0].path, 'name')).toEqual(['Class', 'event']);
});
test('hierarchy - nesting', function() {
const comments = evaluate(function() {
/**
* @name Parent
* @class
*/
/**
* @name enum
* @memberof Parent
*/
/**
* @name Parent
* @memberof Parent.enum
*/
/**
* @name Child
* @memberof Parent.enum
*/
});
expect(map(comments, 'name')).toEqual(['Parent']);
const classMembers = comments[0].members;
expect(map(classMembers.static, 'name')).toEqual(['enum']);
const enumMembers = classMembers.static[0].members;
expect(map(enumMembers.static, 'name')).toEqual(['Parent', 'Child']);
expect(map(enumMembers.static[0].path, 'name')).toEqual([
'Parent',
'enum',
'Parent'
]);
expect(map(enumMembers.static[1].path, 'name')).toEqual([
'Parent',
'enum',
'Child'
]);
});
test('hierarchy - multisignature', function() {
const comments = evaluate(function() {
/**
* @name Parent
* @class
*/
/**
* @name foo
* @memberof Parent
* @instance
*/
/**
* @name foo
* @memberof Parent
* @instance
*/
});
expect(map(comments[0].members.instance, 'name')).toEqual(['foo', 'foo']);
});
test('hierarchy - missing memberof', function() {
const test = evaluate(function() {
/**
* @name test
* @memberof DoesNotExist
*/
})[0];
expect(test.errors).toEqual([
{
message: '@memberof reference to DoesNotExist not found',
commentLineNumber: 2
}
]);
});
test('hierarchy - anonymous', function() {
const result = evaluate(function() {
/** Test */
})[0];
expect(result.errors).toEqual([
{
message: 'could not determine @name for hierarchy'
}
]);
});
test('hierarchy - object prototype member names', function() {
const comments = evaluate(function() {
/**
* @name should
* @function
*/
/**
* @name Assertion
* @class
* @memberof should
*/
/**
* @name hasOwnProperty
* @memberof should.Assertion
* @instance
* @function
**/
/**
* @name otherMethod
* @memberof should.Assertion
* @instance
* @function
**/
});
expect(map(comments[0].members.static[0].members.instance, 'name')).toEqual([
'hasOwnProperty',
'otherMethod'
]);
});
test('hierarchy - member namespace defaults to static', function() {
const comments = evaluate(function() {
/**
* @namespace fooNamespace
*/
/**
* @class BarClass
* @memberof fooNamespace
*/
});
expect(comments[0].members.static[0].namespace).toEqual(
'fooNamespace.BarClass'
);
});