Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: pkg/analysis_server/test/domain_analysis_test.dart

Issue 307963006: Implementation of 'notification.highlight' API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library test.domain.analysis; 5 library test.domain.analysis;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/analysis_server.dart'; 9 import 'package:analysis_server/src/analysis_server.dart';
10 import 'package:analysis_server/src/computers.dart';
10 import 'package:analysis_server/src/domain_analysis.dart'; 11 import 'package:analysis_server/src/domain_analysis.dart';
11 import 'package:analysis_server/src/protocol.dart'; 12 import 'package:analysis_server/src/protocol.dart';
12 import 'package:analysis_server/src/resource.dart'; 13 import 'package:analysis_server/src/resource.dart';
13 import 'package:unittest/unittest.dart'; 14 import 'package:unittest/unittest.dart';
14 15
15 import 'mocks.dart'; 16 import 'mocks.dart';
16 17
17 18
18 main() { 19 main() {
19 groupSep = ' | '; 20 groupSep = ' | ';
20 21
21 MockServerChannel serverChannel; 22 MockServerChannel serverChannel;
22 AnalysisServer server; 23 AnalysisServer server;
23 AnalysisDomainHandler handler; 24 AnalysisDomainHandler handler;
24 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); 25 MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
25 26
26 setUp(() { 27 setUp(() {
27 serverChannel = new MockServerChannel(); 28 serverChannel = new MockServerChannel();
28 server = new AnalysisServer(serverChannel, resourceProvider); 29 server = new AnalysisServer(serverChannel, resourceProvider);
29 handler = new AnalysisDomainHandler(server); 30 handler = new AnalysisDomainHandler(server);
30 }); 31 });
31 32
32 group('notification.errors', testNotificationErrors); 33 group('notification.errors', testNotificationErrors);
34 group('notification.highlights', testNotificationHighlights);
33 group('updateContent', testUpdateContent); 35 group('updateContent', testUpdateContent);
34 36
35 group('AnalysisDomainHandler', () { 37 group('AnalysisDomainHandler', () {
36 test('getFixes', () { 38 test('getFixes', () {
37 var request = new Request('0', AnalysisDomainHandler.GET_FIXES_METHOD); 39 var request = new Request('0', AnalysisDomainHandler.GET_FIXES_METHOD);
38 request.setParameter(AnalysisDomainHandler.ERRORS_PARAM, []); 40 request.setParameter(AnalysisDomainHandler.ERRORS_PARAM, []);
39 var response = handler.handleRequest(request); 41 var response = handler.handleRequest(request);
40 // TODO(scheglov) implement 42 // TODO(scheglov) implement
41 expect(response, isNull); 43 expect(response, isNull);
42 }); 44 });
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 ['/dart/sdk-1.2']); 137 ['/dart/sdk-1.2']);
136 request.setParameter(AnalysisDomainHandler.DEFAULT_PARAM, '/dart/sdk-1.4') ; 138 request.setParameter(AnalysisDomainHandler.DEFAULT_PARAM, '/dart/sdk-1.4') ;
137 var response = handler.handleRequest(request); 139 var response = handler.handleRequest(request);
138 // TODO(scheglov) implement 140 // TODO(scheglov) implement
139 expect(response, isNull); 141 expect(response, isNull);
140 }); 142 });
141 }); 143 });
142 } 144 }
143 145
144 146
147 class AnalysisError {
148 final String file;
149 final String errorCode;
150 final int offset;
151 final int length;
152 final String message;
153 final String correction;
154 AnalysisError(this.file, this.errorCode, this.offset, this.length,
155 this.message, this.correction);
156
157 @override
158 String toString() {
159 return 'NotificationError(file=$file; errorCode=$errorCode; '
160 'offset=$offset; length=$length; message=$message)';
161 }
162 }
163
164
165 AnalysisError jsonToAnalysisError(Map<String, Object> json) {
166 return new AnalysisError(
167 json['file'],
168 json['errorCode'],
169 json['offset'],
170 json['length'],
171 json['message'],
172 json['correction']);
173 }
174
175
145 /** 176 /**
146 * A helper to test 'analysis.*' requests. 177 * A helper to test 'analysis.*' requests.
147 */ 178 */
148 class AnalysisTestHelper { 179 class AnalysisTestHelper {
149 MockServerChannel serverChannel; 180 MockServerChannel serverChannel;
150 MemoryResourceProvider resourceProvider; 181 MemoryResourceProvider resourceProvider;
151 AnalysisServer server; 182 AnalysisServer server;
152 AnalysisDomainHandler handler; 183 AnalysisDomainHandler handler;
153 184
154 Map<String, List<AnalysisError>> filesErrors = {}; 185 Map<String, List<AnalysisError>> filesErrors = {};
186 Map<String, List<Map<String, Object>>> filesHighlights = {};
155 187
156 String testFile = '/project/bin/test.dart'; 188 String testFile = '/project/bin/test.dart';
157 String testCode; 189 String testCode;
158 190
159 AnalysisTestHelper() { 191 AnalysisTestHelper() {
160 serverChannel = new MockServerChannel(); 192 serverChannel = new MockServerChannel();
161 resourceProvider = new MemoryResourceProvider(); 193 resourceProvider = new MemoryResourceProvider();
162 server = new AnalysisServer(serverChannel, resourceProvider); 194 server = new AnalysisServer(serverChannel, resourceProvider);
163 handler = new AnalysisDomainHandler(server); 195 handler = new AnalysisDomainHandler(server);
164 // listen for notifications 196 // listen for notifications
165 Stream<Notification> notificationStream = serverChannel.notificationControll er.stream; 197 Stream<Notification> notificationStream = serverChannel.notificationControll er.stream;
166 notificationStream.listen((Notification notification) { 198 notificationStream.listen((Notification notification) {
167 if (notification.event == AnalysisDomainHandler.ERRORS_NOTIFICATION) { 199 if (notification.event == AnalysisDomainHandler.ERRORS_NOTIFICATION) {
168 String file = notification.getParameter(AnalysisServer.FILE_PARAM); 200 String file = notification.getParameter(AnalysisServer.FILE_PARAM);
169 List<Map<String, Object>> errorMaps = notification.getParameter(Analysis Server.ERRORS_PARAM); 201 List<Map<String, Object>> errorMaps = notification.getParameter(Analysis Server.ERRORS_PARAM);
170 filesErrors[file] = errorMaps.map(jsonToAnalysisError).toList(); 202 filesErrors[file] = errorMaps.map(jsonToAnalysisError).toList();
171 } 203 }
204 if (notification.event == AnalysisDomainHandler.HIGHLIGHTS_NOTIFICATION) {
205 String file = notification.getParameter(AnalysisServer.FILE_PARAM);
206 filesHighlights[file] = notification.getParameter(AnalysisDomainHandler. REGIONS_PARAM);
207 }
172 }); 208 });
173 } 209 }
174 210
175 /** 211 /**
176 * Returns a [Future] that completes when this this helper finished all its 212 * Returns a [Future] that completes when this this helper finished all its
177 * scheduled tasks. 213 * scheduled tasks.
178 */ 214 */
179 Future waitForTasksFinished() { 215 Future waitForTasksFinished() {
180 return waitForServerTasksFinished(server); 216 return waitForServerTasksFinished(server);
181 } 217 }
182 218
183 /** 219 /**
184 * Returns [AnalysisError]s recorded for the given [file]. 220 * Returns [AnalysisError]s recorded for the given [file].
185 * May be empty, but not `null`. 221 * May be empty, but not `null`.
186 */ 222 */
187 List<AnalysisError> getErrors(String file) { 223 List<AnalysisError> getErrors(String file) {
188 List<AnalysisError> errors = filesErrors[file]; 224 List<AnalysisError> errors = filesErrors[file];
189 if (errors != null) { 225 if (errors != null) {
190 return errors; 226 return errors;
191 } 227 }
192 return <AnalysisError>[]; 228 return <AnalysisError>[];
193 } 229 }
194 230
195 /** 231 /**
232 * Returns highlights recorded for the given [file].
233 * May be empty, but not `null`.
234 */
235 List<Map<String, Object>> getHighlights(String file) {
236 List<Map<String, Object>> highlights = filesHighlights[file];
237 if (highlights != null) {
238 return highlights;
239 }
240 return [];
241 }
242
243 /**
196 * Returns [AnalysisError]s recorded for the [testFile]. 244 * Returns [AnalysisError]s recorded for the [testFile].
197 * May be empty, but not `null`. 245 * May be empty, but not `null`.
198 */ 246 */
199 List<AnalysisError> getTestErrors() { 247 List<AnalysisError> getTestErrors() {
200 return getErrors(testFile); 248 return getErrors(testFile);
201 } 249 }
202 250
203 /** 251 /**
252 * Returns highlights recorded for the given [testFile].
253 * May be empty, but not `null`.
254 */
255 List<Map<String, Object>> getTestHighlights() {
256 return getHighlights(testFile);
257 }
258
259 /**
204 * Creates a project with a single Dart file `/project/bin/test.dart` with 260 * Creates a project with a single Dart file `/project/bin/test.dart` with
205 * the given [code]. 261 * the given [code].
206 */ 262 */
207 void createSingleFileProject(code) { 263 void createSingleFileProject(code) {
208 this.testCode = _getCodeString(code); 264 this.testCode = _getCodeString(code);
209 resourceProvider.newFolder('/project'); 265 resourceProvider.newFolder('/project');
210 resourceProvider.newFile('/project/pubspec.yaml', 'name: project'); 266 resourceProvider.newFile('/project/pubspec.yaml', 'name: project');
211 resourceProvider.newFile(testFile, testCode); 267 resourceProvider.newFile(testFile, testCode);
212 Request request = new Request('0', AnalysisDomainHandler.SET_ANALYSIS_ROOTS_ METHOD); 268 Request request = new Request('0', AnalysisDomainHandler.SET_ANALYSIS_ROOTS_ METHOD);
213 request.setParameter(AnalysisDomainHandler.INCLUDED_PARAM, ['/project']); 269 request.setParameter(AnalysisDomainHandler.INCLUDED_PARAM, ['/project']);
214 request.setParameter(AnalysisDomainHandler.EXCLUDED_PARAM, []); 270 request.setParameter(AnalysisDomainHandler.EXCLUDED_PARAM, []);
215 handleSuccessfulRequest(request); 271 handleSuccessfulRequest(request);
216 } 272 }
217 273
274 void setFileContent(String path, String content) {
275 resourceProvider.newFile(path, content);
276 }
277
218 /** 278 /**
219 * Validates that the given [request] is handled successfully. 279 * Validates that the given [request] is handled successfully.
220 */ 280 */
221 void handleSuccessfulRequest(Request request) { 281 void handleSuccessfulRequest(Request request) {
222 Response response = handler.handleRequest(request); 282 Response response = handler.handleRequest(request);
223 expect(response, isResponseSuccess('0')); 283 expect(response, isResponseSuccess('0'));
224 } 284 }
225 285
226 /** 286 /**
227 * Stops the associated server. 287 * Stops the associated server.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 return helper.waitForTasksFinished().then((_) { 328 return helper.waitForTasksFinished().then((_) {
269 List<AnalysisError> errors = helper.getTestErrors(); 329 List<AnalysisError> errors = helper.getTestErrors();
270 expect(errors, hasLength(1)); 330 expect(errors, hasLength(1));
271 AnalysisError error = errors[0]; 331 AnalysisError error = errors[0];
272 expect(error.errorCode, 'StaticWarningCode.UNDEFINED_IDENTIFIER'); 332 expect(error.errorCode, 'StaticWarningCode.UNDEFINED_IDENTIFIER');
273 }); 333 });
274 }); 334 });
275 } 335 }
276 336
277 337
338 class NotificationHighlightHelper extends AnalysisTestHelper {
339 List<Map<String, Object>> regions;
340
341 Future prepareRegions(then()) {
342 return waitForTasksFinished().then((_) {
343 regions = getTestHighlights();
344 then();
345 });
346 }
347
348 void assertHasRawRegion(HighlightType type, int offset, int length) {
349 for (Map<String, Object> region in regions) {
350 if (region['offset'] == offset && region['length'] == length
351 && region['type'] == type.name) {
352 return;
353 }
354 }
355 fail('Expected to find (offset=$offset; length=$length; type=$type) in\n'
356 '${regions.join('\n')}');
357 }
358
359 void assertNoRawRegion(HighlightType type, int offset, int length) {
360 for (Map<String, Object> region in regions) {
361 if (region['offset'] == offset && region['length'] == length
362 && region['type'] == type.name) {
363 fail('Not expected to find (offset=$offset; length=$length; type=$type) in\n'
364 '${regions.join('\n')}');
365 }
366 }
367 }
368
369 void assertHasRegion(HighlightType type, String search, [int length = -1]) {
370 int offset = testCode.indexOf(search);
371 expect(offset, isNot(-1));
372 length = findRegionLength(search, length);
373 assertHasRawRegion(type, offset, length);
374 }
375
376 void assertNoRegion(HighlightType type, String search, [int length = -1]) {
377 int offset = testCode.indexOf(search);
378 expect(offset, isNot(-1));
379 length = findRegionLength(search, length);
380 assertNoRawRegion(type, offset, length);
381 }
382
383 int findRegionLength(String search, int length) {
384 if (length == -1) {
385 length = 0;
386 while (length < search.length) {
387 int c = search.codeUnitAt(length);
388 if (length == 0 && c == '@'.codeUnitAt(0)) {
389 length++;
390 continue;
391 }
392 if (!(c >= 'a'.codeUnitAt(0) && c <= 'z'.codeUnitAt(0) ||
393 c >= 'A'.codeUnitAt(0) && c <= 'Z'.codeUnitAt(0) ||
394 c >= '0'.codeUnitAt(0) && c <= '9'.codeUnitAt(0))) {
395 break;
396 }
397 length++;
398 }
399 }
400 return length;
401 }
402 }
403
404
405 testNotificationHighlights() {
406 Future testRegions(String code, then(NotificationHighlightHelper helper)) {
407 var helper = new NotificationHighlightHelper();
408 helper.createSingleFileProject(code);
409 return helper.prepareRegions(() {
410 then(helper);
411 });
412 }
413
414 group('ANNOTATION', () {
415 test('no arguments', () {
416 var code = '''
417 const AAA = 42;
418 @AAA main() {}
419 ''';
420 return testRegions(code, (NotificationHighlightHelper helper) {
421 helper.assertHasRegion(HighlightType.ANNOTATION, '@AAA');
422 });
423 });
424
425 test('has arguments', () {
426 var code = '''
427 class AAA {
428 const AAA(a, b, c);
429 }
430 @AAA(1, 2, 3) main() {}
431 ''';
432 return testRegions(code, (NotificationHighlightHelper helper) {
433 helper.assertHasRegion(HighlightType.ANNOTATION, '@AAA(', '@AAA('.length );
434 helper.assertHasRegion(HighlightType.ANNOTATION, ') main', ')'.length);
435 });
436 });
437 });
438
439 group('BUILT_IN', () {
440 test('abstract', () {
441 var code = '''
442 abstract class A {};
443 main() {
444 var abstract = 42;
445 }''';
446 return testRegions(code, (NotificationHighlightHelper helper) {
447 helper.assertHasRegion(HighlightType.BUILT_IN, 'abstract class');
448 helper.assertNoRegion(HighlightType.BUILT_IN, 'abstract = 42');
449 });
450 });
451
452 test('as', () {
453 var code = '''
454 import 'dart:math' as math;
455 main() {
456 p as int;
457 var as = 42;
458 }''';
459 return testRegions(code, (NotificationHighlightHelper helper) {
460 helper.assertHasRegion(HighlightType.BUILT_IN, 'as math');
461 helper.assertHasRegion(HighlightType.BUILT_IN, 'as int');
462 helper.assertNoRegion(HighlightType.BUILT_IN, 'as = 42');
463 });
464 });
465
466 test('deferred', () {
467 var code = '''
468 import 'dart:math' deferred as math;
469 main() {
470 var deferred = 42;
471 }''';
472 return testRegions(code, (NotificationHighlightHelper helper) {
473 helper.assertHasRegion(HighlightType.BUILT_IN, 'deferred as math');
474 helper.assertNoRegion(HighlightType.BUILT_IN, 'deferred = 42');
475 });
476 });
477
478 test('export', () {
479 var code = '''
480 export "dart:math";
481 main() {
482 var export = 42;
483 }''';
484 return testRegions(code, (NotificationHighlightHelper helper) {
485 helper.assertHasRegion(HighlightType.BUILT_IN, 'export "dart:');
486 helper.assertNoRegion(HighlightType.BUILT_IN, 'export = 42');
487 });
488 });
489
490 test('external', () {
491 var code = '''
492 class A {
493 external A();
494 external aaa();
495 }
496 external main() {
497 var external = 42;
498 }''';
499 return testRegions(code, (NotificationHighlightHelper helper) {
500 helper.assertHasRegion(HighlightType.BUILT_IN, 'external A()');
501 helper.assertHasRegion(HighlightType.BUILT_IN, 'external aaa()');
502 helper.assertHasRegion(HighlightType.BUILT_IN, 'external main()');
503 helper.assertNoRegion(HighlightType.BUILT_IN, 'external = 42');
504 });
505 });
506
507 test('factory', () {
508 var code = '''
509 class A {
510 factory A() => null;
511 }
512 main() {
513 var factory = 42;
514 }''';
515 return testRegions(code, (NotificationHighlightHelper helper) {
516 helper.assertHasRegion(HighlightType.BUILT_IN, 'factory A()');
517 helper.assertNoRegion(HighlightType.BUILT_IN, 'factory = 42');
518 });
519 });
520
521 test('get', () {
522 var code = '''
523 get aaa => 1;
524 class A {
525 get bbb => 2;
526 }
527 main() {
528 var get = 42;
529 }''';
530 return testRegions(code, (NotificationHighlightHelper helper) {
531 helper.assertHasRegion(HighlightType.BUILT_IN, 'get aaa =>');
532 helper.assertHasRegion(HighlightType.BUILT_IN, 'get bbb =>');
533 helper.assertNoRegion(HighlightType.BUILT_IN, 'get = 42');
534 });
535 });
536
537 test('hide', () {
538 var code = '''
539 import 'foo.dart' hide Foo;
540 main() {
541 var hide = 42;
542 }''';
543 return testRegions(code, (NotificationHighlightHelper helper) {
544 helper.assertHasRegion(HighlightType.BUILT_IN, 'hide Foo');
545 helper.assertNoRegion(HighlightType.BUILT_IN, 'hide = 42');
546 });
547 });
548
549 test('implements', () {
550 var code = '''
551 class A {}
552 class B implements A {}
553 main() {
554 var implements = 42;
555 }''';
556 return testRegions(code, (NotificationHighlightHelper helper) {
557 helper.assertHasRegion(HighlightType.BUILT_IN, 'implements A {}');
558 helper.assertNoRegion(HighlightType.BUILT_IN, 'implements = 42');
559 });
560 });
561
562 test('import', () {
563 var code = '''
564 import "foo.dart";
565 main() {
566 var import = 42;
567 }''';
568 return testRegions(code, (NotificationHighlightHelper helper) {
569 helper.assertHasRegion(HighlightType.BUILT_IN, 'import "');
570 helper.assertNoRegion(HighlightType.BUILT_IN, 'import = 42');
571 });
572 });
573
574 test('library', () {
575 var code = '''
576 library lib;
577 main() {
578 var library = 42;
579 }''';
580 return testRegions(code, (NotificationHighlightHelper helper) {
581 helper.assertHasRegion(HighlightType.BUILT_IN, 'library lib;');
582 helper.assertNoRegion(HighlightType.BUILT_IN, 'library = 42');
583 });
584 });
585
586 test('native', () {
587 var code = '''
588 class A native "A_native" {}
589 class B {
590 bbb() native "bbb_native";
591 }
592 main() {
593 var native = 42;
594 }''';
595 return testRegions(code, (NotificationHighlightHelper helper) {
596 helper.assertHasRegion(HighlightType.BUILT_IN, 'native "A_');
597 helper.assertHasRegion(HighlightType.BUILT_IN, 'native "bbb_');
598 helper.assertNoRegion(HighlightType.BUILT_IN, 'native = 42');
599 });
600 });
601
602 test('on', () {
603 var code = '''
604 main() {
605 try {
606 } on int catch (e) {
607 }
608 var on = 42;
609 }''';
610 return testRegions(code, (NotificationHighlightHelper helper) {
611 helper.assertHasRegion(HighlightType.BUILT_IN, 'on int');
612 helper.assertNoRegion(HighlightType.BUILT_IN, 'on = 42');
613 });
614 });
615
616 test('operator', () {
617 var code = '''
618 class A {
619 operator +(x) => null;
620 }
621 main() {
622 var operator = 42;
623 }''';
624 return testRegions(code, (NotificationHighlightHelper helper) {
625 helper.assertHasRegion(HighlightType.BUILT_IN, 'operator +(');
626 helper.assertNoRegion(HighlightType.BUILT_IN, 'operator = 42');
627 });
628 });
629
630 test('part', () {
631 var helper = new NotificationHighlightHelper();
632 var code = '''
633 part "my_part.dart";
634 main() {
635 var part = 42;
636 }''';
637 helper.createSingleFileProject(code);
638 helper.setFileContent('/project/bin/my_part.dart', 'part of lib;');
639 return helper.prepareRegions(() {
640 helper.assertHasRegion(HighlightType.BUILT_IN, 'part "my_');
641 helper.assertNoRegion(HighlightType.BUILT_IN, 'part = 42');
642 });
643 });
644
645 test('part', () {
646 var helper = new NotificationHighlightHelper();
647 var code = '''
648 part of lib;
649 main() {
650 var part = 1;
651 var of = 2;
652 }''';
653 helper.createSingleFileProject(code);
654 helper.setFileContent('/project/bin/lib.dart', '''
655 library lib;
656 part 'test.dart';
657 ''');
658 return helper.prepareRegions(() {
659 helper.assertHasRegion(HighlightType.BUILT_IN, 'part of', 'part of'.leng th);
660 helper.assertNoRegion(HighlightType.BUILT_IN, 'part = 1');
661 helper.assertNoRegion(HighlightType.BUILT_IN, 'of = 2');
662 });
663 });
664
665 test('set', () {
666 var code = '''
667 set aaa(x) {}
668 class A
669 set bbb(x) {}
670 }
671 main() {
672 var set = 42;
673 }''';
674 return testRegions(code, (NotificationHighlightHelper helper) {
675 helper.assertHasRegion(HighlightType.BUILT_IN, 'set aaa(');
676 helper.assertHasRegion(HighlightType.BUILT_IN, 'set bbb(');
677 helper.assertNoRegion(HighlightType.BUILT_IN, 'set = 42');
678 });
679 });
680
681 test('show', () {
682 var code = '''
683 import 'foo.dart' show Foo;
684 main() {
685 var show = 42;
686 }''';
687 return testRegions(code, (NotificationHighlightHelper helper) {
688 helper.assertHasRegion(HighlightType.BUILT_IN, 'show Foo');
689 helper.assertNoRegion(HighlightType.BUILT_IN, 'show = 42');
690 });
691 });
692
693 test('static', () {
694 var code = '''
695 class A {
696 static aaa;
697 static bbb() {}
698 }
699 main() {
700 var static = 42;
701 }''';
702 return testRegions(code, (NotificationHighlightHelper helper) {
703 helper.assertHasRegion(HighlightType.BUILT_IN, 'static aaa;');
704 helper.assertHasRegion(HighlightType.BUILT_IN, 'static bbb()');
705 helper.assertNoRegion(HighlightType.BUILT_IN, 'static = 42');
706 });
707 });
708
709 test('typedef', () {
710 var code = '''
711 typedef A();
712 main() {
713 var typedef = 42;
714 }''';
715 return testRegions(code, (NotificationHighlightHelper helper) {
716 helper.assertHasRegion(HighlightType.BUILT_IN, 'typedef A();');
717 helper.assertNoRegion(HighlightType.BUILT_IN, 'typedef = 42');
718 });
719 });
720 });
721
722 group('CLASS', () {
723 test('CLASS', () {
724 var code = '''
725 class AAA {}
726 AAA aaa;
727 ''';
728 return testRegions(code, (NotificationHighlightHelper helper) {
729 helper.assertHasRegion(HighlightType.CLASS, 'AAA {}');
730 helper.assertHasRegion(HighlightType.CLASS, 'AAA aaa');
731 });
732 });
733
734 test('not dynamic', () {
735 var code = '''
736 dynamic f() {}
737 ''';
738 return testRegions(code, (NotificationHighlightHelper helper) {
739 helper.assertNoRegion(HighlightType.CLASS, 'dynamic f()');
740 });
741 });
742
743 test('not void', () {
744 var code = '''
745 void f() {}
746 ''';
747 return testRegions(code, (NotificationHighlightHelper helper) {
748 helper.assertNoRegion(HighlightType.CLASS, 'void f()');
749 });
750 });
751 });
752
753 test('CONSTRUCTOR', () {
754 var code = '''
755 class AAA {
756 AAA() {}
757 AAA.name(p) {}
758 }
759 main() {
760 new AAA();
761 new AAA.name(42);
762 }
763 ''';
764 return testRegions(code, (NotificationHighlightHelper helper) {
765 helper.assertHasRegion(HighlightType.CONSTRUCTOR, 'name(p)');
766 helper.assertHasRegion(HighlightType.CONSTRUCTOR, 'name(42)');
767 helper.assertNoRegion(HighlightType.CONSTRUCTOR, 'AAA() {}');
768 helper.assertNoRegion(HighlightType.CONSTRUCTOR, 'AAA();');
769 });
770 });
771
772 test('DYNAMIC_TYPE', () {
773 var code = '''
774 f() {}
775 main(p) {
776 print(p);
777 var v1 = f();
778 int v2;
779 var v3 = v2;
780 }
781 ''';
782 return testRegions(code, (NotificationHighlightHelper helper) {
783 helper.assertHasRegion(HighlightType.DYNAMIC_TYPE, 'p)');
784 helper.assertHasRegion(HighlightType.DYNAMIC_TYPE, 'v1 =');
785 helper.assertNoRegion(HighlightType.DYNAMIC_TYPE, 'v2;');
786 helper.assertNoRegion(HighlightType.DYNAMIC_TYPE, 'v3 =');
787 });
788 });
789
790 test('FIELD', () {
791 var code = '''
792 class A {
793 int aaa = 1;
794 int bbb = 2;
795 A([this.bbb = 3]);
796 }
797 main(A a) {
798 a.aaa = 4;
799 a.bbb = 5;
800 }
801 ''';
802 return testRegions(code, (NotificationHighlightHelper helper) {
803 helper.assertHasRegion(HighlightType.FIELD, 'aaa = 1');
804 helper.assertHasRegion(HighlightType.FIELD, 'bbb = 2');
805 helper.assertHasRegion(HighlightType.FIELD, 'bbb = 3');
806 helper.assertHasRegion(HighlightType.FIELD, 'aaa = 4');
807 helper.assertHasRegion(HighlightType.FIELD, 'bbb = 5');
808 });
809 });
810
811 test('FIELD_STATIC', () {
812 var code = '''
813 class A {
814 static aaa = 1;
815 static get bbb => null;
816 static set ccc(x) {}
817 }
818 main() {
819 A.aaa = 2;
820 A.bbb;
821 A.ccc = 3;
822 }
823 ''';
824 return testRegions(code, (NotificationHighlightHelper helper) {
825 helper.assertHasRegion(HighlightType.FIELD_STATIC, 'aaa = 1');
826 helper.assertHasRegion(HighlightType.FIELD_STATIC, 'aaa = 2');
827 helper.assertHasRegion(HighlightType.FIELD_STATIC, 'bbb;');
828 helper.assertHasRegion(HighlightType.FIELD_STATIC, 'ccc = 3');
829 });
830 });
831
832 test('FUNCTION', () {
833 var code = '''
834 fff(p) {}
835 main() {
836 fff(42);
837 }
838 ''';
839 return testRegions(code, (NotificationHighlightHelper helper) {
840 helper.assertHasRegion(HighlightType.FUNCTION_DECLARATION, 'fff(p) {}');
841 helper.assertHasRegion(HighlightType.FUNCTION, 'fff(42)');
842 });
843 });
844
845 test('FUNCTION_TYPE_ALIAS', () {
846 var code = '''
847 typedef FFF(p);
848 main(FFF fff) {
849 }
850 ''';
851 return testRegions(code, (NotificationHighlightHelper helper) {
852 helper.assertHasRegion(HighlightType.FUNCTION_TYPE_ALIAS, 'FFF(p)');
853 helper.assertHasRegion(HighlightType.FUNCTION_TYPE_ALIAS, 'FFF fff)');
854 });
855 });
856
857 test('GETTER_DECLARATION', () {
858 var code = '''
859 get aaa => null;
860 class A {
861 get bbb => null;
862 }
863 main(A a) {
864 aaa;
865 a.bbb;
866 }
867 ''';
868 return testRegions(code, (NotificationHighlightHelper helper) {
869 helper.assertHasRegion(HighlightType.GETTER_DECLARATION, 'aaa => null');
870 helper.assertHasRegion(HighlightType.GETTER_DECLARATION, 'bbb => null');
871 helper.assertHasRegion(HighlightType.FIELD_STATIC, 'aaa;');
872 helper.assertHasRegion(HighlightType.FIELD, 'bbb;');
873 });
874 });
875
876 test('IDENTIFIER_DEFAULT', () {
877 var code = '''
878 main() {
879 aaa = 42;
880 bbb(84);
881 CCC ccc;
882 }
883 ''';
884 return testRegions(code, (NotificationHighlightHelper helper) {
885 helper.assertHasRegion(HighlightType.IDENTIFIER_DEFAULT, 'aaa = 42');
886 helper.assertHasRegion(HighlightType.IDENTIFIER_DEFAULT, 'bbb(84)');
887 helper.assertHasRegion(HighlightType.IDENTIFIER_DEFAULT, 'CCC ccc');
888 });
889 });
890
891 test('IMPORT_PREFIX', () {
892 var code = '''
893 import 'dart:math' as ma;
894 main() {
895 ma.max(1, 2);
896 }
897 ''';
898 return testRegions(code, (NotificationHighlightHelper helper) {
899 helper.assertHasRegion(HighlightType.IMPORT_PREFIX, 'ma;');
900 helper.assertHasRegion(HighlightType.IMPORT_PREFIX, 'ma.max');
901 });
902 });
903
904 test('KEYWORD void', () {
905 var code = '''
906 void main() {
907 }
908 ''';
909 return testRegions(code, (NotificationHighlightHelper helper) {
910 helper.assertHasRegion(HighlightType.KEYWORD, 'void main()');
911 });
912 });
913
914 test('LITERAL_BOOLEAN', () {
915 var code = 'var V = true;';
916 return testRegions(code, (NotificationHighlightHelper helper) {
917 helper.assertHasRegion(HighlightType.LITERAL_BOOLEAN, 'true;');
918 });
919 });
920
921 test('LITERAL_DOUBLE', () {
922 var code = 'var V = 4.2;';
923 return testRegions(code, (NotificationHighlightHelper helper) {
924 helper.assertHasRegion(HighlightType.LITERAL_DOUBLE, '4.2;', '4.2'.length) ;
925 });
926 });
927
928 test('LITERAL_INTEGER', () {
929 var code = 'var V = 42;';
930 return testRegions(code, (NotificationHighlightHelper helper) {
931 helper.assertHasRegion(HighlightType.LITERAL_INTEGER, '42;');
932 });
933 });
934
935 test('LITERAL_STRING', () {
936 var code = 'var V = "abc";';
937 return testRegions(code, (NotificationHighlightHelper helper) {
938 helper.assertHasRegion(HighlightType.LITERAL_STRING, '"abc";', '"abc"'.len gth);
939 });
940 });
941
942 test('LOCAL_VARIABLE', () {
943 var code = '''
944 main() {
945 int vvv = 0;
946 vvv;
947 vvv = 1;
948 }
949 ''';
950 return testRegions(code, (NotificationHighlightHelper helper) {
951 helper.assertHasRegion(HighlightType.LOCAL_VARIABLE_DECLARATION, 'vvv = 0' );
952 helper.assertHasRegion(HighlightType.LOCAL_VARIABLE, 'vvv;');
953 helper.assertHasRegion(HighlightType.LOCAL_VARIABLE, 'vvv = 1;');
954 });
955 });
956
957 test('METHOD', () {
958 var code = '''
959 class A {
960 aaa() {}
961 static bbb() {}
962 }
963 main(A a) {
964 a.aaa();
965 a.aaa;
966 A.bbb();
967 A.bbb;
968 }
969 ''';
970 return testRegions(code, (NotificationHighlightHelper helper) {
971 helper.assertHasRegion(HighlightType.METHOD_DECLARATION, 'aaa() {}');
972 helper.assertHasRegion(HighlightType.METHOD_DECLARATION_STATIC, 'bbb() {}' );
973 helper.assertHasRegion(HighlightType.METHOD, 'aaa();');
974 helper.assertHasRegion(HighlightType.METHOD, 'aaa;');
975 helper.assertHasRegion(HighlightType.METHOD_STATIC, 'bbb();');
976 helper.assertHasRegion(HighlightType.METHOD_STATIC, 'bbb;');
977 });
978 });
979
980 test('METHOD best type', () {
981 var code = '''
982 main(p) {
983 if (p is List) {
984 p.add(null);
985 }
986 }
987 ''';
988 return testRegions(code, (NotificationHighlightHelper helper) {
989 helper.assertHasRegion(HighlightType.METHOD, 'add(null)');
990 });
991 });
992
993 test('PARAMETER', () {
994 var code = '''
995 main(int p) {
996 p;
997 p = 42;
998 }
999 ''';
1000 return testRegions(code, (NotificationHighlightHelper helper) {
1001 helper.assertHasRegion(HighlightType.PARAMETER, 'p) {');
1002 helper.assertHasRegion(HighlightType.PARAMETER, 'p;');
1003 helper.assertHasRegion(HighlightType.PARAMETER, 'p = 42');
1004 });
1005 });
1006
1007 test('SETTER_DECLARATION', () {
1008 var code = '''
1009 set aaa(x) {}
1010 class A {
1011 set bbb(x) {}
1012 }
1013 main(A a) {
1014 aaa = 1;
1015 a.bbb = 2;
1016 }
1017 ''';
1018 return testRegions(code, (NotificationHighlightHelper helper) {
1019 helper.assertHasRegion(HighlightType.SETTER_DECLARATION, 'aaa(x)');
1020 helper.assertHasRegion(HighlightType.SETTER_DECLARATION, 'bbb(x)');
1021 helper.assertHasRegion(HighlightType.FIELD_STATIC, 'aaa = 1');
1022 helper.assertHasRegion(HighlightType.FIELD, 'bbb = 2');
1023 });
1024 });
1025
1026 test('TOP_LEVEL_VARIABLE', () {
1027 var code = '''
1028 var VVV = 0;
1029 main() {
1030 print(VVV);
1031 VVV = 1;
1032 }
1033 ''';
1034 return testRegions(code, (NotificationHighlightHelper helper) {
1035 helper.assertHasRegion(HighlightType.TOP_LEVEL_VARIABLE, 'VVV = 0');
1036 helper.assertHasRegion(HighlightType.FIELD_STATIC, 'VVV);');
1037 helper.assertHasRegion(HighlightType.FIELD_STATIC, 'VVV = 1');
1038 });
1039 });
1040
1041 test('TYPE_NAME_DYNAMIC', () {
1042 var code = '''
1043 dynamic main() {
1044 dynamic = 42;
1045 }
1046 ''';
1047 return testRegions(code, (NotificationHighlightHelper helper) {
1048 helper.assertHasRegion(HighlightType.TYPE_NAME_DYNAMIC, 'dynamic main()');
1049 helper.assertNoRegion(HighlightType.IDENTIFIER_DEFAULT, 'dynamic main()');
1050 helper.assertNoRegion(HighlightType.TYPE_NAME_DYNAMIC, 'dynamic = 42');
1051 });
1052 });
1053
1054 test('TYPE_PARAMETER', () {
1055 var code = '''
1056 class A<T> {
1057 T fff;
1058 T mmm(T p) => null;
1059 }
1060 ''';
1061 return testRegions(code, (NotificationHighlightHelper helper) {
1062 helper.assertHasRegion(HighlightType.TYPE_PARAMETER, 'T> {');
1063 helper.assertHasRegion(HighlightType.TYPE_PARAMETER, 'T fff;');
1064 helper.assertHasRegion(HighlightType.TYPE_PARAMETER, 'T mmm(');
1065 helper.assertHasRegion(HighlightType.TYPE_PARAMETER, 'T p)');
1066 });
1067 });
1068 }
1069
1070
278 testUpdateContent() { 1071 testUpdateContent() {
279 test('full content', () { 1072 test('full content', () {
280 AnalysisTestHelper helper = new AnalysisTestHelper(); 1073 AnalysisTestHelper helper = new AnalysisTestHelper();
281 helper.createSingleFileProject('// empty'); 1074 helper.createSingleFileProject('// empty');
282 return helper.waitForTasksFinished().then((_) { 1075 return helper.waitForTasksFinished().then((_) {
283 // no errors initially 1076 // no errors initially
284 List<AnalysisError> errors = helper.getTestErrors(); 1077 List<AnalysisError> errors = helper.getTestErrors();
285 expect(errors, isEmpty); 1078 expect(errors, isEmpty);
286 // update code 1079 // update code
287 { 1080 {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 helper.handleSuccessfulRequest(request); 1117 helper.handleSuccessfulRequest(request);
325 } 1118 }
326 // wait, there is an error 1119 // wait, there is an error
327 helper.waitForTasksFinished().then((_) { 1120 helper.waitForTasksFinished().then((_) {
328 List<AnalysisError> errors = helper.getTestErrors(); 1121 List<AnalysisError> errors = helper.getTestErrors();
329 expect(errors, hasLength(1)); 1122 expect(errors, hasLength(1));
330 }); 1123 });
331 }); 1124 });
332 }); 1125 });
333 } 1126 }
334
335
336 class AnalysisError {
337 final String file;
338 final String errorCode;
339 final int offset;
340 final int length;
341 final String message;
342 final String correction;
343 AnalysisError(this.file, this.errorCode, this.offset, this.length,
344 this.message, this.correction);
345
346 @override
347 String toString() {
348 return 'NotificationError(file=$file; errorCode=$errorCode; '
349 'offset=$offset; length=$length; message=$message)';
350 }
351 }
352
353
354 AnalysisError jsonToAnalysisError(Map<String, Object> json) {
355 return new AnalysisError(
356 json['file'],
357 json['errorCode'],
358 json['offset'],
359 json['length'],
360 json['message'],
361 json['correction']);
362 }
OLDNEW
« pkg/analysis_server/lib/src/computers.dart ('K') | « pkg/analysis_server/lib/src/computers.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698