Chromium Code Reviews| 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 |
|
Brian Wilkerson
2012/07/12 15:35:11
nit: copyright year
| |
| 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 package com.google.dart.compiler; | 4 package com.google.dart.compiler; |
| 5 | 5 |
| 6 import com.google.common.base.Joiner; | 6 import com.google.common.base.Joiner; |
| 7 import com.google.dart.compiler.CompilerConfiguration.ErrorFormat; | 7 import com.google.dart.compiler.CompilerConfiguration.ErrorFormat; |
| 8 import com.google.dart.compiler.parser.DartScanner.Location; | 8 import com.google.dart.compiler.parser.DartScanner.Location; |
| 9 import com.google.dart.compiler.parser.DartScanner.Position; | |
| 10 import com.google.dart.compiler.resolver.ResolverErrorCode; | 9 import com.google.dart.compiler.resolver.ResolverErrorCode; |
| 11 import com.google.dart.compiler.resolver.TypeErrorCode; | 10 import com.google.dart.compiler.resolver.TypeErrorCode; |
| 12 | 11 |
| 12 import com.sun.tools.javac.util.Position; | |
| 13 | |
| 13 import junit.framework.TestCase; | 14 import junit.framework.TestCase; |
| 14 | 15 |
| 15 import java.io.ByteArrayOutputStream; | 16 import java.io.ByteArrayOutputStream; |
| 16 import java.io.IOException; | 17 import java.io.IOException; |
| 17 import java.io.PrintStream; | 18 import java.io.PrintStream; |
| 18 import java.io.Reader; | 19 import java.io.Reader; |
| 19 | 20 |
| 20 /** | 21 /** |
| 21 * Test for {@link PrettyErrorFormatter}. | 22 * Test for {@link PrettyErrorFormatter}. |
| 22 */ | 23 */ |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 34 /** | 35 /** |
| 35 * Not a {@link DartSource}, rollback to {@link DefaultErrorFormatter}. | 36 * Not a {@link DartSource}, rollback to {@link DefaultErrorFormatter}. |
| 36 */ | 37 */ |
| 37 public void test_notDartSource() throws Exception { | 38 public void test_notDartSource() throws Exception { |
| 38 Source emptyDartSource = new SourceTest("Test.dart") { | 39 Source emptyDartSource = new SourceTest("Test.dart") { |
| 39 @Override | 40 @Override |
| 40 public Reader getSourceReader() { | 41 public Reader getSourceReader() { |
| 41 return null; | 42 return null; |
| 42 } | 43 } |
| 43 }; | 44 }; |
| 44 Location location = new Location(new Position(-1, 1, 3)); | 45 Location location = new Location(2); |
| 45 DartCompilationError error = | 46 DartCompilationError error = |
| 46 new DartCompilationError(emptyDartSource, location, TypeErrorCode.NO_SUC H_TYPE, "Foo"); | 47 new DartCompilationError(emptyDartSource, location, TypeErrorCode.NO_SUC H_TYPE, "Foo"); |
| 47 // | 48 // |
| 48 String errorString = getErrorString(error, false, false); | 49 String errorString = getErrorString(error, false, false); |
| 49 assertEquals("Test.dart:1:3: no such type \"Foo\"\n", errorString); | 50 assertEquals("Test.dart:1:3: no such type \"Foo\"\n", errorString); |
| 50 } | 51 } |
| 51 | 52 |
| 52 /** | 53 /** |
| 53 * Use {@link DartSource} with source {@link Reader} which throws {@link IOExc eption}, rollback to | 54 * Use {@link DartSource} with source {@link Reader} which throws {@link IOExc eption}, rollback to |
| 54 * {@link DefaultErrorFormatter}. | 55 * {@link DefaultErrorFormatter}. |
| 55 */ | 56 */ |
| 56 public void test_throwsIOException() throws Exception { | 57 public void test_throwsIOException() throws Exception { |
| 57 Source badDartSource = new DartSourceTest("my/path/Test.dart", "", new MockL ibrarySource()) { | 58 Source badDartSource = new DartSourceTest("my/path/Test.dart", "", new MockL ibrarySource()) { |
| 58 @Override | 59 @Override |
| 59 public Reader getSourceReader() { | 60 public Reader getSourceReader() { |
| 60 return new Reader() { | 61 return new Reader() { |
| 61 @Override | 62 @Override |
| 62 public int read(char[] cbuf, int off, int len) throws IOException { | 63 public int read(char[] cbuf, int off, int len) throws IOException { |
| 63 throw new IOException("boo!"); | 64 throw new IOException("boo!"); |
| 64 } | 65 } |
| 65 | 66 |
| 66 @Override | 67 @Override |
| 67 public void close() { | 68 public void close() { |
| 68 } | 69 } |
| 69 }; | 70 }; |
| 70 } | 71 } |
| 71 }; | 72 }; |
| 72 Location location = new Location(new Position(-1, 1, 3)); | 73 Location location = new Location(2); |
| 73 DartCompilationError error = | 74 DartCompilationError error = |
| 74 new DartCompilationError(badDartSource, location, TypeErrorCode.NO_SUCH_ TYPE, "Foo"); | 75 new DartCompilationError(badDartSource, location, TypeErrorCode.NO_SUCH_ TYPE, "Foo"); |
| 75 // | 76 // |
| 76 String errorString = getErrorString(error, false, false); | 77 String errorString = getErrorString(error, false, false); |
| 77 assertEquals( | 78 assertEquals( |
| 78 "my/path/Test.dart:1:3: no such type \"Foo\" (sourced from Test_app)\n", | 79 "my/path/Test.dart:1:3: no such type \"Foo\" (sourced from Test_app)\n", |
| 79 errorString); | 80 errorString); |
| 80 } | 81 } |
| 81 | 82 |
| 82 /** | 83 /** |
| 83 * Empty {@link DartSource}, rollback to {@link DefaultErrorFormatter}. | 84 * Empty {@link DartSource}, rollback to {@link DefaultErrorFormatter}. |
| 84 */ | 85 */ |
| 85 public void test_emptyDartSource() throws Exception { | 86 public void test_emptyDartSource() throws Exception { |
| 86 Source emptyDartSource = new DartSourceTest("my/path/Test.dart", "", new Moc kLibrarySource()); | 87 Source emptyDartSource = new DartSourceTest("my/path/Test.dart", "", new Moc kLibrarySource()); |
| 87 Location location = new Location(new Position(-1, 1, 3)); | 88 Location location = new Location(2); |
| 88 DartCompilationError error = | 89 DartCompilationError error = |
| 89 new DartCompilationError(emptyDartSource, location, TypeErrorCode.NO_SUC H_TYPE, "Foo"); | 90 new DartCompilationError(emptyDartSource, location, TypeErrorCode.NO_SUC H_TYPE, "Foo"); |
| 90 // | 91 // |
| 91 String errorString = getErrorString(error, false, false); | 92 String errorString = getErrorString(error, false, false); |
| 92 assertEquals( | 93 assertEquals( |
| 93 "my/path/Test.dart:1:3: no such type \"Foo\" (sourced from Test_app)\n", | 94 "my/path/Test.dart:1:3: no such type \"Foo\" (sourced from Test_app)\n", |
| 94 errorString); | 95 errorString); |
| 95 } | 96 } |
| 96 | 97 |
| 97 /** | 98 /** |
| 98 * Error on first line, so no previous line printed. | 99 * Error on first line, so no previous line printed. |
| 99 */ | 100 */ |
| 100 public void test_noColor_notMachine_firstLine() throws Exception { | 101 public void test_noColor_notMachine_firstLine() throws Exception { |
| 101 Location location = new Location(new Position(3, 1, 3), new Position(6, 1, 6 )); | 102 Location location = new Location(2, 5); |
| 102 DartCompilationError error = | 103 DartCompilationError error = |
| 103 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); | 104 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); |
| 104 // | 105 // |
| 105 String errorString = getErrorString(error, false, false); | 106 String errorString = getErrorString(error, false, false); |
| 106 assertEquals( | 107 assertEquals( |
| 107 Joiner.on("\n").join( | 108 Joiner.on("\n").join( |
| 108 "my/path/Test.dart:1: no such type \"Foo\" (sourced from Test_app)", | 109 "my/path/Test.dart:1: no such type \"Foo\" (sourced from Test_app)", |
| 109 " 1: lineAAA", | 110 " 1: lineAAA", |
| 110 " ~~~", | 111 " ~~~", |
| 111 ""), | 112 ""), |
| 112 errorString); | 113 errorString); |
| 113 } | 114 } |
| 114 | 115 |
| 115 /** | 116 /** |
| 116 * {@link Location} with single {@link Position}, underline single character. | 117 * {@link Location} with single {@link Position}, underline single character. |
| 117 */ | 118 */ |
| 118 public void test_noColor_notMachine_singlePosition() throws Exception { | 119 public void test_noColor_notMachine_singlePosition() throws Exception { |
| 119 Location location = new Location(new Position(-1, 2, 3)); | 120 Location location = new Location(10); |
| 120 DartCompilationError error = | 121 DartCompilationError error = |
| 121 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); | 122 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); |
| 122 // | 123 // |
| 123 String errorString = getErrorString(error, false, false); | 124 String errorString = getErrorString(error, false, false); |
| 124 assertEquals( | 125 assertEquals( |
| 125 Joiner.on("\n").join( | 126 Joiner.on("\n").join( |
| 126 "my/path/Test.dart:2: no such type \"Foo\" (sourced from Test_app)", | 127 "my/path/Test.dart:2: no such type \"Foo\" (sourced from Test_app)", |
| 127 " 1: lineAAA", | 128 " 1: lineAAA", |
| 128 " 2: lineBBB", | 129 " 2: lineBBB", |
| 129 " ~~~~~", | 130 " ~~~~~", |
| 130 ""), | 131 ""), |
| 131 errorString); | 132 errorString); |
| 132 } | 133 } |
| 133 | 134 |
| 134 /** | 135 /** |
| 135 * {@link Location} with single {@link Position}, underline single character. | 136 * {@link Location} with single {@link Position}, underline single character. |
| 136 */ | 137 */ |
| 137 public void test_withColor_notMachine_singlePosition() throws Exception { | 138 public void test_withColor_notMachine_singlePosition() throws Exception { |
| 138 Location location = new Location(new Position(-1, 2, 3)); | 139 Location location = new Location(10); |
| 139 DartCompilationError error = | 140 DartCompilationError error = |
| 140 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); | 141 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); |
| 141 String errorString = getErrorString(error, true, false); | 142 String errorString = getErrorString(error, true, false); |
| 142 assertEquals( | 143 assertEquals( |
| 143 Joiner.on("\n").join( | 144 Joiner.on("\n").join( |
| 144 WARNING_BOLD_COLOR | 145 WARNING_BOLD_COLOR |
| 145 + "my/path/Test.dart:2: no such type \"Foo\" (sourced from Test_ app)" | 146 + "my/path/Test.dart:2: no such type \"Foo\" (sourced from Test_ app)" |
| 146 + NO_COLOR, | 147 + NO_COLOR, |
| 147 " 1: lineAAA", | 148 " 1: lineAAA", |
| 148 " 2: li" + WARNING_COLOR + "neBBB" + NO_COLOR, | 149 " 2: li" + WARNING_COLOR + "neBBB" + NO_COLOR, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 159 " 1: lineAAA", | 160 " 1: lineAAA", |
| 160 " 2: li" + ERROR_COLOR + "neBBB" + NO_COLOR, | 161 " 2: li" + ERROR_COLOR + "neBBB" + NO_COLOR, |
| 161 ""), | 162 ""), |
| 162 errorString); | 163 errorString); |
| 163 } | 164 } |
| 164 | 165 |
| 165 /** | 166 /** |
| 166 * Underline range of characters. | 167 * Underline range of characters. |
| 167 */ | 168 */ |
| 168 public void test_noColor_notMachine() throws Exception { | 169 public void test_noColor_notMachine() throws Exception { |
| 169 Location location = new Location(new Position(8 + 3, 2, 3), new Position(8 + 6, 2, 6)); | 170 Location location = new Location(10, 10 + 3); |
| 170 DartCompilationError error = | 171 DartCompilationError error = |
| 171 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); | 172 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); |
| 172 // | 173 // |
| 173 String errorString = getErrorString(error, false, false); | 174 String errorString = getErrorString(error, false, false); |
| 174 assertEquals( | 175 assertEquals( |
| 175 Joiner.on("\n").join( | 176 Joiner.on("\n").join( |
| 176 "my/path/Test.dart:2: no such type \"Foo\" (sourced from Test_app)", | 177 "my/path/Test.dart:2: no such type \"Foo\" (sourced from Test_app)", |
| 177 " 1: lineAAA", | 178 " 1: lineAAA", |
| 178 " 2: lineBBB", | 179 " 2: lineBBB", |
| 179 " ~~~", | 180 " ~~~", |
| 180 ""), | 181 ""), |
| 181 errorString); | 182 errorString); |
| 182 } | 183 } |
| 183 | 184 |
| 184 /** | 185 /** |
| 185 * Use color to highlight range of characters. | 186 * Use color to highlight range of characters. |
| 186 */ | 187 */ |
| 187 public void test_withColor_notMachine() throws Exception { | 188 public void test_withColor_notMachine() throws Exception { |
| 188 Location location = new Location(new Position(8 + 3, 2, 3), new Position(8 + 6, 2, 6)); | 189 Location location = new Location(10, 10 + 3); |
| 189 DartCompilationError error = | 190 DartCompilationError error = |
| 190 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); | 191 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); |
| 191 String errorString = getErrorString(error, true, false); | 192 String errorString = getErrorString(error, true, false); |
| 192 assertEquals( | 193 assertEquals( |
| 193 Joiner.on("\n").join( | 194 Joiner.on("\n").join( |
| 194 WARNING_BOLD_COLOR | 195 WARNING_BOLD_COLOR |
| 195 + "my/path/Test.dart:2: no such type \"Foo\" (sourced from Test_ app)" | 196 + "my/path/Test.dart:2: no such type \"Foo\" (sourced from Test_ app)" |
| 196 + NO_COLOR, | 197 + NO_COLOR, |
| 197 " 1: lineAAA", | 198 " 1: lineAAA", |
| 198 " 2: li" + WARNING_COLOR + "neB" + NO_COLOR + "BB", | 199 " 2: li" + WARNING_COLOR + "neB" + NO_COLOR + "BB", |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 209 " 1: lineAAA", | 210 " 1: lineAAA", |
| 210 " 2: li" + ERROR_COLOR + "neB" + NO_COLOR + "BB", | 211 " 2: li" + ERROR_COLOR + "neB" + NO_COLOR + "BB", |
| 211 ""), | 212 ""), |
| 212 errorString); | 213 errorString); |
| 213 } | 214 } |
| 214 | 215 |
| 215 /** | 216 /** |
| 216 * Include all information about error context. | 217 * Include all information about error context. |
| 217 */ | 218 */ |
| 218 public void test_noColor_forMachine() throws Exception { | 219 public void test_noColor_forMachine() throws Exception { |
| 219 Location location = new Location(new Position(8 + 3, 2, 3), new Position(8 + 7, 2, 7)); | 220 Location location = new Location(10, 10 + 4); |
| 220 DartCompilationError error = | 221 DartCompilationError error = |
| 221 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); | 222 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); |
| 222 // | 223 // |
| 223 String errorString = getErrorString(error, false, true); | 224 String errorString = getErrorString(error, false, true); |
| 224 assertEquals( | 225 assertEquals( |
| 225 Joiner.on("\n").join( | 226 Joiner.on("\n").join( |
| 226 "WARNING|STATIC_TYPE|NO_SUCH_TYPE|my/path/Test.dart|2|3|4|no such ty pe \"Foo\"", | 227 "WARNING|STATIC_TYPE|NO_SUCH_TYPE|my/path/Test.dart|2|3|4|no such ty pe \"Foo\"", |
| 227 " 1: lineAAA", | 228 " 1: lineAAA", |
| 228 " 2: lineBBB", | 229 " 2: lineBBB", |
| 229 " ~~~~", | 230 " ~~~~", |
| 230 ""), | 231 ""), |
| 231 errorString); | 232 errorString); |
| 232 } | 233 } |
| 233 | 234 |
| 234 /** | 235 /** |
| 235 * Use color to highlight range of characters. Include all information about e rror context. | 236 * Use color to highlight range of characters. Include all information about e rror context. |
| 236 */ | 237 */ |
| 237 public void test_withColor_forMachine() throws Exception { | 238 public void test_withColor_forMachine() throws Exception { |
| 238 Location location = new Location(new Position(8 + 3, 2, 3), new Position(8 + 7, 2, 7)); | 239 Location location = new Location(10, 10 + 4); |
| 239 DartCompilationError error = | 240 DartCompilationError error = |
| 240 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); | 241 new DartCompilationError(SOURCE, location, TypeErrorCode.NO_SUCH_TYPE, " Foo"); |
| 241 String errorString = getErrorString(error, true, true); | 242 String errorString = getErrorString(error, true, true); |
| 242 assertEquals( | 243 assertEquals( |
| 243 Joiner.on("\n").join( | 244 Joiner.on("\n").join( |
| 244 WARNING_BOLD_COLOR | 245 WARNING_BOLD_COLOR |
| 245 + "WARNING|STATIC_TYPE|NO_SUCH_TYPE|my/path/Test.dart|2|3|4|no s uch type \"Foo\"" | 246 + "WARNING|STATIC_TYPE|NO_SUCH_TYPE|my/path/Test.dart|2|3|4|no s uch type \"Foo\"" |
| 246 + NO_COLOR, | 247 + NO_COLOR, |
| 247 " 1: lineAAA", | 248 " 1: lineAAA", |
| 248 " 2: li" + WARNING_COLOR + "neBB" + NO_COLOR + "B", | 249 " 2: li" + WARNING_COLOR + "neBB" + NO_COLOR + "B", |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 271 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | 272 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 272 PrintStream printStream = new PrintStream(outputStream); | 273 PrintStream printStream = new PrintStream(outputStream); |
| 273 ErrorFormatter errorFormatter = | 274 ErrorFormatter errorFormatter = |
| 274 new PrettyErrorFormatter(printStream, useColor, printMachineProblems | 275 new PrettyErrorFormatter(printStream, useColor, printMachineProblems |
| 275 ? ErrorFormat.MACHINE | 276 ? ErrorFormat.MACHINE |
| 276 : ErrorFormat.NORMAL); | 277 : ErrorFormat.NORMAL); |
| 277 errorFormatter.format(error); | 278 errorFormatter.format(error); |
| 278 return outputStream.toString(); | 279 return outputStream.toString(); |
| 279 } | 280 } |
| 280 } | 281 } |
| OLD | NEW |