Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.services.correction.util; | 5 library test.services.correction.util; |
| 6 | 6 |
| 7 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 7 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 8 import 'package:analysis_server/src/services/correction/strings.dart'; | 8 import 'package:analysis_server/src/services/correction/strings.dart'; |
| 9 import 'package:analysis_server/src/services/correction/util.dart'; | 9 import 'package:analysis_server/src/services/correction/util.dart'; |
| 10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:test/test.dart'; |
| 11 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/source.dart'; |
| 12 import 'package:test/test.dart'; | |
| 13 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 12 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 14 | 13 |
| 15 import '../../abstract_single_unit.dart'; | 14 import '../../abstract_single_unit.dart'; |
| 16 | 15 |
| 17 main() { | 16 main() { |
| 18 defineReflectiveSuite(() { | 17 defineReflectiveSuite(() { |
| 19 defineReflectiveTests(UtilTest); | 18 defineReflectiveTests(UtilTest); |
| 20 }); | 19 }); |
| 21 } | 20 } |
| 22 | 21 |
| 23 @reflectiveTest | 22 @reflectiveTest |
| 24 class UtilTest extends AbstractSingleUnitTest { | 23 class UtilTest extends AbstractSingleUnitTest { |
| 25 test_addLibraryImports_dart_hasImports_between() { | 24 test_addLibraryImports_dart_hasImports_between() { |
| 26 resolveTestUnit(''' | 25 resolveTestUnit(''' |
| 27 import 'dart:async'; | 26 import 'dart:async'; |
| 28 import 'dart:math'; | 27 import 'dart:math'; |
| 29 '''); | 28 '''); |
| 30 LibraryElement newLibrary = _getDartLibrary('dart:collection'); | 29 Source newLibrary = _getDartSourceInfo('dart:collection'); |
| 31 _assertAddLibraryImport( | 30 _assertAddLibraryImport( |
| 32 <LibraryElement>[newLibrary], | 31 <Source>[newLibrary], |
| 33 ''' | 32 ''' |
| 34 import 'dart:async'; | 33 import 'dart:async'; |
| 35 import 'dart:collection'; | 34 import 'dart:collection'; |
| 36 import 'dart:math'; | 35 import 'dart:math'; |
| 37 '''); | 36 '''); |
| 38 } | 37 } |
| 39 | 38 |
| 40 test_addLibraryImports_dart_hasImports_first() { | 39 test_addLibraryImports_dart_hasImports_first() { |
| 41 resolveTestUnit(''' | 40 resolveTestUnit(''' |
| 42 import 'dart:collection'; | 41 import 'dart:collection'; |
| 43 import 'dart:math'; | 42 import 'dart:math'; |
| 44 '''); | 43 '''); |
| 45 LibraryElement newLibrary = _getDartLibrary('dart:async'); | 44 Source newLibrary = _getDartSourceInfo('dart:async'); |
| 46 _assertAddLibraryImport( | 45 _assertAddLibraryImport( |
| 47 <LibraryElement>[newLibrary], | 46 <Source>[newLibrary], |
| 48 ''' | 47 ''' |
| 49 import 'dart:async'; | 48 import 'dart:async'; |
| 50 import 'dart:collection'; | 49 import 'dart:collection'; |
| 51 import 'dart:math'; | 50 import 'dart:math'; |
| 52 '''); | 51 '''); |
| 53 } | 52 } |
| 54 | 53 |
| 55 test_addLibraryImports_dart_hasImports_last() { | 54 test_addLibraryImports_dart_hasImports_last() { |
| 56 resolveTestUnit(''' | 55 resolveTestUnit(''' |
| 57 import 'dart:async'; | 56 import 'dart:async'; |
| 58 import 'dart:collection'; | 57 import 'dart:collection'; |
| 59 '''); | 58 '''); |
| 60 LibraryElement newLibrary = _getDartLibrary('dart:math'); | 59 Source newLibrary = _getDartSourceInfo('dart:math'); |
| 61 _assertAddLibraryImport( | 60 _assertAddLibraryImport( |
| 62 <LibraryElement>[newLibrary], | 61 <Source>[newLibrary], |
| 63 ''' | 62 ''' |
| 64 import 'dart:async'; | 63 import 'dart:async'; |
| 65 import 'dart:collection'; | 64 import 'dart:collection'; |
| 66 import 'dart:math'; | 65 import 'dart:math'; |
| 67 '''); | 66 '''); |
| 68 } | 67 } |
| 69 | 68 |
| 70 test_addLibraryImports_dart_hasImports_multiple() { | 69 test_addLibraryImports_dart_hasImports_multiple() { |
| 71 resolveTestUnit(''' | 70 resolveTestUnit(''' |
| 72 import 'dart:collection'; | 71 import 'dart:collection'; |
| 73 import 'dart:math'; | 72 import 'dart:math'; |
| 74 '''); | 73 '''); |
| 75 LibraryElement newLibrary1 = _getDartLibrary('dart:async'); | 74 Source newLibrary1 = _getDartSourceInfo('dart:async'); |
| 76 LibraryElement newLibrary2 = _getDartLibrary('dart:html'); | 75 Source newLibrary2 = _getDartSourceInfo('dart:html'); |
| 77 _assertAddLibraryImport( | 76 _assertAddLibraryImport( |
| 78 <LibraryElement>[newLibrary1, newLibrary2], | 77 <Source>[newLibrary1, newLibrary2], |
| 79 ''' | 78 ''' |
| 80 import 'dart:async'; | 79 import 'dart:async'; |
| 81 import 'dart:collection'; | 80 import 'dart:collection'; |
| 82 import 'dart:html'; | 81 import 'dart:html'; |
| 83 import 'dart:math'; | 82 import 'dart:math'; |
| 84 '''); | 83 '''); |
| 85 } | 84 } |
| 86 | 85 |
| 87 test_addLibraryImports_dart_hasImports_multiple_first() { | 86 test_addLibraryImports_dart_hasImports_multiple_first() { |
| 88 resolveTestUnit(''' | 87 resolveTestUnit(''' |
| 89 import 'dart:html'; | 88 import 'dart:html'; |
| 90 import 'dart:math'; | 89 import 'dart:math'; |
| 91 '''); | 90 '''); |
| 92 LibraryElement newLibrary1 = _getDartLibrary('dart:async'); | 91 Source newLibrary1 = _getDartSourceInfo('dart:async'); |
| 93 LibraryElement newLibrary2 = _getDartLibrary('dart:collection'); | 92 Source newLibrary2 = _getDartSourceInfo('dart:collection'); |
| 94 _assertAddLibraryImport( | 93 _assertAddLibraryImport( |
| 95 <LibraryElement>[newLibrary1, newLibrary2], | 94 <Source>[newLibrary1, newLibrary2], |
| 96 ''' | 95 ''' |
| 97 import 'dart:async'; | 96 import 'dart:async'; |
| 98 import 'dart:collection'; | 97 import 'dart:collection'; |
| 99 import 'dart:html'; | 98 import 'dart:html'; |
| 100 import 'dart:math'; | 99 import 'dart:math'; |
| 101 '''); | 100 '''); |
| 102 } | 101 } |
| 103 | 102 |
| 104 test_addLibraryImports_dart_hasImports_multiple_last() { | 103 test_addLibraryImports_dart_hasImports_multiple_last() { |
| 105 resolveTestUnit(''' | 104 resolveTestUnit(''' |
| 106 import 'dart:async'; | 105 import 'dart:async'; |
| 107 import 'dart:collection'; | 106 import 'dart:collection'; |
| 108 '''); | 107 '''); |
| 109 LibraryElement newLibrary1 = _getDartLibrary('dart:html'); | 108 Source newLibrary1 = _getDartSourceInfo('dart:html'); |
| 110 LibraryElement newLibrary2 = _getDartLibrary('dart:math'); | 109 Source newLibrary2 = _getDartSourceInfo('dart:math'); |
| 111 _assertAddLibraryImport( | 110 _assertAddLibraryImport( |
| 112 <LibraryElement>[newLibrary1, newLibrary2], | 111 <Source>[newLibrary1, newLibrary2], |
| 113 ''' | 112 ''' |
| 114 import 'dart:async'; | 113 import 'dart:async'; |
| 115 import 'dart:collection'; | 114 import 'dart:collection'; |
| 116 import 'dart:html'; | 115 import 'dart:html'; |
| 117 import 'dart:math'; | 116 import 'dart:math'; |
| 118 '''); | 117 '''); |
| 119 } | 118 } |
| 120 | 119 |
| 121 test_addLibraryImports_dart_hasLibraryDirective() { | 120 test_addLibraryImports_dart_hasLibraryDirective() { |
| 122 resolveTestUnit(''' | 121 resolveTestUnit(''' |
| 123 library test; | 122 library test; |
| 124 | 123 |
| 125 class A {} | 124 class A {} |
| 126 '''); | 125 '''); |
| 127 LibraryElement newLibrary1 = _getDartLibrary('dart:math'); | 126 Source newLibrary1 = _getDartSourceInfo('dart:math'); |
| 128 LibraryElement newLibrary2 = _getDartLibrary('dart:async'); | 127 Source newLibrary2 = _getDartSourceInfo('dart:async'); |
| 129 _assertAddLibraryImport( | 128 _assertAddLibraryImport( |
| 130 <LibraryElement>[newLibrary1, newLibrary2], | 129 <Source>[newLibrary1, newLibrary2], |
| 131 ''' | 130 ''' |
| 132 library test; | 131 library test; |
| 133 | 132 |
| 134 import 'dart:async'; | 133 import 'dart:async'; |
| 135 import 'dart:math'; | 134 import 'dart:math'; |
| 136 | 135 |
| 137 class A {} | 136 class A {} |
| 138 '''); | 137 '''); |
| 139 } | 138 } |
| 140 | 139 |
| 141 test_addLibraryImports_dart_noDirectives_hasComment() { | 140 test_addLibraryImports_dart_noDirectives_hasComment() { |
| 142 resolveTestUnit(''' | 141 resolveTestUnit(''' |
| 143 /// Comment. | 142 /// Comment. |
| 144 | 143 |
| 145 class A {} | 144 class A {} |
| 146 '''); | 145 '''); |
| 147 LibraryElement newLibrary1 = _getDartLibrary('dart:math'); | 146 Source newLibrary1 = _getDartSourceInfo('dart:math'); |
| 148 LibraryElement newLibrary2 = _getDartLibrary('dart:async'); | 147 Source newLibrary2 = _getDartSourceInfo('dart:async'); |
| 149 _assertAddLibraryImport( | 148 _assertAddLibraryImport( |
| 150 <LibraryElement>[newLibrary1, newLibrary2], | 149 <Source>[newLibrary1, newLibrary2], |
| 151 ''' | 150 ''' |
| 152 /// Comment. | 151 /// Comment. |
| 153 | 152 |
| 154 import 'dart:async'; | 153 import 'dart:async'; |
| 155 import 'dart:math'; | 154 import 'dart:math'; |
| 156 | 155 |
| 157 class A {} | 156 class A {} |
| 158 '''); | 157 '''); |
| 159 } | 158 } |
| 160 | 159 |
| 161 test_addLibraryImports_dart_noDirectives_hasShebang() { | 160 test_addLibraryImports_dart_noDirectives_hasShebang() { |
| 162 resolveTestUnit(''' | 161 resolveTestUnit(''' |
| 163 #!/bin/dart | 162 #!/bin/dart |
| 164 | 163 |
| 165 class A {} | 164 class A {} |
| 166 '''); | 165 '''); |
| 167 LibraryElement newLibrary1 = _getDartLibrary('dart:math'); | 166 Source newLibrary1 = _getDartSourceInfo('dart:math'); |
| 168 LibraryElement newLibrary2 = _getDartLibrary('dart:async'); | 167 Source newLibrary2 = _getDartSourceInfo('dart:async'); |
| 169 _assertAddLibraryImport( | 168 _assertAddLibraryImport( |
| 170 <LibraryElement>[newLibrary1, newLibrary2], | 169 <Source>[newLibrary1, newLibrary2], |
| 171 ''' | 170 ''' |
| 172 #!/bin/dart | 171 #!/bin/dart |
| 173 | 172 |
| 174 import 'dart:async'; | 173 import 'dart:async'; |
| 175 import 'dart:math'; | 174 import 'dart:math'; |
| 176 | 175 |
| 177 class A {} | 176 class A {} |
| 178 '''); | 177 '''); |
| 179 } | 178 } |
| 180 | 179 |
| 181 test_addLibraryImports_dart_noDirectives_noShebang() { | 180 test_addLibraryImports_dart_noDirectives_noShebang() { |
| 182 resolveTestUnit(''' | 181 resolveTestUnit(''' |
| 183 class A {} | 182 class A {} |
| 184 '''); | 183 '''); |
| 185 LibraryElement newLibrary1 = _getDartLibrary('dart:math'); | 184 Source newLibrary1 = _getDartSourceInfo('dart:math'); |
| 186 LibraryElement newLibrary2 = _getDartLibrary('dart:async'); | 185 Source newLibrary2 = _getDartSourceInfo('dart:async'); |
| 187 _assertAddLibraryImport( | 186 _assertAddLibraryImport( |
| 188 <LibraryElement>[newLibrary1, newLibrary2], | 187 <Source>[newLibrary1, newLibrary2], |
| 189 ''' | 188 ''' |
| 190 import 'dart:async'; | 189 import 'dart:async'; |
| 191 import 'dart:math'; | 190 import 'dart:math'; |
| 192 | 191 |
| 193 class A {} | 192 class A {} |
| 194 '''); | 193 '''); |
| 195 } | 194 } |
| 196 | 195 |
| 197 test_addLibraryImports_package_hasDart_hasPackages_insertAfter() { | 196 test_addLibraryImports_package_hasDart_hasPackages_insertAfter() { |
| 198 resolveTestUnit(''' | 197 resolveTestUnit(''' |
| 199 import 'dart:async'; | 198 import 'dart:async'; |
| 200 | 199 |
| 201 import 'package:aaa/aaa.dart'; | 200 import 'package:aaa/aaa.dart'; |
| 202 '''); | 201 '''); |
| 203 LibraryElement newLibrary = | 202 Source newLibrary = |
| 204 _mockLibraryElement('/lib/bbb.dart', 'package:bbb/bbb.dart'); | 203 _getSourceInfo('/lib/bbb.dart', 'package:bbb/bbb.dart'); |
| 205 _assertAddLibraryImport( | 204 _assertAddLibraryImport( |
| 206 <LibraryElement>[newLibrary], | 205 <Source>[newLibrary], |
| 207 ''' | 206 ''' |
| 208 import 'dart:async'; | 207 import 'dart:async'; |
| 209 | 208 |
| 210 import 'package:aaa/aaa.dart'; | 209 import 'package:aaa/aaa.dart'; |
| 211 import 'package:bbb/bbb.dart'; | 210 import 'package:bbb/bbb.dart'; |
| 212 '''); | 211 '''); |
| 213 } | 212 } |
| 214 | 213 |
| 215 test_addLibraryImports_package_hasDart_hasPackages_insertBefore() { | 214 test_addLibraryImports_package_hasDart_hasPackages_insertBefore() { |
| 216 resolveTestUnit(''' | 215 resolveTestUnit(''' |
| 217 import 'dart:async'; | 216 import 'dart:async'; |
| 218 | 217 |
| 219 import 'package:bbb/bbb.dart'; | 218 import 'package:bbb/bbb.dart'; |
| 220 '''); | 219 '''); |
| 221 LibraryElement newLibrary = | 220 Source newLibrary = |
| 222 _mockLibraryElement('/lib/aaa.dart', 'package:aaa/aaa.dart'); | 221 _getSourceInfo('/lib/aaa.dart', 'package:aaa/aaa.dart'); |
| 223 _assertAddLibraryImport( | 222 _assertAddLibraryImport( |
| 224 <LibraryElement>[newLibrary], | 223 <Source>[newLibrary], |
| 225 ''' | 224 ''' |
| 226 import 'dart:async'; | 225 import 'dart:async'; |
| 227 | 226 |
| 228 import 'package:aaa/aaa.dart'; | 227 import 'package:aaa/aaa.dart'; |
| 229 import 'package:bbb/bbb.dart'; | 228 import 'package:bbb/bbb.dart'; |
| 230 '''); | 229 '''); |
| 231 } | 230 } |
| 232 | 231 |
| 233 test_addLibraryImports_package_hasImports_between() { | 232 test_addLibraryImports_package_hasImports_between() { |
| 234 resolveTestUnit(''' | 233 resolveTestUnit(''' |
| 235 import 'package:aaa/aaa.dart'; | 234 import 'package:aaa/aaa.dart'; |
| 236 import 'package:ddd/ddd.dart'; | 235 import 'package:ddd/ddd.dart'; |
| 237 '''); | 236 '''); |
| 238 LibraryElement newLibrary1 = | 237 Source newLibrary1 = |
| 239 _mockLibraryElement('/lib/bbb.dart', 'package:bbb/bbb.dart'); | 238 _getSourceInfo('/lib/bbb.dart', 'package:bbb/bbb.dart'); |
| 240 LibraryElement newLibrary2 = | 239 Source newLibrary2 = |
| 241 _mockLibraryElement('/lib/ccc.dart', 'package:ccc/ccc.dart'); | 240 _getSourceInfo('/lib/ccc.dart', 'package:ccc/ccc.dart'); |
| 242 _assertAddLibraryImport( | 241 _assertAddLibraryImport( |
| 243 <LibraryElement>[newLibrary1, newLibrary2], | 242 <Source>[newLibrary1, newLibrary2], |
| 244 ''' | 243 ''' |
| 245 import 'package:aaa/aaa.dart'; | 244 import 'package:aaa/aaa.dart'; |
| 246 import 'package:bbb/bbb.dart'; | 245 import 'package:bbb/bbb.dart'; |
| 247 import 'package:ccc/ccc.dart'; | 246 import 'package:ccc/ccc.dart'; |
| 248 import 'package:ddd/ddd.dart'; | 247 import 'package:ddd/ddd.dart'; |
| 249 '''); | 248 '''); |
| 250 } | 249 } |
| 251 | 250 |
| 252 void _assertAddLibraryImport( | 251 void _assertAddLibraryImport( |
| 253 List<LibraryElement> newLibraries, String expectedCode) { | 252 List<Source> newLibraries, String expectedCode) { |
| 254 SourceChange change = new SourceChange(''); | 253 SourceChange change = new SourceChange(''); |
| 255 addLibraryImports(change, testLibraryElement, newLibraries.toSet()); | 254 addLibraryImports(change, testLibraryElement, newLibraries.toSet()); |
| 256 SourceFileEdit testEdit = change.getFileEdit(testFile); | 255 SourceFileEdit testEdit = change.getFileEdit(testFile); |
| 257 expect(testEdit, isNotNull); | 256 expect(testEdit, isNotNull); |
| 258 String resultCode = SourceEdit.applySequence(testCode, testEdit.edits); | 257 String resultCode = SourceEdit.applySequence(testCode, testEdit.edits); |
| 259 expect(resultCode, expectedCode); | 258 expect(resultCode, expectedCode); |
| 260 } | 259 } |
| 261 | 260 |
| 262 LibraryElement _getDartLibrary(String uri) { | 261 Source _getDartSourceInfo(String uri) { |
|
Brian Wilkerson
2016/12/01 18:20:42
Maybe drop the "Info" here and in _getSourceInfo?
scheglov
2016/12/01 18:21:29
Done.
| |
| 263 String path = removeStart(uri, 'dart:'); | 262 String path = removeStart(uri, 'dart:'); |
| 264 Source newSource = new _SourceMock('/sdk/lib/$path.dart', Uri.parse(uri)); | 263 return new _SourceMock('/sdk/lib/$path.dart', Uri.parse(uri)); |
| 265 return new _LibraryElementMock(newSource); | |
| 266 } | 264 } |
| 267 | 265 |
| 268 LibraryElement _mockLibraryElement(String path, String uri) { | 266 Source _getSourceInfo(String path, String uri) { |
| 269 Source newSource = new _SourceMock(path, Uri.parse(uri)); | 267 return new _SourceMock(path, Uri.parse(uri)); |
| 270 return new _LibraryElementMock(newSource); | |
| 271 } | 268 } |
| 272 } | 269 } |
| 273 | 270 |
| 274 class _LibraryElementMock implements LibraryElement { | |
| 275 @override | |
| 276 final Source source; | |
| 277 | |
| 278 _LibraryElementMock(this.source); | |
| 279 | |
| 280 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
| 281 } | |
| 282 | |
| 283 class _SourceMock implements Source { | 271 class _SourceMock implements Source { |
| 284 @override | 272 @override |
| 285 final String fullName; | 273 final String fullName; |
| 286 | 274 |
| 287 @override | 275 @override |
| 288 final Uri uri; | 276 final Uri uri; |
| 289 | 277 |
| 290 _SourceMock(this.fullName, this.uri); | 278 _SourceMock(this.fullName, this.uri); |
| 291 | 279 |
| 292 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 280 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 293 } | 281 } |
| OLD | NEW |