[analysis_server] Add support for range marker shorthand in TestCode

Change-Id: I70efc2fd32de128351aa9fd8c10555dbd1391da4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/264864
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny 2022-10-19 18:13:57 +00:00 committed by Commit Queue
parent eea6fb106d
commit eea14037e0
2 changed files with 110 additions and 46 deletions

View file

@ -17,26 +17,6 @@ void main() {
@reflectiveTest
class TestCodeFormatTest {
void test_caret() {
final rawCode = '''
int ^a = 1
''';
final expectedCode = '''
int a = 1
''';
final code = TestCode.parse(rawCode);
expect(code.rawCode, rawCode);
expect(code.code, expectedCode);
expect(code.positions, hasLength(1));
expect(code.position.offset, 4);
expect(code.position.offset, code.positions[0].offset);
expect(code.position.position, lsp.Position(line: 0, character: 4));
expect(code.position.position, code.positions[0].position);
expect(code.ranges, isEmpty);
}
void test_noMarkers() {
final rawCode = '''
int a = 1;
@ -48,26 +28,6 @@ int a = 1;
expect(code.ranges, isEmpty);
}
void test_nonPositionCaret() {
final rawCode = '''
String /*0*/a = '^^^';
''';
final expectedCode = '''
String a = '^^^';
''';
final code = TestCode.parse(rawCode, treatCaretAsPosition: false);
expect(code.rawCode, rawCode);
expect(code.code, expectedCode);
expect(code.positions, hasLength(1));
expect(code.position.offset, 7);
expect(code.position.offset, code.positions[0].offset);
expect(code.position.position, lsp.Position(line: 0, character: 7));
expect(code.position.position, code.positions[0].position);
expect(code.ranges, isEmpty);
}
void test_positions() {
final rawCode = '''
int /*0*/a = 1;/*1*/
@ -91,23 +51,63 @@ int b = 2;
expect(code.positions[2].position, lsp.Position(line: 1, character: 5));
}
void test_positions_reused() {
void test_positions_nonShorthandCaret() {
final rawCode = '''
String /*0*/a = '^^^';
''';
final expectedCode = '''
String a = '^^^';
''';
final code = TestCode.parse(rawCode, positionShorthand: false);
expect(code.rawCode, rawCode);
expect(code.code, expectedCode);
expect(code.positions, hasLength(1));
expect(code.position.offset, 7);
expect(code.position.offset, code.positions[0].offset);
expect(code.position.position, lsp.Position(line: 0, character: 7));
expect(code.position.position, code.positions[0].position);
expect(code.ranges, isEmpty);
}
void test_positions_numberReused() {
final rawCode = '''
/*0*/ /*1*/ /*0*/
''';
expect(() => TestCode.parse(rawCode), throwsArgumentError);
}
void test_positions_reusedCaret() {
void test_positions_shorthand() {
final rawCode = '''
int ^a = 1
''';
final expectedCode = '''
int a = 1
''';
final code = TestCode.parse(rawCode);
expect(code.rawCode, rawCode);
expect(code.code, expectedCode);
expect(code.positions, hasLength(1));
expect(code.position.offset, 4);
expect(code.position.offset, code.positions[0].offset);
expect(code.position.position, lsp.Position(line: 0, character: 4));
expect(code.position.position, code.positions[0].position);
expect(code.ranges, isEmpty);
}
void test_positions_shorthandReused() {
final rawCode = '''
^ ^
''';
expect(() => TestCode.parse(rawCode), throwsArgumentError);
}
void test_positions_reusedCaretNumber() {
void test_positions_shorthandReusedNumber() {
final rawCode = '''
/*1*/ ^
/*0*/ ^
''';
expect(() => TestCode.parse(rawCode), throwsArgumentError);
}
@ -159,6 +159,59 @@ int b = 2;
expect(() => TestCode.parse(rawCode), throwsArgumentError);
}
void test_ranges_nonShorthandMarkers() {
final rawCode = '''
String a = '[!not markers!]';
''';
final code = TestCode.parse(rawCode, rangeShorthand: false);
expect(code.rawCode, rawCode);
expect(code.code, rawCode); // No change.
expect(code.positions, isEmpty);
expect(code.ranges, isEmpty);
}
void test_ranges_shorthand() {
final rawCode = '''
int [!a = 1;!]
int b = 2;
''';
final expectedCode = '''
int a = 1;
int b = 2;
''';
final code = TestCode.parse(rawCode);
expect(code.rawCode, rawCode);
expect(code.code, expectedCode);
expect(code.positions, isEmpty);
expect(code.ranges, hasLength(1));
expect(code.ranges[0].sourceRange, SourceRange(4, 6));
expect(
code.ranges[0].range,
lsp.Range(
start: lsp.Position(line: 0, character: 4),
end: lsp.Position(line: 0, character: 10)));
expect(code.ranges[0].text, 'a = 1;');
}
void test_ranges_shorthandReused() {
final rawCode = '''
int [!a = 1;!]
int [!b = 2!];
''';
expect(() => TestCode.parse(rawCode), throwsArgumentError);
}
void test_ranges_shorthandReusedNumber() {
final rawCode = '''
int [!a = 1;!]
int /*[0*/b = 2/*0]*/;
''';
expect(() => TestCode.parse(rawCode), throwsArgumentError);
}
void test_ranges_startReused() {
final rawCode = '''
/*[0*/ /*0]*/

View file

@ -26,6 +26,9 @@ import 'package:collection/collection.dart';
/// For convenience, a single position can also be marked with a `^` (which
/// behaves the same as `/*0*/).
class TestCode {
static final _positionShorthand = '^';
static final _rangeStartShorthand = '[!';
static final _rangeEndShorthand = '!]';
static final _positionPattern = RegExp(r'\/\*(\d+)\*\/');
static final _rangeStartPattern = RegExp(r'\/\*\[(\d+)\*\/');
static final _rangeEndPattern = RegExp(r'\/\*(\d+)\]\*\/');
@ -48,7 +51,11 @@ class TestCode {
TestCodePosition get position => positions.single;
TestCodeRange get range => ranges.single;
static TestCode parse(String rawCode, {bool treatCaretAsPosition = true}) {
static TestCode parse(
String rawCode, {
bool positionShorthand = true,
bool rangeShorthand = true,
}) {
final scanner = _StringScanner(rawCode);
final codeBuffer = StringBuffer();
final positionOffsets = <int, int>{};
@ -94,10 +101,14 @@ class TestCode {
while (!scanner.isDone) {
start = codeBuffer.length;
if (treatCaretAsPosition && scanner.scan('^')) {
if (positionShorthand && scanner.scan(_positionShorthand)) {
recordPosition(0);
} else if (scanner.scan(_positionPattern)) {
recordPosition(scannedNumber());
} else if (rangeShorthand && scanner.scan(_rangeStartShorthand)) {
recordRangeStart(0);
} else if (rangeShorthand && scanner.scan(_rangeEndShorthand)) {
recordRangeEnd(0);
} else if (scanner.scan(_rangeStartPattern)) {
recordRangeStart(scannedNumber());
} else if (scanner.scan(_rangeEndPattern)) {