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.integration.analysis.occurrences; | 5 library test.integration.analysis.occurrences; |
6 | 6 |
7 import 'package:analysis_server/src/protocol.dart'; | 7 import 'package:analysis_server/src/protocol.dart'; |
8 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 8 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
10 | 10 |
| 11 import '../../utils.dart'; |
11 import '../integration_tests.dart'; | 12 import '../integration_tests.dart'; |
12 | 13 |
13 main() { | 14 main() { |
| 15 initializeTestEnvironment(); |
14 defineReflectiveTests(Test); | 16 defineReflectiveTests(Test); |
15 } | 17 } |
16 | 18 |
17 @reflectiveTest | 19 @reflectiveTest |
18 class Test extends AbstractAnalysisServerIntegrationTest { | 20 class Test extends AbstractAnalysisServerIntegrationTest { |
19 test_occurrences() { | 21 test_occurrences() { |
20 String pathname = sourcePath('test.dart'); | 22 String pathname = sourcePath('test.dart'); |
21 String text = r''' | 23 String text = r''' |
22 main() { | 24 main() { |
23 int sum = 0; | 25 int sum = 0; |
24 for (int i = 0; i < 10; i++) { | 26 for (int i = 0; i < 10; i++) { |
25 for (int j = 0; j < i; j++) { | 27 for (int j = 0; j < i; j++) { |
26 sum += j; | 28 sum += j; |
27 } | 29 } |
28 } | 30 } |
29 print(sum); | 31 print(sum); |
30 } | 32 } |
31 '''; | 33 '''; |
32 writeFile(pathname, text); | 34 writeFile(pathname, text); |
33 standardAnalysisSetup(); | 35 standardAnalysisSetup(); |
34 sendAnalysisSetSubscriptions({AnalysisService.OCCURRENCES: [pathname]}); | 36 sendAnalysisSetSubscriptions({ |
| 37 AnalysisService.OCCURRENCES: [pathname] |
| 38 }); |
35 List<Occurrences> occurrences; | 39 List<Occurrences> occurrences; |
36 onAnalysisOccurrences.listen((AnalysisOccurrencesParams params) { | 40 onAnalysisOccurrences.listen((AnalysisOccurrencesParams params) { |
37 expect(params.file, equals(pathname)); | 41 expect(params.file, equals(pathname)); |
38 occurrences = params.occurrences; | 42 occurrences = params.occurrences; |
39 }); | 43 }); |
40 return analysisFinished.then((_) { | 44 return analysisFinished.then((_) { |
41 expect(currentAnalysisErrors[pathname], isEmpty); | 45 expect(currentAnalysisErrors[pathname], isEmpty); |
42 Set<int> findOffsets(String elementName) { | 46 Set<int> findOffsets(String elementName) { |
43 for (Occurrences occurrence in occurrences) { | 47 for (Occurrences occurrence in occurrences) { |
44 if (occurrence.element.name == elementName) { | 48 if (occurrence.element.name == elementName) { |
45 return occurrence.offsets.toSet(); | 49 return occurrence.offsets.toSet(); |
46 } | 50 } |
47 } | 51 } |
48 fail('No element found matching $elementName'); | 52 fail('No element found matching $elementName'); |
49 return null; | 53 return null; |
50 } | 54 } |
51 void check(String elementName, Iterable<String> expectedOccurrences) { | 55 void check(String elementName, Iterable<String> expectedOccurrences) { |
52 Set<int> expectedOffsets = expectedOccurrences | 56 Set<int> expectedOffsets = expectedOccurrences |
53 .map((String substring) => text.indexOf(substring)) | 57 .map((String substring) => text.indexOf(substring)) |
54 .toSet(); | 58 .toSet(); |
55 Set<int> foundOffsets = findOffsets(elementName); | 59 Set<int> foundOffsets = findOffsets(elementName); |
56 expect(foundOffsets, equals(expectedOffsets)); | 60 expect(foundOffsets, equals(expectedOffsets)); |
57 } | 61 } |
58 check('i', ['i = 0', 'i < 10', 'i++', 'i;']); | 62 check('i', ['i = 0', 'i < 10', 'i++', 'i;']); |
59 check('j', ['j = 0', 'j < i', 'j++', 'j;']); | 63 check('j', ['j = 0', 'j < i', 'j++', 'j;']); |
60 check('sum', ['sum = 0', 'sum +=', 'sum)']); | 64 check('sum', ['sum = 0', 'sum +=', 'sum)']); |
61 }); | 65 }); |
62 } | 66 } |
63 } | 67 } |
OLD | NEW |