@@ -7,15 +7,114 @@ import 'dart:convert';
7
7
8
8
import 'package:analysis_server/lsp_protocol/protocol.dart' ;
9
9
import 'package:analysis_server/protocol/protocol_constants.dart' ;
10
+ import 'package:analysis_server/src/lsp/constants.dart' ;
10
11
import 'package:analysis_server/src/protocol/protocol_internal.dart' ;
11
12
import 'package:analysis_server/src/protocol_server.dart' ;
13
+ import 'package:analyzer/file_system/file_system.dart' ;
14
+ import 'package:analyzer/src/utilities/extensions/file_system.dart' ;
12
15
import 'package:analyzer_plugin/src/utilities/client_uri_converter.dart' ;
16
+ import 'package:collection/collection.dart' ;
13
17
import 'package:path/path.dart' as path;
14
18
import 'package:test/test.dart' ;
15
19
16
20
import '../analysis_server_base.dart' ;
17
21
import '../lsp/change_verifier.dart' ;
18
22
import '../lsp/request_helpers_mixin.dart' ;
23
+ import '../services/completion/dart/text_expectations.dart' ;
24
+ import '../utils/tree_string_sink.dart' ;
25
+
26
+ class EventsCollector {
27
+ final ContextResolutionTest test;
28
+ List <Object > events = [];
29
+
30
+ EventsCollector (this .test) {
31
+ test.notificationListener = (notification) {
32
+ switch (notification.event) {
33
+ case ANALYSIS_NOTIFICATION_ERRORS :
34
+ events.add (
35
+ AnalysisErrorsParams .fromNotification (notification),
36
+ );
37
+ case ANALYSIS_NOTIFICATION_FLUSH_RESULTS :
38
+ events.add (
39
+ AnalysisFlushResultsParams .fromNotification (notification),
40
+ );
41
+ case LSP_NOTIFICATION_NOTIFICATION :
42
+ final params = LspNotificationParams .fromNotification (notification);
43
+ events.add (params.lspNotification);
44
+ default :
45
+ throw StateError (notification.event);
46
+ }
47
+ };
48
+ }
49
+
50
+ List <Object > take () {
51
+ final result = events;
52
+ events = [];
53
+ return result;
54
+ }
55
+ }
56
+
57
+ class EventsPrinter {
58
+ final EventsPrinterConfiguration configuration;
59
+ final ResourceProvider resourceProvider;
60
+ final TreeStringSink sink;
61
+
62
+ EventsPrinter ({
63
+ required this .configuration,
64
+ required this .resourceProvider,
65
+ required this .sink,
66
+ });
67
+
68
+ void write (List <Object > events) {
69
+ for (final event in events) {
70
+ switch (event) {
71
+ case AnalysisErrorsParams ():
72
+ sink.writelnWithIndent ('AnalysisErrors' );
73
+ sink.withIndent (() {
74
+ _writelnFile (name: 'file' , event.file);
75
+ if (event.errors.isNotEmpty) {
76
+ sink.writelnWithIndent ('errors: notEmpty' );
77
+ } else {
78
+ sink.writelnWithIndent ('errors: empty' );
79
+ }
80
+ });
81
+ case AnalysisFlushResultsParams ():
82
+ sink.writeElements (
83
+ 'AnalysisFlushResults' ,
84
+ event.files.sorted (),
85
+ _writelnFile,
86
+ );
87
+ case NotificationMessage ():
88
+ switch (event.method) {
89
+ case CustomMethods .dartTextDocumentContentDidChange:
90
+ sink.writelnWithIndent (event.method);
91
+ var params =
92
+ event.params as DartTextDocumentContentDidChangeParams ;
93
+ sink.withIndent (() {
94
+ sink.writeIndentedLine (() {
95
+ sink.write ('uri: ' );
96
+ sink.write (params.uri);
97
+ });
98
+ });
99
+ }
100
+ default :
101
+ throw UnimplementedError ('${event .runtimeType }' );
102
+ }
103
+ }
104
+ }
105
+
106
+ void _writelnFile (String path, {String ? name}) {
107
+ sink.writeIndentedLine (() {
108
+ if (name != null ) {
109
+ sink.write ('$name : ' );
110
+ }
111
+ final file = resourceProvider.getFile (path);
112
+ sink.write (file.posixPath);
113
+ });
114
+ }
115
+ }
116
+
117
+ class EventsPrinterConfiguration {}
19
118
20
119
abstract class LspOverLegacyTest extends PubPackageAnalysisServerTest
21
120
with
@@ -61,6 +160,31 @@ abstract class LspOverLegacyTest extends PubPackageAnalysisServerTest
61
160
);
62
161
}
63
162
163
+ Future <void > assertEventsText (
164
+ EventsCollector collector,
165
+ String expected,
166
+ ) async {
167
+ await pumpEventQueue (times: 5000 );
168
+
169
+ final buffer = StringBuffer ();
170
+ final sink = TreeStringSink (sink: buffer, indent: '' );
171
+
172
+ final events = collector.take ();
173
+ EventsPrinter (
174
+ configuration: EventsPrinterConfiguration (),
175
+ resourceProvider: resourceProvider,
176
+ sink: sink,
177
+ ).write (events);
178
+
179
+ final actual = buffer.toString ();
180
+ if (actual != expected) {
181
+ print ('-------- Actual --------' );
182
+ print ('$actual ------------------------' );
183
+ TextExpectationsCollector .add (actual);
184
+ }
185
+ expect (actual, expected);
186
+ }
187
+
64
188
/// Creates a legacy request with an auto-assigned ID.
65
189
Request createLegacyRequest (RequestParams params) {
66
190
return params.toRequest ('${_nextLspRequestId ++}' );
0 commit comments