33// BSD-style license that can be found in the LICENSE file. 
44
55import  'package:analyzer/src/test_utilities/test_code_format.dart' ;
6+ import  'package:collection/collection.dart' ;
67import  'package:linter/src/rules.dart' ;
78import  'package:test/test.dart' ;
89import  'package:test_reflective_loader/test_reflective_loader.dart' ;
@@ -30,43 +31,43 @@ class DocumentLinkTest extends AbstractLspAnalysisServerTest {
3031  Future <void > test_analysisOptions_empty () async  {
3132    var  content =  '' ;
3233
33-     await  _test_analysisOptions_links (content, isEmpty );
34+     await  _test_analysisOptions_links (content, [] );
3435  }
3536
3637  Future <void > test_analysisOptions_lint_links_list () async  {
3738    var  content =  ''' 
3839linter: 
3940  rules: 
40-     - prefer_relative_imports 
41-     - prefer_single_quotes 
42-     - prefer_int_literals 
41+     - /*[0*/ prefer_relative_imports/*0]*/  
42+     - /*[1*/ prefer_single_quotes/*1]*/  
43+     - /*[2*/ prefer_int_literals/*2]*/  
4344''' ;
4445
45-     var  expectedLinks =  { 
46-       'prefer_relative_imports'  :   ' ${_lintBase }prefer_relative_imports' ,
47-       'prefer_single_quotes'  :   ' ${_lintBase }prefer_single_quotes' ,
48-       'prefer_int_literals'  :   ' ${_lintBase }prefer_int_literals' ,
49-     } ;
46+     var  expectedLinks =  [ 
47+       '${_lintBase }prefer_relative_imports' ,
48+       '${_lintBase }prefer_single_quotes' ,
49+       '${_lintBase }prefer_int_literals' ,
50+     ] ;
5051
51-     await  _test_analysisOptions_links (content, equals ( expectedLinks) );
52+     await  _test_analysisOptions_links (content, expectedLinks);
5253  }
5354
5455  Future <void > test_analysisOptions_lint_links_map () async  {
5556    var  content =  ''' 
5657linter: 
5758  rules: 
58-     prefer_relative_imports: true 
59-     prefer_single_quotes: true 
60-     prefer_int_literals: false 
59+     /*[0*/ prefer_relative_imports/*0]*/ : true 
60+     /*[1*/ prefer_single_quotes/*1]*/ : true 
61+     /*[2*/ prefer_int_literals/*2]*/ : false 
6162''' ;
6263
63-     var  expectedLinks =  { 
64-       'prefer_relative_imports'  :   ' ${_lintBase }prefer_relative_imports' ,
65-       'prefer_single_quotes'  :   ' ${_lintBase }prefer_single_quotes' ,
66-       'prefer_int_literals'  :   ' ${_lintBase }prefer_int_literals' ,
67-     } ;
64+     var  expectedLinks =  [ 
65+       '${_lintBase }prefer_relative_imports' ,
66+       '${_lintBase }prefer_single_quotes' ,
67+       '${_lintBase }prefer_int_literals' ,
68+     ] ;
6869
69-     await  _test_analysisOptions_links (content, equals ( expectedLinks) );
70+     await  _test_analysisOptions_links (content, expectedLinks);
7071  }
7172
7273  Future <void > test_analysisOptions_linterRules_empty () async  {
@@ -75,22 +76,20 @@ linter:
7576  rules: 
7677''' ;
7778
78-     await  _test_pubspec_links (content, isEmpty );
79+     await  _test_pubspec_links (content, [] );
7980  }
8081
8182  Future <void > test_analysisOptions_undefinedLint () async  {
8283    var  content =  ''' 
8384linter: 
8485  rules: 
8586    - undefined 
86-     - prefer_single_quotes 
87+     - /*[0*/ prefer_single_quotes/*0]*/  
8788''' ;
8889
89-     var  expectedLinks =  {
90-       'prefer_single_quotes' :  '${_lintBase }prefer_single_quotes' ,
91-     };
90+     var  expectedLinks =  ['${_lintBase }prefer_single_quotes' ];
9291
93-     await  _test_analysisOptions_links (content, equals ( expectedLinks) );
92+     await  _test_analysisOptions_links (content, expectedLinks);
9493  }
9594
9695  Future <void > test_exampleLink () async  {
@@ -118,73 +117,73 @@ class A {}
118117  Future <void > test_pubspec_empty () async  {
119118    var  content =  '' ;
120119
121-     await  _test_pubspec_links (content, isEmpty );
120+     await  _test_pubspec_links (content, [] );
122121  }
123122
124123  Future <void > test_pubspec_packages_empty () async  {
125124    var  content =  ''' 
126125dependencies: 
127126''' ;
128127
129-     await  _test_pubspec_links (content, isEmpty );
128+     await  _test_pubspec_links (content, [] );
130129  }
131130
132131  Future <void > test_pubspec_packages_git () async  {
133132    var  content =  ''' 
134133dependencies: 
135-   github_package_1: 
134+   /*[0*/ github_package_1/*0]*/ : 
136135    git: https://github.com/dart-lang/sdk.git 
137-   github_package_2: 
136+   /*[1*/ github_package_2/*1]*/ : 
138137    git: [email protected] :dart-lang/sdk.git 139-   github_package_3: 
138+   /*[2*/ github_package_3/*2]*/ : 
140139    git: 
141140      url: https://github.com/dart-lang/sdk.git 
142141''' ;
143142
144-     var  expectedLinks =  { 
145-       'github_package_1'  :   ' https://github.com/dart-lang/sdk.git' ,
146-       'github_package_2'  :   ' https://github.com/dart-lang/sdk.git' ,
147-       'github_package_3'  :   ' https://github.com/dart-lang/sdk.git' ,
148-     } ;
143+     var  expectedLinks =  [ 
144+       'https://github.com/dart-lang/sdk.git' ,
145+       'https://github.com/dart-lang/sdk.git' ,
146+       'https://github.com/dart-lang/sdk.git' ,
147+     ] ;
149148
150-     await  _test_pubspec_links (content, equals ( expectedLinks) );
149+     await  _test_pubspec_links (content, expectedLinks);
151150  }
152151
153152  Future <void > test_pubspec_packages_hosted () async  {
154153    var  content =  ''' 
155154dependencies: 
156-   hosted_package_1: 
155+   /*[0*/ hosted_package_1/*0]*/ : 
157156    hosted: https://custom.dart.dev/ 
158-   hosted_package_2: 
157+   /*[1*/ hosted_package_2/*1]*/ : 
159158    hosted: 
160159      url: https://custom.dart.dev/ 
161160''' ;
162161
163-     var  expectedLinks =  { 
164-       'hosted_package_1'  :   ' https://custom.dart.dev/packages/hosted_package_1' ,
165-       'hosted_package_2'  :   ' https://custom.dart.dev/packages/hosted_package_2' ,
166-     } ;
162+     var  expectedLinks =  [ 
163+       'https://custom.dart.dev/packages/hosted_package_1' ,
164+       'https://custom.dart.dev/packages/hosted_package_2' ,
165+     ] ;
167166
168-     await  _test_pubspec_links (content, equals ( expectedLinks) );
167+     await  _test_pubspec_links (content, expectedLinks);
169168  }
170169
171170  Future <void > test_pubspec_packages_pub () async  {
172171    var  content =  ''' 
173172dependencies: 
174-   pub_package_1: 1.2.3 
175-   pub_package_2: ^1.2.3 
176-   pub_package_3: 
177-   pub_package_4: any 
173+   /*[0*/ pub_package_1/*0]*/ : 1.2.3 
174+   /*[1*/ pub_package_2/*1]*/ : ^1.2.3 
175+   /*[2*/ pub_package_3/*2]*/ : 
176+   /*[3*/ pub_package_4/*3]*/ : any 
178177''' ;
179178
180-     var  expectedLinks =  { 
181-       'pub_package_1'  :   ' ${_pubBase }pub_package_1' ,
182-       'pub_package_2'  :   ' ${_pubBase }pub_package_2' ,
183-       'pub_package_3'  :   ' ${_pubBase }pub_package_3' ,
184-       'pub_package_4'  :   ' ${_pubBase }pub_package_4' ,
185-     } ;
179+     var  expectedLinks =  [ 
180+       '${_pubBase }pub_package_1' ,
181+       '${_pubBase }pub_package_2' ,
182+       '${_pubBase }pub_package_3' ,
183+       '${_pubBase }pub_package_4' ,
184+     ] ;
186185
187-     await  _test_pubspec_links (content, equals ( expectedLinks) );
186+     await  _test_pubspec_links (content, expectedLinks);
188187  }
189188
190189  Future <void > test_pubspec_packages_unknown () async  {
@@ -198,29 +197,43 @@ dependencies:
198197    future_unknown_kind: 
199198''' ;
200199
201-     await  _test_pubspec_links (content, isEmpty);
200+     await  _test_pubspec_links (content, []);
201+   }
202+ 
203+   Future <void > test_pubspec_packages_withDependencyOverrides () async  {
204+     var  content =  ''' 
205+ dependencies: 
206+   /*[0*/dep_package/*0]*/: 1.0.0 
207+ 
208+ dependency_overrides: 
209+   /*[1*/dep_package/*1]*/: 1.2.3 
210+ ''' ;
211+ 
212+     var  expectedLinks =  ['${_pubBase }dep_package' , '${_pubBase }dep_package' ];
213+ 
214+     await  _test_pubspec_links (content, expectedLinks);
202215  }
203216
204217  Future <void > test_pubspec_packages_withDevDependencies () async  {
205218    var  content =  ''' 
206219dependencies: 
207-   dep_package: 1.2.3 
220+   /*[0*/ dep_package/*0]*/ : 1.2.3 
208221
209222dev_dependencies: 
210-   dev_dep_package: 
223+   /*[1*/ dev_dep_package/*1]*/ : 
211224''' ;
212225
213-     var  expectedLinks =  { 
214-       'dep_package'  :   ' ${_pubBase }dep_package' ,
215-       'dev_dep_package'  :   ' ${_pubBase }dev_dep_package' ,
216-     } ;
226+     var  expectedLinks =  [ 
227+       '${_pubBase }dep_package' ,
228+       '${_pubBase }dev_dep_package' ,
229+     ] ;
217230
218-     await  _test_pubspec_links (content, equals ( expectedLinks) );
231+     await  _test_pubspec_links (content, expectedLinks);
219232  }
220233
221234  Future <void > _test_analysisOptions_links (
222235    String  content,
223-     Matcher  expected,
236+     List < String >  expected,
224237  ) async  {
225238    await  _test_file_links (
226239      analysisOptionsUri,
@@ -234,24 +247,33 @@ dev_dependencies:
234247    Uri  fileUri,
235248    String  filePath,
236249    String  content,
237-     Matcher  expected ,
250+     List < String > expectedLinks ,
238251  ) async  {
239-     newFile (filePath, content);
252+     var  code =  TestCode .parse (content);
253+ 
254+     // Combine expectedLinks with the ranges from the markers in the content 
255+     // so we can verify both ranges and links. 
256+     expect (expectedLinks.length, code.ranges.length);
257+     var  expectedLinksWithRanges =  expectedLinks.mapIndexed (
258+       (i, link) =>  (code.ranges[i].range, link),
259+     );
260+ 
261+     newFile (filePath, code.code);
240262
241263    await  initialize ();
242264    var  links =  await  getDocumentLinks (fileUri);
243265
244-     // Build a map of the links and their text from the document for easy 
245-     // comparison. 
246-     var  linkMap =  {
247-       for  (var  link in  links! )
248-         getTextForRange (content, link.range):  link.target? .toString (),
249-     };
266+     // Convert the results into the same format as expectedLinksWithRanges. 
267+     var  linkData = 
268+         links! .map ((link) =>  (link.range, link.target? .toString ())).toList ();
250269
251-     expect (linkMap, expected );
270+     expect (linkData,  equals (expectedLinksWithRanges) );
252271  }
253272
254-   Future <void > _test_pubspec_links (String  content, Matcher  expected) async  {
273+   Future <void > _test_pubspec_links (
274+     String  content,
275+     List <String > expected,
276+   ) async  {
255277    await  _test_file_links (pubspecFileUri, pubspecFilePath, content, expected);
256278  }
257279}
0 commit comments