|
| 1 | +// Copyright (c) 2017, 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 | +// Increase timeouts on this test which resolves source code and can be slow. |
| 6 | +import 'package:analyzer/dart/element/element.dart'; |
| 7 | +import 'package:analyzer/src/generated/source_io.dart'; |
| 8 | +import 'package:source_gen/source_gen.dart'; |
| 9 | +import 'package:test/test.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + LibraryReader reader; |
| 13 | + |
| 14 | + final packageA = Uri.parse('package:a/a.dart'); |
| 15 | + final packageB = Uri.parse('package:b/b.dart'); |
| 16 | + final assetPackageA = Uri.parse('asset:a/lib/a.dart'); |
| 17 | + final assetPackageB = Uri.parse('asset:b/lib/b.dart'); |
| 18 | + final packageATestDir = Uri.parse('asset:a/test/a.dart'); |
| 19 | + final packageATestDirFileB = Uri.parse('asset:a/test/b.dart'); |
| 20 | + final packageATestDirDeepFile = Uri.parse('asset:a/test/in/a/folder/a.dart'); |
| 21 | + final packageBTestDir = Uri.parse('asset:b/test/b.dart'); |
| 22 | + final dartAsync = Uri.parse('dart:async'); |
| 23 | + final dartAsyncPrivate = Uri.parse('dart:async/zone.dart'); |
| 24 | + |
| 25 | + group('from a package URL to', () { |
| 26 | + setUpAll(() { |
| 27 | + reader = new LibraryReader(new _FakeLibraryElement(packageA)); |
| 28 | + }); |
| 29 | + |
| 30 | + test('a dart SDK library', () { |
| 31 | + expect(reader.pathToUrl(dartAsync), dartAsync); |
| 32 | + }); |
| 33 | + |
| 34 | + test('a dart SDK private library', () { |
| 35 | + expect(reader.pathToUrl(dartAsyncPrivate), dartAsync); |
| 36 | + }); |
| 37 | + |
| 38 | + test('the same package', () { |
| 39 | + expect(reader.pathToUrl(packageA), packageA); |
| 40 | + }); |
| 41 | + |
| 42 | + test('the same package as an asset URL', () { |
| 43 | + expect(reader.pathToUrl(assetPackageA), packageA); |
| 44 | + }); |
| 45 | + |
| 46 | + test('another package', () { |
| 47 | + expect(reader.pathToUrl(packageB), packageB); |
| 48 | + }); |
| 49 | + |
| 50 | + test('another package as an asset URL', () { |
| 51 | + expect(reader.pathToUrl(assetPackageB), packageB); |
| 52 | + }); |
| 53 | + |
| 54 | + test('the same package outside of lib should throw', () { |
| 55 | + expect(() => reader.pathToUrl(packageATestDir), throwsArgumentError); |
| 56 | + }); |
| 57 | + |
| 58 | + test('another package outside of lib should throw', () { |
| 59 | + expect(() => reader.pathToUrl(packageBTestDir), throwsArgumentError); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + group('from an asset URL representing a package to', () { |
| 64 | + setUpAll(() { |
| 65 | + reader = new LibraryReader(new _FakeLibraryElement(assetPackageA)); |
| 66 | + }); |
| 67 | + |
| 68 | + test('a dart SDK library', () { |
| 69 | + expect(reader.pathToUrl(dartAsync), dartAsync); |
| 70 | + }); |
| 71 | + |
| 72 | + test('a dart SDK private library', () { |
| 73 | + expect(reader.pathToUrl(dartAsyncPrivate), dartAsync); |
| 74 | + }); |
| 75 | + |
| 76 | + test('the same package', () { |
| 77 | + expect(reader.pathToUrl(packageA), packageA); |
| 78 | + }); |
| 79 | + |
| 80 | + test('the same package as an asset URL', () { |
| 81 | + expect(reader.pathToUrl(assetPackageA), packageA); |
| 82 | + }); |
| 83 | + |
| 84 | + test('another package', () { |
| 85 | + expect(reader.pathToUrl(packageB), packageB); |
| 86 | + }); |
| 87 | + |
| 88 | + test('another package as an asset URL', () { |
| 89 | + expect(reader.pathToUrl(assetPackageB), packageB); |
| 90 | + }); |
| 91 | + |
| 92 | + test('the same package outside of lib should throw', () { |
| 93 | + expect(() => reader.pathToUrl(packageATestDir), throwsArgumentError); |
| 94 | + }); |
| 95 | + |
| 96 | + test('another package outside of lib should throw', () { |
| 97 | + expect(() => reader.pathToUrl(packageBTestDir), throwsArgumentError); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + group('from an asset URL representing a test directory to', () { |
| 102 | + setUpAll(() { |
| 103 | + reader = new LibraryReader(new _FakeLibraryElement(packageATestDir)); |
| 104 | + }); |
| 105 | + |
| 106 | + test('a dart SDK library', () { |
| 107 | + expect(reader.pathToUrl(dartAsync), dartAsync); |
| 108 | + }); |
| 109 | + |
| 110 | + test('a dart SDK private library', () { |
| 111 | + expect(reader.pathToUrl(dartAsyncPrivate), dartAsync); |
| 112 | + }); |
| 113 | + |
| 114 | + test('the same package', () { |
| 115 | + expect(reader.pathToUrl(packageA), packageA); |
| 116 | + }); |
| 117 | + |
| 118 | + test('the same package as an asset URL', () { |
| 119 | + expect(reader.pathToUrl(assetPackageA), packageA); |
| 120 | + }); |
| 121 | + |
| 122 | + test('another package', () { |
| 123 | + expect(reader.pathToUrl(packageB), packageB); |
| 124 | + }); |
| 125 | + |
| 126 | + test('another package as an asset URL', () { |
| 127 | + expect(reader.pathToUrl(assetPackageB), packageB); |
| 128 | + }); |
| 129 | + |
| 130 | + test('the same package in the test directory', () { |
| 131 | + expect(reader.pathToUrl(packageATestDir), Uri.parse('a.dart')); |
| 132 | + }); |
| 133 | + |
| 134 | + test('the same package in the test directory, different file', () { |
| 135 | + expect(reader.pathToUrl(packageATestDirFileB), Uri.parse('b.dart')); |
| 136 | + }); |
| 137 | + |
| 138 | + test('the same package in the test directory, different deeper file', () { |
| 139 | + expect( |
| 140 | + reader.pathToUrl(packageATestDirDeepFile), |
| 141 | + Uri.parse('in/a/folder/a.dart'), |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + test('in the same package in the test directory, a shallow file', () { |
| 146 | + reader = new LibraryReader( |
| 147 | + new _FakeLibraryElement(packageATestDirDeepFile), |
| 148 | + ); |
| 149 | + expect( |
| 150 | + reader.pathToUrl(packageATestDir), |
| 151 | + Uri.parse('../../../a.dart'), |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + test('the same package in the tool directory should throw', () { |
| 156 | + final packageAToolDir = Uri.parse('asset:a/tool/a.dart'); |
| 157 | + expect(() => reader.pathToUrl(packageAToolDir), throwsArgumentError); |
| 158 | + }); |
| 159 | + |
| 160 | + test('another package in the test directory should throw', () { |
| 161 | + expect(() => reader.pathToUrl(packageBTestDir), throwsArgumentError); |
| 162 | + }); |
| 163 | + }); |
| 164 | +} |
| 165 | + |
| 166 | +class _FakeLibraryElement implements LibraryElement { |
| 167 | + final Uri _sourceUri; |
| 168 | + |
| 169 | + _FakeLibraryElement(this._sourceUri); |
| 170 | + |
| 171 | + @override |
| 172 | + noSuchMethod(i) => super.noSuchMethod(i); |
| 173 | + |
| 174 | + @override |
| 175 | + Source get source => new _FakeSource(_sourceUri); |
| 176 | +} |
| 177 | + |
| 178 | +class _FakeSource implements Source { |
| 179 | + @override |
| 180 | + final Uri uri; |
| 181 | + |
| 182 | + const _FakeSource(this.uri); |
| 183 | + |
| 184 | + @override |
| 185 | + noSuchMethod(i) => super.noSuchMethod(i); |
| 186 | +} |
0 commit comments