OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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.src.context.context_test; | 5 library test.src.context.context_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analyzer/src/cancelable_future.dart'; | 9 import 'package:analyzer/src/cancelable_future.dart'; |
10 import 'package:analyzer/src/context/cache.dart'; | 10 import 'package:analyzer/src/context/cache.dart'; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 import 'abstract_context.dart'; | 47 import 'abstract_context.dart'; |
48 | 48 |
49 main() { | 49 main() { |
50 groupSep = ' | '; | 50 groupSep = ' | '; |
51 runReflectiveTests(AnalysisContextImplTest); | 51 runReflectiveTests(AnalysisContextImplTest); |
52 runReflectiveTests(LimitedInvalidateTest); | 52 runReflectiveTests(LimitedInvalidateTest); |
53 } | 53 } |
54 | 54 |
55 @reflectiveTest | 55 @reflectiveTest |
56 class AnalysisContextImplTest extends AbstractContextTest { | 56 class AnalysisContextImplTest extends AbstractContextTest { |
57 void test_applyChanges_empty() { | |
58 context.applyChanges(new ChangeSet()); | |
59 expect(context.performAnalysisTask().changeNotices, isNull); | |
60 } | |
61 | |
62 void fail_parseHtmlUnit_resolveDirectives() { | 57 void fail_parseHtmlUnit_resolveDirectives() { |
63 Source libSource = addSource("/lib.dart", r''' | 58 Source libSource = addSource("/lib.dart", r''' |
64 library lib; | 59 library lib; |
65 class ClassA {}'''); | 60 class ClassA {}'''); |
66 Source source = addSource("/lib.html", r''' | 61 Source source = addSource("/lib.html", r''' |
67 <!DOCTYPE html> | 62 <!DOCTYPE html> |
68 <html> | 63 <html> |
69 <head> | 64 <head> |
70 <script type='application/dart'> | 65 <script type='application/dart'> |
71 import 'lib.dart'; | 66 import 'lib.dart'; |
(...skipping 19 matching lines...) Expand all Loading... |
91 // import directive should be resolved | 86 // import directive should be resolved |
92 ht.XmlTagNode htmlNode = unit.tagNodes[0]; | 87 ht.XmlTagNode htmlNode = unit.tagNodes[0]; |
93 ht.XmlTagNode headNode = htmlNode.tagNodes[0]; | 88 ht.XmlTagNode headNode = htmlNode.tagNodes[0]; |
94 ht.HtmlScriptTagNode scriptNode = headNode.tagNodes[0]; | 89 ht.HtmlScriptTagNode scriptNode = headNode.tagNodes[0]; |
95 CompilationUnit script = scriptNode.script; | 90 CompilationUnit script = scriptNode.script; |
96 ImportDirective importNode = script.directives[0] as ImportDirective; | 91 ImportDirective importNode = script.directives[0] as ImportDirective; |
97 expect(importNode.uriContent, isNotNull); | 92 expect(importNode.uriContent, isNotNull); |
98 expect(importNode.source, libSource); | 93 expect(importNode.source, libSource); |
99 } | 94 } |
100 | 95 |
101 void test_performAnalysisTask_getContentException_dart() { | |
102 Source source = _addSourceWithException('test.dart'); | |
103 // prepare errors | |
104 _analyzeAll_assertFinished(); | |
105 List<AnalysisError> errors = context.getErrors(source).errors; | |
106 // validate errors | |
107 expect(errors, hasLength(1)); | |
108 AnalysisError error = errors[0]; | |
109 expect(error.source, same(source)); | |
110 expect(error.errorCode, ScannerErrorCode.UNABLE_GET_CONTENT); | |
111 } | |
112 | |
113 void test_performAnalysisTask_getContentException_html() { | |
114 Source source = _addSourceWithException('test.html'); | |
115 // prepare errors | |
116 _analyzeAll_assertFinished(); | |
117 List<AnalysisError> errors = context.getErrors(source).errors; | |
118 // validate errors | |
119 expect(errors, hasLength(1)); | |
120 AnalysisError error = errors[0]; | |
121 expect(error.source, same(source)); | |
122 expect(error.errorCode, ScannerErrorCode.UNABLE_GET_CONTENT); | |
123 } | |
124 | |
125 void fail_performAnalysisTask_importedLibraryDelete_html() { | 96 void fail_performAnalysisTask_importedLibraryDelete_html() { |
126 // NOTE: This was failing before converting to the new task model. | 97 // NOTE: This was failing before converting to the new task model. |
127 Source htmlSource = addSource("/page.html", r''' | 98 Source htmlSource = addSource("/page.html", r''' |
128 <html><body><script type="application/dart"> | 99 <html><body><script type="application/dart"> |
129 import 'libB.dart'; | 100 import 'libB.dart'; |
130 main() {print('hello dart');} | 101 main() {print('hello dart');} |
131 </script></body></html>'''); | 102 </script></body></html>'''); |
132 Source libBSource = addSource("/libB.dart", "library libB;"); | 103 Source libBSource = addSource("/libB.dart", "library libB;"); |
133 _analyzeAll_assertFinished(); | 104 _analyzeAll_assertFinished(); |
134 context.computeErrors(htmlSource); | 105 context.computeErrors(htmlSource); |
135 expect( | 106 expect( |
136 context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull, | 107 context.getResolvedCompilationUnit2(libBSource, libBSource), isNotNull, |
137 reason: "libB resolved 1"); | 108 reason: "libB resolved 1"); |
138 expect(!_hasAnalysisErrorWithErrorSeverity(context.getErrors(htmlSource)), | 109 expect(!_hasAnalysisErrorWithErrorSeverity(context.getErrors(htmlSource)), |
139 isTrue, reason: "htmlSource doesn't have errors"); | 110 isTrue, reason: "htmlSource doesn't have errors"); |
140 // remove libB.dart content and analyze | 111 // remove libB.dart content and analyze |
141 context.setContents(libBSource, null); | 112 context.setContents(libBSource, null); |
142 _analyzeAll_assertFinished(); | 113 _analyzeAll_assertFinished(); |
143 context.computeErrors(htmlSource); | 114 context.computeErrors(htmlSource); |
144 AnalysisErrorInfo errors = context.getErrors(htmlSource); | 115 AnalysisErrorInfo errors = context.getErrors(htmlSource); |
145 expect(_hasAnalysisErrorWithErrorSeverity(errors), isTrue, | 116 expect(_hasAnalysisErrorWithErrorSeverity(errors), isTrue, |
146 reason: "htmlSource has an error"); | 117 reason: "htmlSource has an error"); |
147 } | 118 } |
148 | 119 |
149 void test_performAnalysisTask_IOException() { | |
150 TestSource source = _addSourceWithException2("/test.dart", "library test;"); | |
151 source.generateExceptionOnRead = false; | |
152 _analyzeAll_assertFinished(); | |
153 expect(source.readCount, 1); | |
154 _changeSource(source, ""); | |
155 source.generateExceptionOnRead = true; | |
156 _analyzeAll_assertFinished(); | |
157 expect(source.readCount, 3); | |
158 } | |
159 | |
160 void fail_recordLibraryElements() { | 120 void fail_recordLibraryElements() { |
161 fail("Implement this"); | 121 fail("Implement this"); |
162 } | 122 } |
163 | 123 |
164 void fail_setAnalysisOptions_reduceAnalysisPriorityOrder() { | |
165 AnalysisOptionsImpl options = | |
166 new AnalysisOptionsImpl.from(context.analysisOptions); | |
167 List<Source> sources = new List<Source>(); | |
168 for (int index = 0; index < options.cacheSize; index++) { | |
169 sources.add(addSource("/lib.dart$index", "")); | |
170 } | |
171 context.analysisPriorityOrder = sources; | |
172 int oldPriorityOrderSize = _getPriorityOrder(context).length; | |
173 options.cacheSize = options.cacheSize - 10; | |
174 context.analysisOptions = options; | |
175 expect(oldPriorityOrderSize > _getPriorityOrder(context).length, isTrue); | |
176 } | |
177 | |
178 void fail_setAnalysisPriorityOrder_lessThanCacheSize() { | |
179 AnalysisOptions options = context.analysisOptions; | |
180 List<Source> sources = new List<Source>(); | |
181 for (int index = 0; index < options.cacheSize; index++) { | |
182 sources.add(addSource("/lib.dart$index", "")); | |
183 } | |
184 context.analysisPriorityOrder = sources; | |
185 expect(options.cacheSize > _getPriorityOrder(context).length, isTrue); | |
186 } | |
187 | |
188 Future fail_setChangedContents_libraryWithPart() { | |
189 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | |
190 options.incremental = true; | |
191 context.analysisOptions = options; | |
192 SourcesChangedListener listener = new SourcesChangedListener(); | |
193 context.onSourcesChanged.listen(listener.onData); | |
194 String oldCode = r''' | |
195 library lib; | |
196 part 'part.dart'; | |
197 int a = 0;'''; | |
198 Source librarySource = addSource("/lib.dart", oldCode); | |
199 String partContents = r''' | |
200 part of lib; | |
201 int b = a;'''; | |
202 Source partSource = addSource("/part.dart", partContents); | |
203 LibraryElement element = context.computeLibraryElement(librarySource); | |
204 CompilationUnit unit = | |
205 context.getResolvedCompilationUnit(librarySource, element); | |
206 expect(unit, isNotNull); | |
207 int offset = oldCode.indexOf("int a") + 4; | |
208 String newCode = r''' | |
209 library lib; | |
210 part 'part.dart'; | |
211 int ya = 0;'''; | |
212 expect(_getIncrementalAnalysisCache(context), isNull); | |
213 context.setChangedContents(librarySource, newCode, offset, 0, 1); | |
214 expect(context.getContents(librarySource).data, newCode); | |
215 IncrementalAnalysisCache incrementalCache = | |
216 _getIncrementalAnalysisCache(context); | |
217 expect(incrementalCache.librarySource, librarySource); | |
218 expect(incrementalCache.resolvedUnit, same(unit)); | |
219 expect( | |
220 context.getResolvedCompilationUnit2(partSource, librarySource), isNull); | |
221 expect(incrementalCache.newContents, newCode); | |
222 return pumpEventQueue().then((_) { | |
223 listener.assertEvent(wereSourcesAdded: true); | |
224 listener.assertEvent(changedSources: [librarySource]); | |
225 listener.assertEvent(wereSourcesAdded: true); | |
226 listener.assertEvent(changedSources: [partSource]); | |
227 listener.assertEvent(changedSources: [librarySource]); | |
228 listener.assertNoMoreEvents(); | |
229 }); | |
230 } | |
231 | |
232 void fail_unreadableSource() { | 124 void fail_unreadableSource() { |
233 Source test1 = addSource("/test1.dart", r''' | 125 Source test1 = addSource("/test1.dart", r''' |
234 import 'test2.dart'; | 126 import 'test2.dart'; |
235 library test1;'''); | 127 library test1;'''); |
236 Source test2 = addSource("/test2.dart", r''' | 128 Source test2 = addSource("/test2.dart", r''' |
237 import 'test1.dart'; | 129 import 'test1.dart'; |
238 import 'test3.dart'; | 130 import 'test3.dart'; |
239 library test2;'''); | 131 library test2;'''); |
240 Source test3 = _addSourceWithException("/test3.dart"); | 132 Source test3 = _addSourceWithException("/test3.dart"); |
241 _analyzeAll_assertFinished(); | 133 _analyzeAll_assertFinished(); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 changeSet2.addedSource(source2); | 274 changeSet2.addedSource(source2); |
383 changeSet2.changedRange(source, 'library test;', 0, 0, 13); | 275 changeSet2.changedRange(source, 'library test;', 0, 0, 13); |
384 context.applyChanges(changeSet2); | 276 context.applyChanges(changeSet2); |
385 return pumpEventQueue().then((_) { | 277 return pumpEventQueue().then((_) { |
386 listener.assertEvent(wereSourcesAdded: true); | 278 listener.assertEvent(wereSourcesAdded: true); |
387 listener.assertEvent(wereSourcesAdded: true, changedSources: [source]); | 279 listener.assertEvent(wereSourcesAdded: true, changedSources: [source]); |
388 listener.assertNoMoreEvents(); | 280 listener.assertNoMoreEvents(); |
389 }); | 281 }); |
390 } | 282 } |
391 | 283 |
| 284 void test_applyChanges_empty() { |
| 285 context.applyChanges(new ChangeSet()); |
| 286 expect(context.performAnalysisTask().changeNotices, isNull); |
| 287 } |
| 288 |
392 void test_applyChanges_overriddenSource() { | 289 void test_applyChanges_overriddenSource() { |
393 // Note: addSource adds the source to the contentCache. | 290 // Note: addSource adds the source to the contentCache. |
394 Source source = addSource("/test.dart", "library test;"); | 291 Source source = addSource("/test.dart", "library test;"); |
395 context.computeErrors(source); | 292 context.computeErrors(source); |
396 while (!context.sourcesNeedingProcessing.isEmpty) { | 293 while (!context.sourcesNeedingProcessing.isEmpty) { |
397 context.performAnalysisTask(); | 294 context.performAnalysisTask(); |
398 } | 295 } |
399 // Adding the source as a changedSource should have no effect since | 296 // Adding the source as a changedSource should have no effect since |
400 // it is already overridden in the content cache. | 297 // it is already overridden in the content cache. |
401 ChangeSet changeSet = new ChangeSet(); | 298 ChangeSet changeSet = new ChangeSet(); |
(...skipping 1188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1590 expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull, | 1487 expect(context.getResolvedCompilationUnit2(partSource, libSource), isNull, |
1591 reason: "part changed 3"); | 1488 reason: "part changed 3"); |
1592 _analyzeAll_assertFinished(); | 1489 _analyzeAll_assertFinished(); |
1593 expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull, | 1490 expect(context.getResolvedCompilationUnit2(libSource, libSource), isNotNull, |
1594 reason: "library resolved 3"); | 1491 reason: "library resolved 3"); |
1595 expect( | 1492 expect( |
1596 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull, | 1493 context.getResolvedCompilationUnit2(partSource, libSource), isNotNull, |
1597 reason: "part resolved 3"); | 1494 reason: "part resolved 3"); |
1598 } | 1495 } |
1599 | 1496 |
| 1497 void test_performAnalysisTask_getContentException_dart() { |
| 1498 Source source = _addSourceWithException('test.dart'); |
| 1499 // prepare errors |
| 1500 _analyzeAll_assertFinished(); |
| 1501 List<AnalysisError> errors = context.getErrors(source).errors; |
| 1502 // validate errors |
| 1503 expect(errors, hasLength(1)); |
| 1504 AnalysisError error = errors[0]; |
| 1505 expect(error.source, same(source)); |
| 1506 expect(error.errorCode, ScannerErrorCode.UNABLE_GET_CONTENT); |
| 1507 } |
| 1508 |
| 1509 void test_performAnalysisTask_getContentException_html() { |
| 1510 Source source = _addSourceWithException('test.html'); |
| 1511 // prepare errors |
| 1512 _analyzeAll_assertFinished(); |
| 1513 List<AnalysisError> errors = context.getErrors(source).errors; |
| 1514 // validate errors |
| 1515 expect(errors, hasLength(1)); |
| 1516 AnalysisError error = errors[0]; |
| 1517 expect(error.source, same(source)); |
| 1518 expect(error.errorCode, ScannerErrorCode.UNABLE_GET_CONTENT); |
| 1519 } |
| 1520 |
1600 void test_performAnalysisTask_importedLibraryAdd() { | 1521 void test_performAnalysisTask_importedLibraryAdd() { |
1601 Source libASource = | 1522 Source libASource = |
1602 addSource("/libA.dart", "library libA; import 'libB.dart';"); | 1523 addSource("/libA.dart", "library libA; import 'libB.dart';"); |
1603 _analyzeAll_assertFinished(); | 1524 _analyzeAll_assertFinished(); |
1604 expect( | 1525 expect( |
1605 context.getResolvedCompilationUnit2(libASource, libASource), isNotNull, | 1526 context.getResolvedCompilationUnit2(libASource, libASource), isNotNull, |
1606 reason: "libA resolved 1"); | 1527 reason: "libA resolved 1"); |
1607 expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)), | 1528 expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)), |
1608 isTrue, reason: "libA has an error"); | 1529 isTrue, reason: "libA has an error"); |
1609 // add libB.dart and analyze | 1530 // add libB.dart and analyze |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1660 // remove libB.dart and analyze | 1581 // remove libB.dart and analyze |
1661 _removeSource(libBSource); | 1582 _removeSource(libBSource); |
1662 _analyzeAll_assertFinished(); | 1583 _analyzeAll_assertFinished(); |
1663 expect( | 1584 expect( |
1664 context.getResolvedCompilationUnit2(libASource, libASource), isNotNull, | 1585 context.getResolvedCompilationUnit2(libASource, libASource), isNotNull, |
1665 reason: "libA resolved 2"); | 1586 reason: "libA resolved 2"); |
1666 expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)), | 1587 expect(_hasAnalysisErrorWithErrorSeverity(context.getErrors(libASource)), |
1667 isTrue, reason: "libA has an error"); | 1588 isTrue, reason: "libA has an error"); |
1668 } | 1589 } |
1669 | 1590 |
| 1591 void test_performAnalysisTask_IOException() { |
| 1592 TestSource source = _addSourceWithException2("/test.dart", "library test;"); |
| 1593 source.generateExceptionOnRead = false; |
| 1594 _analyzeAll_assertFinished(); |
| 1595 expect(source.readCount, 1); |
| 1596 _changeSource(source, ""); |
| 1597 source.generateExceptionOnRead = true; |
| 1598 _analyzeAll_assertFinished(); |
| 1599 expect(source.readCount, 3); |
| 1600 } |
| 1601 |
1670 void test_performAnalysisTask_missingPart() { | 1602 void test_performAnalysisTask_missingPart() { |
1671 Source source = | 1603 Source source = |
1672 addSource("/test.dart", "library lib; part 'no-such-file.dart';"); | 1604 addSource("/test.dart", "library lib; part 'no-such-file.dart';"); |
1673 _analyzeAll_assertFinished(); | 1605 _analyzeAll_assertFinished(); |
1674 expect(context.getLibraryElement(source), isNotNull, | 1606 expect(context.getLibraryElement(source), isNotNull, |
1675 reason: "performAnalysisTask failed to compute an element model"); | 1607 reason: "performAnalysisTask failed to compute an element model"); |
1676 } | 1608 } |
1677 | 1609 |
1678 void test_performAnalysisTask_modifiedAfterParse() { | 1610 void test_performAnalysisTask_modifiedAfterParse() { |
1679 // TODO(scheglov) no threads in Dart | 1611 // TODO(scheglov) no threads in Dart |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1716 _analyzeAll_assertFinished(); | 1648 _analyzeAll_assertFinished(); |
1717 // verify | 1649 // verify |
1718 expect(libraryElementUris, contains('dart:core')); | 1650 expect(libraryElementUris, contains('dart:core')); |
1719 expect(libraryElementUris, contains('file:///test.dart')); | 1651 expect(libraryElementUris, contains('file:///test.dart')); |
1720 expect(parsedUnitUris, contains('dart:core')); | 1652 expect(parsedUnitUris, contains('dart:core')); |
1721 expect(parsedUnitUris, contains('file:///test.dart')); | 1653 expect(parsedUnitUris, contains('file:///test.dart')); |
1722 expect(resolvedUnitUris, contains('dart:core')); | 1654 expect(resolvedUnitUris, contains('dart:core')); |
1723 expect(resolvedUnitUris, contains('file:///test.dart')); | 1655 expect(resolvedUnitUris, contains('file:///test.dart')); |
1724 } | 1656 } |
1725 | 1657 |
1726 // void test_resolveCompilationUnit_sourceChangeDuringResolution() { | |
1727 // _context = new _AnalysisContext_sourceChangeDuringResolution(); | |
1728 // AnalysisContextFactory.initContextWithCore(_context); | |
1729 // _sourceFactory = _context.sourceFactory; | |
1730 // Source source = _addSource("/lib.dart", "library lib;"); | |
1731 // CompilationUnit compilationUnit = | |
1732 // _context.resolveCompilationUnit2(source, source); | |
1733 // expect(compilationUnit, isNotNull); | |
1734 // expect(_context.getLineInfo(source), isNotNull); | |
1735 // } | |
1736 | |
1737 void test_resolveCompilationUnit_import_relative() { | 1658 void test_resolveCompilationUnit_import_relative() { |
1738 Source sourceA = | 1659 Source sourceA = |
1739 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}"); | 1660 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}"); |
1740 addSource("/libB.dart", "library libB; class B{}"); | 1661 addSource("/libB.dart", "library libB; class B{}"); |
1741 CompilationUnit compilationUnit = | 1662 CompilationUnit compilationUnit = |
1742 context.resolveCompilationUnit2(sourceA, sourceA); | 1663 context.resolveCompilationUnit2(sourceA, sourceA); |
1743 expect(compilationUnit, isNotNull); | 1664 expect(compilationUnit, isNotNull); |
1744 LibraryElement library = compilationUnit.element.library; | 1665 LibraryElement library = compilationUnit.element.library; |
1745 List<LibraryElement> importedLibraries = library.importedLibraries; | 1666 List<LibraryElement> importedLibraries = library.importedLibraries; |
1746 assertNamedElements(importedLibraries, ["dart.core", "libB"]); | 1667 assertNamedElements(importedLibraries, ["dart.core", "libB"]); |
(...skipping 20 matching lines...) Expand all Loading... |
1767 List<LibraryElement> visibleLibraries = library.visibleLibraries; | 1688 List<LibraryElement> visibleLibraries = library.visibleLibraries; |
1768 assertNamedElements(visibleLibraries, [ | 1689 assertNamedElements(visibleLibraries, [ |
1769 "dart.core", | 1690 "dart.core", |
1770 "dart.async", | 1691 "dart.async", |
1771 "dart.math", | 1692 "dart.math", |
1772 "libA", | 1693 "libA", |
1773 "libB" | 1694 "libB" |
1774 ]); | 1695 ]); |
1775 } | 1696 } |
1776 | 1697 |
| 1698 // void test_resolveCompilationUnit_sourceChangeDuringResolution() { |
| 1699 // _context = new _AnalysisContext_sourceChangeDuringResolution(); |
| 1700 // AnalysisContextFactory.initContextWithCore(_context); |
| 1701 // _sourceFactory = _context.sourceFactory; |
| 1702 // Source source = _addSource("/lib.dart", "library lib;"); |
| 1703 // CompilationUnit compilationUnit = |
| 1704 // _context.resolveCompilationUnit2(source, source); |
| 1705 // expect(compilationUnit, isNotNull); |
| 1706 // expect(_context.getLineInfo(source), isNotNull); |
| 1707 // } |
| 1708 |
1777 void test_resolveCompilationUnit_library() { | 1709 void test_resolveCompilationUnit_library() { |
1778 Source source = addSource("/lib.dart", "library lib;"); | 1710 Source source = addSource("/lib.dart", "library lib;"); |
1779 LibraryElement library = context.computeLibraryElement(source); | 1711 LibraryElement library = context.computeLibraryElement(source); |
1780 CompilationUnit compilationUnit = | 1712 CompilationUnit compilationUnit = |
1781 context.resolveCompilationUnit(source, library); | 1713 context.resolveCompilationUnit(source, library); |
1782 expect(compilationUnit, isNotNull); | 1714 expect(compilationUnit, isNotNull); |
1783 expect(compilationUnit.element, isNotNull); | 1715 expect(compilationUnit.element, isNotNull); |
1784 } | 1716 } |
1785 | 1717 |
1786 void test_resolveCompilationUnit_source() { | 1718 void test_resolveCompilationUnit_source() { |
1787 Source source = addSource("/lib.dart", "library lib;"); | 1719 Source source = addSource("/lib.dart", "library lib;"); |
1788 CompilationUnit compilationUnit = | 1720 CompilationUnit compilationUnit = |
1789 context.resolveCompilationUnit2(source, source); | 1721 context.resolveCompilationUnit2(source, source); |
1790 expect(compilationUnit, isNotNull); | 1722 expect(compilationUnit, isNotNull); |
1791 } | 1723 } |
1792 | 1724 |
1793 void test_setAnalysisOptions() { | 1725 void test_setAnalysisOptions() { |
1794 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 1726 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
1795 options.cacheSize = 42; | 1727 options.cacheSize = 42; |
1796 options.dart2jsHint = false; | 1728 options.dart2jsHint = false; |
1797 options.hint = false; | 1729 options.hint = false; |
1798 context.analysisOptions = options; | 1730 context.analysisOptions = options; |
1799 AnalysisOptions result = context.analysisOptions; | 1731 AnalysisOptions result = context.analysisOptions; |
1800 expect(result.cacheSize, options.cacheSize); | 1732 expect(result.cacheSize, options.cacheSize); |
1801 expect(result.dart2jsHint, options.dart2jsHint); | 1733 expect(result.dart2jsHint, options.dart2jsHint); |
1802 expect(result.hint, options.hint); | 1734 expect(result.hint, options.hint); |
1803 } | 1735 } |
1804 | 1736 |
| 1737 void test_setAnalysisPriorityOrder() { |
| 1738 int priorityCount = 4; |
| 1739 List<Source> sources = new List<Source>(); |
| 1740 for (int index = 0; index < priorityCount; index++) { |
| 1741 sources.add(addSource("/lib.dart$index", "")); |
| 1742 } |
| 1743 context.analysisPriorityOrder = sources; |
| 1744 expect(_getPriorityOrder(context).length, priorityCount); |
| 1745 } |
| 1746 |
1805 void test_setAnalysisPriorityOrder_empty() { | 1747 void test_setAnalysisPriorityOrder_empty() { |
1806 context.analysisPriorityOrder = new List<Source>(); | 1748 context.analysisPriorityOrder = new List<Source>(); |
1807 } | 1749 } |
1808 | 1750 |
1809 void test_setAnalysisPriorityOrder_nonEmpty() { | 1751 void test_setAnalysisPriorityOrder_nonEmpty() { |
1810 List<Source> sources = new List<Source>(); | 1752 List<Source> sources = new List<Source>(); |
1811 sources.add(addSource("/lib.dart", "library lib;")); | 1753 sources.add(addSource("/lib.dart", "library lib;")); |
1812 context.analysisPriorityOrder = sources; | 1754 context.analysisPriorityOrder = sources; |
1813 } | 1755 } |
1814 | 1756 |
| 1757 Future test_setChangedContents_libraryWithPart() { |
| 1758 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
| 1759 options.incremental = true; |
| 1760 context.analysisOptions = options; |
| 1761 SourcesChangedListener listener = new SourcesChangedListener(); |
| 1762 context.onSourcesChanged.listen(listener.onData); |
| 1763 String oldCode = r''' |
| 1764 library lib; |
| 1765 part 'part.dart'; |
| 1766 int a = 0;'''; |
| 1767 Source librarySource = addSource("/lib.dart", oldCode); |
| 1768 String partContents = r''' |
| 1769 part of lib; |
| 1770 int b = a;'''; |
| 1771 Source partSource = addSource("/part.dart", partContents); |
| 1772 LibraryElement element = context.computeLibraryElement(librarySource); |
| 1773 CompilationUnit unit = |
| 1774 context.resolveCompilationUnit(librarySource, element); |
| 1775 expect(unit, isNotNull); |
| 1776 int offset = oldCode.indexOf("int a") + 4; |
| 1777 String newCode = r''' |
| 1778 library lib; |
| 1779 part 'part.dart'; |
| 1780 int ya = 0;'''; |
| 1781 context.setChangedContents(librarySource, newCode, offset, 0, 1); |
| 1782 expect(context.getContents(librarySource).data, newCode); |
| 1783 expect( |
| 1784 context.getResolvedCompilationUnit2(partSource, librarySource), isNull); |
| 1785 return pumpEventQueue().then((_) { |
| 1786 listener.assertEvent(wereSourcesAdded: true); |
| 1787 listener.assertEvent(wereSourcesAdded: true); |
| 1788 listener.assertEvent(changedSources: [librarySource]); |
| 1789 listener.assertNoMoreEvents(); |
| 1790 }); |
| 1791 } |
| 1792 |
1815 void test_setChangedContents_notResolved() { | 1793 void test_setChangedContents_notResolved() { |
1816 AnalysisOptionsImpl options = | 1794 AnalysisOptionsImpl options = |
1817 new AnalysisOptionsImpl.from(context.analysisOptions); | 1795 new AnalysisOptionsImpl.from(context.analysisOptions); |
1818 options.incremental = true; | 1796 options.incremental = true; |
1819 context.analysisOptions = options; | 1797 context.analysisOptions = options; |
1820 String oldCode = r''' | 1798 String oldCode = r''' |
1821 library lib; | 1799 library lib; |
1822 int a = 0;'''; | 1800 int a = 0;'''; |
1823 Source librarySource = addSource("/lib.dart", oldCode); | 1801 Source librarySource = addSource("/lib.dart", oldCode); |
1824 int offset = oldCode.indexOf("int a") + 4; | 1802 int offset = oldCode.indexOf("int a") + 4; |
1825 String newCode = r''' | 1803 String newCode = r''' |
1826 library lib; | 1804 library lib; |
1827 int ya = 0;'''; | 1805 int ya = 0;'''; |
1828 context.setChangedContents(librarySource, newCode, offset, 0, 1); | 1806 context.setChangedContents(librarySource, newCode, offset, 0, 1); |
1829 expect(context.getContents(librarySource).data, newCode); | 1807 expect(context.getContents(librarySource).data, newCode); |
1830 expect(_getIncrementalAnalysisCache(context), isNull); | |
1831 } | 1808 } |
1832 | 1809 |
1833 Future test_setContents_libraryWithPart() { | 1810 Future test_setContents_libraryWithPart() { |
1834 SourcesChangedListener listener = new SourcesChangedListener(); | 1811 SourcesChangedListener listener = new SourcesChangedListener(); |
1835 context.onSourcesChanged.listen(listener.onData); | 1812 context.onSourcesChanged.listen(listener.onData); |
1836 String libraryContents1 = r''' | 1813 String libraryContents1 = r''' |
1837 library lib; | 1814 library lib; |
1838 part 'part.dart'; | 1815 part 'part.dart'; |
1839 int a = 0;'''; | 1816 int a = 0;'''; |
1840 Source librarySource = addSource("/lib.dart", libraryContents1); | 1817 Source librarySource = addSource("/lib.dart", libraryContents1); |
1841 String partContents1 = r''' | 1818 String partContents1 = r''' |
1842 part of lib; | 1819 part of lib; |
1843 int b = a;'''; | 1820 int b = a;'''; |
1844 Source partSource = addSource("/part.dart", partContents1); | 1821 Source partSource = addSource("/part.dart", partContents1); |
1845 context.computeLibraryElement(librarySource); | 1822 context.computeLibraryElement(librarySource); |
1846 IncrementalAnalysisCache incrementalCache = new IncrementalAnalysisCache( | |
1847 librarySource, librarySource, null, null, null, 0, 0, 0); | |
1848 _setIncrementalAnalysisCache(context, incrementalCache); | |
1849 expect(_getIncrementalAnalysisCache(context), same(incrementalCache)); | |
1850 String libraryContents2 = r''' | 1823 String libraryContents2 = r''' |
1851 library lib; | 1824 library lib; |
1852 part 'part.dart'; | 1825 part 'part.dart'; |
1853 int aa = 0;'''; | 1826 int aa = 0;'''; |
1854 context.setContents(librarySource, libraryContents2); | 1827 context.setContents(librarySource, libraryContents2); |
1855 expect( | 1828 expect( |
1856 context.getResolvedCompilationUnit2(partSource, librarySource), isNull); | 1829 context.getResolvedCompilationUnit2(partSource, librarySource), isNull); |
1857 expect(_getIncrementalAnalysisCache(context), isNull); | |
1858 return pumpEventQueue().then((_) { | 1830 return pumpEventQueue().then((_) { |
1859 listener.assertEvent(wereSourcesAdded: true); | 1831 listener.assertEvent(wereSourcesAdded: true); |
1860 listener.assertEvent(wereSourcesAdded: true); | 1832 listener.assertEvent(wereSourcesAdded: true); |
1861 listener.assertEvent(changedSources: [librarySource]); | 1833 listener.assertEvent(changedSources: [librarySource]); |
1862 listener.assertNoMoreEvents(); | 1834 listener.assertNoMoreEvents(); |
1863 }); | 1835 }); |
1864 } | 1836 } |
1865 | 1837 |
1866 void test_setContents_null() { | 1838 void test_setContents_null() { |
1867 Source librarySource = addSource("/lib.dart", r''' | 1839 Source librarySource = addSource("/lib.dart", r''' |
1868 library lib; | 1840 library lib; |
1869 int a = 0;'''); | 1841 int a = 0;'''); |
1870 context.setContents(librarySource, '// different'); | 1842 context.setContents(librarySource, '// different'); |
1871 context.computeLibraryElement(librarySource); | 1843 context.computeLibraryElement(librarySource); |
1872 IncrementalAnalysisCache incrementalCache = new IncrementalAnalysisCache( | |
1873 librarySource, librarySource, null, null, null, 0, 0, 0); | |
1874 _setIncrementalAnalysisCache(context, incrementalCache); | |
1875 expect(_getIncrementalAnalysisCache(context), same(incrementalCache)); | |
1876 context.setContents(librarySource, null); | 1844 context.setContents(librarySource, null); |
1877 expect(context.getResolvedCompilationUnit2(librarySource, librarySource), | 1845 expect(context.getResolvedCompilationUnit2(librarySource, librarySource), |
1878 isNull); | 1846 isNull); |
1879 expect(_getIncrementalAnalysisCache(context), isNull); | |
1880 } | 1847 } |
1881 | 1848 |
1882 void test_setContents_unchanged_consistentModificationTime() { | 1849 void test_setContents_unchanged_consistentModificationTime() { |
1883 String contents = "// foo"; | 1850 String contents = "// foo"; |
1884 Source source = addSource("/test.dart", contents); | 1851 Source source = addSource("/test.dart", contents); |
1885 context.setContents(source, contents); | 1852 context.setContents(source, contents); |
1886 // do all, no tasks | 1853 // do all, no tasks |
1887 _analyzeAll_assertFinished(); | 1854 _analyzeAll_assertFinished(); |
1888 { | 1855 { |
1889 AnalysisResult result = context.performAnalysisTask(); | 1856 AnalysisResult result = context.performAnalysisTask(); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1996 } | 1963 } |
1997 return null; | 1964 return null; |
1998 } | 1965 } |
1999 | 1966 |
2000 void _flushAst(Source source) { | 1967 void _flushAst(Source source) { |
2001 CacheEntry entry = | 1968 CacheEntry entry = |
2002 context.getCacheEntry(new LibrarySpecificUnit(source, source)); | 1969 context.getCacheEntry(new LibrarySpecificUnit(source, source)); |
2003 entry.setState(RESOLVED_UNIT, CacheState.FLUSHED); | 1970 entry.setState(RESOLVED_UNIT, CacheState.FLUSHED); |
2004 } | 1971 } |
2005 | 1972 |
2006 IncrementalAnalysisCache _getIncrementalAnalysisCache( | |
2007 AnalysisContextImpl context2) { | |
2008 return context2.test_incrementalAnalysisCache; | |
2009 } | |
2010 | |
2011 List<Source> _getPriorityOrder(AnalysisContextImpl context2) { | 1973 List<Source> _getPriorityOrder(AnalysisContextImpl context2) { |
2012 return context2.test_priorityOrder; | 1974 return context2.test_priorityOrder; |
2013 } | 1975 } |
2014 | 1976 |
2015 void _performPendingAnalysisTasks([int maxTasks = 512]) { | 1977 void _performPendingAnalysisTasks([int maxTasks = 512]) { |
2016 for (int i = 0; context.performAnalysisTask().hasMoreWork; i++) { | 1978 for (int i = 0; context.performAnalysisTask().hasMoreWork; i++) { |
2017 if (i > maxTasks) { | 1979 if (i > maxTasks) { |
2018 fail('Analysis did not terminate.'); | 1980 fail('Analysis did not terminate.'); |
2019 } | 1981 } |
2020 } | 1982 } |
2021 } | 1983 } |
2022 | 1984 |
2023 void _removeSource(Source source) { | 1985 void _removeSource(Source source) { |
2024 resourceProvider.deleteFile(source.fullName); | 1986 resourceProvider.deleteFile(source.fullName); |
2025 ChangeSet changeSet = new ChangeSet(); | 1987 ChangeSet changeSet = new ChangeSet(); |
2026 changeSet.removedSource(source); | 1988 changeSet.removedSource(source); |
2027 context.applyChanges(changeSet); | 1989 context.applyChanges(changeSet); |
2028 } | 1990 } |
2029 | 1991 |
2030 void _setIncrementalAnalysisCache( | |
2031 AnalysisContextImpl context, IncrementalAnalysisCache incrementalCache) { | |
2032 context.test_incrementalAnalysisCache = incrementalCache; | |
2033 } | |
2034 | |
2035 /** | 1992 /** |
2036 * Returns `true` if there is an [AnalysisError] with [ErrorSeverity.ERROR] in | 1993 * Returns `true` if there is an [AnalysisError] with [ErrorSeverity.ERROR] in |
2037 * the given [AnalysisErrorInfo]. | 1994 * the given [AnalysisErrorInfo]. |
2038 */ | 1995 */ |
2039 static bool _hasAnalysisErrorWithErrorSeverity(AnalysisErrorInfo errorInfo) { | 1996 static bool _hasAnalysisErrorWithErrorSeverity(AnalysisErrorInfo errorInfo) { |
2040 List<AnalysisError> errors = errorInfo.errors; | 1997 List<AnalysisError> errors = errorInfo.errors; |
2041 for (AnalysisError analysisError in errors) { | 1998 for (AnalysisError analysisError in errors) { |
2042 if (analysisError.errorCode.errorSeverity == ErrorSeverity.ERROR) { | 1999 if (analysisError.errorCode.errorSeverity == ErrorSeverity.ERROR) { |
2043 return true; | 2000 return true; |
2044 } | 2001 } |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2205 } | 2162 } |
2206 } | 2163 } |
2207 | 2164 |
2208 class _AnalysisContextImplTest_test_applyChanges_removeContainer | 2165 class _AnalysisContextImplTest_test_applyChanges_removeContainer |
2209 implements SourceContainer { | 2166 implements SourceContainer { |
2210 Source libB; | 2167 Source libB; |
2211 _AnalysisContextImplTest_test_applyChanges_removeContainer(this.libB); | 2168 _AnalysisContextImplTest_test_applyChanges_removeContainer(this.libB); |
2212 @override | 2169 @override |
2213 bool contains(Source source) => source == libB; | 2170 bool contains(Source source) => source == libB; |
2214 } | 2171 } |
OLD | NEW |