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