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

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

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

Powered by Google App Engine
This is Rietveld 408576698