Skip to content

Commit

Permalink
Re-implemented unicode preprocessor, see #1930.
Browse files Browse the repository at this point in the history
  • Loading branch information
haumacher committed Jun 12, 2019
1 parent 1645338 commit 5ef8d93
Show file tree
Hide file tree
Showing 2 changed files with 407 additions and 365 deletions.
@@ -1,36 +1,141 @@
/*
* Copyright (C) 2019 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser; package com.github.javaparser;


import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;

import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;


import static org.junit.jupiter.api.Assertions.assertEquals; /**
import static org.junit.jupiter.api.Assertions.assertThrows; * Test case for {@link UnicodeEscapeProcessingProvider}.

*/
class UnicodeEscapeProcessingProviderTest { public class UnicodeEscapeProcessingProviderTest {
@Test
void readABadUnicodeEscape() { @Test
UnicodeEscapeProcessingProvider provider = new UnicodeEscapeProcessingProvider(new StringProvider("13\\u12")); void testUnicodeEscape() throws IOException {
char[] buffer = new char[10]; assertEquals("13" + '\u12aA' + "98", new String(read("13\\u12aA98")));
assertThrows(RuntimeException.class, () -> provider.read(buffer, 0, buffer.length)); }
}

@Test
@Test void testEscapedUnicodeEscape() throws IOException {
void readSomethingWithAUnicodeEscape() { assertEquals("13\\\\u12aA98", new String(read("13\\\\u12aA98")));
UnicodeEscapeProcessingProvider provider = new UnicodeEscapeProcessingProvider(new StringProvider("13\\u123498")); }
char[] buffer = new char[10];
int read = provider.read(buffer, 0, buffer.length); @Test

void testUnicodeEscapeWithMultipleUs() throws IOException {
assertEquals(5, read); assertEquals("13" + '\u12aA' + "98", new String(read("13\\uuuuuu12aA98")));
assertEquals("13ሴ98\0\0\0\0\0", new String(buffer)); }
}

@Test
@Test void testInputEndingInBackslash() throws IOException {
void readFromAnEmptyProvider() { assertEquals("foobar\\", new String(read("foobar\\")));
UnicodeEscapeProcessingProvider provider = new UnicodeEscapeProcessingProvider(new StringProvider("")); }
char[] buffer = new char[10];
int read = provider.read(buffer, 0, buffer.length); @Test

void testInputEndingInBackslashU() throws IOException {
assertEquals(-1, read); assertEquals("foobar\\u", new String(read("foobar\\u")));
assertEquals("\0\0\0\0\0\0\0\0\0\0", new String(buffer)); }
}
} @Test
void testInputEndingInBackslashUs() throws IOException {
assertEquals("foobar\\uuuuuu", new String(read("foobar\\uuuuuu")));
}

@Test
void testInputEndingInBackslashU1() throws IOException {
assertEquals("foobar\\uA", new String(read("foobar\\uA")));
}

@Test
void testInputEndingInBackslashU2() throws IOException {
assertEquals("foobar\\uAB", new String(read("foobar\\uAB")));
}

@Test
void testInputEndingInBackslashU3() throws IOException {
assertEquals("foobar\\uABC", new String(read("foobar\\uABC")));
}

@Test
void testInputEndingUnicodeEscape() throws IOException {
assertEquals("foobar\uABCD", new String(read("foobar\\uABCD")));
}

@Test
void testEmptyInput() throws IOException {
assertEquals("", new String(read("")));
}


@Test
void testBadUnicodeEscape0() throws IOException {
assertEquals("13\\ux", new String(read("13\\ux")));
}

@Test
void testBadUnicodeEscape1() throws IOException {
assertEquals("13\\u1x", new String(read("13\\u1x")));
}

@Test
void testBadUnicodeEscape2() throws IOException {
assertEquals("13\\u1Ax", new String(read("13\\u1Ax")));
}

@Test
void testBadUnicodeEscape3() throws IOException {
assertEquals("13\\u1ABx", new String(read("13\\u1ABx")));
}

@Test
void testBadUnicodeEscapeMultipleUs() throws IOException {
assertEquals("13\\uuuuuu1ABx", new String(read("13\\uuuuuu1ABx")));
}

@Test
void testPushBackWithFullBuffer() throws IOException {
assertEquals("12345678\\uuxxxxxxxxxxxxxxxxxxxxxxx", new String(read("12345678\\uuxxxxxxxxxxxxxxxxxxxxxxx")));
}

@Test
void testPushBackWithBufferShift() throws IOException {
assertEquals("12345678\\uuxx", new String(read("12345678\\uuxx")));
}

private String read(String source) throws IOException {
UnicodeEscapeProcessingProvider provider = new UnicodeEscapeProcessingProvider(10,
new StringProvider(source));

StringBuilder result = new StringBuilder();
char[] buffer = new char[10];
while (true) {
int direct = provider.read(buffer, 0, buffer.length);
if (direct < 0) {
break;
}
result.append(buffer, 0, direct);
}

provider.close();

return result.toString();
}
}

0 comments on commit 5ef8d93

Please sign in to comment.