aboutsummaryrefslogtreecommitdiff
path: root/javatests/com/google/turbine/parse/ParseErrorTest.java
blob: eeb39239324760e4e3c41f0f623b601174cc7341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.turbine.parse;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Joiner;
import com.google.turbine.diag.SourceFile;
import com.google.turbine.diag.TurbineError;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for best-effort parser error handling. */
@RunWith(JUnit4.class)
public class ParseErrorTest {

  @Test
  public void intBound() {
    StreamLexer lexer =
        new StreamLexer(
            new UnicodeEscapePreprocessor(new SourceFile("<>", String.valueOf("2147483648"))));
    ConstExpressionParser parser = new ConstExpressionParser(lexer, lexer.next());
    TurbineError e = assertThrows(TurbineError.class, () -> parser.expression());
    assertThat(e).hasMessageThat().contains("invalid literal");
  }

  @Test
  public void hexIntBound() {
    StreamLexer lexer =
        new StreamLexer(
            new UnicodeEscapePreprocessor(new SourceFile("<>", String.valueOf("0x100000000"))));
    ConstExpressionParser parser = new ConstExpressionParser(lexer, lexer.next());
    TurbineError e = assertThrows(TurbineError.class, () -> parser.expression());
    assertThat(e).hasMessageThat().contains("invalid literal");
  }

  @Test
  public void unexpectedTopLevel() {
    String input = "public static void main(String[] args) {}";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unexpected token: void",
                "public static void main(String[] args) {}",
                "              ^"));
  }

  @Test
  public void unexpectedIdentifier() {
    String input = "public clas Test {}";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unexpected identifier 'clas'", //
                "public clas Test {}",
                "       ^"));
  }

  @Test
  public void missingTrailingCloseBrace() {
    String input = "public class Test {\n\n";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:2: error: unexpected end of input", //
                "",
                "^"));
  }

  @Test
  public void annotationArgument() {
    String input = "@A(x = System.err.println()) class Test {}\n";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: invalid annotation argument", //
                "@A(x = System.err.println()) class Test {}",
                "                         ^"));
  }

  @Test
  public void dropParens() {
    String input = "enum E { ONE(";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unexpected end of input", //
                "enum E { ONE(",
                "            ^"));
  }

  @Test
  public void dropBlocks() {
    String input = "class T { Object f = new Object() {";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unexpected end of input", //
                "class T { Object f = new Object() {",
                "                                  ^"));
  }

  @Test
  public void unterminatedString() {
    String input = "class T { String s = \"hello\nworld\"; }";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unterminated string literal", //
                "class T { String s = \"hello",
                "                           ^"));
  }

  @Test
  public void emptyChar() {
    String input = "class T { char c = ''; }";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: empty char literal", //
                "class T { char c = ''; }",
                "                    ^"));
  }

  @Test
  public void unterminatedChar() {
    String input = "class T { char c = '; }";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unterminated char literal", //
                "class T { char c = '; }",
                "                     ^"));
  }

  @Test
  public void unterminatedExpr() {
    String input = "class T { String s = hello + world }";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unterminated expression, expected ';' not found", //
                "class T { String s = hello + world }",
                "                     ^"));
  }

  @Test
  public void abruptMultivariableDeclaration() {
    String input = "class T { int x,; }";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: expected token <identifier>", //
                "class T { int x,; }",
                "                ^"));
  }

  @Test
  public void invalidAnnotation() {
    String input = "@Foo(x =  @E [] x) class T {}";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: invalid annotation argument", //
                "@Foo(x =  @E [] x) class T {}",
                "                ^"));
  }

  @Test
  public void unclosedComment() {
    String input = "/** *\u001a/ class Test {}";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unclosed comment", //
                "/** *\u001a/ class Test {}",
                "^"));
  }

  @Test
  public void unclosedGenerics() {
    String input = "enum\te{l;p u@.<@";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unexpected end of input", //
                "enum\te{l;p u@.<@",
                "               ^"));
  }

  @Test
  public void arrayDot() {
    String input = "enum\te{p;ullt[].<~>>>L\0";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unexpected token: <", //
                "enum\te{p;ullt[].<~>>>L\0",
                "                ^"));
  }

  @Test
  public void implementsBeforeExtends() {
    String input = "class T implements A extends B {}";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: 'extends' must come before 'implements'",
                "class T implements A extends B {}",
                "                     ^"));
  }

  @Test
  public void unpairedSurrogate() {
    String input = "import pkg\uD800.PackageTest;";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unpaired surrogate 0xd800",
                "import pkg\uD800.PackageTest;",
                "           ^"));
  }

  @Test
  public void abruptSurrogate() {
    String input = "import pkg\uD800";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines("<>:1: error: unpaired surrogate 0xd800", "import pkg\uD800", "          ^"));
  }

  @Test
  public void unexpectedSurrogate() {
    String input = "..\uD800\uDC00";
    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            lines(
                "<>:1: error: unexpected input: U+10000", //
                "..\uD800\uDC00",
                "   ^"));
  }

  private static String lines(String... lines) {
    return Joiner.on(System.lineSeparator()).join(lines);
  }
}