1 /* 2 * Copyright 2016 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.turbine.parse; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import com.google.common.base.Joiner; 23 import com.google.turbine.diag.SourceFile; 24 import com.google.turbine.diag.TurbineError; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Tests for best-effort parser error handling. */ 30 @RunWith(JUnit4.class) 31 public class ParseErrorTest { 32 33 @Test 34 public void intBound() { 35 StreamLexer lexer = 36 new StreamLexer( 37 new UnicodeEscapePreprocessor(new SourceFile("<>", String.valueOf("2147483648")))); 38 ConstExpressionParser parser = new ConstExpressionParser(lexer, lexer.next()); 39 try { 40 parser.expression(); 41 fail("expected parsing to fail"); 42 } catch (TurbineError e) { 43 assertThat(e.getMessage()).contains("invalid literal"); 44 } 45 } 46 47 @Test 48 public void hexIntBound() { 49 StreamLexer lexer = 50 new StreamLexer( 51 new UnicodeEscapePreprocessor(new SourceFile("<>", String.valueOf("0x100000000")))); 52 ConstExpressionParser parser = new ConstExpressionParser(lexer, lexer.next()); 53 try { 54 parser.expression(); 55 fail("expected parsing to fail"); 56 } catch (TurbineError e) { 57 assertThat(e.getMessage()).contains("invalid literal"); 58 } 59 } 60 61 @Test 62 public void unexpectedTopLevel() { 63 String input = "public static void main(String[] args) {}"; 64 try { 65 Parser.parse(input); 66 fail("expected parsing to fail"); 67 } catch (TurbineError e) { 68 assertThat(e.getMessage()) 69 .isEqualTo( 70 Joiner.on('\n') 71 .join( 72 "<>:1: error: unexpected token: void", 73 "public static void main(String[] args) {}", 74 " ^")); 75 } 76 } 77 78 @Test 79 public void unexpectedIdentifier() { 80 String input = "public clas Test {}"; 81 try { 82 Parser.parse(input); 83 fail("expected parsing to fail"); 84 } catch (TurbineError e) { 85 assertThat(e.getMessage()) 86 .isEqualTo( 87 Joiner.on('\n') 88 .join( 89 "<>:1: error: unexpected identifier 'clas'", // 90 "public clas Test {}", 91 " ^")); 92 } 93 } 94 95 @Test 96 public void missingTrailingCloseBrace() { 97 String input = "public class Test {\n\n"; 98 try { 99 Parser.parse(input); 100 fail("expected parsing to fail"); 101 } catch (TurbineError e) { 102 assertThat(e.getMessage()) 103 .isEqualTo( 104 Joiner.on('\n') 105 .join( 106 "<>:2: error: unexpected end of input", // 107 "", 108 "^")); 109 } 110 } 111 } 112