| OLD | NEW |
| 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/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
| 11 import 'package:analysis_server/src/domain_analysis.dart'; | 11 import 'package:analysis_server/src/domain_analysis.dart'; |
| 12 import 'package:analysis_server/src/protocol.dart'; | 12 import 'package:analysis_server/src/protocol.dart'; |
| 13 import 'package:analyzer/file_system/memory_file_system.dart'; | 13 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 14 import 'package:analyzer/instrumentation/instrumentation.dart'; | 14 import 'package:analyzer/instrumentation/instrumentation.dart'; |
| 15 import 'package:path/path.dart'; | 15 import 'package:path/path.dart'; |
| 16 import 'package:unittest/unittest.dart'; | 16 import 'package:unittest/unittest.dart'; |
| 17 | 17 |
| 18 import 'analysis_abstract.dart'; | 18 import 'analysis_abstract.dart'; |
| 19 import 'mock_sdk.dart'; | 19 import 'mock_sdk.dart'; |
| 20 import 'mocks.dart'; | 20 import 'mocks.dart'; |
| 21 import 'reflective_tests.dart'; | 21 import 'reflective_tests.dart'; |
| 22 | 22 |
| 23 main() { | 23 main() { |
| 24 groupSep = ' | '; | 24 groupSep = ' | '; |
| 25 | 25 |
| 26 runReflectiveTests(AnalysisDomainTest); | 26 runReflectiveTests(AnalysisDomainTest); |
| 27 runReflectiveTests(SetSubscriptionsTest); |
| 27 | 28 |
| 28 MockServerChannel serverChannel; | 29 MockServerChannel serverChannel; |
| 29 MemoryResourceProvider resourceProvider; | 30 MemoryResourceProvider resourceProvider; |
| 30 AnalysisServer server; | 31 AnalysisServer server; |
| 31 AnalysisDomainHandler handler; | 32 AnalysisDomainHandler handler; |
| 32 | 33 |
| 33 setUp(() { | 34 setUp(() { |
| 34 serverChannel = new MockServerChannel(); | 35 serverChannel = new MockServerChannel(); |
| 35 resourceProvider = new MemoryResourceProvider(); | 36 resourceProvider = new MemoryResourceProvider(); |
| 36 server = new AnalysisServer(serverChannel, resourceProvider, | 37 server = new AnalysisServer(serverChannel, resourceProvider, |
| 37 new MockPackageMapProvider(), null, new AnalysisServerOptions(), | 38 new MockPackageMapProvider(), null, new AnalysisServerOptions(), |
| 38 new MockSdk(), InstrumentationService.NULL_SERVICE); | 39 new MockSdk(), InstrumentationService.NULL_SERVICE); |
| 39 handler = new AnalysisDomainHandler(server); | 40 handler = new AnalysisDomainHandler(server); |
| 40 }); | 41 }); |
| 41 | 42 |
| 42 group('updateContent', testUpdateContent); | 43 group('updateContent', testUpdateContent); |
| 43 group('setSubscriptions', test_setSubscriptions); | |
| 44 | 44 |
| 45 group('AnalysisDomainHandler', () { | 45 group('AnalysisDomainHandler', () { |
| 46 group('setAnalysisRoots', () { | 46 group('setAnalysisRoots', () { |
| 47 Response testSetAnalysisRoots( | 47 Response testSetAnalysisRoots( |
| 48 List<String> included, List<String> excluded) { | 48 List<String> included, List<String> excluded) { |
| 49 Request request = new AnalysisSetAnalysisRootsParams(included, excluded) | 49 Request request = new AnalysisSetAnalysisRootsParams(included, excluded) |
| 50 .toRequest('0'); | 50 .toRequest('0'); |
| 51 return handler.handleRequest(request); | 51 return handler.handleRequest(request); |
| 52 } | 52 } |
| 53 | 53 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // null is allowed as a synonym for {}. | 137 // null is allowed as a synonym for {}. |
| 138 var request = | 138 var request = |
| 139 new Request('0', ANALYSIS_UPDATE_OPTIONS, {OPTIONS: null}); | 139 new Request('0', ANALYSIS_UPDATE_OPTIONS, {OPTIONS: null}); |
| 140 var response = handler.handleRequest(request); | 140 var response = handler.handleRequest(request); |
| 141 expect(response, isResponseSuccess('0')); | 141 expect(response, isResponseSuccess('0')); |
| 142 }); | 142 }); |
| 143 }); | 143 }); |
| 144 }); | 144 }); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void test_setSubscriptions() { | |
| 148 test('before analysis', () { | |
| 149 AnalysisTestHelper helper = new AnalysisTestHelper(); | |
| 150 // subscribe | |
| 151 helper.addAnalysisSubscriptionHighlights(helper.testFile); | |
| 152 // create project | |
| 153 helper.createSingleFileProject('int V = 42;'); | |
| 154 // wait, there are highlight regions | |
| 155 helper.onAnalysisComplete.then((_) { | |
| 156 var highlights = helper.getHighlights(helper.testFile); | |
| 157 expect(highlights, isNotEmpty); | |
| 158 }); | |
| 159 }); | |
| 160 | |
| 161 test('after analysis', () { | |
| 162 AnalysisTestHelper helper = new AnalysisTestHelper(); | |
| 163 // create project | |
| 164 helper.createSingleFileProject('int V = 42;'); | |
| 165 // wait, no regions initially | |
| 166 return helper.onAnalysisComplete.then((_) { | |
| 167 var highlights = helper.getHighlights(helper.testFile); | |
| 168 expect(highlights, isEmpty); | |
| 169 // subscribe | |
| 170 helper.addAnalysisSubscriptionHighlights(helper.testFile); | |
| 171 // wait, has regions | |
| 172 return helper.onAnalysisComplete.then((_) { | |
| 173 var highlights = helper.getHighlights(helper.testFile); | |
| 174 expect(highlights, isNotEmpty); | |
| 175 }); | |
| 176 }); | |
| 177 }); | |
| 178 | |
| 179 test('after analysis, no such file', () { | |
| 180 AnalysisTestHelper helper = new AnalysisTestHelper(); | |
| 181 helper.createSingleFileProject('int V = 42;'); | |
| 182 return helper.onAnalysisComplete.then((_) { | |
| 183 String noFile = '/no-such-file.dart'; | |
| 184 helper.addAnalysisSubscriptionHighlights(noFile); | |
| 185 return helper.onAnalysisComplete.then((_) { | |
| 186 var highlights = helper.getHighlights(noFile); | |
| 187 expect(highlights, isEmpty); | |
| 188 }); | |
| 189 }); | |
| 190 }); | |
| 191 | |
| 192 test('after analysis, SDK file', () { | |
| 193 AnalysisTestHelper helper = new AnalysisTestHelper(); | |
| 194 helper.createSingleFileProject(''' | |
| 195 main() { | |
| 196 print(42); | |
| 197 } | |
| 198 '''); | |
| 199 return helper.onAnalysisComplete.then((_) { | |
| 200 String file = '/lib/core/core.dart'; | |
| 201 helper.addAnalysisSubscriptionNavigation(file); | |
| 202 return helper.onAnalysisComplete.then((_) { | |
| 203 var navigationRegions = helper.getNavigation(file); | |
| 204 expect(navigationRegions, isNotEmpty); | |
| 205 }); | |
| 206 }); | |
| 207 }); | |
| 208 } | |
| 209 | |
| 210 testUpdateContent() { | 147 testUpdateContent() { |
| 211 test('bad type', () { | 148 test('bad type', () { |
| 212 AnalysisTestHelper helper = new AnalysisTestHelper(); | 149 AnalysisTestHelper helper = new AnalysisTestHelper(); |
| 213 helper.createSingleFileProject('// empty'); | 150 helper.createSingleFileProject('// empty'); |
| 214 return helper.onAnalysisComplete.then((_) { | 151 return helper.onAnalysisComplete.then((_) { |
| 215 Request request = new Request('0', ANALYSIS_UPDATE_CONTENT, { | 152 Request request = new Request('0', ANALYSIS_UPDATE_CONTENT, { |
| 216 'files': {helper.testFile: {TYPE: 'foo',}} | 153 'files': {helper.testFile: {TYPE: 'foo',}} |
| 217 }); | 154 }); |
| 218 Response response = helper.handler.handleRequest(request); | 155 Response response = helper.handler.handleRequest(request); |
| 219 expect(response, isResponseFailure('0')); | 156 expect(response, isResponseFailure('0')); |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 server.done(); | 537 server.done(); |
| 601 } | 538 } |
| 602 | 539 |
| 603 static String _getCodeString(code) { | 540 static String _getCodeString(code) { |
| 604 if (code is List<String>) { | 541 if (code is List<String>) { |
| 605 code = code.join('\n'); | 542 code = code.join('\n'); |
| 606 } | 543 } |
| 607 return code as String; | 544 return code as String; |
| 608 } | 545 } |
| 609 } | 546 } |
| 547 |
| 548 @reflectiveTest |
| 549 class SetSubscriptionsTest extends AbstractAnalysisTest { |
| 550 Map<String, List<HighlightRegion>> filesHighlights = {}; |
| 551 |
| 552 void processNotification(Notification notification) { |
| 553 if (notification.event == ANALYSIS_HIGHLIGHTS) { |
| 554 var params = new AnalysisHighlightsParams.fromNotification(notification); |
| 555 filesHighlights[params.file] = params.regions; |
| 556 } |
| 557 } |
| 558 |
| 559 test_afterAnalysis() async { |
| 560 addTestFile('int V = 42;'); |
| 561 createProject(); |
| 562 // wait for analysis, no results initially |
| 563 await waitForTasksFinished(); |
| 564 expect(filesHighlights[testFile], isNull); |
| 565 // subscribe |
| 566 addAnalysisSubscription(AnalysisService.HIGHLIGHTS, testFile); |
| 567 await server.onAnalysisComplete; |
| 568 // there are results |
| 569 expect(filesHighlights[testFile], isNotEmpty); |
| 570 } |
| 571 |
| 572 test_afterAnalysis_noSuchFile() async { |
| 573 String file = '/no-such-file.dart'; |
| 574 addTestFile('// no matter'); |
| 575 createProject(); |
| 576 // wait for analysis, no results initially |
| 577 await waitForTasksFinished(); |
| 578 expect(filesHighlights[testFile], isNull); |
| 579 // subscribe |
| 580 addAnalysisSubscription(AnalysisService.HIGHLIGHTS, file); |
| 581 await server.onAnalysisComplete; |
| 582 // there are results |
| 583 expect(filesHighlights[file], isNull); |
| 584 } |
| 585 |
| 586 test_afterAnalysis_packageFile_external() async { |
| 587 String pkgFile = '/packages/pkgA/lib/libA.dart'; |
| 588 resourceProvider.newFile(pkgFile, ''' |
| 589 library lib_a; |
| 590 class A {} |
| 591 '''); |
| 592 packageMapProvider.packageMap = { |
| 593 'pkgA': [(resourceProvider.newFolder('/packages/pkgA/lib'))] |
| 594 }; |
| 595 // |
| 596 addTestFile(''' |
| 597 import 'package:pkgA/libA.dart'; |
| 598 main() { |
| 599 new A(); |
| 600 } |
| 601 '''); |
| 602 createProject(); |
| 603 // wait for analysis, no results initially |
| 604 await waitForTasksFinished(); |
| 605 expect(filesHighlights[pkgFile], isNull); |
| 606 // subscribe |
| 607 addAnalysisSubscription(AnalysisService.HIGHLIGHTS, pkgFile); |
| 608 await server.onAnalysisComplete; |
| 609 // there are results |
| 610 expect(filesHighlights[pkgFile], isNotEmpty); |
| 611 } |
| 612 |
| 613 test_afterAnalysis_packageFile_inRoot() async { |
| 614 String pkgA = '/pkgA'; |
| 615 String pkgB = '/pkgA'; |
| 616 String pkgFileA = '$pkgA/lib/libA.dart'; |
| 617 String pkgFileB = '$pkgA/lib/libB.dart'; |
| 618 resourceProvider.newFile(pkgFileA, ''' |
| 619 library lib_a; |
| 620 class A {} |
| 621 '''); |
| 622 resourceProvider.newFile(pkgFileB, ''' |
| 623 import 'package:pkgA/libA.dart'; |
| 624 main() { |
| 625 new A(); |
| 626 } |
| 627 '''); |
| 628 packageMapProvider.packageMap = { |
| 629 'pkgA': [ |
| 630 resourceProvider.newFolder('$pkgA/lib'), |
| 631 resourceProvider.newFolder('$pkgB/lib') |
| 632 ] |
| 633 }; |
| 634 // add 'pkgA' and 'pkgB' as projects |
| 635 { |
| 636 resourceProvider.newFolder(projectPath); |
| 637 handleSuccessfulRequest( |
| 638 new AnalysisSetAnalysisRootsParams([pkgA, pkgB], []).toRequest('0')); |
| 639 } |
| 640 // wait for analysis, no results initially |
| 641 await waitForTasksFinished(); |
| 642 expect(filesHighlights[pkgFileA], isNull); |
| 643 // subscribe |
| 644 addAnalysisSubscription(AnalysisService.HIGHLIGHTS, pkgFileA); |
| 645 await server.onAnalysisComplete; |
| 646 // there are results |
| 647 expect(filesHighlights[pkgFileA], isNotEmpty); |
| 648 } |
| 649 |
| 650 test_afterAnalysis_sdkFile() async { |
| 651 String file = '/lib/core/core.dart'; |
| 652 addTestFile('// no matter'); |
| 653 createProject(); |
| 654 // wait for analysis, no results initially |
| 655 await waitForTasksFinished(); |
| 656 expect(filesHighlights[file], isNull); |
| 657 // subscribe |
| 658 addAnalysisSubscription(AnalysisService.HIGHLIGHTS, file); |
| 659 await server.onAnalysisComplete; |
| 660 // there are results |
| 661 expect(filesHighlights[file], isNotEmpty); |
| 662 } |
| 663 |
| 664 test_beforeAnalysis() async { |
| 665 addTestFile('int V = 42;'); |
| 666 createProject(); |
| 667 // subscribe |
| 668 addAnalysisSubscription(AnalysisService.HIGHLIGHTS, testFile); |
| 669 // wait for analysis |
| 670 await waitForTasksFinished(); |
| 671 expect(filesHighlights[testFile], isNotEmpty); |
| 672 } |
| 673 } |
| OLD | NEW |