• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.collect.Iterables.getOnlyElement;
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.junit.Assert.assertThrows;
22 
23 import com.google.turbine.diag.SourceFile;
24 import com.google.turbine.diag.TurbineError;
25 import com.google.turbine.diag.TurbineError.ErrorKind;
26 import java.util.ArrayList;
27 import java.util.List;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 
32 @RunWith(JUnit4.class)
33 public class UnicodeEscapePreprocessorTest {
34 
35   @Test
escape()36   public void escape() {
37     // note that the backslashes have to be escaped
38     assertThat(readAll("\\\\u2122=\\u2122"))
39         .containsExactly('\\', '\\', 'u', '2', '1', '2', '2', '=', '™');
40     assertThat(readAll("\\uD83D\\uDCA9")).containsExactly('\uD83D', '\uDCA9');
41   }
42 
43   @Test
escapeOdd()44   public void escapeOdd() {
45     assertThat(readAll("\\\\\\u2122")).containsExactly('\\', '\\', '™');
46   }
47 
48   @Test
escapeNoSlash()49   public void escapeNoSlash() {
50     assertThat(readAll("\\u005cu005a")).containsExactly('\\', 'u', '0', '0', '5', 'a');
51   }
52 
53   @Test
escapeExtraU()54   public void escapeExtraU() {
55     assertThat(readAll("\\uuuuuuu2122")).containsExactly('™');
56   }
57 
58   @Test
abruptEnd()59   public void abruptEnd() {
60     TurbineError e = assertThrows(TurbineError.class, () -> readAll("\\u00"));
61     assertThat(getOnlyElement(e.diagnostics()).kind()).isEqualTo(ErrorKind.UNEXPECTED_EOF);
62 
63     e = assertThrows(TurbineError.class, () -> readAll("\\u"));
64     assertThat(getOnlyElement(e.diagnostics()).kind()).isEqualTo(ErrorKind.UNEXPECTED_EOF);
65   }
66 
67   @Test
escapeEscape()68   public void escapeEscape() {
69     assertThat(readAll("\\u005C\\\\u005C")).containsExactly('\\', '\\', '\\');
70   }
71 
72   @Test
invalidEscape()73   public void invalidEscape() {
74     TurbineError e = assertThrows(TurbineError.class, () -> readAll("\\uUUUU"));
75     assertThat(getOnlyElement(e.diagnostics()).kind()).isEqualTo(ErrorKind.INVALID_UNICODE);
76   }
77 
readAll(String input)78   private List<Character> readAll(String input) {
79     UnicodeEscapePreprocessor reader = new UnicodeEscapePreprocessor(new SourceFile(null, input));
80     List<Character> result = new ArrayList<>();
81     for (int ch = reader.next(); ch != UnicodeEscapePreprocessor.ASCII_SUB; ch = reader.next()) {
82       assertThat(Character.isBmpCodePoint(ch)).isTrue();
83       result.add((char) ch);
84     }
85     return result;
86   }
87 }
88