OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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.src.task.html_work_manager_test; |
| 6 |
| 7 import 'package:analyzer/src/context/cache.dart'; |
| 8 import 'package:analyzer/src/generated/engine.dart' |
| 9 show |
| 10 AnalysisEngine, |
| 11 AnalysisErrorInfo, |
| 12 AnalysisErrorInfoImpl, |
| 13 CacheState, |
| 14 ChangeNoticeImpl, |
| 15 InternalAnalysisContext; |
| 16 import 'package:analyzer/src/generated/error.dart' |
| 17 show AnalysisError, HtmlErrorCode; |
| 18 import 'package:analyzer/src/generated/java_engine.dart' show CaughtException; |
| 19 import 'package:analyzer/src/generated/scanner.dart' show ScannerErrorCode; |
| 20 import 'package:analyzer/src/generated/source.dart'; |
| 21 import 'package:analyzer/src/task/driver.dart'; |
| 22 import 'package:analyzer/src/task/html.dart'; |
| 23 import 'package:analyzer/src/task/html_work_manager.dart'; |
| 24 import 'package:analyzer/task/dart.dart'; |
| 25 import 'package:analyzer/task/general.dart'; |
| 26 import 'package:analyzer/task/html.dart'; |
| 27 import 'package:analyzer/task/model.dart'; |
| 28 import 'package:typed_mock/typed_mock.dart'; |
| 29 import 'package:unittest/unittest.dart'; |
| 30 |
| 31 import '../../generated/test_support.dart'; |
| 32 import '../../reflective_tests.dart'; |
| 33 |
| 34 main() { |
| 35 groupSep = ' | '; |
| 36 runReflectiveTests(HtmlWorkManagerTest); |
| 37 } |
| 38 |
| 39 @reflectiveTest |
| 40 class HtmlWorkManagerTest { |
| 41 InternalAnalysisContext context = new _InternalAnalysisContextMock(); |
| 42 AnalysisCache cache; |
| 43 HtmlWorkManager manager; |
| 44 |
| 45 CaughtException caughtException = new CaughtException(null, null); |
| 46 |
| 47 Source source1 = new TestSource('1.html'); |
| 48 Source source2 = new TestSource('2.html'); |
| 49 Source source3 = new TestSource('3.html'); |
| 50 Source source4 = new TestSource('4.html'); |
| 51 CacheEntry entry1; |
| 52 CacheEntry entry2; |
| 53 CacheEntry entry3; |
| 54 CacheEntry entry4; |
| 55 |
| 56 void expect_sourceQueue(List<Source> sources) { |
| 57 expect(manager.sourceQueue, unorderedEquals(sources)); |
| 58 } |
| 59 |
| 60 void setUp() { |
| 61 cache = context.analysisCache; |
| 62 manager = new HtmlWorkManager(context); |
| 63 entry1 = context.getCacheEntry(source1); |
| 64 entry2 = context.getCacheEntry(source2); |
| 65 entry3 = context.getCacheEntry(source3); |
| 66 entry4 = context.getCacheEntry(source4); |
| 67 } |
| 68 |
| 69 void test_applyChange_add() { |
| 70 // add source1 |
| 71 manager.applyChange([source1], [], []); |
| 72 expect_sourceQueue([source1]); |
| 73 // add source2 |
| 74 manager.applyChange([source2], [], []); |
| 75 expect_sourceQueue([source1, source2]); |
| 76 } |
| 77 |
| 78 void test_applyChange_add_duplicate() { |
| 79 // add source1 |
| 80 manager.applyChange([source1], [], []); |
| 81 expect_sourceQueue([source1]); |
| 82 // add source1 again |
| 83 manager.applyChange([source1], [], []); |
| 84 expect_sourceQueue([source1]); |
| 85 } |
| 86 |
| 87 void test_applyChange_change() { |
| 88 // change source1 |
| 89 manager.applyChange([], [source1], []); |
| 90 expect_sourceQueue([source1]); |
| 91 } |
| 92 |
| 93 void test_applyChange_change_afterAdd() { |
| 94 manager.applyChange([source1, source2], [], []); |
| 95 // change source1 |
| 96 manager.applyChange([], [source1], []); |
| 97 expect_sourceQueue([source1, source2]); |
| 98 } |
| 99 |
| 100 void test_applyChange_remove() { |
| 101 manager.applyChange([source1, source2], [], []); |
| 102 // remove source1 |
| 103 manager.applyChange([], [], [source1]); |
| 104 expect_sourceQueue([source2]); |
| 105 // remove source2 |
| 106 manager.applyChange([], [], [source2]); |
| 107 expect_sourceQueue([]); |
| 108 // remove source3 |
| 109 manager.applyChange([], [], [source3]); |
| 110 expect_sourceQueue([]); |
| 111 } |
| 112 |
| 113 void test_applyPriorityTargets() { |
| 114 when(context.shouldErrorsBeAnalyzed(source2, null)).thenReturn(true); |
| 115 when(context.shouldErrorsBeAnalyzed(source3, null)).thenReturn(true); |
| 116 manager.priorityResultQueue.add(new TargetedResult(source1, HTML_ERRORS)); |
| 117 manager.priorityResultQueue.add(new TargetedResult(source2, HTML_ERRORS)); |
| 118 // -source1 +source3 |
| 119 manager.applyPriorityTargets([source2, source3]); |
| 120 expect(manager.priorityResultQueue, unorderedEquals([ |
| 121 new TargetedResult(source2, HTML_ERRORS), |
| 122 new TargetedResult(source3, HTML_ERRORS) |
| 123 ])); |
| 124 // get next request |
| 125 TargetedResult request = manager.getNextResult(); |
| 126 expect(request.target, source2); |
| 127 expect(request.result, HTML_ERRORS); |
| 128 } |
| 129 |
| 130 void test_getErrors_fullList() { |
| 131 AnalysisError error1 = |
| 132 new AnalysisError(source1, 1, 0, HtmlErrorCode.PARSE_ERROR, ['']); |
| 133 AnalysisError error2 = |
| 134 new AnalysisError(source1, 2, 0, HtmlErrorCode.PARSE_ERROR, ['']); |
| 135 LineInfo lineInfo = new LineInfo([0]); |
| 136 entry1.setValue(HTML_DOCUMENT_ERRORS, <AnalysisError>[error1], []); |
| 137 entry1.setValue(LINE_INFO, lineInfo, []); |
| 138 |
| 139 DartScript script = new DartScript(source1, []); |
| 140 entry1.setValue(DART_SCRIPTS, [script], []); |
| 141 CacheEntry scriptEntry = context.getCacheEntry(script); |
| 142 scriptEntry.setValue(DART_ERRORS, [error2], []); |
| 143 |
| 144 AnalysisErrorInfo errorInfo = manager.getErrors(source1); |
| 145 expect(errorInfo.errors, unorderedEquals([error1, error2])); |
| 146 expect(errorInfo.lineInfo, lineInfo); |
| 147 } |
| 148 |
| 149 void test_getErrors_partialList() { |
| 150 AnalysisError error1 = |
| 151 new AnalysisError(source1, 1, 0, HtmlErrorCode.PARSE_ERROR, ['']); |
| 152 AnalysisError error2 = |
| 153 new AnalysisError(source1, 2, 0, HtmlErrorCode.PARSE_ERROR, ['']); |
| 154 LineInfo lineInfo = new LineInfo([0]); |
| 155 entry1.setValue(HTML_DOCUMENT_ERRORS, <AnalysisError>[error1, error2], []); |
| 156 entry1.setValue(LINE_INFO, lineInfo, []); |
| 157 |
| 158 AnalysisErrorInfo errorInfo = manager.getErrors(source1); |
| 159 expect(errorInfo.errors, unorderedEquals([error1, error2])); |
| 160 expect(errorInfo.lineInfo, lineInfo); |
| 161 } |
| 162 |
| 163 void test_getNextResult_hasNormal_firstIsError() { |
| 164 entry1.setErrorState(caughtException, [HTML_ERRORS]); |
| 165 manager.sourceQueue.addAll([source1, source2]); |
| 166 TargetedResult request = manager.getNextResult(); |
| 167 expect(request.target, source2); |
| 168 expect(request.result, HTML_ERRORS); |
| 169 // source1 is out, source2 is waiting |
| 170 expect_sourceQueue([source2]); |
| 171 } |
| 172 |
| 173 void test_getNextResult_hasNormal_firstIsInvalid() { |
| 174 entry1.setState(HTML_ERRORS, CacheState.INVALID); |
| 175 manager.sourceQueue.addAll([source1, source2]); |
| 176 TargetedResult request = manager.getNextResult(); |
| 177 expect(request.target, source1); |
| 178 expect(request.result, HTML_ERRORS); |
| 179 // no changes until computed |
| 180 expect_sourceQueue([source1, source2]); |
| 181 } |
| 182 |
| 183 void test_getNextResult_hasNormal_firstIsValid() { |
| 184 entry1.setValue(HTML_ERRORS, [], []); |
| 185 manager.sourceQueue.addAll([source1, source2]); |
| 186 TargetedResult request = manager.getNextResult(); |
| 187 expect(request.target, source2); |
| 188 expect(request.result, HTML_ERRORS); |
| 189 // source1 is out, source2 is waiting |
| 190 expect_sourceQueue([source2]); |
| 191 } |
| 192 |
| 193 void test_getNextResult_hasNormalAndPriority() { |
| 194 entry1.setState(HTML_ERRORS, CacheState.INVALID); |
| 195 manager.sourceQueue.addAll([source1, source2]); |
| 196 manager.addPriorityResult(source3, HTML_ERRORS); |
| 197 |
| 198 TargetedResult request = manager.getNextResult(); |
| 199 expect(request.target, source3); |
| 200 expect(request.result, HTML_ERRORS); |
| 201 // no changes until computed |
| 202 expect_sourceQueue([source1, source2]); |
| 203 } |
| 204 |
| 205 void test_getNextResult_hasPriority() { |
| 206 manager.addPriorityResult(source1, HTML_ERRORS); |
| 207 manager.addPriorityResult(source2, HTML_ERRORS); |
| 208 expect(manager.priorityResultQueue, unorderedEquals([ |
| 209 new TargetedResult(source1, HTML_ERRORS), |
| 210 new TargetedResult(source2, HTML_ERRORS) |
| 211 ])); |
| 212 |
| 213 TargetedResult request = manager.getNextResult(); |
| 214 expect(request.target, source1); |
| 215 expect(request.result, HTML_ERRORS); |
| 216 // no changes until computed |
| 217 expect(manager.priorityResultQueue, unorderedEquals([ |
| 218 new TargetedResult(source1, HTML_ERRORS), |
| 219 new TargetedResult(source2, HTML_ERRORS) |
| 220 ])); |
| 221 } |
| 222 |
| 223 void test_getNextResult_nothingToDo() { |
| 224 TargetedResult request = manager.getNextResult(); |
| 225 expect(request, isNull); |
| 226 } |
| 227 |
| 228 void test_getNextResultPriority_hasPriority() { |
| 229 manager.addPriorityResult(source1, SOURCE_KIND); |
| 230 expect(manager.getNextResultPriority(), WorkOrderPriority.PRIORITY); |
| 231 } |
| 232 |
| 233 void test_getNextResultPriority_hasSource() { |
| 234 manager.sourceQueue.addAll([source1]); |
| 235 expect(manager.getNextResultPriority(), WorkOrderPriority.NORMAL); |
| 236 } |
| 237 |
| 238 void test_getNextResultPriority_nothingToDo() { |
| 239 expect(manager.getNextResultPriority(), WorkOrderPriority.NONE); |
| 240 } |
| 241 |
| 242 void test_onAnalysisOptionsChanged() { |
| 243 when(context.exists(anyObject)).thenReturn(true); |
| 244 // set cache values |
| 245 entry1.setValue(DART_SCRIPTS, [], []); |
| 246 entry1.setValue(HTML_DOCUMENT, null, []); |
| 247 entry1.setValue(HTML_DOCUMENT_ERRORS, [], []); |
| 248 entry1.setValue(HTML_ERRORS, [], []); |
| 249 entry1.setValue(REFERENCED_LIBRARIES, [], []); |
| 250 // notify |
| 251 manager.onAnalysisOptionsChanged(); |
| 252 // Only resolution-based values are invalidated. |
| 253 expect(entry1.getState(DART_SCRIPTS), CacheState.VALID); |
| 254 expect(entry1.getState(HTML_DOCUMENT), CacheState.VALID); |
| 255 expect(entry1.getState(HTML_DOCUMENT_ERRORS), CacheState.VALID); |
| 256 expect(entry1.getState(HTML_ERRORS), CacheState.INVALID); |
| 257 expect(entry1.getState(REFERENCED_LIBRARIES), CacheState.VALID); |
| 258 } |
| 259 |
| 260 void test_onResultInvalidated_scheduleInvalidatedLibraries() { |
| 261 // set HTML_ERRORS for source1 and source3 |
| 262 entry1.setValue(HTML_ERRORS, [], []); |
| 263 entry3.setValue(HTML_ERRORS, [], []); |
| 264 // invalidate HTML_ERRORS for source1, schedule it |
| 265 entry1.setState(HTML_ERRORS, CacheState.INVALID); |
| 266 expect_sourceQueue([source1]); |
| 267 // invalidate HTML_ERRORS for source3, schedule it |
| 268 entry3.setState(HTML_ERRORS, CacheState.INVALID); |
| 269 expect_sourceQueue([source1, source3]); |
| 270 } |
| 271 |
| 272 void test_onSourceFactoryChanged() { |
| 273 when(context.exists(anyObject)).thenReturn(true); |
| 274 // set cache values |
| 275 entry1.setValue(DART_SCRIPTS, [], []); |
| 276 entry1.setValue(HTML_DOCUMENT, null, []); |
| 277 entry1.setValue(HTML_DOCUMENT_ERRORS, [], []); |
| 278 entry1.setValue(HTML_ERRORS, [], []); |
| 279 entry1.setValue(REFERENCED_LIBRARIES, [], []); |
| 280 // notify |
| 281 manager.onSourceFactoryChanged(); |
| 282 // Only resolution-based values are invalidated. |
| 283 expect(entry1.getState(DART_SCRIPTS), CacheState.VALID); |
| 284 expect(entry1.getState(HTML_DOCUMENT), CacheState.VALID); |
| 285 expect(entry1.getState(HTML_DOCUMENT_ERRORS), CacheState.VALID); |
| 286 expect(entry1.getState(HTML_ERRORS), CacheState.INVALID); |
| 287 expect(entry1.getState(REFERENCED_LIBRARIES), CacheState.INVALID); |
| 288 } |
| 289 |
| 290 void test_resultsComputed_errors() { |
| 291 AnalysisError error1 = |
| 292 new AnalysisError(source1, 1, 0, HtmlErrorCode.PARSE_ERROR, ['']); |
| 293 AnalysisError error2 = |
| 294 new AnalysisError(source1, 2, 0, HtmlErrorCode.PARSE_ERROR, ['']); |
| 295 LineInfo lineInfo = new LineInfo([0]); |
| 296 entry1.setValue(LINE_INFO, lineInfo, []); |
| 297 entry1.setValue(HTML_ERRORS, <AnalysisError>[error1, error2], []); |
| 298 // RESOLVED_UNIT is ready, set errors |
| 299 manager.resultsComputed(source1, {HTML_ERRORS: null}); |
| 300 // all of the errors are included |
| 301 ChangeNoticeImpl notice = context.getNotice(source1); |
| 302 expect(notice.errors, unorderedEquals([error1, error2])); |
| 303 expect(notice.lineInfo, lineInfo); |
| 304 } |
| 305 } |
| 306 |
| 307 class _InternalAnalysisContextMock extends TypedMock |
| 308 implements InternalAnalysisContext { |
| 309 @override |
| 310 CachePartition privateAnalysisCachePartition; |
| 311 |
| 312 @override |
| 313 AnalysisCache analysisCache; |
| 314 |
| 315 Map<Source, ChangeNoticeImpl> _pendingNotices = <Source, ChangeNoticeImpl>{}; |
| 316 |
| 317 _InternalAnalysisContextMock() { |
| 318 privateAnalysisCachePartition = new UniversalCachePartition(this); |
| 319 analysisCache = new AnalysisCache([privateAnalysisCachePartition]); |
| 320 } |
| 321 |
| 322 @override |
| 323 CacheEntry getCacheEntry(AnalysisTarget target) { |
| 324 CacheEntry entry = analysisCache.get(target); |
| 325 if (entry == null) { |
| 326 entry = new CacheEntry(target); |
| 327 analysisCache.put(entry); |
| 328 } |
| 329 return entry; |
| 330 } |
| 331 |
| 332 @override |
| 333 AnalysisErrorInfo getErrors(Source source) { |
| 334 String name = source.shortName; |
| 335 List<AnalysisError> errors = AnalysisError.NO_ERRORS; |
| 336 if (AnalysisEngine.isDartFileName(name) || source is DartScript) { |
| 337 errors = getCacheEntry(source).getValue(DART_ERRORS); |
| 338 } else if (AnalysisEngine.isHtmlFileName(name)) { |
| 339 errors = getCacheEntry(source).getValue(HTML_ERRORS); |
| 340 } |
| 341 return new AnalysisErrorInfoImpl( |
| 342 errors, getCacheEntry(source).getValue(LINE_INFO)); |
| 343 } |
| 344 |
| 345 @override |
| 346 ChangeNoticeImpl getNotice(Source source) { |
| 347 return _pendingNotices.putIfAbsent( |
| 348 source, () => new ChangeNoticeImpl(source)); |
| 349 } |
| 350 |
| 351 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 352 } |
OLD | NEW |