/
model_special_cases_test.dart
477 lines (424 loc) · 19.8 KB
/
model_special_cases_test.dart
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// This test library handles checks against the model for configurations
/// that require different PackageGraph configurations. Since those
/// take a long time to initialize, isolate them here to keep model_test
/// fast.
library dartdoc.model_special_cases_test;
import 'dart:io';
import 'package:analyzer/dart/element/type.dart';
import 'package:async/async.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/package_config_provider.dart';
import 'package:dartdoc/src/package_meta.dart';
import 'package:dartdoc/src/special_elements.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';
import '../src/utils.dart' as utils;
final String _platformVersionString = Platform.version.split(' ').first;
final Version _platformVersion = Version.parse(_platformVersionString);
final _testPackageGraphExperimentsMemo = AsyncMemoizer<PackageGraph>();
Future<PackageGraph> get _testPackageGraphExperiments =>
_testPackageGraphExperimentsMemo.runOnce(() => utils.bootBasicPackage(
'testing/test_package_experiments',
pubPackageMetaProvider,
PhysicalPackageConfigProvider(),
additionalArguments: ['--no-link-to-remote']));
final _testPackageGraphGinormousMemo = AsyncMemoizer<PackageGraph>();
Future<PackageGraph> get _testPackageGraphGinormous =>
_testPackageGraphGinormousMemo.runOnce(() => utils.bootBasicPackage(
'testing/test_package',
pubPackageMetaProvider,
PhysicalPackageConfigProvider(),
excludeLibraries: [
'css',
'code_in_commnets',
'excluded'
],
additionalArguments: [
'--auto-include-dependencies',
'--no-link-to-remote'
]));
final _testPackageGraphSdkMemo = AsyncMemoizer<PackageGraph>();
Future<PackageGraph> get _testPackageGraphSdk =>
_testPackageGraphSdkMemo.runOnce(_bootSdkPackage);
Future<PackageGraph> _bootSdkPackage() async {
return PubPackageBuilder(
await utils.contextFromArgv(
['--input', pubPackageMetaProvider.defaultSdkDir.path],
pubPackageMetaProvider),
pubPackageMetaProvider,
PhysicalPackageConfigProvider())
.buildPackageGraph();
}
void main() {
var sdkDir = pubPackageMetaProvider.defaultSdkDir;
if (sdkDir == null) {
print('Warning: unable to locate the Dart SDK.');
exit(1);
}
// We can not use ExperimentalFeature.releaseVersion or even
// ExperimentalFeature.experimentalReleaseVersion as these are set to null
// even when partial analyzer implementations are available, and are often
// set too high after release.
final _generalizedTypedefsAllowed =
VersionRange(min: Version.parse('2.13.0-0'), includeMin: true);
final _genericMetadataAllowed =
VersionRange(min: Version.parse('2.14.0-0'), includeMin: true);
final _tripleShiftAllowed =
VersionRange(min: Version.parse('2.14.0-0'), includeMin: true);
// Experimental features not yet enabled by default. Move tests out of this
// block when the feature is enabled by default.
group('Experiments', () {
group('triple-shift', () {
Library tripleShift;
Class C, E, F;
Extension ShiftIt;
Operator classShift, extensionShift;
Field constantTripleShifted;
setUpAll(() async {
tripleShift = (await _testPackageGraphExperiments)
.libraries
.firstWhere((l) => l.name == 'triple_shift');
C = tripleShift.classes.firstWhere((c) => c.name == 'C');
E = tripleShift.classes.firstWhere((c) => c.name == 'E');
F = tripleShift.classes.firstWhere((c) => c.name == 'F');
ShiftIt = tripleShift.extensions.firstWhere((e) => e.name == 'ShiftIt');
classShift =
C.instanceOperators.firstWhere((o) => o.name.contains('>>>'));
extensionShift =
ShiftIt.instanceOperators.firstWhere((o) => o.name.contains('>>>'));
constantTripleShifted = C.constantFields
.firstWhere((f) => f.name == 'constantTripleShifted');
});
test('constants with triple shift render correctly', () {
expect(constantTripleShifted.constantValue, equals('3 >>> 5'));
});
test('operators exist and are named correctly', () {
expect(classShift.name, equals('operator >>>'));
expect(extensionShift.name, equals('operator >>>'));
});
test(
'inheritance and overriding of triple shift operators works correctly',
() {
var tripleShiftE =
E.instanceOperators.firstWhere((o) => o.name.contains('>>>'));
var tripleShiftF =
F.instanceOperators.firstWhere((o) => o.name.contains('>>>'));
expect(tripleShiftE.isInherited, isTrue);
expect(tripleShiftE.canonicalModelElement, equals(classShift));
expect(tripleShiftE.modelType.returnType.name, equals('C'));
expect(tripleShiftF.isInherited, isFalse);
expect(tripleShiftF.modelType.returnType.name, equals('F'));
});
}, skip: !_tripleShiftAllowed.allows(_platformVersion));
group('generic metadata', () {
Library genericMetadata;
TopLevelVariable f;
Typedef F;
Class C;
Method mp, mn;
setUpAll(() async {
genericMetadata = (await _testPackageGraphExperiments)
.libraries
.firstWhere((l) => l.name == 'generic_metadata');
F = genericMetadata.typedefs.firstWhere((t) => t.name == 'F');
f = genericMetadata.properties.firstWhere((p) => p.name == 'f');
C = genericMetadata.classes.firstWhere((c) => c.name == 'C');
mp = C.instanceMethods.firstWhere((m) => m.name == 'mp');
mn = C.instanceMethods.firstWhere((m) => m.name == 'mn');
});
test(
'Verify annotations and their type arguments render on type parameters for typedefs',
() {
expect((F.aliasedType as FunctionType).typeFormals.first.metadata,
isNotEmpty);
expect((F.aliasedType as FunctionType).parameters.first.metadata,
isNotEmpty);
// TODO(jcollins-g): add rendering verification once we have data from
// analyzer.
}, skip: 'dart-lang/sdk#46064');
test('Verify type arguments on annotations renders, including parameters',
() {
var ab0 =
'@<a href="%%__HTMLBASE_dartdoc_internal__%%generic_metadata/A-class.html">A</a><span class="signature"><<wbr><span class="type-parameter"><a href="%%__HTMLBASE_dartdoc_internal__%%generic_metadata/B.html">B</a></span>></span>(0)';
expect(genericMetadata.annotations.first.linkedNameWithParameters,
equals(ab0));
expect(f.annotations.first.linkedNameWithParameters, equals(ab0));
expect(C.annotations.first.linkedNameWithParameters, equals(ab0));
expect(
C.typeParameters.first.annotations.first.linkedNameWithParameters,
equals(ab0));
expect(
mp.parameters
.map((p) => p.annotations.first.linkedNameWithParameters),
everyElement(equals(ab0)));
expect(
mn.parameters
.map((p) => p.annotations.first.linkedNameWithParameters),
everyElement(equals(ab0)));
expect(genericMetadata.features.map((f) => f.linkedNameWithParameters),
contains(ab0));
expect(
f.features.map((f) => f.linkedNameWithParameters), contains(ab0));
expect(
C.features.map((f) => f.linkedNameWithParameters), contains(ab0));
expect(
C.typeParameters.first.features
.map((f) => f.linkedNameWithParameters),
contains(ab0));
expect(
mp.parameters
.map((p) => p.features.map((f) => f.linkedNameWithParameters)),
everyElement(contains(ab0)));
expect(
mn.parameters
.map((p) => p.features.map((f) => f.linkedNameWithParameters)),
everyElement(contains(ab0)));
});
}, skip: !_genericMetadataAllowed.allows(_platformVersion));
group('generalized typedefs', () {
Library generalizedTypedefs;
Typedef T0, T1, T2, T3, T4, T5, T6, T7;
Class C, C2;
setUpAll(() async {
generalizedTypedefs = (await _testPackageGraphExperiments)
.libraries
.firstWhere((l) => l.name == 'generalized_typedefs');
T0 = generalizedTypedefs.typedefs.firstWhere((a) => a.name == 'T0');
T1 = generalizedTypedefs.typedefs.firstWhere((a) => a.name == 'T1');
T2 = generalizedTypedefs.typedefs.firstWhere((a) => a.name == 'T2');
T3 = generalizedTypedefs.typedefs.firstWhere((a) => a.name == 'T3');
T4 = generalizedTypedefs.typedefs.firstWhere((a) => a.name == 'T4');
T5 = generalizedTypedefs.typedefs.firstWhere((a) => a.name == 'T5');
T6 = generalizedTypedefs.typedefs.firstWhere((a) => a.name == 'T6');
T7 = generalizedTypedefs.typedefs.firstWhere((a) => a.name == 'T7');
C = generalizedTypedefs.classes.firstWhere((c) => c.name == 'C');
C2 = generalizedTypedefs.classes.firstWhere((c) => c.name == 'C2');
});
void expectTypedefs(Typedef t, String modelTypeToString,
Iterable<String> genericParameters) {
expect(t.modelType.toString(), equals(modelTypeToString));
expect(t.typeParameters.map((p) => p.element.toString()),
orderedEquals(genericParameters));
}
void expectAliasedTypeName(AliasedElementType n, expected) {
expect(n.aliasElement.name, expected);
}
test('typedef references display aliases', () {
var g = C.instanceMethods.firstWhere((m) => m.name == 'g');
var c = C2.allFields.firstWhere((f) => f.name == 'c');
var d = C2.instanceMethods.firstWhere((f) => f.name == 'd');
expectAliasedTypeName(c.modelType, equals('T1'));
expectAliasedTypeName(d.modelType.returnType, equals('T2'));
expectAliasedTypeName(d.parameters.first.modelType, equals('T3'));
expectAliasedTypeName(d.parameters.last.modelType, equals('T4'));
expectAliasedTypeName(g.modelType.returnType, equals('T1'));
expectAliasedTypeName(
g.modelType.parameters.first.modelType, equals('T2'));
expectAliasedTypeName(
g.modelType.parameters.last.modelType, equals('T3'));
});
test('typedef references to special types work', () {
var a = generalizedTypedefs.properties.firstWhere((p) => p.name == 'a');
var b = C2.allFields.firstWhere((f) => f.name == 'b');
var f = C.allFields.firstWhere((f) => f.name == 'f');
expectAliasedTypeName(a.modelType, equals('T0'));
expectAliasedTypeName(b.modelType, equals('T0'));
expectAliasedTypeName(f.modelType, equals('T0'));
}, skip: 'dart-lang/sdk#45291');
test('basic non-function typedefs work', () {
expectTypedefs(T0, 'void', []);
expectTypedefs(T1, 'Function', []);
expectTypedefs(T2, 'List<X>', ['out X']);
expectTypedefs(T3, 'Map<X, Y>', ['out X', 'out Y']);
expectTypedefs(T4, 'void Function()', []);
expectTypedefs(T5, 'X Function(X, {X name})', ['inout X']);
expectTypedefs(T6, 'X Function(Y, [Map<Y, Y>])', ['out X', 'in Y']);
expectTypedefs(T7, 'X Function(Y, [Map<Y, Y>])',
['out X extends String', 'in Y extends List<X>']);
});
}, skip: (!_generalizedTypedefsAllowed.allows(_platformVersion)));
});
group('HTML Injection when allowed', () {
Class htmlInjection;
Method injectSimpleHtml;
Method injectHtmlFromTool;
PackageGraph injectionPackageGraph;
Library injectionExLibrary;
setUpAll(() async {
injectionPackageGraph = await utils.bootBasicPackage(
'testing/test_package',
pubPackageMetaProvider,
PhysicalPackageConfigProvider(),
excludeLibraries: ['css', 'code_in_comments', 'excluded'],
additionalArguments: ['--inject-html']);
injectionExLibrary =
injectionPackageGraph.libraries.firstWhere((lib) => lib.name == 'ex');
htmlInjection = injectionExLibrary.classes
.firstWhere((c) => c.name == 'HtmlInjection');
injectSimpleHtml = htmlInjection.instanceMethods
.firstWhere((m) => m.name == 'injectSimpleHtml');
injectHtmlFromTool = htmlInjection.instanceMethods
.firstWhere((m) => m.name == 'injectHtmlFromTool');
injectionPackageGraph.allLocalModelElements
.forEach((m) => m.documentation);
});
test('can inject HTML', () {
expect(injectSimpleHtml.documentationAsHtml,
contains(' <div style="opacity: 0.5;">[HtmlInjection]</div>'));
});
test('can inject HTML from tool', () {
var envLine = RegExp(r'^Env: \{', multiLine: true);
expect(envLine.allMatches(injectHtmlFromTool.documentation).length,
equals(2));
var argLine = RegExp(r'^Args: \[', multiLine: true);
expect(argLine.allMatches(injectHtmlFromTool.documentation).length,
equals(2));
expect(
injectHtmlFromTool.documentation,
contains(
'Invokes more than one tool in the same comment block, and injects HTML.'));
expect(injectHtmlFromTool.documentationAsHtml,
contains('<div class="title">Title</div>'));
expect(injectHtmlFromTool.documentationAsHtml,
isNot(contains('{@inject-html}')));
expect(injectHtmlFromTool.documentationAsHtml,
isNot(contains('{@end-inject-html}')));
});
test('tool outputs a macro which outputs injected HTML', () {
var ToolPrintingMacroWhichInjectsHtml = injectionExLibrary.allClasses
.firstWhere((c) => c.name == 'ToolPrintingMacroWhichInjectsHtml');
var a = ToolPrintingMacroWhichInjectsHtml.instanceFields
.firstWhere((m) => m.name == 'a');
expect(a.documentationAsHtml,
contains('<p>Text.</p>\n<p><div class="title">Title</div></p>'));
var b = ToolPrintingMacroWhichInjectsHtml.instanceFields
.firstWhere((m) => m.name == 'b');
expect(b.documentationAsHtml,
contains('<p>Text.</p>\n<p><div class="title">Title</div></p>'));
});
});
group('Missing and Remote', () {
PackageGraph ginormousPackageGraph;
setUpAll(() async {
ginormousPackageGraph = await _testPackageGraphGinormous;
});
test('Verify that SDK libraries are not canonical when missing', () {
expect(ginormousPackageGraph.publicPackages, isNotEmpty);
});
test(
'Verify that autoIncludeDependencies makes everything document locally',
() {
expect(ginormousPackageGraph.packages.map((p) => p.documentedWhere),
everyElement((x) => x == DocumentLocation.local));
});
test('Verify that ginormousPackageGraph takes in the SDK', () {
expect(
ginormousPackageGraph.packages
.firstWhere((p) => p.isSdk)
.libraries
.length,
greaterThan(1));
expect(
ginormousPackageGraph.packages
.firstWhere((p) => p.isSdk)
.documentedWhere,
equals(DocumentLocation.local));
});
});
group('Category', () {
PackageGraph ginormousPackageGraph;
setUpAll(() async {
ginormousPackageGraph = await _testPackageGraphGinormous;
});
test(
'Verify auto-included dependencies do not use default package category definitions',
() {
var IAmAClassWithCategories = ginormousPackageGraph.localPackages
.firstWhere((Package p) => p.name == 'test_package_imported')
.publicLibraries
.firstWhere((Library l) => l.name == 'categoriesExported')
.publicClasses
.firstWhere((Class c) => c.name == 'IAmAClassWithCategories');
expect(IAmAClassWithCategories.hasCategoryNames, isTrue);
expect(IAmAClassWithCategories.categories.length, equals(1));
expect(
IAmAClassWithCategories.categories.first.name, equals('Excellent'));
expect(IAmAClassWithCategories.displayedCategories, isEmpty);
});
// For flutter, we allow reexports to pick up categories from the package
// they are exposed in.
test('Verify that reexported classes pick up categories', () {
var IAmAClassWithCategoriesReexport = ginormousPackageGraph.localPackages
.firstWhere((Package p) => p.name == 'test_package')
.publicLibraries
.firstWhere((Library l) => l.name == 'fake')
.publicClasses
.firstWhere((Class c) => c.name == 'IAmAClassWithCategories');
expect(IAmAClassWithCategoriesReexport.hasCategoryNames, isTrue);
expect(IAmAClassWithCategoriesReexport.categories.length, equals(1));
expect(IAmAClassWithCategoriesReexport.categories.first.name,
equals('Superb'));
expect(IAmAClassWithCategoriesReexport.displayedCategories, isNotEmpty);
var category = IAmAClassWithCategoriesReexport.displayedCategories.first;
expect(category.categoryIndex, equals(0));
expect(category.isDocumented, isTrue);
});
test('Verify that multiple categories work correctly', () {
var fakeLibrary = ginormousPackageGraph.localPackages
.firstWhere((Package p) => p.name == 'test_package')
.publicLibraries
.firstWhere((Library l) => l.name == 'fake');
var BaseForDocComments = fakeLibrary.publicClasses
.firstWhere((Class c) => c.name == 'BaseForDocComments');
var SubForDocComments = fakeLibrary.publicClasses
.firstWhere((Class c) => c.name == 'SubForDocComments');
expect(BaseForDocComments.hasCategoryNames, isTrue);
// Display both, with the correct order and display name.
expect(BaseForDocComments.displayedCategories.length, equals(2));
expect(
BaseForDocComments.displayedCategories.first.name, equals('Superb'));
expect(
BaseForDocComments.displayedCategories.last.name, equals('Unreal'));
// Subclasses do not inherit category information.
expect(SubForDocComments.hasCategoryNames, isTrue);
expect(SubForDocComments.categories, hasLength(1));
expect(SubForDocComments.categories.first.isDocumented, isFalse);
expect(SubForDocComments.displayedCategories, isEmpty);
});
});
group('Package', () {
PackageGraph sdkAsPackageGraph;
setUpAll(() async {
sdkAsPackageGraph = await _testPackageGraphSdk;
});
// Analyzer's MockSdk's html library doesn't conform to the expectations
// of this test.
test('Verify Interceptor is hidden from inheritance in docs', () {
var htmlLibrary =
sdkAsPackageGraph.libraries.singleWhere((l) => l.name == 'dart:html');
var eventTarget =
htmlLibrary.allClasses.singleWhere((c) => c.name == 'EventTarget');
var hashCode = eventTarget.publicInstanceFields
.singleWhere((f) => f.name == 'hashCode');
var objectModelElement =
sdkAsPackageGraph.specialClasses[SpecialClass.object];
// If this fails, EventTarget might have been changed to no longer
// inherit from Interceptor. If that's true, adjust test case to
// another class that does.
expect(hashCode.inheritance.any((c) => c.name == 'Interceptor'), isTrue);
// If EventTarget really does start implementing hashCode, this will
// fail.
expect(hashCode.href,
equals('${htmlBasePlaceholder}dart-core/Object/hashCode.html'));
expect(hashCode.canonicalEnclosingContainer, equals(objectModelElement));
expect(
eventTarget.publicSuperChainReversed
.any((et) => et.name == 'Interceptor'),
isFalse);
});
});
}