|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:dartdoc/src/markdown_processor.dart'; |
| 6 | +import 'package:dartdoc/src/model/model.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | +import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 9 | + |
| 10 | +import 'dartdoc_test_base.dart'; |
| 11 | +import 'src/test_descriptor_utils.dart' as d; |
| 12 | +import 'src/utils.dart'; |
| 13 | + |
| 14 | +void main() { |
| 15 | + defineReflectiveSuite(() { |
| 16 | + defineReflectiveTests(ExtensionMethodsTest); |
| 17 | + defineReflectiveTests(ExtensionMethodsExportTest); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +@reflectiveTest |
| 22 | +class ExtensionMethodsTest extends DartdocTestBase { |
| 23 | + @override |
| 24 | + String get libraryName => 'extension_methods'; |
| 25 | + |
| 26 | + void test_referenceToExtension() async { |
| 27 | + var library = await bootPackageWithLibrary(''' |
| 28 | +extension Ex on int {} |
| 29 | +
|
| 30 | +/// Text [Ex]. |
| 31 | +var f() {} |
| 32 | +'''); |
| 33 | + |
| 34 | + expect( |
| 35 | + library.functions.named('f').documentationAsHtml, |
| 36 | + contains('<a href="$linkPrefix/Ex.html">Ex</a>'), |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + void test_referenceToExtensionMethod() async { |
| 41 | + var library = await bootPackageWithLibrary(''' |
| 42 | +extension Ex on int { |
| 43 | + void m() {} |
| 44 | +} |
| 45 | +
|
| 46 | +/// Text [Ex.m]. |
| 47 | +var f() {} |
| 48 | +'''); |
| 49 | + |
| 50 | + expect( |
| 51 | + library.functions.named('f').documentationAsHtml, |
| 52 | + contains('<a href="$linkPrefix/Ex/m.html">Ex.m</a>'), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + // TODO(srawlins): Test everything else about extensions. |
| 57 | +} |
| 58 | + |
| 59 | +@reflectiveTest |
| 60 | +class ExtensionMethodsExportTest extends DartdocTestBase { |
| 61 | + @override |
| 62 | + String get libraryName => 'extension_methods'; |
| 63 | + |
| 64 | + late Package package; |
| 65 | + |
| 66 | + /// Verifies that comment reference text, [referenceText] attached to the |
| 67 | + /// function, `f`, is resolved to [expected], and links to [href]. |
| 68 | + void expectReferenceValidFromF( |
| 69 | + String referenceText, ModelElement expected, String href) { |
| 70 | + var fFunction = package.functions.named('f'); |
| 71 | + var reference = getMatchingLinkElement(referenceText, fFunction) |
| 72 | + .commentReferable as ModelElement; |
| 73 | + expect(identical(reference.canonicalModelElement, expected), isTrue); |
| 74 | + expect(expected.isCanonical, isTrue); |
| 75 | + expect(expected.href, endsWith(href)); |
| 76 | + } |
| 77 | + |
| 78 | + /// Sets up a package with three files: |
| 79 | + /// |
| 80 | + /// * a private file containing a class, `C`, an extension on that class, `E`, |
| 81 | + /// `E`, and a method in that extension, `m`. |
| 82 | + /// * A public file that exports some members of the private file. The content |
| 83 | + /// of this file is specified with [exportingLibraryContent]. |
| 84 | + /// * Another public file which is completely unrelated to the first two (no |
| 85 | + /// imports or exports linking them), which contains a function, `f`. |
| 86 | + Future<void> setupWith(String exportingLibraryContent) async { |
| 87 | + var packageGraph = await bootPackageFromFiles([ |
| 88 | + d.dir('lib', [ |
| 89 | + d.dir('src', [ |
| 90 | + d.file('lib.dart', ''' |
| 91 | +class C {} |
| 92 | +extension Ex on C { |
| 93 | + void m() {} |
| 94 | +} |
| 95 | +'''), |
| 96 | + ]), |
| 97 | + d.file('one.dart', exportingLibraryContent), |
| 98 | + d.file('two.dart', ''' |
| 99 | +/// Comment. |
| 100 | +var f() {} |
| 101 | +'''), |
| 102 | + ]) |
| 103 | + ]); |
| 104 | + package = packageGraph.defaultPackage; |
| 105 | + } |
| 106 | + |
| 107 | + void test_reexportWithShow() async { |
| 108 | + await setupWith("export 'src/lib.dart' show C, Ex;"); |
| 109 | + |
| 110 | + var ex = package.extensions.named('Ex'); |
| 111 | + var m = ex.instanceMethods.named('m'); |
| 112 | + expectReferenceValidFromF('Ex', ex, '%one/Ex.html'); |
| 113 | + expectReferenceValidFromF('Ex.m', m, '%one/Ex/m.html'); |
| 114 | + } |
| 115 | + |
| 116 | + void test_reexportWithHide() async { |
| 117 | + await setupWith("export 'src/lib.dart' hide A;"); |
| 118 | + |
| 119 | + var ex = package.extensions.named('Ex'); |
| 120 | + var m = ex.instanceMethods.named('m'); |
| 121 | + expectReferenceValidFromF('Ex', ex, '%one/Ex.html'); |
| 122 | + expectReferenceValidFromF('Ex.m', m, '%one/Ex/m.html'); |
| 123 | + } |
| 124 | + |
| 125 | + void test_reexportFull() async { |
| 126 | + await setupWith("export 'src/lib.dart';"); |
| 127 | + |
| 128 | + var ex = package.extensions.named('Ex'); |
| 129 | + var m = ex.instanceMethods.named('m'); |
| 130 | + expectReferenceValidFromF('Ex', ex, '%one/Ex.html'); |
| 131 | + expectReferenceValidFromF('Ex.m', m, '%one/Ex/m.html'); |
| 132 | + } |
| 133 | +} |
0 commit comments