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

Side by Side Diff: pkg/analyzer/test/generated/test_support.dart

Issue 673233003: Replace custom EngineTestCase.assertEquals() with 'unorderedEquals'. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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
OLDNEW
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library engine.test_support; 8 library engine.test_support;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
11 import "dart:math" as math;
12 11
13 import 'package:analyzer/src/generated/ast.dart' show AstNode, NodeLocator; 12 import 'package:analyzer/src/generated/ast.dart' show AstNode, NodeLocator;
14 import 'package:analyzer/src/generated/element.dart'; 13 import 'package:analyzer/src/generated/element.dart';
15 import 'package:analyzer/src/generated/engine.dart'; 14 import 'package:analyzer/src/generated/engine.dart';
16 import 'package:analyzer/src/generated/error.dart'; 15 import 'package:analyzer/src/generated/error.dart';
17 import 'package:analyzer/src/generated/java_core.dart'; 16 import 'package:analyzer/src/generated/java_core.dart';
18 import 'package:analyzer/src/generated/java_engine.dart'; 17 import 'package:analyzer/src/generated/java_engine.dart';
19 import 'package:analyzer/src/generated/scanner.dart';
20 import 'package:analyzer/src/generated/source.dart'; 18 import 'package:analyzer/src/generated/source.dart';
21 import 'package:unittest/unittest.dart'; 19 import 'package:unittest/unittest.dart';
22 20
23 21
24 /** 22 /**
25 * The class `EngineTestCase` defines utility methods for making assertions. 23 * The class `EngineTestCase` defines utility methods for making assertions.
26 */ 24 */
27 class EngineTestCase { 25 class EngineTestCase {
28 void setUp() {} 26 void setUp() {}
29 27
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 for (MethodElement method in type.element.methods) { 93 for (MethodElement method in type.element.methods) {
96 if (method.name == methodName) { 94 if (method.name == methodName) {
97 return method; 95 return method;
98 } 96 }
99 } 97 }
100 fail("Could not find method named $methodName in ${type.displayName}"); 98 fail("Could not find method named $methodName in ${type.displayName}");
101 return null; 99 return null;
102 } 100 }
103 101
104 /** 102 /**
105 * Assert that the given array is non-`null` and contains the expected element s. The
106 * elements can appear in any order.
107 *
108 * @param array the array being tested
109 * @param expectedElements the expected elements
110 * @throws AssertionFailedError if the array is `null` or does not contain the expected
111 * elements
112 */
113 static void assertContains(List<Object> array,
114 List<Object> expectedElements) {
115 int expectedSize = expectedElements.length;
116 if (array == null) {
117 fail("Expected array of length $expectedSize; found null");
118 }
119 if (array.length != expectedSize) {
120 fail("Expected array of length $expectedSize; contained ${array.length} el ements");
121 }
122 List<bool> found = new List<bool>.filled(expectedSize, false);
123 for (int i = 0; i < expectedSize; i++) {
124 _privateAssertContains(array, found, expectedElements[i]);
125 }
126 }
127
128 /**
129 * Assert that the array of actual values contain exactly the same values as t hose in the array of
130 * expected value, with the exception that the order of the elements is not re quired to be the
131 * same.
132 *
133 * @param expectedValues the values that are expected to be found
134 * @param actualValues the actual values that are being compared against the e xpected values
135 */
136 static void assertEqualsIgnoreOrder(List<Object> expectedValues,
137 List<Object> actualValues) {
138 expect(actualValues, isNotNull);
139 int expectedLength = expectedValues.length;
140 expect(actualValues.length, expectedLength);
141 List<bool> found = new List<bool>.filled(expectedLength, false);
142 for (int i = 0; i < expectedLength; i++) {
143 found[i] = false;
144 }
145 for (Object actualValue in actualValues) {
146 bool wasExpected = false;
147 for (int i = 0; i < expectedLength; i++) {
148 if (!found[i] && expectedValues[i] == actualValue) {
149 found[i] = true;
150 wasExpected = true;
151 break;
152 }
153 }
154 if (!wasExpected) {
155 fail("The actual value $actualValue was not expected");
156 }
157 }
158 }
159
160 /**
161 * Assert that the given object is an instance of the expected class. 103 * Assert that the given object is an instance of the expected class.
162 * 104 *
163 * @param expectedClass the class that the object is expected to be an instanc e of 105 * @param expectedClass the class that the object is expected to be an instanc e of
164 * @param object the object being tested 106 * @param object the object being tested
165 * @return the object that was being tested 107 * @return the object that was being tested
166 * @throws Exception if the object is not an instance of the expected class 108 * @throws Exception if the object is not an instance of the expected class
167 */ 109 */
168 static Object assertInstanceOf(Predicate<Object> predicate, 110 static Object assertInstanceOf(Predicate<Object> predicate,
169 Type expectedClass, Object object) { 111 Type expectedClass, Object object) {
170 if (!predicate(object)) { 112 if (!predicate(object)) {
171 fail("Expected instance of $expectedClass, found ${object == null ? "null" : object.runtimeType}"); 113 fail("Expected instance of $expectedClass, found ${object == null ? "null" : object.runtimeType}");
172 } 114 }
173 return object; 115 return object;
174 } 116 }
175 117
176 /** 118 /**
177 * @return the [AstNode] with requested type at offset of the "prefix". 119 * @return the [AstNode] with requested type at offset of the "prefix".
178 */ 120 */
179 static AstNode findNode(AstNode root, String code, String prefix, 121 static AstNode findNode(AstNode root, String code, String prefix,
180 Predicate<AstNode> predicate) { 122 Predicate<AstNode> predicate) {
181 int offset = code.indexOf(prefix); 123 int offset = code.indexOf(prefix);
182 if (offset == -1) { 124 if (offset == -1) {
183 throw new IllegalArgumentException("Not found '$prefix'."); 125 throw new IllegalArgumentException("Not found '$prefix'.");
184 } 126 }
185 AstNode node = new NodeLocator.con1(offset).searchWithin(root); 127 AstNode node = new NodeLocator.con1(offset).searchWithin(root);
186 return node.getAncestor(predicate); 128 return node.getAncestor(predicate);
187 } 129 }
188
189 static void _privateAssertContains(List<Object> array, List<bool> found,
190 Object element) {
191 if (element == null) {
192 for (int i = 0; i < array.length; i++) {
193 if (!found[i]) {
194 if (array[i] == null) {
195 found[i] = true;
196 return;
197 }
198 }
199 }
200 fail("Does not contain null");
201 } else {
202 for (int i = 0; i < array.length; i++) {
203 if (!found[i]) {
204 if (element == array[i]) {
205 found[i] = true;
206 return;
207 }
208 }
209 }
210 fail("Does not contain $element");
211 }
212 }
213 } 130 }
214 131
215 /** 132 /**
216 * Instances of the class `GatheringErrorListener` implement an error listener t hat collects 133 * Instances of the class `GatheringErrorListener` implement an error listener t hat collects
217 * all of the errors passed to it for later examination. 134 * all of the errors passed to it for later examination.
218 */ 135 */
219 class GatheringErrorListener implements AnalysisErrorListener { 136 class GatheringErrorListener implements AnalysisErrorListener {
220 /** 137 /**
221 * An empty array of errors used when no errors are expected. 138 * An empty array of errors used when no errors are expected.
222 */ 139 */
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 void getContentsToReceiver(Source_ContentReceiver receiver) { 638 void getContentsToReceiver(Source_ContentReceiver receiver) {
722 throw new UnsupportedOperationException(); 639 throw new UnsupportedOperationException();
723 } 640 }
724 Source resolve(String uri) { 641 Source resolve(String uri) {
725 throw new UnsupportedOperationException(); 642 throw new UnsupportedOperationException();
726 } 643 }
727 Uri resolveRelativeUri(Uri uri) { 644 Uri resolveRelativeUri(Uri uri) {
728 return new Uri(scheme: 'file', path: _name).resolveUri(uri); 645 return new Uri(scheme: 'file', path: _name).resolveUri(uri);
729 } 646 }
730 } 647 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/generated/resolver_test.dart ('k') | pkg/analyzer/test/generated/utilities_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698