forked from dart-lang/dartdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_test.dart
3814 lines (3355 loc) · 148 KB
/
model_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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015, 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.
library dartdoc.model_test;
import 'dart:io';
import 'package:dartdoc/dartdoc.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/render/category_renderer.dart';
import 'package:dartdoc/src/render/enum_field_renderer.dart';
import 'package:dartdoc/src/render/model_element_renderer.dart';
import 'package:dartdoc/src/render/parameter_renderer.dart';
import 'package:dartdoc/src/render/typedef_renderer.dart';
import 'package:dartdoc/src/special_elements.dart';
import 'package:dartdoc/src/warnings.dart';
import 'package:test/test.dart';
import 'src/utils.dart' as utils;
/// For testing sort behavior.
class TestLibraryContainer extends LibraryContainer with Nameable {
@override
final List<String> containerOrder;
@override
String enclosingName;
@override
final String name;
@override
bool get isSdk => false;
@override
final PackageGraph packageGraph = null;
TestLibraryContainer(
this.name, this.containerOrder, LibraryContainer enclosingContainer) {
enclosingName = enclosingContainer?.name;
}
}
class TestLibraryContainerSdk extends TestLibraryContainer {
TestLibraryContainerSdk(String name, List<String> containerOrder,
LibraryContainer enclosingContainer)
: super(name, containerOrder, enclosingContainer);
@override
bool get isSdk => true;
}
void main() {
var sdkDir = defaultSdkDir;
if (sdkDir == null) {
print('Warning: unable to locate the Dart SDK.');
exit(1);
}
PackageGraph packageGraph;
Library exLibrary;
Library fakeLibrary;
Library twoExportsLib;
Library interceptorsLib;
Library baseClassLib;
Library dartAsync;
setUpAll(() async {
// Use model_special_cases_test.dart for tests that require
// a different package graph.
packageGraph = await utils.testPackageGraph;
exLibrary = packageGraph.libraries.firstWhere((lib) => lib.name == 'ex');
fakeLibrary =
packageGraph.libraries.firstWhere((lib) => lib.name == 'fake');
dartAsync =
packageGraph.libraries.firstWhere((lib) => lib.name == 'dart:async');
twoExportsLib =
packageGraph.libraries.firstWhere((lib) => lib.name == 'two_exports');
interceptorsLib = packageGraph.libraries
.firstWhere((lib) => lib.name == 'dart:_interceptors');
baseClassLib =
packageGraph.libraries.firstWhere((lib) => lib.name == 'base_class');
});
group('Set Literals', () {
Library set_literals;
TopLevelVariable aComplexSet,
inferredTypeSet,
specifiedSet,
untypedMap,
typedSet;
setUpAll(() async {
set_literals = packageGraph.libraries
.firstWhere((lib) => lib.name == 'set_literals');
aComplexSet =
set_literals.constants.firstWhere((v) => v.name == 'aComplexSet');
inferredTypeSet =
set_literals.constants.firstWhere((v) => v.name == 'inferredTypeSet');
specifiedSet =
set_literals.constants.firstWhere((v) => v.name == 'specifiedSet');
untypedMap =
set_literals.constants.firstWhere((v) => v.name == 'untypedMap');
typedSet = set_literals.constants.firstWhere((v) => v.name == 'typedSet');
});
test('Set literals test', () {
expect(aComplexSet.modelType.name, equals('Set'));
expect(aComplexSet.modelType.typeArguments.map((a) => a.name).toList(),
equals(['AClassContainingLiterals']));
expect(aComplexSet.constantValue,
equals('const {const AClassContainingLiterals(3, 5)}'));
expect(inferredTypeSet.modelType.name, equals('Set'));
expect(
inferredTypeSet.modelType.typeArguments.map((a) => a.name).toList(),
equals(['num']));
expect(inferredTypeSet.constantValue, equals('const {1, 2.5, 3}'));
expect(specifiedSet.modelType.name, equals('Set'));
expect(specifiedSet.modelType.typeArguments.map((a) => a.name).toList(),
equals(['int']));
expect(specifiedSet.constantValue, equals('const {}'));
expect(untypedMap.modelType.name, equals('Map'));
expect(untypedMap.modelType.typeArguments.map((a) => a.name).toList(),
equals(['dynamic', 'dynamic']));
expect(untypedMap.constantValue, equals('const {}'));
expect(typedSet.modelType.name, equals('Set'));
expect(typedSet.modelType.typeArguments.map((a) => a.name).toList(),
equals(['String']));
expect(typedSet.constantValue,
matches(RegExp(r'const <String>\s?{}')));
});
});
group('Tools', () {
Class toolUser;
Class _NonCanonicalToolUser, CanonicalToolUser, PrivateLibraryToolUser;
Class ImplementingClassForTool, CanonicalPrivateInheritedToolUser;
Method invokeTool;
Method invokeToolNoInput;
Method invokeToolMultipleSections;
Method invokeToolNonCanonical, invokeToolNonCanonicalSubclass;
Method invokeToolPrivateLibrary, invokeToolPrivateLibraryOriginal;
Method invokeToolParentDoc, invokeToolParentDocOriginal;
// ignore: omit_local_variable_types
final RegExp packageInvocationIndexRegexp =
RegExp(r'PACKAGE_INVOCATION_INDEX: (\d+)');
setUpAll(() {
_NonCanonicalToolUser = fakeLibrary.allClasses
.firstWhere((c) => c.name == '_NonCanonicalToolUser');
CanonicalToolUser = fakeLibrary.allClasses
.firstWhere((c) => c.name == 'CanonicalToolUser');
PrivateLibraryToolUser = fakeLibrary.allClasses
.firstWhere((c) => c.name == 'PrivateLibraryToolUser');
ImplementingClassForTool = fakeLibrary.allClasses
.firstWhere((c) => c.name == 'ImplementingClassForTool');
CanonicalPrivateInheritedToolUser = fakeLibrary.allClasses
.firstWhere((c) => c.name == 'CanonicalPrivateInheritedToolUser');
toolUser = exLibrary.classes.firstWhere((c) => c.name == 'ToolUser');
invokeTool =
toolUser.instanceMethods.firstWhere((m) => m.name == 'invokeTool');
invokeToolNonCanonical = _NonCanonicalToolUser.instanceMethods
.firstWhere((m) => m.name == 'invokeToolNonCanonical');
invokeToolNonCanonicalSubclass = CanonicalToolUser.instanceMethods
.firstWhere((m) => m.name == 'invokeToolNonCanonical');
invokeToolNoInput = toolUser.instanceMethods
.firstWhere((m) => m.name == 'invokeToolNoInput');
invokeToolMultipleSections = toolUser.instanceMethods
.firstWhere((m) => m.name == 'invokeToolMultipleSections');
invokeToolPrivateLibrary = PrivateLibraryToolUser.instanceMethods
.firstWhere((m) => m.name == 'invokeToolPrivateLibrary');
invokeToolPrivateLibraryOriginal =
(invokeToolPrivateLibrary.definingEnclosingContainer as Class)
.instanceMethods
.firstWhere((m) => m.name == 'invokeToolPrivateLibrary');
invokeToolParentDoc = CanonicalPrivateInheritedToolUser.instanceMethods
.firstWhere((m) => m.name == 'invokeToolParentDoc');
invokeToolParentDocOriginal = ImplementingClassForTool.instanceMethods
.firstWhere((m) => m.name == 'invokeToolParentDoc');
packageGraph.allLocalModelElements.forEach((m) => m.documentation);
});
test(
'invokes tool when inherited documentation is the only means for it to be seen',
() {
// Verify setup of the test is correct.
expect(invokeToolParentDoc.isCanonical, isTrue);
expect(invokeToolParentDoc.documentationComment, isNull);
// Error message here might look strange due to toString() on Methods, but if this
// fails that means we don't have the correct invokeToolParentDoc instance.
expect(invokeToolParentDoc.documentationFrom,
contains(invokeToolParentDocOriginal));
// Tool should be substituted out here.
expect(invokeToolParentDoc.documentation, isNot(contains('{@tool')));
});
group('does _not_ invoke a tool multiple times unnecessarily', () {
test('non-canonical subclass case', () {
expect(invokeToolNonCanonical.isCanonical, isFalse);
expect(invokeToolNonCanonicalSubclass.isCanonical, isTrue);
expect(
packageInvocationIndexRegexp
.firstMatch(invokeToolNonCanonical.documentation)
.group(1),
equals(packageInvocationIndexRegexp
.firstMatch(invokeToolNonCanonicalSubclass.documentation)
.group(1)));
expect(
invokeToolPrivateLibrary.documentation, isNot(contains('{@tool')));
expect(
invokeToolPrivateLibraryOriginal.documentation, contains('{@tool'));
});
test('Documentation borrowed from implementer case', () {
expect(
packageInvocationIndexRegexp
.firstMatch(invokeToolParentDoc.documentation)
.group(1),
equals(packageInvocationIndexRegexp
.firstMatch(invokeToolParentDocOriginal.documentation)
.group(1)));
});
});
test('can invoke a tool and pass args and environment', () {
expect(invokeTool.documentation, contains('--file=<INPUT_FILE>'));
expect(invokeTool.documentation,
contains(RegExp(r'--source=lib[/\\]example\.dart_[0-9]+_[0-9]+, ')));
expect(invokeTool.documentation,
contains(RegExp(r'--package-path=<PACKAGE_PATH>, ')));
expect(
invokeTool.documentation, contains('--package-name=test_package, '));
expect(invokeTool.documentation, contains('--library-name=ex, '));
expect(invokeTool.documentation,
contains('--element-name=ToolUser.invokeTool, '));
expect(invokeTool.documentation,
contains(r'''--special= |\[]!@#\"'$%^&*()_+]'''));
expect(invokeTool.documentation, contains('INPUT: <INPUT_FILE>'));
expect(invokeTool.documentation,
contains(RegExp('SOURCE_COLUMN: [0-9]+, ')));
expect(invokeTool.documentation,
contains(RegExp(r'SOURCE_PATH: lib[/\\]example\.dart, ')));
expect(invokeTool.documentation,
contains(RegExp(r'PACKAGE_PATH: <PACKAGE_PATH>, ')));
expect(
invokeTool.documentation, contains('PACKAGE_NAME: test_package, '));
expect(invokeTool.documentation, contains('LIBRARY_NAME: ex, '));
expect(invokeTool.documentation,
contains('ELEMENT_NAME: ToolUser.invokeTool, '));
expect(invokeTool.documentation,
contains(RegExp('INVOCATION_INDEX: [0-9]+}')));
expect(invokeTool.documentation, contains('## `Yes it is a [Dog]!`'));
});
test('can invoke a tool and add a reference link', () {
expect(invokeTool.documentation,
contains('Yes it is a [Dog]! Is not a [ToolUser].'));
expect(
invokeTool.documentationAsHtml,
contains(
'<a href="${HTMLBASE_PLACEHOLDER}ex/ToolUser-class.html">ToolUser</a>'));
expect(
invokeTool.documentationAsHtml,
contains(
'<a href="${HTMLBASE_PLACEHOLDER}ex/Dog-class.html">Dog</a>'));
});
test(r'can invoke a tool with no $INPUT or args', () {
expect(invokeToolNoInput.documentation, contains('Args: []'));
expect(invokeToolNoInput.documentation,
isNot(contains('This text should not appear in the output')));
expect(invokeToolNoInput.documentation, isNot(contains('[Dog]')));
expect(
invokeToolNoInput.documentationAsHtml,
isNot(contains(
'<a href="${HTMLBASE_PLACEHOLDER}ex/Dog-class.html">Dog</a>')));
});
test('can invoke a tool multiple times in one comment block', () {
var envLine = RegExp(r'^Env: \{', multiLine: true);
expect(
envLine.allMatches(invokeToolMultipleSections.documentation).length,
equals(2));
var argLine = RegExp(r'^Args: \[', multiLine: true);
expect(
argLine.allMatches(invokeToolMultipleSections.documentation).length,
equals(2));
expect(invokeToolMultipleSections.documentation,
contains('Invokes more than one tool in the same comment block.'));
expect(invokeToolMultipleSections.documentation,
contains('This text should appear in the output.'));
expect(invokeToolMultipleSections.documentation,
contains('## `This text should appear in the output.`'));
expect(invokeToolMultipleSections.documentation,
contains('This text should also appear in the output.'));
expect(invokeToolMultipleSections.documentation,
contains('## `This text should also appear in the output.`'));
});
});
group('HTML Injection when not allowed', () {
Class htmlInjection;
Method injectSimpleHtml;
setUpAll(() {
htmlInjection =
exLibrary.classes.firstWhere((c) => c.name == 'HtmlInjection');
injectSimpleHtml = htmlInjection.instanceMethods
.firstWhere((m) => m.name == 'injectSimpleHtml');
});
test("doesn't inject HTML if --inject-html option is not present", () {
expect(
injectSimpleHtml.documentation,
isNot(contains(
'\n<dartdoc-html>bad2bbdd4a5cf9efb3212afff4449904756851aa</dartdoc-html>\n')));
expect(injectSimpleHtml.documentation, isNot(contains('<dartdoc-html>')));
expect(injectSimpleHtml.documentationAsHtml, contains('{@inject-html}'));
});
});
group('Missing and Remote', () {
test(
'Verify that SDK libraries are not canonical when documenting a package',
() {
expect(
dartAsync.package.documentedWhere, equals(DocumentLocation.missing));
expect(dartAsync.isCanonical, isFalse);
});
test('Verify that packageGraph has an SDK but will not document it locally',
() {
expect(packageGraph.packages.firstWhere((p) => p.isSdk).documentedWhere,
isNot(equals(DocumentLocation.local)));
});
});
group('Category', () {
test('Verify categories for test_package', () {
expect(packageGraph.localPackages.length, equals(1));
expect(packageGraph.localPackages.first.hasCategories, isTrue);
var packageCategories = packageGraph.localPackages.first.categories;
expect(packageCategories.length, equals(6));
expect(
packageGraph.localPackages.first.categoriesWithPublicLibraries.length,
equals(3));
expect(
packageCategories.map((c) => c.name).toList(),
orderedEquals([
'Superb',
'Unreal',
'Real Libraries',
'Misc',
'More Excellence',
'NotSoExcellent'
]));
expect(packageCategories.map((c) => c.libraries.length).toList(),
orderedEquals([0, 2, 3, 1, 0, 0]));
});
test('Verify libraries with multiple categories show up in multiple places',
() {
var packageCategories = packageGraph.publicPackages.first.categories;
var realLibraries =
packageCategories.firstWhere((c) => c.name == 'Real Libraries');
var misc = packageCategories.firstWhere((c) => c.name == 'Misc');
expect(
realLibraries.libraries.map((l) => l.name), contains('two_exports'));
expect(misc.libraries.map((l) => l.name), contains('two_exports'));
});
test('Verify that libraries without categories get handled', () {
expect(
packageGraph
.localPackages.first.defaultCategory.publicLibraries.length,
// Only 5 libraries have categories, the rest belong in default.
equals(utils.kTestPackagePublicLibraries - 5));
});
// TODO consider moving these to a separate suite
test('CategoryRendererHtml renders category label', () {
var category = packageGraph.publicPackages.first.categories.first;
var renderer = CategoryRendererHtml();
expect(
renderer.renderCategoryLabel(category),
'<span class="category superb cp-0 linked" title="This is part of the Superb Topic.">'
'<a href="${HTMLBASE_PLACEHOLDER}topics/Superb-topic.html">Superb</a></span>');
});
test('CategoryRendererHtml renders linkedName', () {
var category = packageGraph.publicPackages.first.categories.first;
var renderer = CategoryRendererHtml();
expect(renderer.renderLinkedName(category),
'<a href="${HTMLBASE_PLACEHOLDER}topics/Superb-topic.html">Superb</a>');
});
test('CategoryRendererMd renders category label', () {
var category = packageGraph.publicPackages.first.categories.first;
var renderer = CategoryRendererMd();
expect(renderer.renderCategoryLabel(category),
'[Superb](${HTMLBASE_PLACEHOLDER}topics/Superb-topic.html)');
});
test('CategoryRendererMd renders linkedName', () {
var category = packageGraph.publicPackages.first.categories.first;
var renderer = CategoryRendererMd();
expect(renderer.renderLinkedName(category),
'[Superb](${HTMLBASE_PLACEHOLDER}topics/Superb-topic.html)');
});
});
group('LibraryContainer', () {
TestLibraryContainer topLevel;
List<String> sortOrderBasic;
List<String> containerNames;
setUpAll(() {
topLevel = TestLibraryContainer('topLevel', [], null);
sortOrderBasic = ['theFirst', 'second', 'fruit'];
containerNames = [
'moo',
'woot',
'theFirst',
'topLevel Things',
'toplevel',
'fruit'
];
});
test('multiple containers with specified sort order', () {
var containers = <LibraryContainer>[];
for (var i = 0; i < containerNames.length; i++) {
var name = containerNames[i];
containers.add(TestLibraryContainer(name, sortOrderBasic, topLevel));
}
containers.add(TestLibraryContainerSdk('SDK', sortOrderBasic, topLevel));
containers.sort();
expect(
containers.map((c) => c.name),
orderedEquals([
'theFirst',
'fruit',
'toplevel',
'SDK',
'topLevel Things',
'moo',
'woot'
]));
});
test('multiple containers, no specified sort order', () {
var containers = <LibraryContainer>[];
for (var name in containerNames) {
containers.add(TestLibraryContainer(name, [], topLevel));
}
containers.add(TestLibraryContainerSdk('SDK', [], topLevel));
containers.sort();
expect(
containers.map((c) => c.name),
orderedEquals([
'toplevel',
'SDK',
'topLevel Things',
'fruit',
'moo',
'theFirst',
'woot'
]));
});
});
group('Package', () {
group('test package', () {
test('name', () {
expect(packageGraph.defaultPackage.name, 'test_package');
});
test('libraries', () {
expect(packageGraph.localPublicLibraries,
hasLength(utils.kTestPackagePublicLibraries));
expect(interceptorsLib.isPublic, isFalse);
});
test('homepage', () {
expect(packageGraph.defaultPackage.hasHomepage, true);
expect(packageGraph.defaultPackage.homepage,
equals('http://github.com/dart-lang'));
});
test('packages', () {
expect(packageGraph.localPackages, hasLength(1));
var package = packageGraph.localPackages.first;
expect(package.name, 'test_package');
expect(package.publicLibraries,
hasLength(utils.kTestPackagePublicLibraries));
});
test('is documented in library', () {
expect(exLibrary.isDocumented, isTrue);
});
test('has documentation', () {
expect(packageGraph.defaultPackage.hasDocumentationFile, isTrue);
expect(packageGraph.defaultPackage.hasDocumentation, isTrue);
});
test('documentation exists', () {
expect(
packageGraph.defaultPackage.documentation
.startsWith('# Best Package'),
isTrue);
});
test('documentation can be rendered as HTML', () {
expect(packageGraph.defaultPackage.documentationAsHtml,
contains('<h1 id="best-package">Best Package</h1>'));
});
test('has anonymous libraries', () {
expect(
packageGraph.libraries
.where((lib) => lib.name == 'anonymous_library'),
hasLength(1));
expect(
packageGraph.libraries
.where((lib) => lib.name == 'another_anonymous_lib'),
hasLength(1));
});
});
group('SDK-specific cases', () {
test('Verify pragma is hidden in docs', () {
var HasPragma = fakeLibrary.allClasses
.firstWhere((Class c) => c.name == 'HasPragma');
expect(HasPragma.annotations, isEmpty);
});
});
});
group('Library', () {
Library anonLib,
isDeprecated,
someLib,
reexportOneLib,
reexportTwoLib,
reexportThreeLib;
Class SomeClass,
SomeOtherClass,
YetAnotherClass,
AUnicornClass,
ADuplicateClass;
setUpAll(() {
anonLib = packageGraph.libraries
.firstWhere((lib) => lib.name == 'anonymous_library');
someLib = packageGraph.allLibraries.values
.firstWhere((lib) => lib.name == 'reexport.somelib');
reexportOneLib = packageGraph.libraries
.firstWhere((lib) => lib.name == 'reexport_one');
reexportTwoLib = packageGraph.libraries
.firstWhere((lib) => lib.name == 'reexport_two');
reexportThreeLib = packageGraph.libraries
.firstWhere((lib) => lib.name == 'reexport_three');
SomeClass = someLib.getClassByName('SomeClass');
SomeOtherClass = someLib.getClassByName('SomeOtherClass');
YetAnotherClass = someLib.getClassByName('YetAnotherClass');
AUnicornClass = someLib.getClassByName('AUnicornClass');
ADuplicateClass = reexportThreeLib.getClassByName('ADuplicateClass');
isDeprecated = packageGraph.libraries
.firstWhere((lib) => lib.name == 'is_deprecated');
});
test('has a name', () {
expect(exLibrary.name, 'ex');
});
test('has a line number and column', () {
expect(exLibrary.characterLocation, isNotNull);
});
test('packageName', () {
expect(exLibrary.packageName, 'test_package');
});
test('has a fully qualified name', () {
expect(exLibrary.fullyQualifiedName, 'ex');
});
test('can be deprecated', () {
expect(isDeprecated.isDeprecated, isTrue);
expect(anonLib.isDeprecated, isFalse);
});
test('can be reexported even if the file suffix is not .dart', () {
expect(fakeLibrary.allClasses.map((c) => c.name),
contains('MyClassFromADartFile'));
});
test('that is deprecated has a deprecated css class in linkedName', () {
expect(isDeprecated.linkedName, contains('class="deprecated"'));
});
test('has documentation', () {
expect(exLibrary.documentation,
'a library. testing string escaping: `var s = \'a string\'` <cool>\n');
});
test('has one line docs', () {
expect(
fakeLibrary.oneLineDoc,
equals(
'WOW FAKE PACKAGE IS <strong>BEST</strong> <a href="http://example.org">PACKAGE</a>'));
});
test('has properties', () {
expect(exLibrary.hasPublicProperties, isTrue);
});
test('has constants', () {
expect(exLibrary.hasPublicConstants, isTrue);
});
test('has exceptions', () {
expect(exLibrary.hasPublicExceptions, isTrue);
});
test('has mixins', () {
expect(fakeLibrary.hasPublicMixins, isTrue);
expect(reexportTwoLib.hasPublicMixins, isTrue);
});
test('has enums', () {
expect(exLibrary.hasPublicEnums, isTrue);
});
test('has functions', () {
expect(exLibrary.hasPublicFunctions, isTrue);
});
test('has typedefs', () {
expect(exLibrary.hasPublicTypedefs, isTrue);
});
test('exported class', () {
expect(exLibrary.classes.any((c) => c.name == 'Helper'), isTrue);
});
test('exported function', () {
expect(
exLibrary.functions.any((f) => f.name == 'helperFunction'), isFalse);
});
test('anonymous lib', () {
expect(anonLib.isAnonymous, isTrue);
});
test('with ambiguous reexport warnings', () {
final warningMsg =
'(reexport_one, reexport_two) -> reexport_two (confidence 0.000)';
// Unicorn class has a warning because two @canonicalFors cancel each other out.
expect(
packageGraph.packageWarningCounter.hasWarning(
AUnicornClass, PackageWarning.ambiguousReexport, warningMsg),
isTrue);
// This class is ambiguous without a @canonicalFor
expect(
packageGraph.packageWarningCounter.hasWarning(
YetAnotherClass, PackageWarning.ambiguousReexport, warningMsg),
isTrue);
// These two classes have a @canonicalFor
expect(
packageGraph.packageWarningCounter.hasWarning(
SomeClass, PackageWarning.ambiguousReexport, warningMsg),
isFalse);
expect(
packageGraph.packageWarningCounter.hasWarning(
SomeOtherClass, PackageWarning.ambiguousReexport, warningMsg),
isFalse);
// This library has a canonicalFor with no corresponding item
expect(
packageGraph.packageWarningCounter.hasWarning(reexportTwoLib,
PackageWarning.ignoredCanonicalFor, 'something.ThatDoesntExist'),
isTrue);
});
test('@canonicalFor directive works', () {
expect(SomeOtherClass.canonicalLibrary, reexportOneLib);
expect(SomeClass.canonicalLibrary, reexportTwoLib);
});
test('with correct show/hide behavior', () {
expect(ADuplicateClass.definingLibrary.name, equals('shadowing_lib'));
});
});
group('Macros', () {
Class dog;
Enum MacrosFromAccessors;
Method withMacro, withMacro2, withPrivateMacro, withUndefinedMacro;
EnumField macroReferencedHere;
setUpAll(() {
dog = exLibrary.classes.firstWhere((c) => c.name == 'Dog');
withMacro = dog.instanceMethods.firstWhere((m) => m.name == 'withMacro');
withMacro2 =
dog.instanceMethods.firstWhere((m) => m.name == 'withMacro2');
withPrivateMacro =
dog.instanceMethods.firstWhere((m) => m.name == 'withPrivateMacro');
withUndefinedMacro =
dog.instanceMethods.firstWhere((m) => m.name == 'withUndefinedMacro');
MacrosFromAccessors =
fakeLibrary.enums.firstWhere((e) => e.name == 'MacrosFromAccessors');
macroReferencedHere = MacrosFromAccessors.publicConstantFields
.firstWhere((e) => e.name == 'macroReferencedHere');
});
test('renders a macro defined within a enum', () {
expect(macroReferencedHere.documentationAsHtml,
contains('This is a macro defined in an Enum accessor.'));
});
test("renders a macro within the same comment where it's defined", () {
expect(withMacro.documentation,
equals('Macro method\n\nFoo macro content\n\nMore docs'));
});
test("renders a macro in another method, not the same where it's defined",
() {
expect(withMacro2.documentation, equals('Foo macro content'));
});
test('renders a macro defined in a private symbol', () {
expect(withPrivateMacro.documentation, contains('Private macro content'));
});
test('a warning is generated for unknown macros', () {
// Retrieve documentation first to generate the warning.
withUndefinedMacro.documentation;
expect(
packageGraph.packageWarningCounter.hasWarning(withUndefinedMacro,
PackageWarning.unknownMacro, 'ThatDoesNotExist'),
isTrue);
});
});
group('YouTube', () {
Class dog;
Method withYouTubeWatchUrl;
Method withYouTubeInOneLineDoc;
Method withYouTubeInline;
setUpAll(() {
dog = exLibrary.classes.firstWhere((c) => c.name == 'Dog');
withYouTubeWatchUrl = dog.instanceMethods
.firstWhere((m) => m.name == 'withYouTubeWatchUrl');
withYouTubeInOneLineDoc = dog.instanceMethods
.firstWhere((m) => m.name == 'withYouTubeInOneLineDoc');
withYouTubeInline =
dog.instanceMethods.firstWhere((m) => m.name == 'withYouTubeInline');
});
test(
'renders a YouTube video within the method documentation with correct aspect ratio',
() {
expect(
withYouTubeWatchUrl.documentation,
contains(
'<iframe src="https://www.youtube.com/embed/oHg5SJYRHA0?rel=0"'));
// Video is 560x315, which means height is 56.25% of width.
expect(
withYouTubeWatchUrl.documentation, contains('padding-top: 56.25%;'));
});
test("Doesn't place YouTube video in one line doc", () {
expect(
withYouTubeInOneLineDoc.oneLineDoc,
isNot(contains(
'<iframe src="https://www.youtube.com/embed/oHg5SJYRHA0?rel=0"')));
expect(
withYouTubeInOneLineDoc.documentation,
contains(
'<iframe src="https://www.youtube.com/embed/oHg5SJYRHA0?rel=0"'));
});
test('Handles YouTube video inline properly', () {
// Make sure it doesn't have a double-space before the continued line,
// which would indicate to Markdown to indent the line.
expect(withYouTubeInline.documentation, isNot(contains(' works')));
});
});
group('Animation', () {
Class dog;
Method withAnimation;
Method withNamedAnimation;
Method withQuoteNamedAnimation;
Method withDeprecatedAnimation;
Method withAnimationInOneLineDoc;
Method withAnimationInline;
Method withAnimationOutOfOrder;
Enum enumWithAnimation;
EnumField enumValue1;
EnumField enumValue2;
setUpAll(() {
enumWithAnimation =
exLibrary.enums.firstWhere((c) => c.name == 'EnumWithAnimation');
enumValue1 = enumWithAnimation.constantFields
.firstWhere((m) => m.name == 'value1');
enumValue2 = enumWithAnimation.constantFields
.firstWhere((m) => m.name == 'value2');
dog = exLibrary.classes.firstWhere((c) => c.name == 'Dog');
withAnimation =
dog.instanceMethods.firstWhere((m) => m.name == 'withAnimation');
withNamedAnimation =
dog.instanceMethods.firstWhere((m) => m.name == 'withNamedAnimation');
withQuoteNamedAnimation = dog.instanceMethods
.firstWhere((m) => m.name == 'withQuotedNamedAnimation');
withDeprecatedAnimation = dog.instanceMethods
.firstWhere((m) => m.name == 'withDeprecatedAnimation');
withAnimationInOneLineDoc = dog.instanceMethods
.firstWhere((m) => m.name == 'withAnimationInOneLineDoc');
withAnimationInline = dog.instanceMethods
.firstWhere((m) => m.name == 'withAnimationInline');
withAnimationOutOfOrder = dog.instanceMethods
.firstWhere((m) => m.name == 'withAnimationOutOfOrder');
});
test('renders an unnamed animation within the method documentation', () {
expect(withAnimation.documentation, contains('<video id="animation_1"'));
});
test('renders a named animation within the method documentation', () {
expect(withNamedAnimation.documentation,
contains('<video id="namedAnimation"'));
});
test('renders a quoted, named animation within the method documentation',
() {
expect(withQuoteNamedAnimation.documentation,
contains('<video id="quotedNamedAnimation"'));
expect(withQuoteNamedAnimation.documentation,
contains('<video id="quotedNamedAnimation2"'));
});
test('renders a deprecated-form animation within the method documentation',
() {
expect(withDeprecatedAnimation.documentation,
contains('<video id="deprecatedAnimation"'));
expect(
packageGraph.packageWarningCounter.hasWarning(
withDeprecatedAnimation,
PackageWarning.deprecated,
'Deprecated form of @animation directive, '
'"{@animation deprecatedAnimation 100 100 http://host/path/to/video.mp4}"\n'
'Animation directives are now of the form "{@animation '
'WIDTH HEIGHT URL [id=ID]}" (id is an optional '
'parameter)'),
isTrue);
});
test("Doesn't place animations in one line doc", () {
expect(withAnimationInOneLineDoc.oneLineDoc, isNot(contains('<video')));
expect(withAnimationInOneLineDoc.documentation, contains('<video'));
});
test('Handles animations inline properly', () {
// Make sure it doesn't have a double-space before the continued line,
// which would indicate to Markdown to indent the line.
expect(withAnimationInline.documentation, isNot(contains(' works')));
});
test('Out of order arguments work.', () {
expect(withAnimationOutOfOrder.documentation,
contains('<video id="outOfOrder"'));
});
test('Enum field animation identifiers are unique.', () {
expect(
enumValue1.documentationAsHtml, contains('<video id="animation_1"'));
expect(
enumValue1.documentationAsHtml, contains('<video id="animation_2"'));
expect(enumValue2.documentationAsHtml,
isNot(contains('<video id="animation_1"')));
expect(enumValue2.documentationAsHtml,
isNot(contains('<video id="animation_2"')));
expect(
enumValue2.documentationAsHtml, contains('<video id="animation_3"'));
expect(
enumValue2.documentationAsHtml, contains('<video id="animation_4"'));
});
});
group('MultiplyInheritedExecutableElement handling', () {
Class BaseThingy, BaseThingy2, ImplementingThingy2;
Method aImplementingThingyMethod;
Field aImplementingThingyField;
Field aImplementingThingy;
Accessor aImplementingThingyAccessor;
setUpAll(() {
BaseThingy =
fakeLibrary.classes.firstWhere((c) => c.name == 'BaseThingy');
BaseThingy2 =
fakeLibrary.classes.firstWhere((c) => c.name == 'BaseThingy2');
ImplementingThingy2 = fakeLibrary.classes
.firstWhere((c) => c.name == 'ImplementingThingy2');
aImplementingThingy = ImplementingThingy2.instanceFields
.firstWhere((m) => m.name == 'aImplementingThingy');
aImplementingThingyMethod = ImplementingThingy2.instanceMethods
.firstWhere((m) => m.name == 'aImplementingThingyMethod');
aImplementingThingyField = ImplementingThingy2.instanceFields
.firstWhere((m) => m.name == 'aImplementingThingyField');
aImplementingThingyAccessor = aImplementingThingyField.getter;
});
test('Verify behavior of imperfect resolver', () {
expect(aImplementingThingy.element.enclosingElement,
equals(BaseThingy2.element));
expect(aImplementingThingyMethod.element.enclosingElement,
equals(BaseThingy.element));
expect(aImplementingThingyField.element.enclosingElement,
equals(BaseThingy.element));
expect(aImplementingThingyAccessor.element.enclosingElement,
equals(BaseThingy.element));
});
});
group('Docs as HTML', () {
Class Apple, B, superAwesomeClass, foo2;
TopLevelVariable incorrectDocReferenceFromEx;
TopLevelVariable bulletDoced;
ModelFunction thisIsAsync;
ModelFunction topLevelFunction;
Class extendedClass;
TopLevelVariable testingCodeSyntaxInOneLiners;
Class specialList;
Class baseForDocComments;
Method doAwesomeStuff;
Class subForDocComments;
ModelFunction short;
setUpAll(() {
incorrectDocReferenceFromEx = exLibrary.constants
.firstWhere((c) => c.name == 'incorrectDocReferenceFromEx');
B = exLibrary.classes.firstWhere((c) => c.name == 'B');
Apple = exLibrary.classes.firstWhere((c) => c.name == 'Apple');
specialList =
fakeLibrary.classes.firstWhere((c) => c.name == 'SpecialList');
bulletDoced =
fakeLibrary.constants.firstWhere((c) => c.name == 'bulletDoced');
topLevelFunction =
fakeLibrary.functions.firstWhere((f) => f.name == 'topLevelFunction');
thisIsAsync =
fakeLibrary.functions.firstWhere((f) => f.name == 'thisIsAsync');
testingCodeSyntaxInOneLiners = fakeLibrary.constants
.firstWhere((c) => c.name == 'testingCodeSyntaxInOneLiners');
superAwesomeClass = fakeLibrary.classes
.firstWhere((cls) => cls.name == 'SuperAwesomeClass');
foo2 = fakeLibrary.classes.firstWhere((cls) => cls.name == 'Foo2');
assert(twoExportsLib != null);
extendedClass = twoExportsLib.allClasses
.firstWhere((clazz) => clazz.name == 'ExtendingClass');
subForDocComments =
fakeLibrary.classes.firstWhere((c) => c.name == 'SubForDocComments');
baseForDocComments =
fakeLibrary.classes.firstWhere((c) => c.name == 'BaseForDocComments');
doAwesomeStuff = baseForDocComments.instanceMethods
.firstWhere((m) => m.name == 'doAwesomeStuff');
short = fakeLibrary.functions.firstWhere((f) => f.name == 'short');
});
group('markdown extensions', () {
Class DocumentWithATable;
String docsAsHtml;
setUpAll(() {
DocumentWithATable = fakeLibrary.classes
.firstWhere((cls) => cls.name == 'DocumentWithATable');
docsAsHtml = DocumentWithATable.documentationAsHtml;
});
test('Verify table appearance', () {
expect(docsAsHtml.contains('<table><thead><tr><th>Component</th>'),
isTrue);
});
test('Verify links inside of table headers', () {
expect(
docsAsHtml.contains(
'<th><a href="${HTMLBASE_PLACEHOLDER}fake/Annotation-class.html">Annotation</a></th>'),
isTrue);
});
test('Verify links inside of table body', () {
expect(
docsAsHtml.contains(
'<tbody><tr><td><a href="${HTMLBASE_PLACEHOLDER}fake/DocumentWithATable/foo-constant.html">foo</a></td>'),
isTrue);
});
test('Verify there is no emoji support', () {
var tpvar = fakeLibrary.constants
.firstWhere((t) => t.name == 'hasMarkdownInDoc');