OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.integration.analysis.get.hover; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_testing/reflective_tests.dart'; |
| 10 import 'package:path/path.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 import '../integration_tests.dart'; |
| 14 |
| 15 @ReflectiveTestCase() |
| 16 class AnalysisGetHoverIntegrationTest extends |
| 17 AbstractAnalysisServerIntegrationTest { |
| 18 /** |
| 19 * Pathname of the file containing Dart code. |
| 20 */ |
| 21 String pathname; |
| 22 |
| 23 /** |
| 24 * Dart code under test. |
| 25 */ |
| 26 final String text = |
| 27 r''' |
| 28 library lib.test; |
| 29 |
| 30 List topLevelVar; |
| 31 |
| 32 /** |
| 33 * Documentation for func |
| 34 */ |
| 35 void func(int param) { |
| 36 num localVar = topLevelVar.length; |
| 37 topLevelVar.length = param; |
| 38 topLevelVar.add(localVar); |
| 39 } |
| 40 |
| 41 main() { |
| 42 // comment |
| 43 func(35); |
| 44 } |
| 45 '''; |
| 46 |
| 47 setUp() { |
| 48 return super.setUp().then((_) { |
| 49 pathname = sourcePath('test.dart'); |
| 50 }); |
| 51 } |
| 52 |
| 53 test_getHover() { |
| 54 writeFile(pathname, text); |
| 55 standardAnalysisSetup(); |
| 56 |
| 57 // Note: analysis.getHover doesn't wait for analysis to complete--it simply |
| 58 // returns the latest results that are available at the time that the |
| 59 // request is made. So wait for analysis to finish before testing anything. |
| 60 return analysisFinished.then((_) { |
| 61 List<Future> tests = []; |
| 62 tests.add(checkHover('topLevelVar;', 11, ['List', 'topLevelVar'], |
| 63 'top level variable', ['List'])); |
| 64 tests.add(checkHover('func(', 4, ['func', 'int', 'param'], 'function', |
| 65 ['int', 'void'], docRegexp: 'Documentation for func')); |
| 66 tests.add(checkHover('int param', 3, ['int'], 'class', ['int'], isCore: |
| 67 true, docRegexp: '.*')); |
| 68 tests.add(checkHover('param)', 5, ['int', 'param'], 'parameter', ['int'], |
| 69 docRegexp: 'Documentation for func')); |
| 70 tests.add(checkHover('num localVar', 3, ['num'], 'class', ['num'], isCore: |
| 71 true, docRegexp: '.*')); |
| 72 tests.add(checkHover('localVar =', 8, ['num', 'localVar'], |
| 73 'local variable', ['num'], propagatedType: 'int')); |
| 74 tests.add(checkHover('topLevelVar.length;', 11, ['List', 'topLevelVar'], |
| 75 'top level variable', ['List'])); |
| 76 tests.add(checkHover('length;', 6, ['get', 'length', 'int'], 'getter', |
| 77 ['int'], isCore: true, docRegexp: '.*')); |
| 78 tests.add(checkHover('length =', 6, ['set', 'length', 'int'], 'setter', |
| 79 ['int'], isCore: true, docRegexp: '.*')); |
| 80 tests.add(checkHover('param;', 5, ['int', 'param'], 'parameter', ['int'], |
| 81 docRegexp: 'Documentation for func')); |
| 82 tests.add(checkHover('add(', 3, ['List', 'add'], 'method', null, isCore: |
| 83 true, docRegexp: '.*')); |
| 84 tests.add(checkHover('localVar)', 8, ['num', 'localVar'], |
| 85 'local variable', ['num'], parameterRegexps: ['.*'], propagatedType: '
int')); |
| 86 tests.add(checkHover('func(35', 4, ['func', 'int', 'param'], 'function', |
| 87 null, docRegexp: 'Documentation for func')); |
| 88 tests.add(checkHover('35', 2, null, null, ['int'], isLiteral: true, |
| 89 parameterRegexps: ['int', 'param'])); |
| 90 tests.add(checkNoHover('comment')); |
| 91 return Future.wait(tests); |
| 92 }); |
| 93 } |
| 94 |
| 95 /** |
| 96 * Check that a getHover request on the substring [target] produces a result |
| 97 * which has length [length], has an elementDescription matching every |
| 98 * regexp in [descriptionRegexps], has a kind of [kind], and has a staticType |
| 99 * matching [staticTypeRegexps]. |
| 100 * |
| 101 * [isCore] means the hover info should indicate that the element is defined |
| 102 * in dart.core. [docRegexp], if specified, should match the documentation |
| 103 * string of the element. [isLiteral] means the hover should indicate a |
| 104 * literal value. [parameterRegexps] means is a set of regexps which should |
| 105 * match the hover parameters. [propagatedType], if specified, is the |
| 106 * expected propagated type of the element. |
| 107 */ |
| 108 checkHover(String target, int length, List<String> descriptionRegexps, String |
| 109 kind, List<String> staticTypeRegexps, {bool isCore: false, String docRegex
p: |
| 110 null, bool isLiteral: false, List<String> parameterRegexps: |
| 111 null, propagatedType: null}) { |
| 112 int offset = text.indexOf(target); |
| 113 return sendAnalysisGetHover(pathname, offset).then((result) { |
| 114 expect(result['hovers'], hasLength(1)); |
| 115 var info = result['hovers'][0]; |
| 116 expect(info['offset'], equals(offset)); |
| 117 expect(info['length'], equals(length)); |
| 118 if (isCore) { |
| 119 expect(basename(info['containingLibraryPath']), equals('core.dart')); |
| 120 expect(info['containingLibraryName'], equals('dart.core')); |
| 121 } else if (isLiteral) { |
| 122 expect(info['containingLibraryPath'], isNull); |
| 123 expect(info['containingLibraryName'], isNull); |
| 124 } else { |
| 125 expect(info['containingLibraryPath'], equals(pathname)); |
| 126 expect(info['containingLibraryName'], equals('lib.test')); |
| 127 } |
| 128 if (docRegexp == null) { |
| 129 expect(info['dartdoc'], isNull); |
| 130 } else { |
| 131 expect(info['dartdoc'], matches(docRegexp)); |
| 132 } |
| 133 if (descriptionRegexps == null) { |
| 134 expect(info['elementDescription'], isNull); |
| 135 } else { |
| 136 expect(info['elementDescription'], isString); |
| 137 for (String descriptionRegexp in descriptionRegexps) { |
| 138 expect(info['elementDescription'], matches(descriptionRegexp)); |
| 139 } |
| 140 } |
| 141 expect(info['elementKind'], equals(kind)); |
| 142 if (parameterRegexps == null) { |
| 143 expect(info['parameter'], isNull); |
| 144 } else { |
| 145 expect(info['parameter'], isString); |
| 146 for (String parameterRegexp in parameterRegexps) { |
| 147 expect(info['parameter'], matches(parameterRegexp)); |
| 148 } |
| 149 } |
| 150 expect(info['propagatedType'], equals(propagatedType)); |
| 151 if (staticTypeRegexps == null) { |
| 152 expect(info['staticType'], isNull); |
| 153 } else { |
| 154 expect(info['staticType'], isString); |
| 155 for (String staticTypeRegexp in staticTypeRegexps) { |
| 156 expect(info['staticType'], matches(staticTypeRegexp)); |
| 157 } |
| 158 } |
| 159 }); |
| 160 } |
| 161 |
| 162 /** |
| 163 * Check that a getHover request on the substring [target] produces no |
| 164 * results. |
| 165 */ |
| 166 Future checkNoHover(String target) { |
| 167 int offset = text.indexOf(target); |
| 168 return sendAnalysisGetHover(pathname, offset).then((result) { |
| 169 expect(result['hovers'], hasLength(0)); |
| 170 }); |
| 171 } |
| 172 } |
| 173 |
| 174 main() { |
| 175 runReflectiveTests(AnalysisGetHoverIntegrationTest); |
| 176 } |
OLD | NEW |