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

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

Issue 2365553004: Convert subclasses of Enum (Closed)
Patch Set: Created 4 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
« no previous file with comments | « pkg/analyzer/lib/src/generated/utilities_dart.dart ('k') | 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 analyzer.test.enum_test;
6
7 import 'dart:mirrors';
8
9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/error/error.dart';
11 import 'package:analyzer/src/dart/element/element.dart';
12 import 'package:analyzer/src/generated/engine.dart';
13 import 'package:analyzer/src/generated/java_core.dart';
14 import 'package:analyzer/src/generated/resolver.dart';
15 import 'package:analyzer/src/generated/source.dart';
16 import 'package:analyzer/src/generated/utilities_dart.dart';
17 import 'package:test_reflective_loader/test_reflective_loader.dart';
18 import 'package:unittest/unittest.dart';
19
20 import 'utils.dart';
21
22 void main() {
23 initializeTestEnvironment();
24 defineReflectiveTests(EnumTest);
25 }
26
27 @reflectiveTest
28 class EnumTest {
29 void test_AnalysisLevel() {
30 new EnumTester<AnalysisLevel>()
31 ..check_getters()
32 ..check_explicit_values();
33 }
34
35 void test_CacheState() {
36 new EnumTester<CacheState>()
37 ..check_getters()
38 ..check_explicit_values();
39 }
40
41 void test_ElementKind() {
42 new EnumTester<ElementKind>()
43 ..check_getters()
44 ..check_explicit_values();
45 }
46
47 void test_ErrorProperty() {
48 new EnumTester<ErrorProperty>()
49 ..check_getters()
50 ..check_explicit_values();
51 }
52
53 void test_ErrorSeverity() {
54 new EnumTester<ErrorSeverity>()
55 ..check_getters()
56 ..check_explicit_values();
57 }
58
59 void test_ErrorType() {
60 new EnumTester<ErrorType>()
61 ..check_getters()
62 ..check_explicit_values();
63 }
64
65 void test_INIT_STATE() {
66 new EnumTester<INIT_STATE>()
67 ..check_getters()
68 ..check_explicit_values();
69 }
70
71 void test_Modifier() {
72 new EnumTester<Modifier>(
73 ignoreGetters: ["persistedValues", "transientValues"])
74 ..check_getters()
75 ..check_explicit_values();
76 }
77
78 void test_ParameterKind() {
79 new EnumTester<ParameterKind>()
80 ..check_getters()
81 ..check_explicit_values();
82 }
83
84 void test_RedirectingConstructorKind() {
85 new EnumTester<RedirectingConstructorKind>()
86 ..check_getters()
87 ..check_explicit_values();
88 }
89
90 void test_SourceKind() {
91 new EnumTester<SourceKind>()
92 ..check_getters()
93 ..check_explicit_values();
94 }
95
96 void test_UriKind() {
97 new EnumTester<UriKind>()
98 ..check_getters()
99 ..check_explicit_values();
100 }
101 }
102
103 /**
104 * Helper class for testing invariants of enumerated types.
105 */
106 class EnumTester<C extends Enum> {
107 /**
108 * Set of getter names which should be ignored when looking for getters
109 * representing enum values.
110 */
111 Set<String> _ignoreGetters = new Set<String>();
112
113 EnumTester({List<String> ignoreGetters}) {
114 // Always ignore a getter called "values".
115 _ignoreGetters.add('values');
116
117 if (ignoreGetters != null) {
118 for (String getterName in ignoreGetters) {
119 _ignoreGetters.add(getterName);
120 }
121 }
122 }
123
124 /**
125 * Get a map from getter name to the value returned by the getter, for all
126 * static getters in [C] whose name isn't in [_ignoreGetters].
127 */
128 Map<String, C> get _getters {
129 Map<String, C> result = <String, C>{};
130 ClassMirror reflectedClass = reflectClass(C);
131 reflectedClass.staticMembers.forEach((Symbol symbol, MethodMirror method) {
132 if (!method.isGetter) {
133 return;
134 }
135 String name = MirrorSystem.getName(symbol);
136 if (_ignoreGetters.contains(name)) {
137 return;
138 }
139 C value = reflectedClass.getField(symbol).reflectee as C;
140 result[name] = value;
141 });
142 return result;
143 }
144
145 /**
146 * Check invariants on the list of enum values accessible via the static
147 * getter "values".
148 */
149 void check_explicit_values() {
150 ClassMirror reflectedClass = reflectClass(C);
151 List<C> values = reflectedClass.getField(#values).reflectee as List<C>;
152 Map<C, int> reverseMap = <C, int>{};
153
154 // Check that "values" is a list of values of type C, with no duplicates.
155 expect(values, isList);
156 for (int i = 0; i < values.length; i++) {
157 C value = values[i];
158 expect(value, new isInstanceOf<C>(), reason: 'values[$i]');
159 if (reverseMap.containsKey(value)) {
160 fail('values[$i] and values[${reverseMap[value]}] both equal $value');
161 }
162 reverseMap[value] = i;
163 }
164
165 // Check that the set of values in the "values" list matches the set of
166 // values accessible via static fields.
167 expect(reverseMap.keys.toSet(), equals(_getters.values.toSet()));
168
169 // Make sure the order of the list matches the ordinal numbers.
170 for (int i = 0; i < values.length; i++) {
171 expect(values[i].ordinal, equals(i), reason: 'values[$i].ordinal');
172 }
173 }
174
175 /**
176 * Check invariants on the set of enum values accessible via the static
177 * getters defined in the class [C] (with the exception of a getter called
178 * "values").
179 */
180 void check_getters() {
181 Map<int, String> ordinals = <int, String>{};
182 int numValues = 0;
183
184 _getters.forEach((String name, C value) {
185 String reason = 'getter: $name';
186 ++numValues;
187
188 // Check the type of the value
189 expect(value, new isInstanceOf<C>(), reason: reason);
190
191 // Check that the name of the getter matches the name stored in the enum.
192 expect(value.name, equals(name), reason: reason);
193
194 // Check that there are no duplicate ordinals.
195 if (ordinals.containsKey(value.ordinal)) {
196 fail(
197 'Getters $name and ${ordinals[value.ordinal]} have ordinal value ${v alue.ordinal}');
198 }
199 ordinals[value.ordinal] = name;
200 });
201
202 // Check that the set of ordinals runs from 0 to N-1, where N is the number
203 // of enumerated values.
204 Set<int> expectedOrdinals = new Set<int>();
205 for (int i = 0; i < numValues; i++) {
206 expectedOrdinals.add(i);
207 }
208 expect(ordinals.keys.toSet(), equals(expectedOrdinals));
209 }
210 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/utilities_dart.dart ('k') | pkg/analyzer/test/test_all.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698