Skip to content

Commit 95f0052

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Add tests for unversioned documents for EditableArguments
The legacy protocol does not currently version documents, so this adds some additional tests for that. The tests are in the Legacy class and not the shared class because in LSP versions are not optional for opening/changing documents (they are optional in some of the other APIs, but not for open/change). Change-Id: I7e75b813025b3f67ed143e1b3a8b717224bfa80f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404820 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 7f5fc25 commit 95f0052

File tree

3 files changed

+126
-35
lines changed

3 files changed

+126
-35
lines changed

pkg/analysis_server/test/lsp_over_legacy/abstract_lsp_over_legacy.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,32 @@ abstract class SharedLspOverLegacyTest extends LspOverLegacyTest
401401

402402
@override
403403
Future<void> openFile(Uri uri, String content, {int version = 1}) async {
404+
// TODO(dantup): Add version here when legacy protocol supports.
405+
await addOverlay(fromUri(uri), content);
406+
}
407+
408+
/// Opens a file content without providing a version number.
409+
///
410+
/// This method should be used only for specifically testing unversioned
411+
/// changes and [openFile] used by default since going forwards, we expect
412+
/// all clients to start providing version numbers.
413+
Future<void> openFileUnversioned(Uri uri, String content) async {
404414
await addOverlay(fromUri(uri), content);
405415
}
406416

407417
@override
408418
Future<void> replaceFile(int newVersion, Uri uri, String content) async {
419+
// TODO(dantup): Add version here when legacy protocol supports.
420+
// For legacy, we can use addOverlay to replace the whole file.
421+
await addOverlay(fromUri(uri), content);
422+
}
423+
424+
/// Replaces a file content without providing a version number.
425+
///
426+
/// This method should be used only for specifically testing unversioned
427+
/// changes and [replaceFile] used by default since going forwards, we expect
428+
/// all clients to start providing version numbers.
429+
Future<void> replaceFileUnversioned(Uri uri, String content) async {
409430
// For legacy, we can use addOverlay to replace the whole file.
410431
await addOverlay(fromUri(uri), content);
411432
}

pkg/analysis_server/test/lsp_over_legacy/editable_arguments_test.dart

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analysis_server/lsp_protocol/protocol.dart';
6+
import 'package:test/test.dart';
57
import 'package:test_reflective_loader/test_reflective_loader.dart';
68

79
import '../shared/shared_editable_arguments_tests.dart';
10+
import '../utils/test_code_extensions.dart';
811
import 'abstract_lsp_over_legacy.dart';
912

1013
void main() {
@@ -31,19 +34,85 @@ class EditableArgumentsTest extends SharedLspOverLegacyTest
3134
writeTestPackageConfig(flutter: true);
3235
}
3336

37+
/// Over the legacy protocol, document versions are optional so we must also
38+
/// support this.
39+
test_textDocument_unversioned() async {
40+
var result = await getEditableArgumentsFor('''
41+
class MyWidget extends StatelessWidget {
42+
const MyWidget(String a1);
43+
44+
@override
45+
Widget build(BuildContext context) => MyW^idget('value1');
46+
}
47+
''', open: openFileUnversioned);
48+
49+
// Verify initial content has no version.
50+
expect(
51+
result!.textDocument,
52+
isA<OptionalVersionedTextDocumentIdentifier>()
53+
.having((td) => td.uri, 'uri', testFileUri)
54+
.having((td) => td.version, 'version', isNull),
55+
);
56+
57+
// Update the content.
58+
await replaceFileUnversioned(testFileUri, '${code.code}\n// extra comment');
59+
60+
// Verify new results have no version.
61+
result = await getEditableArguments(testFileUri, code.position.position);
62+
expect(
63+
result!.textDocument,
64+
isA<OptionalVersionedTextDocumentIdentifier>()
65+
.having((td) => td.uri, 'uri', testFileUri)
66+
.having((td) => td.version, 'version', isNull),
67+
);
68+
}
69+
70+
/// Over the legacy protocol, document versions are optional so we must also
71+
/// support this.
72+
test_textDocument_unversioned_closedFile() async {
73+
var result = await getEditableArgumentsFor('''
74+
class MyWidget extends StatelessWidget {
75+
const MyWidget(String a1);
76+
77+
@override
78+
Widget build(BuildContext context) => MyW^idget('value1');
79+
}
80+
''', open: openFileUnversioned);
81+
82+
// Verify initial content has no version.
83+
expect(
84+
result!.textDocument,
85+
isA<OptionalVersionedTextDocumentIdentifier>()
86+
.having((td) => td.uri, 'uri', testFileUri)
87+
.having((td) => td.version, 'version', isNull),
88+
);
89+
90+
// Close the file.
91+
await closeFile(testFileUri);
92+
93+
// Verify new results have no version.
94+
result = await getEditableArguments(testFileUri, code.position.position);
95+
expect(
96+
result!.textDocument,
97+
isA<OptionalVersionedTextDocumentIdentifier>()
98+
.having((td) => td.uri, 'uri', testFileUri)
99+
.having((td) => td.version, 'version', isNull),
100+
);
101+
}
102+
34103
@override
35104
@FailingTest(reason: 'Document versions not currently supported for legacy')
36-
test_textDocument_closedFile() {
105+
test_textDocument_versioned() {
37106
// TODO(dantup): Implement support for version numbers in the legacy
38107
// protocol.
39-
return super.test_textDocument_closedFile();
108+
return super.test_textDocument_versioned();
40109
}
41110

42111
@override
43112
@FailingTest(reason: 'Document versions not currently supported for legacy')
44-
test_textDocument_versions() {
113+
test_textDocument_versioned_closedFile() {
45114
// TODO(dantup): Implement support for version numbers in the legacy
46115
// protocol.
47-
return super.test_textDocument_versions();
116+
return super.test_textDocument_versioned_closedFile();
48117
}
49118
}

pkg/analysis_server/test/shared/shared_editable_arguments_tests.dart

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ mixin SharedEditableArgumentsTests
1919
/// Initializes the server with [content] and fetches editable arguments.
2020
Future<EditableArguments?> getEditableArgumentsFor(
2121
String content, {
22-
bool open = true,
22+
Future<void> Function(Uri, String)? open,
2323
}) async {
24+
// Default to the standart openFile function if we weren't overridden.
25+
open ??= openFile;
26+
2427
code = TestCode.parse('''
2528
import 'package:flutter/widgets.dart';
2629
2730
$content
2831
''');
2932
createFile(testFilePath, code.code);
3033
await initializeServer();
31-
if (open) {
32-
await openFile(testFileUri, code.code);
33-
}
34+
await open(testFileUri, code.code);
3435
await currentAnalysis;
3536
return await getEditableArguments(testFileUri, code.position.position);
3637
}
@@ -755,57 +756,57 @@ class MyWidget extends StatelessWidget {
755756
);
756757
}
757758

