| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:analysis_server/protocol/protocol_generated.dart'; | 7 import 'package:analysis_server/protocol/protocol_generated.dart'; |
| 8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 9 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 10 | 10 |
| 11 import '../support/integration_tests.dart'; | 11 import '../support/integration_tests.dart'; |
| 12 | 12 |
| 13 main() { | 13 main() { |
| 14 defineReflectiveSuite(() { | 14 defineReflectiveSuite(() { |
| 15 defineReflectiveTests(AnalysisGetImportedElementsIntegrationTest); | 15 defineReflectiveTests(AnalysisGetImportedElementsIntegrationTest); |
| 16 }); | 16 }); |
| 17 } | 17 } |
| 18 | 18 |
| 19 @reflectiveTest | 19 @reflectiveTest |
| 20 class AnalysisGetImportedElementsIntegrationTest | 20 class AnalysisGetImportedElementsIntegrationTest |
| 21 extends AbstractAnalysisServerIntegrationTest { | 21 extends AbstractAnalysisServerIntegrationTest { |
| 22 /** | 22 /** |
| 23 * Pathname of the file containing Dart code. | 23 * Pathname of the file containing Dart code. |
| 24 */ | 24 */ |
| 25 String pathname; | 25 String pathname; |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Dart code under test. | 28 * Dart code under test. |
| 29 */ | 29 */ |
| 30 final String text = r''' | 30 String text; |
| 31 main() {} | |
| 32 '''; | |
| 33 | 31 |
| 34 /** | 32 /** |
| 35 * Check that an analysis.getImportedElements request on the region starting | 33 * Check that an analysis.getImportedElements request on the region starting |
| 36 * with the first character that matches [target] and having the given | 34 * with the first character that matches [target] and having the given |
| 37 * [length] matches the given list of [expected] imported elements. | 35 * [length] matches the given list of [expected] imported elements. |
| 38 */ | 36 */ |
| 39 checkElements( | 37 checkElements(String target, List<ImportedElements> expected) async { |
| 40 String target, int length, List<ImportedElements> expected) async { | 38 bool equals( |
| 39 ImportedElements actualElements, ImportedElements expectedElements) { |
| 40 // We can't test the path because the paths are relative to a generated |
| 41 // temporary directory. |
| 42 if (actualElements.prefix == expectedElements.prefix) { |
| 43 List<String> actual = actualElements.elements; |
| 44 List<String> expected = expectedElements.elements; |
| 45 if (actual.length == expected.length) { |
| 46 for (int i = 0; i < actual.length; i++) { |
| 47 if (!expected.contains(actual[i])) { |
| 48 return false; |
| 49 } |
| 50 } |
| 51 return true; |
| 52 } |
| 53 } |
| 54 return false; |
| 55 } |
| 56 |
| 57 int find(List<ImportedElements> actual, ImportedElements expectedElements) { |
| 58 for (int i = 0; i < actual.length; i++) { |
| 59 ImportedElements actualElements = actual[i]; |
| 60 if (equals(actualElements, expectedElements)) { |
| 61 return i; |
| 62 } |
| 63 } |
| 64 return -1; |
| 65 } |
| 66 |
| 41 int offset = text.indexOf(target); | 67 int offset = text.indexOf(target); |
| 42 AnalysisGetImportedElementsResult result = | 68 AnalysisGetImportedElementsResult result = |
| 43 await sendAnalysisGetImportedElements(pathname, offset, length); | 69 await sendAnalysisGetImportedElements(pathname, offset, target.length); |
| 44 | 70 |
| 45 expect(result.elements, hasLength(expected.length)); | 71 List<ImportedElements> actual = result.elements; |
| 46 // TODO(brianwilkerson) Finish implementing this. | 72 expect(actual, hasLength(expected.length)); |
| 73 for (ImportedElements elements in expected) { |
| 74 int index = find(actual, elements); |
| 75 if (index < 0) { |
| 76 fail('Expected $elements; not found'); |
| 77 } |
| 78 actual.removeAt(index); |
| 79 } |
| 47 } | 80 } |
| 48 | 81 |
| 49 /** | 82 /** |
| 50 * Check that an analysis.getImportedElements request on the region matching | 83 * Check that an analysis.getImportedElements request on the region matching |
| 51 * [target] produces an empty list of elements. | 84 * [target] produces an empty list of elements. |
| 52 */ | 85 */ |
| 53 Future<Null> checkNoElements(String target) async { | 86 Future<Null> checkNoElements(String target) async { |
| 54 int offset = text.indexOf(target); | 87 int offset = text.indexOf(target); |
| 55 AnalysisGetImportedElementsResult result = | 88 AnalysisGetImportedElementsResult result = |
| 56 await sendAnalysisGetImportedElements(pathname, offset, target.length); | 89 await sendAnalysisGetImportedElements(pathname, offset, target.length); |
| 57 | 90 |
| 58 expect(result.elements, hasLength(0)); | 91 expect(result.elements, hasLength(0)); |
| 59 } | 92 } |
| 60 | 93 |
| 61 setUp() { | 94 setUp() { |
| 62 return super.setUp().then((_) { | 95 return super.setUp().then((_) { |
| 63 pathname = sourcePath('test.dart'); | 96 pathname = sourcePath('test.dart'); |
| 64 }); | 97 }); |
| 65 } | 98 } |
| 66 | 99 |
| 67 test_getImportedElements() async { | 100 test_getImportedElements_none() async { |
| 101 text = r''' |
| 102 main() {} |
| 103 '''; |
| 68 writeFile(pathname, text); | 104 writeFile(pathname, text); |
| 69 standardAnalysisSetup(); | 105 standardAnalysisSetup(); |
| 70 await analysisFinished; | 106 await analysisFinished; |
| 71 | 107 |
| 72 List<Future> tests = []; | 108 List<Future> tests = []; |
| 73 tests.add(checkNoElements('main() {}')); | 109 tests.add(checkNoElements('main() {}')); |
| 74 return Future.wait(tests); | 110 return Future.wait(tests); |
| 75 } | 111 } |
| 112 |
| 113 test_getImportedElements_some() async { |
| 114 text = r''' |
| 115 main() { |
| 116 String s = ''; |
| 117 print(s); |
| 76 } | 118 } |
| 119 '''; |
| 120 writeFile(pathname, text); |
| 121 standardAnalysisSetup(); |
| 122 await analysisFinished; |
| 123 |
| 124 List<Future> tests = []; |
| 125 tests.add(checkElements(text, [ |
| 126 new ImportedElements('', '', ['String', 'print']) |
| 127 ])); |
| 128 return Future.wait(tests); |
| 129 } |
| 130 } |
| OLD | NEW |