| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.collect.ImmutableList; |
| 20 |
import com.google.common.collect.Iterables; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import junit.framework.TestCase; |
| 23 |
|
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class RhinoErrorReporterTest extends TestCase { |
| 31 |
|
| 32 |
private boolean reportMisplacedTypeAnnotations; |
| 33 |
|
| 34 |
@Override |
| 35 |
protected void setUp() throws Exception { |
| 36 |
reportMisplacedTypeAnnotations = false; |
| 37 |
super.setUp(); |
| 38 |
} |
| 39 |
|
| 40 |
public void testTrailingComma() throws Exception { |
| 41 |
String message = |
| 42 |
"Parse error. IE8 (and below) will parse trailing commas in " + |
| 43 |
"array and object literals incorrectly. " + |
| 44 |
"If you are targeting newer versions of JS, " + |
| 45 |
"set the appropriate language_in option."; |
| 46 |
assertError( |
| 47 |
"var x = [1,];", |
| 48 |
RhinoErrorReporter.TRAILING_COMMA, |
| 49 |
message); |
| 50 |
JSError error = assertError( |
| 51 |
"var x = {\n" + |
| 52 |
" 1: 2,\n" + |
| 53 |
"};", |
| 54 |
RhinoErrorReporter.TRAILING_COMMA, |
| 55 |
message); |
| 56 |
|
| 57 |
assertEquals(2, error.getLineNumber()); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
assertEquals(4, error.getCharno()); |
| 62 |
} |
| 63 |
|
| 64 |
public void testMisplacedTypeAnnotation() throws Exception { |
| 65 |
reportMisplacedTypeAnnotations = false; |
| 66 |
|
| 67 |
assertNoWarningOrError("var x = /** @type {string} */ y;"); |
| 68 |
|
| 69 |
reportMisplacedTypeAnnotations = true; |
| 70 |
|
| 71 |
String message = |
| 72 |
"Type annotations are not allowed here. " + |
| 73 |
"Are you missing parentheses?"; |
| 74 |
assertWarning( |
| 75 |
"var x = /** @type {string} */ y;", |
| 76 |
RhinoErrorReporter.MISPLACED_TYPE_ANNOTATION, |
| 77 |
message); |
| 78 |
JSError error = assertWarning( |
| 79 |
"var x = /** @type {string} */ y;", |
| 80 |
RhinoErrorReporter.MISPLACED_TYPE_ANNOTATION, |
| 81 |
message); |
| 82 |
|
| 83 |
assertEquals(1, error.getLineNumber()); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
assertEquals(0, error.getCharno()); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
private void assertNoWarningOrError(String code) { |
| 94 |
Compiler compiler = parseCode(code); |
| 95 |
assertEquals("Expected error", 0, compiler.getErrorCount()); |
| 96 |
assertEquals("Expected warning", 0, compiler.getErrorCount()); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private JSError assertError( |
| 103 |
String code, DiagnosticType type, String description) { |
| 104 |
Compiler compiler = parseCode(code); |
| 105 |
assertEquals("Expected error", 1, compiler.getErrorCount()); |
| 106 |
|
| 107 |
JSError error = |
| 108 |
Iterables.getOnlyElement(Lists.newArrayList(compiler.getErrors())); |
| 109 |
assertEquals(type, error.getType()); |
| 110 |
assertEquals(description, error.description); |
| 111 |
return error; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
private JSError assertWarning( |
| 118 |
String code, DiagnosticType type, String description) { |
| 119 |
Compiler compiler = parseCode(code); |
| 120 |
assertEquals("Expected warning", 1, compiler.getWarningCount()); |
| 121 |
|
| 122 |
JSError error = |
| 123 |
Iterables.getOnlyElement(Lists.newArrayList(compiler.getWarnings())); |
| 124 |
assertEquals(type, error.getType()); |
| 125 |
assertEquals(description, error.description); |
| 126 |
return error; |
| 127 |
} |
| 128 |
|
| 129 |
private Compiler parseCode(String code) { |
| 130 |
Compiler compiler = new Compiler(); |
| 131 |
CompilerOptions options = new CompilerOptions(); |
| 132 |
if (reportMisplacedTypeAnnotations) { |
| 133 |
options.setWarningLevel( |
| 134 |
DiagnosticGroups.MISPLACED_TYPE_ANNOTATION, |
| 135 |
CheckLevel.WARNING); |
| 136 |
} |
| 137 |
|
| 138 |
List<SourceFile> externs = ImmutableList.of(); |
| 139 |
List<SourceFile> inputs = ImmutableList.of( |
| 140 |
SourceFile.fromCode("input", code)); |
| 141 |
compiler.init(externs, inputs, options); |
| 142 |
compiler.parseInputs(); |
| 143 |
return compiler; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
} |