Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, 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_utils; | |
|
pquitslund
2015/03/13 22:42:38
Interesting that this is dead code. Looks like th
Brian Wilkerson
2015/03/14 00:11:59
The earliest commit of this file is https://code.g
| |
| 6 | |
| 7 import 'package:analyzer/src/generated/ast.dart'; | |
| 8 import 'package:analyzer/src/generated/engine.dart'; | |
| 9 import 'package:analyzer/src/generated/error.dart'; | |
| 10 import 'package:analyzer/src/generated/parser.dart'; | |
| 11 import 'package:analyzer/src/generated/scanner.dart'; | |
| 12 import 'package:analyzer/src/generated/source.dart'; | |
| 13 import 'package:unittest/unittest.dart'; | |
| 14 | |
| 15 /// Parse the given [source] as a statement and assert, if provided, that | |
| 16 /// exactly a given set of [expectedErrorCodes] are encountered. | |
| 17 Statement parseStatement(String source, [List<ErrorCode> expectedErrorCodes]) { | |
| 18 var listener = new _GatheringErrorListener(); | |
| 19 var reader = new CharSequenceReader(source); | |
| 20 var scanner = new Scanner(null, reader, listener); | |
| 21 listener.setLineInfo(new _TestSource(), scanner.lineStarts); | |
| 22 | |
| 23 var token = scanner.tokenize(); | |
| 24 var parser = new Parser(null, listener); | |
| 25 var statement = parser.parseStatement(token); | |
| 26 expect(statement, isNotNull); | |
| 27 | |
| 28 if (expectedErrorCodes != null) { | |
| 29 listener.expectErrors(expectedErrorCodes); | |
| 30 } | |
| 31 | |
| 32 return statement; | |
| 33 } | |
| 34 | |
| 35 Set<_MapEntry> _getMapEntrySet(Map m) { | |
| 36 var result = new Set(); | |
| 37 m.forEach((k, v) { | |
| 38 result.add(new _MapEntry(k, v)); | |
| 39 }); | |
| 40 return result; | |
| 41 } | |
| 42 | |
| 43 _unsupported() => throw new _UnsupportedOperationException(); | |
| 44 | |
| 45 /// Instances of the class [_GatheringErrorListener] implement an error listener | |
| 46 /// that collects all of the errors passed to it for later examination. | |
| 47 class _GatheringErrorListener implements AnalysisErrorListener { | |
| 48 /// A list containing the errors that were collected. | |
| 49 final List<AnalysisError> _errors = new List<AnalysisError>(); | |
| 50 | |
| 51 /// A table mapping sources to the line information for the source. | |
| 52 final Map<Source, LineInfo> _lineInfoMap = new Map<Source, LineInfo>(); | |
| 53 | |
| 54 /// Asserts that the number of errors that have been gathered matches the | |
| 55 /// number of errors that are given and that they have the expected error | |
| 56 /// codes. The order in which the errors were gathered is ignored. | |
| 57 void expectErrors(List<ErrorCode> expectedErrorCodes) { | |
| 58 var builder = new StringBuffer(); | |
| 59 var expectedCounts = new Map<ErrorCode, int>(); | |
| 60 | |
| 61 for (var code in expectedErrorCodes) { | |
| 62 var count = expectedCounts[code]; | |
| 63 if (count == null) { | |
| 64 count = 1; | |
| 65 } else { | |
| 66 count = count + 1; | |
| 67 } | |
| 68 expectedCounts[code] = count; | |
| 69 } | |
| 70 | |
| 71 var errorsByCode = new Map<ErrorCode, List<AnalysisError>>(); | |
| 72 for (var error in _errors) { | |
| 73 var code = error.errorCode; | |
| 74 var list = errorsByCode[code]; | |
| 75 if (list == null) { | |
| 76 list = new List<AnalysisError>(); | |
| 77 errorsByCode[code] = list; | |
| 78 } | |
| 79 list.add(error); | |
| 80 } | |
| 81 | |
| 82 for (var entry in _getMapEntrySet(expectedCounts)) { | |
| 83 var code = entry.getKey(); | |
| 84 var expectedCount = entry.getValue(); | |
| 85 var actualCount; | |
| 86 | |
| 87 var list = errorsByCode.remove(code); | |
| 88 if (list == null) { | |
| 89 actualCount = 0; | |
| 90 } else { | |
| 91 actualCount = list.length; | |
| 92 } | |
| 93 | |
| 94 if (actualCount != expectedCount) { | |
| 95 if (builder.length == 0) { | |
| 96 builder.write('Expected '); | |
| 97 } else { | |
| 98 builder.write('; '); | |
| 99 } | |
| 100 builder.write(expectedCount); | |
| 101 builder.write(' errors of type '); | |
| 102 builder.write(code); | |
| 103 builder.write(', found '); | |
| 104 builder.write(actualCount); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 for (var entry in _getMapEntrySet(errorsByCode)) { | |
| 109 var code = entry.getKey(); | |
| 110 var actualErrors = entry.getValue(); | |
| 111 var actualCount = actualErrors.length; | |
| 112 | |
| 113 if (builder.length == 0) { | |
| 114 builder.write('Expected '); | |
| 115 } else { | |
| 116 builder.write('; '); | |
| 117 } | |
| 118 | |
| 119 builder.write('0 errors of type '); | |
| 120 builder.write(code); | |
| 121 builder.write(', found '); | |
| 122 builder.write(actualCount); | |
| 123 builder.write(' ('); | |
| 124 | |
| 125 for (int i = 0; i < actualErrors.length; i++) { | |
| 126 var error = actualErrors[i]; | |
| 127 if (i > 0) { | |
| 128 builder.write(', '); | |
| 129 } | |
| 130 builder.write(error.offset); | |
| 131 } | |
| 132 | |
| 133 builder.write(')'); | |
| 134 } | |
| 135 | |
| 136 if (builder.length > 0) { | |
| 137 fail(builder.toString()); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 void onError(AnalysisError error) { | |
| 142 _errors.add(error); | |
| 143 } | |
| 144 | |
| 145 /// Sets the line information associated with the given source to the given | |
| 146 /// information. | |
| 147 void setLineInfo(Source source, List<int> lineStarts) { | |
| 148 _lineInfoMap[source] = new LineInfo(lineStarts); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 class _MapEntry<K, V> { | |
| 153 K _key; | |
| 154 V _value; | |
| 155 _MapEntry(this._key, this._value); | |
| 156 K getKey() => _key; | |
| 157 V getValue() => _value; | |
| 158 } | |
| 159 | |
| 160 class _TestSource extends Source { | |
| 161 TimestampedData<String> get contents => _unsupported(); | |
| 162 | |
| 163 AnalysisContext get context => _unsupported(); | |
| 164 | |
| 165 String get encoding => _unsupported(); | |
| 166 | |
| 167 String get fullName => _unsupported(); | |
| 168 | |
| 169 bool get isInSystemLibrary => _unsupported(); | |
| 170 | |
| 171 int get modificationStamp => _unsupported(); | |
| 172 | |
| 173 String get shortName => _unsupported(); | |
| 174 | |
| 175 Uri get uri => _unsupported(); | |
| 176 | |
| 177 UriKind get uriKind => _unsupported(); | |
| 178 | |
| 179 bool operator ==(Object object) => object is _TestSource; | |
| 180 | |
| 181 bool exists() => true; | |
| 182 | |
| 183 void getContentsToReceiver(Source_ContentReceiver receiver) => _unsupported(); | |
| 184 | |
| 185 Source resolve(String uri) => _unsupported(); | |
| 186 | |
| 187 Uri resolveRelativeUri(Uri uri) => _unsupported(); | |
| 188 } | |
| 189 | |
| 190 class _UnsupportedOperationException implements Exception { | |
| 191 String toString() => 'UnsupportedOperationException'; | |
| 192 } | |
| OLD | NEW |