Skip to content

Commit aaeb9e7

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Use check() and extensions for SourceChange/SourceFileEdit.
Change-Id: I7372fa152aa78fc6db01a11306a903e0ce38463e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/219742 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 9074957 commit aaeb9e7

File tree

2 files changed

+78
-64
lines changed

2 files changed

+78
-64
lines changed

pkg/analysis_server/test/domain_completion_test.dart

Lines changed: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
2222
import 'package:analyzer_plugin/protocol/protocol.dart' as plugin;
2323
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
2424
import 'package:analyzer_utilities/check/check.dart';
25+
import 'package:meta/meta.dart';
2526
import 'package:test/test.dart';
2627
import 'package:test_reflective_loader/test_reflective_loader.dart';
2728

2829
import 'domain_completion_util.dart';
2930
import 'mocks.dart';
3031
import 'src/plugin/plugin_manager_test.dart';
32+
import 'utils/change_check.dart';
3133

3234
void main() {
3335
defineReflectiveSuite(() {
@@ -43,30 +45,31 @@ class CompletionDomainHandlerGetSuggestionDetails2Test
4345
Future<void> test_alreadyImported() async {
4446
await _configureWithWorkspaceRoot();
4547

46-
var validator = await _getTestCodeDetails('''
48+
var details = await _getTestCodeDetails('''
4749
import 'dart:math';
4850
void f() {
4951
Rand^
5052
}
5153
''', completion: 'Random', libraryUri: 'dart:math');
52-
validator
53-
..hasCompletion('Random')
54-
..hasChange().assertNoFileEdits();
54+
check(details)
55+
..completion.isEqualTo('Random')
56+
..change.edits.isEmpty;
5557
}
5658

5759
Future<void> test_import_dart() async {
5860
await _configureWithWorkspaceRoot();
5961

60-
var validator = await _getTestCodeDetails('''
62+
var details = await _getTestCodeDetails('''
6163
void f() {
6264
R^
6365
}
6466
''', completion: 'Random', libraryUri: 'dart:math');
65-
validator
66-
..hasCompletion('Random')
67-
..hasChange()
67+
check(details)
68+
..completion.isEqualTo('Random')
69+
..change
6870
.hasFileEdit(testFilePathPlatform)
69-
.whenApplied(testFileContent, r'''
71+
.appliedTo(testFileContent)
72+
.isEqualTo(r'''
7073
import 'dart:math';
7174
7275
void f() {
@@ -94,16 +97,17 @@ class Test {}
9497

9598
await _configureWithWorkspaceRoot();
9699

97-
var validator = await _getTestCodeDetails('''
100+
var details = await _getTestCodeDetails('''
98101
void f() {
99102
T^
100103
}
101104
''', completion: 'Test', libraryUri: 'package:aaa/a.dart');
102-
validator
103-
..hasCompletion('Test')
104-
..hasChange()
105+
check(details)
106+
..completion.isEqualTo('Test')
107+
..change
105108
.hasFileEdit(testFilePathPlatform)
106-
.whenApplied(testFileContent, r'''
109+
.appliedTo(testFileContent)
110+
.isEqualTo(r'''
107111
import 'package:aaa/a.dart';
108112
109113
void f() {
@@ -119,16 +123,17 @@ class Test {}
119123

120124
await _configureWithWorkspaceRoot();
121125

122-
var validator = await _getTestCodeDetails('''
126+
var details = await _getTestCodeDetails('''
123127
void f() {
124128
T^
125129
}
126130
''', completion: 'Test', libraryUri: 'package:test/a.dart');
127-
validator
128-
..hasCompletion('Test')
129-
..hasChange()
131+
check(details)
132+
..completion.isEqualTo('Test')
133+
..change
130134
.hasFileEdit(testFilePathPlatform)
131-
.whenApplied(testFileContent, r'''
135+
.appliedTo(testFileContent)
136+
.isEqualTo(r'''
132137
import 'package:test/a.dart';
133138
134139
void f() {
@@ -160,7 +165,7 @@ void f() {
160165
expect(response.error?.code, RequestErrorCode.INVALID_FILE_PATH_FORMAT);
161166
}
162167

163-
Future<GetSuggestionDetails2Validator> _getCodeDetails({
168+
Future<CompletionGetSuggestionDetails2Result> _getCodeDetails({
164169
required String path,
165170
required String content,
166171
required String completion,
@@ -184,7 +189,7 @@ void f() {
184189
);
185190
}
186191

187-
Future<GetSuggestionDetails2Validator> _getDetails({
192+
Future<CompletionGetSuggestionDetails2Result> _getDetails({
188193
required String path,
189194
required int completionOffset,
190195
required String completion,
@@ -198,11 +203,10 @@ void f() {
198203
).toRequest('0');
199204

200205
var response = await _handleSuccessfulRequest(request);
201-
var result = CompletionGetSuggestionDetails2Result.fromResponse(response);
202-
return GetSuggestionDetails2Validator(result);
206+
return CompletionGetSuggestionDetails2Result.fromResponse(response);
203207
}
204208

205-
Future<GetSuggestionDetails2Validator> _getTestCodeDetails(
209+
Future<CompletionGetSuggestionDetails2Result> _getTestCodeDetails(
206210
String content, {
207211
required String completion,
208212
required String libraryUri,
@@ -2548,20 +2552,6 @@ class CompletionGetSuggestions2ResponseValidator {
25482552
}
25492553
}
25502554

2551-
class GetSuggestionDetails2Validator {
2552-
final CompletionGetSuggestionDetails2Result result;
2553-
2554-
GetSuggestionDetails2Validator(this.result);
2555-
2556-
SourceChangeValidator hasChange() {
2557-
return SourceChangeValidator(result.change);
2558-
}
2559-
2560-
void hasCompletion(Object completion) {
2561-
expect(result.completion, completion);
2562-
}
2563-
}
2564-
25652555
class PubPackageAnalysisServerTest with ResourceProviderMixin {
25662556
late final MockServerChannel serverChannel;
25672557
late final AnalysisServer server;
@@ -2739,32 +2729,6 @@ class SingleSuggestionValidator {
27392729
}
27402730
}
27412731

2742-
class SourceChangeValidator {
2743-
final SourceChange change;
2744-
2745-
SourceChangeValidator(this.change);
2746-
2747-
void assertNoFileEdits() {
2748-
expect(change.edits, isEmpty);
2749-
}
2750-
2751-
SourceFileEditValidator hasFileEdit(String path) {
2752-
var edit = change.edits.singleWhere((e) => e.file == path);
2753-
return SourceFileEditValidator(edit);
2754-
}
2755-
}
2756-
2757-
class SourceFileEditValidator {
2758-
final SourceFileEdit edit;
2759-
2760-
SourceFileEditValidator(this.edit);
2761-
2762-
void whenApplied(String applyTo, Object expected) {
2763-
var actual = SourceEdit.applySequence(applyTo, edit.edits);
2764-
expect(actual, expected);
2765-
}
2766-
}
2767-
27682732
class SuggestionsValidator {
27692733
final List<CompletionSuggestion> suggestions;
27702734
final List<String>? libraryUrisToImport;
@@ -2853,3 +2817,21 @@ class SuggestionsValidator {
28532817
return withKind(CompletionSuggestionKind.IDENTIFIER);
28542818
}
28552819
}
2820+
2821+
extension on CheckTarget<CompletionGetSuggestionDetails2Result> {
2822+
@useResult
2823+
CheckTarget<String> get completion {
2824+
return nest(
2825+
value.completion,
2826+
(selected) => 'has completion ${valueStr(selected)}',
2827+
);
2828+
}
2829+
2830+
@useResult
2831+
CheckTarget<SourceChange> get change {
2832+
return nest(
2833+
value.change,
2834+
(selected) => 'has change ${valueStr(selected)}',
2835+
);
2836+
}
2837+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/protocol_server.dart';
6+
import 'package:analyzer_utilities/check/check.dart';
7+
import 'package:meta/meta.dart';
8+
9+
extension SourceChangeExtension on CheckTarget<SourceChange> {
10+
@useResult
11+
CheckTarget<List<SourceFileEdit>> get edits {
12+
return nest(value.edits, (value) => 'has edits ${valueStr(value)}');
13+
}
14+
15+
CheckTarget<SourceFileEdit> hasFileEdit(String path) {
16+
return nest(
17+
value.edits.singleWhere((e) => e.file == path),
18+
(selected) => 'has edit ${valueStr(selected)}',
19+
);
20+
}
21+
}
22+
23+
extension SourceFileEditExtension on CheckTarget<SourceFileEdit> {
24+
@useResult
25+
CheckTarget<String> appliedTo(String applyTo) {
26+
var actual = SourceEdit.applySequence(applyTo, value.edits);
27+
return nest(
28+
actual,
29+
(selected) => 'produces ${valueStr(selected)}',
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)