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.services.correction.status; | |
6 | |
7 import 'package:analysis_services/correction/status.dart'; | |
8 import 'package:analysis_services/search/search_engine.dart'; | |
9 import 'package:analysis_services/src/correction/source_range.dart'; | |
10 import 'package:analysis_testing/abstract_single_unit.dart'; | |
11 import 'package:analysis_testing/reflective_tests.dart'; | |
12 import 'package:analyzer/src/generated/ast.dart'; | |
13 import 'package:analyzer/src/generated/element.dart'; | |
14 import 'package:analyzer/src/generated/source.dart'; | |
15 import 'package:typed_mock/typed_mock.dart'; | |
16 import 'package:unittest/unittest.dart'; | |
17 | |
18 | |
19 main() { | |
20 groupSep = ' | '; | |
21 runReflectiveTests(RefactoringStatusContextTest); | |
22 runReflectiveTests(RefactoringStatusEntryTest); | |
23 runReflectiveTests(RefactoringStatusTest); | |
24 } | |
25 | |
26 | |
27 @ReflectiveTestCase() | |
28 class RefactoringStatusContextTest extends AbstractSingleUnitTest { | |
29 void test_new_forElement() { | |
30 resolveTestUnit('class MyClass {}'); | |
31 Element element = findElement('MyClass'); | |
32 var statusContext = new RefactoringStatusContext.forElement(element); | |
33 // access | |
34 expect(statusContext.context, context); | |
35 expect(statusContext.source, testSource); | |
36 expect( | |
37 statusContext.range, | |
38 rangeStartLength(element.nameOffset, 'MyClass'.length)); | |
39 } | |
40 | |
41 void test_new_forMatch() { | |
42 resolveTestUnit('class MyClass {}'); | |
43 Element element = findElement('MyClass'); | |
44 SourceRange range = rangeElementName(element); | |
45 SearchMatch match = new SearchMatch(null, element, range, true, false); | |
46 var statusContext = new RefactoringStatusContext.forMatch(match); | |
47 // access | |
48 expect(statusContext.context, context); | |
49 expect(statusContext.source, testSource); | |
50 expect(statusContext.range, range); | |
51 } | |
52 | |
53 void test_new_forNode() { | |
54 resolveTestUnit(''' | |
55 main() { | |
56 } | |
57 '''); | |
58 AstNode node = findNodeAtString('main'); | |
59 var statusContext = new RefactoringStatusContext.forNode(node); | |
60 // access | |
61 expect(statusContext.context, context); | |
62 expect(statusContext.source, testSource); | |
63 expect(statusContext.range, rangeNode(node)); | |
64 } | |
65 | |
66 void test_new_forUnit() { | |
67 resolveTestUnit(''); | |
68 SourceRange range = rangeStartLength(10, 20); | |
69 var statusContext = new RefactoringStatusContext.forUnit(testUnit, range); | |
70 // access | |
71 expect(statusContext.context, context); | |
72 expect(statusContext.source, testSource); | |
73 expect(statusContext.range, range); | |
74 } | |
75 } | |
76 | |
77 | |
78 @ReflectiveTestCase() | |
79 class RefactoringStatusEntryTest { | |
80 void test_new_withContext() { | |
81 RefactoringStatusContext context = new _MockRefactoringStatusContext(); | |
82 RefactoringStatusEntry entry = | |
83 new RefactoringStatusEntry( | |
84 RefactoringStatusSeverity.ERROR, | |
85 "my message", | |
86 context); | |
87 // access | |
88 expect(entry.severity, RefactoringStatusSeverity.ERROR); | |
89 expect(entry.message, 'my message'); | |
90 expect(entry.context, context); | |
91 } | |
92 | |
93 void test_new_withoutContext() { | |
94 RefactoringStatusEntry entry = | |
95 new RefactoringStatusEntry(RefactoringStatusSeverity.ERROR, "my message"
); | |
96 // access | |
97 expect(entry.severity, RefactoringStatusSeverity.ERROR); | |
98 expect(entry.message, 'my message'); | |
99 expect(entry.context, isNull); | |
100 // isX | |
101 expect(entry.isFatalError, isFalse); | |
102 expect(entry.isError, isTrue); | |
103 expect(entry.isWarning, isFalse); | |
104 } | |
105 } | |
106 | |
107 | |
108 @ReflectiveTestCase() | |
109 class RefactoringStatusTest { | |
110 void test_addError() { | |
111 RefactoringStatus refactoringStatus = new RefactoringStatus(); | |
112 // initial state | |
113 expect(refactoringStatus.severity, RefactoringStatusSeverity.OK); | |
114 // add ERROR | |
115 refactoringStatus.addError('msg'); | |
116 expect(refactoringStatus.severity, RefactoringStatusSeverity.ERROR); | |
117 expect(refactoringStatus.isOK, isFalse); | |
118 expect(refactoringStatus.hasFatalError, isFalse); | |
119 expect(refactoringStatus.hasError, isTrue); | |
120 // entries | |
121 List<RefactoringStatusEntry> entries = refactoringStatus.entries; | |
122 expect(entries, hasLength(1)); | |
123 expect(entries[0].message, 'msg'); | |
124 } | |
125 | |
126 void test_addFatalError_withContext() { | |
127 RefactoringStatusContext context = new _MockRefactoringStatusContext(); | |
128 RefactoringStatus refactoringStatus = new RefactoringStatus(); | |
129 // initial state | |
130 expect(refactoringStatus.severity, RefactoringStatusSeverity.OK); | |
131 // add FATAL | |
132 refactoringStatus.addFatalError('msg', context); | |
133 expect(refactoringStatus.severity, RefactoringStatusSeverity.FATAL); | |
134 expect(refactoringStatus.isOK, isFalse); | |
135 expect(refactoringStatus.hasFatalError, isTrue); | |
136 expect(refactoringStatus.hasError, isTrue); | |
137 // entries | |
138 List<RefactoringStatusEntry> entries = refactoringStatus.entries; | |
139 expect(entries, hasLength(1)); | |
140 expect(entries[0].message, 'msg'); | |
141 expect(entries[0].context, context); | |
142 // add WARNING, resulting severity is still FATAL | |
143 refactoringStatus.addWarning("warning"); | |
144 expect(refactoringStatus.severity, RefactoringStatusSeverity.FATAL); | |
145 } | |
146 | |
147 void test_addFatalError_withoutContext() { | |
148 RefactoringStatus refactoringStatus = new RefactoringStatus(); | |
149 // initial state | |
150 expect(refactoringStatus.severity, RefactoringStatusSeverity.OK); | |
151 // add FATAL | |
152 refactoringStatus.addFatalError('msg'); | |
153 expect(refactoringStatus.severity, RefactoringStatusSeverity.FATAL); | |
154 expect(refactoringStatus.isOK, isFalse); | |
155 expect(refactoringStatus.hasFatalError, isTrue); | |
156 expect(refactoringStatus.hasError, isTrue); | |
157 // entries | |
158 List<RefactoringStatusEntry> entries = refactoringStatus.entries; | |
159 expect(entries, hasLength(1)); | |
160 expect(entries[0].message, 'msg'); | |
161 expect(entries[0].context, isNull); | |
162 } | |
163 | |
164 void test_addStatus_Error_withWarning() { | |
165 RefactoringStatus refactoringStatus = new RefactoringStatus(); | |
166 refactoringStatus.addError("err"); | |
167 expect(refactoringStatus.severity, RefactoringStatusSeverity.ERROR); | |
168 // merge with OK | |
169 { | |
170 RefactoringStatus other = new RefactoringStatus(); | |
171 other.addWarning("warn"); | |
172 refactoringStatus.addStatus(other); | |
173 } | |
174 expect(refactoringStatus.severity, RefactoringStatusSeverity.ERROR); | |
175 expect(refactoringStatus.message, 'err'); | |
176 } | |
177 | |
178 void test_addStatus_Warning_null() { | |
179 RefactoringStatus refactoringStatus = new RefactoringStatus(); | |
180 refactoringStatus.addWarning("warn"); | |
181 expect(refactoringStatus.severity, RefactoringStatusSeverity.WARNING); | |
182 // merge with "null" | |
183 refactoringStatus.addStatus(null); | |
184 expect(refactoringStatus.severity, RefactoringStatusSeverity.WARNING); | |
185 } | |
186 | |
187 void test_addStatus_Warning_withError() { | |
188 RefactoringStatus refactoringStatus = new RefactoringStatus(); | |
189 refactoringStatus.addWarning("warn"); | |
190 expect(refactoringStatus.severity, RefactoringStatusSeverity.WARNING); | |
191 // merge with OK | |
192 { | |
193 RefactoringStatus other = new RefactoringStatus(); | |
194 other.addError("err"); | |
195 refactoringStatus.addStatus(other); | |
196 } | |
197 expect(refactoringStatus.severity, RefactoringStatusSeverity.ERROR); | |
198 expect(refactoringStatus.message, 'err'); | |
199 } | |
200 | |
201 void test_addWarning() { | |
202 RefactoringStatus refactoringStatus = new RefactoringStatus(); | |
203 // initial state | |
204 expect(refactoringStatus.severity, RefactoringStatusSeverity.OK); | |
205 // add WARNING | |
206 refactoringStatus.addWarning('msg'); | |
207 expect(refactoringStatus.severity, RefactoringStatusSeverity.WARNING); | |
208 expect(refactoringStatus.isOK, isFalse); | |
209 expect(refactoringStatus.hasFatalError, isFalse); | |
210 expect(refactoringStatus.hasError, isFalse); | |
211 expect(refactoringStatus.hasWarning, isTrue); | |
212 // entries | |
213 List<RefactoringStatusEntry> entries = refactoringStatus.entries; | |
214 expect(entries, hasLength(1)); | |
215 expect(entries[0].message, 'msg'); | |
216 } | |
217 | |
218 void test_escalateErrorToFatal() { | |
219 RefactoringStatus refactoringStatus = new RefactoringStatus.error('msg'); | |
220 expect(refactoringStatus.severity, RefactoringStatusSeverity.ERROR); | |
221 // escalated | |
222 RefactoringStatus escalated = refactoringStatus.escalateErrorToFatal(); | |
223 expect(escalated.severity, RefactoringStatusSeverity.FATAL); | |
224 } | |
225 | |
226 void test_get_entryWithHighestSeverity() { | |
227 RefactoringStatus refactoringStatus = new RefactoringStatus(); | |
228 // no entries | |
229 expect(refactoringStatus.entryWithHighestSeverity, isNull); | |
230 expect(refactoringStatus.message, isNull); | |
231 // add entries | |
232 refactoringStatus.addError('msgError'); | |
233 refactoringStatus.addWarning('msgWarning'); | |
234 refactoringStatus.addFatalError('msgFatalError'); | |
235 // get entry | |
236 { | |
237 RefactoringStatusEntry entry = refactoringStatus.entryWithHighestSeverity; | |
238 expect(entry.severity, RefactoringStatusSeverity.FATAL); | |
239 expect(entry.message, 'msgFatalError'); | |
240 } | |
241 // get message | |
242 expect(refactoringStatus.message, 'msgFatalError'); | |
243 } | |
244 | |
245 void test_newError() { | |
246 RefactoringStatusContext context = new _MockRefactoringStatusContext(); | |
247 RefactoringStatus refactoringStatus = | |
248 new RefactoringStatus.error('msg', context); | |
249 expect(refactoringStatus.severity, RefactoringStatusSeverity.ERROR); | |
250 expect(refactoringStatus.message, 'msg'); | |
251 expect(refactoringStatus.entryWithHighestSeverity.context, context); | |
252 } | |
253 | |
254 void test_newFatalError() { | |
255 RefactoringStatus refactoringStatus = new RefactoringStatus.fatal('msg'); | |
256 expect(refactoringStatus.severity, RefactoringStatusSeverity.FATAL); | |
257 expect(refactoringStatus.message, 'msg'); | |
258 } | |
259 | |
260 void test_newWarning() { | |
261 RefactoringStatus refactoringStatus = new RefactoringStatus.warning('msg'); | |
262 expect(refactoringStatus.severity, RefactoringStatusSeverity.WARNING); | |
263 expect(refactoringStatus.message, 'msg'); | |
264 } | |
265 } | |
266 | |
267 | |
268 class _MockRefactoringStatusContext extends TypedMock implements | |
269 RefactoringStatusContext { | |
270 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
271 } | |
OLD | NEW |