Files
sdk/pkg/analysis_server/test/lsp/document_highlights_test.dart
T
Danny Tuppeny f7e288e3ad Return empty responses instead of errors for non-Dart files in LSP server
Previously we could return "invalid path" responses for any non-Dart files. This allows clients to send requests for other files and just successful (though empty) responses.

Change-Id: I192e83ac2ebd2124c03c22e3e048a066cfd33f17
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101990
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Danny Tuppeny <dantup@google.com>
2019-05-13 09:04:36 +00:00

107 lines
2.7 KiB
Dart

// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'server_abstract.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(DocumentHighlightsTest);
});
}
@reflectiveTest
class DocumentHighlightsTest extends AbstractLspAnalysisServerTest {
test_functions() => _testMarkedContent('''
[[main]]() {
[[mai^n]]();
}
''');
test_localVariable() => _testMarkedContent('''
main() {
var [[f^oo]] = 1;
print([[foo]]);
[[foo]] = 2;
}
''');
test_nonDartFile() async {
await initialize();
await openFile(pubspecFileUri, simplePubspecContent);
final highlights =
await getDocumentHighlights(pubspecFileUri, startOfDocPos);
// Non-Dart files should return empty results, not errors.
expect(highlights, isEmpty);
}
test_noResult() => _testMarkedContent('''
main() {
// This one is in a ^ comment!
}
''');
test_onlySelf() => _testMarkedContent('''
main() {
[[prin^t]]();
}
''');
test_shadow_inner() => _testMarkedContent('''
main() {
var foo = 1;
func() {
var [[fo^o]] = 2;
print([[foo]]);
}
}
''');
test_shadow_outer() => _testMarkedContent('''
main() {
var [[foo]] = 1;
func() {
var foo = 2;
print(foo);
}
print([[fo^o]]);
}
''');
test_topLevelVariable() => _testMarkedContent('''
String [[foo]] = 'bar';
main() {
print([[foo]]);
[[fo^o]] = 2;
}
''');
/// Tests highlights in a Dart file using the provided content.
/// The content should be marked with a ^ where the highlights request should
/// be invoked and with `[[double brackets]]` around each range expected to
/// be highlighted (eg. all references to the symbol under ^).
/// If the content does not include any `[[double brackets]]` then the response
/// is expected to be `null`.
_testMarkedContent(String content) async {
final expectedRanges = rangesFromMarkers(content);
await initialize();
await openFile(mainFileUri, withoutMarkers(content));
final pos = positionFromMarker(content);
final highlights = await getDocumentHighlights(mainFileUri, pos);
if (expectedRanges.isEmpty) {
expect(highlights, isNull);
} else {
final highlightRanges = highlights.map((h) => h.range).toList();
expect(highlightRanges, equals(expectedRanges));
}
}
}