758-
test_textDocument_closedFile() async {
759+
test_textDocument_unopenedFile() async {
759760
var result = await getEditableArgumentsFor('''
760761
class MyWidget extends StatelessWidget {
761762
const MyWidget(String a1);
762763
763764
@override
764765
Widget build(BuildContext context) => MyW^idget('value1');
765766
}
766-
''');
767+
''', open: (_, _) async {});
767768

768-
// Verify initial content of 1.
769-
expect(
770-
result!.textDocument,
771-
isA<VersionedTextDocumentIdentifier>()
772-
.having((td) => td.uri, 'uri', testFileUri)
773-
.having((td) => td.version, 'version', 1),
774-
);
775-
776-
// Close the file.
777-
await closeFile(testFileUri);
778-
779-
// Verify new results have null version.
780-
result = await getEditableArguments(testFileUri, code.position.position);
769+
// Verify null version for unopened file.
781770
expect(
782771
result!.textDocument,
783772
isA<OptionalVersionedTextDocumentIdentifier>()
784773
.having((td) => td.uri, 'uri', testFileUri)
785-
.having((td) => td.version, 'version', isNull),
774+
.having((td) => td.version, 'version', null),
786775
);
787776
}
788777

789-
test_textDocument_unopenedFile() async {
778+
test_textDocument_versioned() async {
790779
var result = await getEditableArgumentsFor('''
791780
class MyWidget extends StatelessWidget {
792781
const MyWidget(String a1);
793782
794783
@override
795784
Widget build(BuildContext context) => MyW^idget('value1');
796785
}
797-
''', open: false);
786+
''');
798787

799-
// Verify null version for unopened file.
788+
// Verify initial content of 1.
800789
expect(
801790
result!.textDocument,
802-
isA<OptionalVersionedTextDocumentIdentifier>()
791+
isA<VersionedTextDocumentIdentifier>()
803792
.having((td) => td.uri, 'uri', testFileUri)
804-
.having((td) => td.version, 'version', null),
793+
.having((td) => td.version, 'version', 1),
794+
);
795+
796+
// Update the content to v5.
797+
await replaceFile(5, testFileUri, '${code.code}\n// extra comment');
798+
799+
// Verify new results have version 5.
800+
result = await getEditableArguments(testFileUri, code.position.position);
801+
expect(
802+
result!.textDocument,
803+
isA<VersionedTextDocumentIdentifier>()
804+
.having((td) => td.uri, 'uri', testFileUri)
805+
.having((td) => td.version, 'version', 5),
805806
);
806807
}
807808

808-
test_textDocument_versions() async {
809+
test_textDocument_versioned_closedFile() async {
809810
var result = await getEditableArgumentsFor('''
810811
class MyWidget extends StatelessWidget {
811812
const MyWidget(String a1);
@@ -823,16 +824,16 @@ class MyWidget extends StatelessWidget {
823824
.having((td) => td.version, 'version', 1),
824825
);
825826

826-
// Update the content to v5.
827-
await replaceFile(5, testFileUri, '${code.code}\n// extra comment');
827+
// Close the file.
828+
await closeFile(testFileUri);
828829

829-
// Verify new results have version 5.
830+
// Verify new results have null version.
830831
result = await getEditableArguments(testFileUri, code.position.position);
831832
expect(
832833
result!.textDocument,
833-
isA<VersionedTextDocumentIdentifier>()
834+
isA<OptionalVersionedTextDocumentIdentifier>()
834835
.having((td) => td.uri, 'uri', testFileUri)
835-
.having((td) => td.version, 'version', 5),
836+
.having((td) => td.version, 'version', isNull),
836837
);
837838
}
838839

0 commit comments

Comments
 (0)