44
55import 'package:analysis_server/src/services/search/element_visitors.dart' ;
66import 'package:analyzer/dart/element/element.dart' ;
7+ import 'package:analyzer/src/test_utilities/test_code_format.dart' ;
78import 'package:test/test.dart' ;
89import 'package:test_reflective_loader/test_reflective_loader.dart' ;
910
@@ -17,53 +18,57 @@ void main() {
1718
1819@reflectiveTest
1920class FindElementByNameOffsetTest extends AbstractSingleUnitTest {
21+ late TestCode code;
22+
23+ late List <int > offsets = code.positions.map ((p) => p.offset).toList ();
24+
2025 LibraryFragment get testUnitFragment => testUnit.declaredFragment! ;
2126
2227 @override
23- void setUp ( ) {
24- useLineEndingsForPlatform = false ;
25- super .setUp ( );
28+ Future < void > resolveTestCode ( String content ) {
29+ code = TestCode . parse ( normalizeSource (content)) ;
30+ return super .resolveTestCode (code.code );
2631 }
2732
2833 Future <void > test_class () async {
2934 await resolveTestCode (r'''
30- class AAA {}
31- class BBB {}
35+ class /*0*/ AAA {}
36+ class /*1*/ BBB {}
3237''' );
33- _assertElement (6 , ElementKind .CLASS , 'AAA' );
34- _assertElement (19 , ElementKind .CLASS , 'BBB' );
38+ _assertElement (offsets[ 0 ] , ElementKind .CLASS , 'AAA' );
39+ _assertElement (offsets[ 1 ] , ElementKind .CLASS , 'BBB' );
3540 }
3641
3742 Future <void > test_function () async {
3843 await resolveTestCode (r'''
39- void aaa() {}
40- void bbb() {}
44+ void /*0*/ aaa() {}
45+ void /*1*/ bbb() {}
4146''' );
42- _assertElement (5 , ElementKind .FUNCTION , 'aaa' );
43- _assertElement (19 , ElementKind .FUNCTION , 'bbb' );
47+ _assertElement (offsets[ 0 ] , ElementKind .FUNCTION , 'aaa' );
48+ _assertElement (offsets[ 1 ] , ElementKind .FUNCTION , 'bbb' );
4449 }
4550
4651 Future <void > test_null () async {
4752 await resolveTestCode (r'''
48- class AAA {}
53+ /*0*/c/*1*/lass/*2*/ A/*3*/AA {}
4954class BBB {}
5055''' );
5156
52- expect (findFragmentByNameOffset (testUnitFragment, 0 ), isNull);
53- expect (findFragmentByNameOffset (testUnitFragment, 1 ), isNull);
57+ expect (findFragmentByNameOffset (testUnitFragment, offsets[ 0 ] ), isNull);
58+ expect (findFragmentByNameOffset (testUnitFragment, offsets[ 1 ] ), isNull);
5459
55- expect (findFragmentByNameOffset (testUnitFragment, 5 ), isNull);
56- expect (findFragmentByNameOffset (testUnitFragment, 7 ), isNull);
60+ expect (findFragmentByNameOffset (testUnitFragment, offsets[ 2 ] ), isNull);
61+ expect (findFragmentByNameOffset (testUnitFragment, offsets[ 3 ] ), isNull);
5762 }
5863
5964 Future <void > test_topLevelVariable () async {
6065 await resolveTestCode (r'''
61- int? aaa, bbb;
62- int? ccc;
66+ int? /*0*/ aaa, /*1*/ bbb;
67+ int? /*2*/ ccc;
6368''' );
64- _assertElement (5 , ElementKind .TOP_LEVEL_VARIABLE , 'aaa' );
65- _assertElement (10 , ElementKind .TOP_LEVEL_VARIABLE , 'bbb' );
66- _assertElement (20 , ElementKind .TOP_LEVEL_VARIABLE , 'ccc' );
69+ _assertElement (offsets[ 0 ] , ElementKind .TOP_LEVEL_VARIABLE , 'aaa' );
70+ _assertElement (offsets[ 1 ] , ElementKind .TOP_LEVEL_VARIABLE , 'bbb' );
71+ _assertElement (offsets[ 2 ] , ElementKind .TOP_LEVEL_VARIABLE , 'ccc' );
6772 }
6873
6974 void _assertElement (int nameOffset, ElementKind kind, String name) {
0 commit comments