OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 package com.google.dart.compiler.common; | 5 package com.google.dart.compiler.common; |
6 | 6 |
7 import com.google.dart.compiler.DartCompilationError; | 7 import com.google.dart.compiler.DartCompilationError; |
8 import com.google.dart.compiler.ErrorCode; | 8 import com.google.dart.compiler.ErrorCode; |
9 | 9 |
10 import junit.framework.Assert; | 10 import junit.framework.Assert; |
(...skipping 10 matching lines...) Expand all Loading... |
21 this.errorCode = errorCode; | 21 this.errorCode = errorCode; |
22 this.line = line; | 22 this.line = line; |
23 this.column = column; | 23 this.column = column; |
24 this.length = length; | 24 this.length = length; |
25 } | 25 } |
26 | 26 |
27 public static ErrorExpectation errEx(ErrorCode errorCode, int line, int column
, int length) { | 27 public static ErrorExpectation errEx(ErrorCode errorCode, int line, int column
, int length) { |
28 return new ErrorExpectation(errorCode, line, column, length); | 28 return new ErrorExpectation(errorCode, line, column, length); |
29 } | 29 } |
30 | 30 |
| 31 public static void formatExpectations(StringBuffer out, List<DartCompilationEr
ror> errors, |
| 32 ErrorExpectation[] expectedErrors) { |
| 33 out.append(String.format("Expected %d errors\n", expectedErrors.length)); |
| 34 for (ErrorExpectation errEx : expectedErrors) { |
| 35 out.append(String.format(" %s (%d,%d/%d)\n", errEx.errorCode.toString(), |
| 36 errEx.line, errEx.column, errEx.length)); |
| 37 } |
| 38 out.append(String.format("Encountered %d errors\n", errors.size())); |
| 39 for (DartCompilationError error : errors) { |
| 40 out.append(String.format(" %s (%d,%d/%d): %s\n", error.getErrorCode().toS
tring(), |
| 41 error.getLineNumber(), error.getColumnNumber(), |
| 42 error.getLength(), error.getMessage())); |
| 43 } |
| 44 } |
31 /** | 45 /** |
32 * Asserts that given list of {@link DartCompilationError} is exactly same as
expected. | 46 * Asserts that given list of {@link DartCompilationError} is exactly same as
expected. |
33 */ | 47 */ |
34 public static void assertErrors(List<DartCompilationError> errors, | 48 public static void assertErrors(List<DartCompilationError> errors, |
35 ErrorExpectation... expectedErrors) { | 49 ErrorExpectation... expectedErrors) { |
36 StringBuffer errorMessage = new StringBuffer(); | 50 StringBuffer errorMessage = new StringBuffer(); |
37 // count of errors | 51 // count of errors |
38 if (errors.size() != expectedErrors.length) { | 52 if (errors.size() != expectedErrors.length) { |
39 String out = | 53 errorMessage.append(String.format("Wrong number of errors encountered\n", |
40 String.format( | 54 expectedErrors.length, |
41 "Expected %s errors, but got %s: %s", | 55 errors.size())); |
42 expectedErrors.length, | 56 |
43 errors.size(), | 57 formatExpectations(errorMessage, errors, expectedErrors); |
44 errors); | |
45 errorMessage.append(out + "\n"); | |
46 } else { | 58 } else { |
47 // content of errors | 59 // content of errors |
48 for (int i = 0; i < expectedErrors.length; i++) { | 60 for (int i = 0; i < expectedErrors.length; i++) { |
49 ErrorExpectation expectedError = expectedErrors[i]; | 61 ErrorExpectation expectedError = expectedErrors[i]; |
50 DartCompilationError actualError = errors.get(i); | 62 DartCompilationError actualError = errors.get(i); |
51 if (actualError.getErrorCode() != expectedError.errorCode | 63 if (actualError.getErrorCode() != expectedError.errorCode |
52 || actualError.getLineNumber() != expectedError.line | 64 || actualError.getLineNumber() != expectedError.line |
53 || actualError.getColumnNumber() != expectedError.column | 65 || actualError.getColumnNumber() != expectedError.column |
54 || actualError.getLength() != expectedError.length) { | 66 || actualError.getLength() != expectedError.length) { |
55 String out = | 67 errorMessage.append(String.format("Expected errors didn't match actual
\n")); |
56 String.format( | 68 formatExpectations(errorMessage, errors, expectedErrors); |
57 "Expected %s:%d:%d/%d, but got %s:%d:%d/%d", | 69 break; |
58 expectedError.errorCode, | |
59 expectedError.line, | |
60 expectedError.column, | |
61 expectedError.length, | |
62 actualError.getErrorCode(), | |
63 actualError.getLineNumber(), | |
64 actualError.getColumnNumber(), | |
65 actualError.getLength()); | |
66 errorMessage.append(out + "\n"); | |
67 } | 70 } |
68 } | 71 } |
69 } | 72 } |
70 // fail | 73 // fail |
71 if (errorMessage.length() > 0) { | 74 if (errorMessage.length() > 0) { |
72 System.err.println(errorMessage); | 75 System.err.println(errorMessage); |
73 Assert.fail(errorMessage.toString()); | 76 Assert.fail(errorMessage.toString()); |
74 } | 77 } |
75 } | 78 } |
76 } | 79 } |
OLD | NEW |