55import 'package:analysis_server/plugin/protocol/protocol_dart.dart' ;
66import 'package:analyzer/dart/element/element.dart' as engine;
77import 'package:analyzer/src/dart/element/element.dart' as engine;
8+ import 'package:analyzer/src/test_utilities/test_code_format.dart' ;
89import 'package:analyzer_plugin/protocol/protocol_common.dart' ;
910import 'package:test/test.dart' ;
1011import 'package:test_reflective_loader/test_reflective_loader.dart' ;
1112
1213import '../abstract_single_unit.dart' ;
14+ import '../utils/test_code_extensions.dart' ;
1315
1416void main () {
1517 defineReflectiveSuite (() {
@@ -20,16 +22,10 @@ void main() {
2022
2123@reflectiveTest
2224class ConvertElementTest extends AbstractSingleUnitTest {
23- @override
24- void setUp () {
25- useLineEndingsForPlatform = false ;
26- super .setUp ();
27- }
28-
2925 Future <void > test_CLASS () async {
3026 await resolveTestCode ('''
3127@deprecated
32- abstract class _A {}
28+ abstract class [!_A!] {}
3329class B<K, V> {}''' );
3430 {
3531 var engineElement = findElement2.class_ ('_A' );
@@ -41,10 +37,8 @@ class B<K, V> {}''');
4137 {
4238 var location = element.location! ;
4339 expect (location.file, testFile.path);
44- expect (location.offset, 27 );
4540 expect (location.length, '_A' .length);
46- expect (location.startLine, 2 );
47- expect (location.startColumn, 16 );
41+ _expectLocation (location, parsedTestCode.range);
4842 }
4943 expect (element.parameters, isNull);
5044 expect (
@@ -66,7 +60,7 @@ class B<K, V> {}''');
6660 Future <void > test_CONSTRUCTOR () async {
6761 await resolveTestCode ('''
6862class A {
69- const A.myConstructor(int a, [String? b]);
63+ const A.[! myConstructor!] (int a, [String? b]);
7064}''' );
7165 var engineElement = findElement2.constructor ('myConstructor' );
7266 // create notification Element
@@ -77,10 +71,8 @@ class A {
7771 {
7872 var location = element.location! ;
7973 expect (location.file, testFile.path);
80- expect (location.offset, 20 );
8174 expect (location.length, 'myConstructor' .length);
82- expect (location.startLine, 2 );
83- expect (location.startColumn, 11 );
75+ _expectLocation (location, parsedTestCode.range);
8476 }
8577 expect (element.parameters, '(int a, [String? b])' );
8678 expect (element.returnType, 'A' );
@@ -153,7 +145,7 @@ class A {
153145 Future <void > test_ENUM () async {
154146 await resolveTestCode ('''
155147@deprecated
156- enum _E1 { one, two }
148+ enum [! _E1!] { one, two }
157149enum E2 { three, four }''' );
158150 {
159151 var engineElement = findElement2.enum_ ('_E1' );
@@ -166,10 +158,8 @@ enum E2 { three, four }''');
166158 {
167159 var location = element.location! ;
168160 expect (location.file, testFile.path);
169- expect (location.offset, 17 );
170161 expect (location.length, '_E1' .length);
171- expect (location.startLine, 2 );
172- expect (location.startColumn, 6 );
162+ _expectLocation (location, parsedTestCode.range);
173163 }
174164 expect (element.parameters, isNull);
175165 expect (
@@ -192,8 +182,8 @@ enum E2 { three, four }''');
192182 Future <void > test_ENUM_CONSTANT () async {
193183 await resolveTestCode ('''
194184@deprecated
195- enum _E1 { one, two }
196- enum E2 { three, four }''' );
185+ enum _E1 { /*[0*/ one/*0]*/ , two }
186+ enum E2 { /*[1*/ three/*1]*/ , four }''' );
197187 {
198188 var engineElement = findElement2.field ('one' );
199189 // create notification Element
@@ -203,10 +193,8 @@ enum E2 { three, four }''');
203193 {
204194 var location = element.location! ;
205195 expect (location.file, testFile.path);
206- expect (location.offset, 23 );
207196 expect (location.length, 'one' .length);
208- expect (location.startLine, 2 );
209- expect (location.startColumn, 12 );
197+ _expectLocation (location, parsedTestCode.ranges[0 ]);
210198 }
211199 expect (element.parameters, isNull);
212200 expect (element.returnType, '_E1' );
@@ -227,10 +215,8 @@ enum E2 { three, four }''');
227215 {
228216 var location = element.location! ;
229217 expect (location.file, testFile.path);
230- expect (location.offset, 44 );
231218 expect (location.length, 'three' .length);
232- expect (location.startLine, 3 );
233- expect (location.startColumn, 11 );
219+ _expectLocation (location, parsedTestCode.ranges[1 ]);
234220 }
235221 expect (element.parameters, isNull);
236222 expect (element.returnType, 'E2' );
@@ -259,7 +245,7 @@ enum E2 { three, four }''');
259245 Future <void > test_FIELD () async {
260246 await resolveTestCode ('''
261247class A {
262- static const myField = 42;
248+ static const [! myField!] = 42;
263249}''' );
264250 var engineElement = findElement2.field ('myField' );
265251 // create notification Element
@@ -269,10 +255,8 @@ class A {
269255 {
270256 var location = element.location! ;
271257 expect (location.file, testFile.path);
272- expect (location.offset, 25 );
273258 expect (location.length, 'myField' .length);
274- expect (location.startLine, 2 );
275- expect (location.startColumn, 16 );
259+ _expectLocation (location, parsedTestCode.range);
276260 }
277261 expect (element.parameters, isNull);
278262 expect (element.returnType, 'int' );
@@ -353,7 +337,7 @@ typedef int F<T>(String x);
353337 verifyNoTestUnitErrors = false ;
354338 await resolveTestCode ('''
355339class A {
356- String get myGetter => 42;
340+ String get [! myGetter!] => 42;
357341}''' );
358342 var engineElement = findElement2.getter ('myGetter' );
359343 // create notification Element
@@ -363,10 +347,8 @@ class A {
363347 {
364348 var location = element.location! ;
365349 expect (location.file, testFile.path);
366- expect (location.offset, 23 );
367350 expect (location.length, 'myGetter' .length);
368- expect (location.startLine, 2 );
369- expect (location.startColumn, 14 );
351+ _expectLocation (location, parsedTestCode.range);
370352 }
371353 expect (element.parameters, isNull);
372354 expect (element.returnType, 'String' );
@@ -376,7 +358,7 @@ class A {
376358 Future <void > test_LABEL () async {
377359 await resolveTestCode ('''
378360void f() {
379- myLabel:
361+ [! myLabel!] :
380362 while (true) {
381363 break myLabel;
382364 }
@@ -389,10 +371,8 @@ myLabel:
389371 {
390372 var location = element.location! ;
391373 expect (location.file, testFile.path);
392- expect (location.offset, 11 );
393374 expect (location.length, 'myLabel' .length);
394- expect (location.startLine, 2 );
395- expect (location.startColumn, 1 );
375+ _expectLocation (location, parsedTestCode.range);
396376 }
397377 expect (element.parameters, isNull);
398378 expect (element.returnType, isNull);
@@ -402,7 +382,7 @@ myLabel:
402382 Future <void > test_METHOD () async {
403383 await resolveTestCode ('''
404384class A {
405- static List<String> myMethod(int a, {String? b, int? c}) {
385+ static List<String> [! myMethod!] (int a, {String? b, int? c}) {
406386 return [];
407387 }
408388}''' );
@@ -414,10 +394,8 @@ class A {
414394 {
415395 var location = element.location! ;
416396 expect (location.file, testFile.path);
417- expect (location.offset, 32 );
418397 expect (location.length, 'myGetter' .length);
419- expect (location.startLine, 2 );
420- expect (location.startColumn, 23 );
398+ _expectLocation (location, parsedTestCode.range);
421399 }
422400 expect (element.parameters, '(int a, {String? b, int? c})' );
423401 expect (element.returnType, 'List<String>' );
@@ -451,7 +429,7 @@ mixin A {}
451429 Future <void > test_SETTER () async {
452430 await resolveTestCode ('''
453431class A {
454- set mySetter(String x) {}
432+ set [! mySetter!] (String x) {}
455433}''' );
456434 var engineElement = findElement2.setter ('mySetter' );
457435 // create notification Element
@@ -461,15 +439,20 @@ class A {
461439 {
462440 var location = element.location! ;
463441 expect (location.file, testFile.path);
464- expect (location.offset, 16 );
465442 expect (location.length, 'mySetter' .length);
466- expect (location.startLine, 2 );
467- expect (location.startColumn, 7 );
443+ _expectLocation (location, parsedTestCode.range);
468444 }
469445 expect (element.parameters, '(String x)' );
470446 expect (element.returnType, isNull);
471447 expect (element.flags, 0 );
472448 }
449+
450+ void _expectLocation (Location actual, TestCodeRange expected) {
451+ expect (actual.offset, expected.sourceRange.offset);
452+ // LSP uses zero-based line/col, but server does uses one-based.
453+ expect (actual.startLine, expected.range.start.line + 1 );
454+ expect (actual.startColumn, expected.range.start.character + 1 );
455+ }
473456}
474457
475458@reflectiveTest
0 commit comments