| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 library test_utils; | 5 library test_utils; |
| 6 | 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'; |
| 7 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 8 | 14 |
| 9 import 'package:analyzer/src/generated/engine.dart'; | 15 |
| 10 import 'package:analyzer/src/generated/source.dart'; | 16 /// Parse the given [source] as a statement and assert, if provided, that |
| 11 import 'package:analyzer/src/generated/error.dart'; | 17 /// exactly a given set of [expectedErrorCodes] are encountered. |
| 12 import 'package:analyzer/src/generated/scanner.dart'; | 18 Statement parseStatement(String source, [List<ErrorCode> expectedErrorCodes]) { |
| 13 import 'package:analyzer/src/generated/ast.dart'; | 19 |
| 14 import 'package:analyzer/src/generated/parser.dart'; | 20 var listener = new _GatheringErrorListener(); |
| 21 var reader = new CharSequenceReader(source); |
| 22 var scanner = new Scanner(null, reader, listener); |
| 23 listener.setLineInfo(new _TestSource(), scanner.lineStarts); |
| 24 |
| 25 var token = scanner.tokenize(); |
| 26 var parser = new Parser(null, listener); |
| 27 var statement = parser.parseStatement(token); |
| 28 expect(statement, isNotNull); |
| 29 |
| 30 if (expectedErrorCodes != null) { |
| 31 listener.expectErrors(expectedErrorCodes); |
| 32 } |
| 33 |
| 34 return statement; |
| 35 } |
| 36 |
| 37 |
| 38 Set<_MapEntry> _getMapEntrySet(Map m) { |
| 39 var result = new Set(); |
| 40 m.forEach((k, v) { |
| 41 result.add(new _MapEntry(k, v)); |
| 42 }); |
| 43 return result; |
| 44 } |
| 45 |
| 46 |
| 47 _unsupported() => throw new _UnsupportedOperationException(); |
| 15 | 48 |
| 16 | 49 |
| 17 /// Instances of the class [_GatheringErrorListener] implement an error listener | 50 /// Instances of the class [_GatheringErrorListener] implement an error listener |
| 18 /// that collects all of the errors passed to it for later examination. | 51 /// that collects all of the errors passed to it for later examination. |
| 19 class _GatheringErrorListener implements AnalysisErrorListener { | 52 class _GatheringErrorListener implements AnalysisErrorListener { |
| 20 /// A list containing the errors that were collected. | 53 /// A list containing the errors that were collected. |
| 21 final List<AnalysisError> _errors = new List<AnalysisError>(); | 54 final List<AnalysisError> _errors = new List<AnalysisError>(); |
| 22 | 55 |
| 23 /// A table mapping sources to the line information for the source. | 56 /// A table mapping sources to the line information for the source. |
| 24 final Map<Source, LineInfo> _lineInfoMap = new Map<Source, LineInfo>(); | 57 final Map<Source, LineInfo> _lineInfoMap = new Map<Source, LineInfo>(); |
| 25 | 58 |
| 26 void onError(AnalysisError error) { | |
| 27 _errors.add(error); | |
| 28 } | |
| 29 | |
| 30 | |
| 31 /// Sets the line information associated with the given source to the given | |
| 32 /// information. | |
| 33 void setLineInfo(Source source, List<int> lineStarts) { | |
| 34 _lineInfoMap[source] = new LineInfo(lineStarts); | |
| 35 } | |
| 36 | |
| 37 | |
| 38 /// Asserts that the number of errors that have been gathered matches the | 59 /// Asserts that the number of errors that have been gathered matches the |
| 39 /// number of errors that are given and that they have the expected error | 60 /// number of errors that are given and that they have the expected error |
| 40 /// codes. The order in which the errors were gathered is ignored. | 61 /// codes. The order in which the errors were gathered is ignored. |
| 41 void expectErrors(List<ErrorCode> expectedErrorCodes) { | 62 void expectErrors(List<ErrorCode> expectedErrorCodes) { |
| 42 var builder = new StringBuffer(); | 63 var builder = new StringBuffer(); |
| 43 var expectedCounts = new Map<ErrorCode, int>(); | 64 var expectedCounts = new Map<ErrorCode, int>(); |
| 44 | 65 |
| 45 for (var code in expectedErrorCodes) { | 66 for (var code in expectedErrorCodes) { |
| 46 var count = expectedCounts[code]; | 67 var count = expectedCounts[code]; |
| 47 if (count == null) { | 68 if (count == null) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 } | 136 } |
| 116 | 137 |
| 117 builder.write(')'); | 138 builder.write(')'); |
| 118 } | 139 } |
| 119 | 140 |
| 120 if (builder.length > 0) { | 141 if (builder.length > 0) { |
| 121 fail(builder.toString()); | 142 fail(builder.toString()); |
| 122 } | 143 } |
| 123 } | 144 } |
| 124 | 145 |
| 146 |
| 147 void onError(AnalysisError error) { |
| 148 _errors.add(error); |
| 149 } |
| 150 |
| 151 |
| 152 /// Sets the line information associated with the given source to the given |
| 153 /// information. |
| 154 void setLineInfo(Source source, List<int> lineStarts) { |
| 155 _lineInfoMap[source] = new LineInfo(lineStarts); |
| 156 } |
| 157 |
| 125 } | 158 } |
| 126 | 159 |
| 127 | 160 |
| 128 Set<_MapEntry> _getMapEntrySet(Map m) { | |
| 129 var result = new Set(); | |
| 130 m.forEach((k, v) { | |
| 131 result.add(new _MapEntry(k, v)); | |
| 132 }); | |
| 133 return result; | |
| 134 } | |
| 135 | |
| 136 | |
| 137 class _MapEntry<K, V> { | 161 class _MapEntry<K, V> { |
| 138 K _key; | 162 K _key; |
| 139 V _value; | 163 V _value; |
| 140 _MapEntry(this._key, this._value); | 164 _MapEntry(this._key, this._value); |
| 141 K getKey() => _key; | 165 K getKey() => _key; |
| 142 V getValue() => _value; | 166 V getValue() => _value; |
| 143 } | 167 } |
| 144 | 168 |
| 169 class _TestSource extends Source { |
| 145 | 170 |
| 146 class _TestSource implements Source { | 171 TimestampedData<String> get contents => _unsupported(); |
| 147 | |
| 148 bool operator == (Object object) => object is _TestSource; | |
| 149 | 172 |
| 150 AnalysisContext get context => _unsupported(); | 173 AnalysisContext get context => _unsupported(); |
| 151 | 174 |
| 152 void getContentsToReceiver(Source_ContentReceiver receiver) => _unsupported(); | 175 String get encoding => _unsupported(); |
| 153 | 176 |
| 154 String get fullName => _unsupported(); | 177 String get fullName => _unsupported(); |
| 155 | 178 |
| 179 bool get isInSystemLibrary => _unsupported(); |
| 180 |
| 181 int get modificationStamp => _unsupported(); |
| 182 |
| 156 String get shortName => _unsupported(); | 183 String get shortName => _unsupported(); |
| 157 | 184 |
| 158 String get encoding => _unsupported(); | 185 Uri get uri => _unsupported(); |
| 159 | |
| 160 int get modificationStamp =>_unsupported(); | |
| 161 | 186 |
| 162 UriKind get uriKind => _unsupported(); | 187 UriKind get uriKind => _unsupported(); |
| 163 | 188 |
| 189 bool operator ==(Object object) => object is _TestSource; |
| 190 |
| 164 bool exists() => true; | 191 bool exists() => true; |
| 165 | 192 |
| 166 bool get isInSystemLibrary => _unsupported(); | 193 void getContentsToReceiver(Source_ContentReceiver receiver) => _unsupported(); |
| 167 | |
| 168 Uri get uri => _unsupported(); | |
| 169 | 194 |
| 170 Source resolve(String uri) => _unsupported(); | 195 Source resolve(String uri) => _unsupported(); |
| 171 | 196 |
| 172 Uri resolveRelativeUri(Uri uri) => _unsupported(); | 197 Uri resolveRelativeUri(Uri uri) => _unsupported(); |
| 173 | |
| 174 TimestampedData<String> get contents => _unsupported(); | |
| 175 } | 198 } |
| 176 | 199 |
| 177 | 200 |
| 178 _unsupported() => throw new _UnsupportedOperationException(); | |
| 179 | |
| 180 class _UnsupportedOperationException implements Exception { | 201 class _UnsupportedOperationException implements Exception { |
| 181 String toString() => 'UnsupportedOperationException'; | 202 String toString() => 'UnsupportedOperationException'; |
| 182 } | 203 } |
| 183 | |
| 184 | |
| 185 /// Parse the given [source] as a statement and assert, if provided, that | |
| 186 /// exactly a given set of [expectedErrorCodes] are encountered. | |
| 187 Statement parseStatement(String source, [List<ErrorCode> expectedErrorCodes]) { | |
| 188 | |
| 189 var listener = new _GatheringErrorListener(); | |
| 190 var reader = new CharSequenceReader(source); | |
| 191 var scanner = new Scanner(null, reader, listener); | |
| 192 listener.setLineInfo(new _TestSource(), scanner.lineStarts); | |
| 193 | |
| 194 var token = scanner.tokenize(); | |
| 195 var parser = new Parser(null, listener); | |
| 196 var statement = parser.parseStatement(token); | |
| 197 expect(statement, isNotNull); | |
| 198 | |
| 199 if (expectedErrorCodes != null) { | |
| 200 listener.expectErrors(expectedErrorCodes); | |
| 201 } | |
| 202 | |
| 203 return statement; | |
| 204 } | |
| OLD | NEW |