Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(791)

Side by Side Diff: pkg/analyzer/test/enum_test.dart

Issue 639293004: Verify correctness of classes derived from Enum. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analyzer/test/test_all.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.enums;
6
7 import 'dart:mirrors';
8
9 import 'package:analyzer/src/generated/ast.dart';
10 import 'package:analyzer/src/generated/element.dart';
11 import 'package:analyzer/src/generated/engine.dart';
12 import 'package:analyzer/src/generated/error.dart';
13 import 'package:analyzer/src/generated/html.dart' as html;
14 import 'package:analyzer/src/generated/instrumentation.dart';
15 import 'package:analyzer/src/generated/java_core.dart';
16 import 'package:analyzer/src/generated/parser.dart';
17 import 'package:analyzer/src/generated/resolver.dart';
18 import 'package:analyzer/src/generated/scanner.dart' as scanner;
19 import 'package:analyzer/src/generated/source.dart';
20 import 'package:analyzer/src/generated/utilities_dart.dart';
21 import 'package:unittest/unittest.dart';
22
23 import 'generated/ast_test.dart';
24 import 'reflective_tests.dart';
25
26 void main() {
27 groupSep = ' | ';
28 runReflectiveTests(EnumTest);
29 }
30
31
32 @ReflectiveTestCase()
33 class EnumTest {
34 void test_AnalysisLevel() {
35 new EnumTester<AnalysisLevel>()
36 ..check_getters()
37 ..check_explicit_values();
38 }
39
40 void test_AngularCode() {
41 new EnumTester<AngularCode>()
42 ..check_getters()
43 ..check_explicit_values();
44 }
45
46 void test_AngularPropertyKind() {
47 new EnumTester<AngularPropertyKind>()
48 ..check_getters()
49 ..check_explicit_values();
50 }
51
52 void test_AssignmentKind() {
53 new EnumTester<AssignmentKind>()
54 ..check_getters()
55 ..check_explicit_values();
56 }
57
58 void test_CacheState() {
59 new EnumTester<CacheState>()
60 ..check_getters()
61 ..check_explicit_values();
62 }
63
64 void test_CheckedModeCompileTimeErrorCode() {
65 new EnumTester<CheckedModeCompileTimeErrorCode>()
66 ..check_getters()
67 ..check_explicit_values();
68 }
69
70 void test_CommentType() {
71 new EnumTester<CommentType>()
72 ..check_getters()
73 ..check_explicit_values();
74 }
75
76 void test_CompileTimeErrorCode() {
77 new EnumTester<CompileTimeErrorCode>()
78 ..check_getters()
79 ..check_explicit_values();
80 }
81
82 void test_ElementKind() {
83 new EnumTester<ElementKind>()
84 ..check_getters()
85 ..check_explicit_values();
86 }
87
88 void test_ErrorProperty() {
89 new EnumTester<ErrorProperty>()
90 ..check_getters()
91 ..check_explicit_values();
92 }
93
94 void test_ErrorSeverity() {
95 new EnumTester<ErrorSeverity>()
96 ..check_getters()
97 ..check_explicit_values();
98 }
99
100 void test_ErrorType() {
101 new EnumTester<ErrorType>()
102 ..check_getters()
103 ..check_explicit_values();
104 }
105
106 void test_HintCode() {
107 new EnumTester<HintCode>()
108 ..check_getters()
109 ..check_explicit_values();
110 }
111
112 void test_html_TokenType() {
113 new EnumTester<html.TokenType>()
114 ..check_getters()
115 ..check_explicit_values();
116 }
117
118 void test_HtmlWarningCode() {
119 new EnumTester<HtmlWarningCode>()
120 ..check_getters()
121 ..check_explicit_values();
122 }
123
124 void test_INIT_STATE() {
125 new EnumTester<INIT_STATE>()
126 ..check_getters()
127 ..check_explicit_values();
128 }
129
130 void test_InstrumentationLevel() {
131 new EnumTester<InstrumentationLevel>()
132 ..check_getters()
133 ..check_explicit_values();
134 }
135
136 void test_Keyword() {
137 new EnumTester<scanner.Keyword>(ignoreGetters: ['keywords'])
138 ..check_getters()
139 ..check_explicit_values();
140 }
141
142 void test_Modifier() {
143 new EnumTester<Modifier>()
144 ..check_getters()
145 ..check_explicit_values();
146 }
147
148 void test_ParameterKind() {
149 new EnumTester<ParameterKind>()
150 ..check_getters()
151 ..check_explicit_values();
152 }
153
154 void test_ParserErrorCode() {
155 new EnumTester<ParserErrorCode>()
156 ..check_getters()
157 ..check_explicit_values();
158 }
159
160 void test_PolymerCode() {
161 new EnumTester<PolymerCode>()
162 ..check_getters()
163 ..check_explicit_values();
164 }
165
166 void test_RedirectingConstructorKind() {
167 new EnumTester<RedirectingConstructorKind>()
168 ..check_getters()
169 ..check_explicit_values();
170 }
171
172 void test_ResolverErrorCode() {
173 new EnumTester<ResolverErrorCode>()
174 ..check_getters()
175 ..check_explicit_values();
176 }
177
178 void test_RetentionPriority() {
179 new EnumTester<RetentionPriority>()
180 ..check_getters()
181 ..check_explicit_values();
182 }
183
184 void test_scanner_TokenType() {
185 new EnumTester<scanner.TokenType>()
186 ..check_getters()
187 ..check_explicit_values();
188 }
189
190 void test_ScannerErrorCode() {
191 new EnumTester<scanner.ScannerErrorCode>()
192 ..check_getters()
193 ..check_explicit_values();
194 }
195
196 void test_SourceKind() {
197 new EnumTester<SourceKind>()
198 ..check_getters()
199 ..check_explicit_values();
200 }
201
202 void test_SourcePriority() {
203 new EnumTester<SourcePriority>()
204 ..check_getters()
205 ..check_explicit_values();
206 }
207
208 void test_StaticTypeWarningCode() {
209 new EnumTester<StaticTypeWarningCode>()
210 ..check_getters()
211 ..check_explicit_values();
212 }
213
214 void test_StaticWarningCode() {
215 new EnumTester<StaticWarningCode>()
216 ..check_getters()
217 ..check_explicit_values();
218 }
219
220 void test_TodoCode() {
221 new EnumTester<TodoCode>(ignoreGetters: ['TODO_REGEX'])
222 ..check_getters()
223 ..check_explicit_values();
224 }
225
226 void test_TokenClass() {
227 new EnumTester<scanner.TokenClass>()
228 ..check_getters()
229 ..check_explicit_values();
230 }
231
232 void test_UriKind() {
233 new EnumTester<UriKind>()
234 ..check_getters()
235 ..check_explicit_values();
236 }
237
238 void test_UriValidationCode() {
239 new EnumTester<UriValidationCode>()
240 ..check_getters()
241 ..check_explicit_values();
242 }
243
244 void test_WrapperKind() {
245 new EnumTester<WrapperKind>()
246 ..check_getters()
247 ..check_explicit_values();
248 }
249 }
250
251
252 /**
253 * Helper class for testing invariants of enumerated types.
254 */
255 class EnumTester<C extends Enum> {
256 /**
257 * Set of getter names which should be ignored when looking for getters
258 * representing enum values.
259 */
260 Set<String> _ignoreGetters = new Set<String>();
261
262 EnumTester({List<String> ignoreGetters}) {
263 // Always ignore a getter called "values".
264 _ignoreGetters.add('values');
265
266 if (ignoreGetters != null) {
267 for (String getterName in ignoreGetters) {
268 _ignoreGetters.add(getterName);
269 }
270 }
271 }
272
273 /**
274 * Get a map from getter name to the value returned by the getter, for all
275 * static getters in [C] whose name isn't in [_ignoreGetters].
276 */
277 Map<String, C> get _getters {
278 Map<String, C> result = <String, C>{};
279 ClassMirror reflectedClass = reflectClass(C);
280 reflectedClass.staticMembers.forEach((Symbol symbol, MethodMirror method) {
281 if (!method.isGetter) {
282 return;
283 }
284 String name = MirrorSystem.getName(symbol);
285 if (_ignoreGetters.contains(name)) {
286 return;
287 }
288 C value = reflectedClass.getField(symbol).reflectee;
289 result[name] = value;
290 });
291 return result;
292 }
293
294 /**
295 * Check invariants on the list of enum values accessible via the static
296 * getter "values".
297 */
298 void check_explicit_values() {
299 ClassMirror reflectedClass = reflectClass(C);
300 List<C> values = reflectedClass.getField(#values).reflectee;
301 Map<C, int> reverseMap = <C, int>{};
302
303 // Check that "values" is a list of values of type C, with no duplicates.
304 expect(values, isList);
305 for (int i = 0; i < values.length; i++) {
306 C value = values[i];
307 expect(value, new isInstanceOf<C>(), reason: 'values[$i]');
308 if (reverseMap.containsKey(value)) {
309 fail('values[$i] and values[${reverseMap[value]}] both equal $value');
310 }
311 reverseMap[value] = i;
312 }
313
314 // Check that the set of values in the "values" list matches the set of
315 // values accessible via static fields.
316 expect(reverseMap.keys.toSet(), equals(_getters.values.toSet()));
317
318 // Make sure the order of the list matches the ordinal numbers.
319 for (int i = 0; i < values.length; i++) {
320 expect(values[i].ordinal, equals(i), reason: 'values[$i].ordinal');
321 }
322 }
323
324 /**
325 * Check invariants on the set of enum values accessible via the static
326 * getters defined in the class [C] (with the exception of a getter called
327 * "values").
328 */
329 void check_getters() {
330 Map<int, String> ordinals = <int, String>{};
331 int numValues = 0;
332
333 _getters.forEach((String name, C value) {
334 String reason = 'getter: $name';
335 ++numValues;
336
337 // Check the type of the value
338 expect(value, new isInstanceOf<C>(), reason: reason);
339
340 // Check that the name of the getter matches the name stored in the enum.
341 expect(value.name, equals(name), reason: reason);
342
343 // Check that there are no duplicate ordinals.
344 if (ordinals.containsKey(value.ordinal)) {
345 fail(
346 'Getters $name and ${ordinals[value.ordinal]} have ordinal value ${v alue.ordinal}');
ricow1 2014/10/14 09:08:10 long line
347 }
348 ordinals[value.ordinal] = name;
349 });
350
351 // Check that the set of ordinals runs from 0 to N-1, where N is the number
352 // of enumerated values.
353 Set<int> expectedOrdinals = new Set<int>();
354 for (int i = 0; i < numValues; i++) {
355 expectedOrdinals.add(i);
356 }
357 expect(ordinals.keys.toSet(), equals(expectedOrdinals));
358 }
359 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/test_all.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698