83 lines
2.7 KiB
Java
83 lines
2.7 KiB
Java
package com.alibaba.fastjson.parser;
|
|
|
|
import com.alibaba.fastjson.JSONException;
|
|
import org.junit.Assert;
|
|
import org.junit.Test;
|
|
|
|
|
|
public class JSONScannerTest2 {
|
|
@Test
|
|
public void stringUtf8BomTest() throws Throwable {
|
|
JSONScanner scanner = new JSONScanner("\uFEFF,");
|
|
// After construction the UTF-8 BOM should be safely skipped
|
|
scanner.nextToken();
|
|
Assert.assertEquals(JSONToken.COMMA, scanner.token());
|
|
}
|
|
|
|
@Test
|
|
public void stringHasSpecialTest() throws Throwable {
|
|
JSONScanner scanner = new JSONScanner("\"\\b\\n\\t\\\"\\uABED\"");
|
|
scanner.nextToken();
|
|
Assert.assertEquals(JSONToken.LITERAL_STRING, scanner.token());
|
|
Assert.assertEquals("\b\n\t\"\uABED", scanner.stringVal());
|
|
}
|
|
|
|
@Test
|
|
public void stringWithIso8601Date() throws Throwable {
|
|
JSONScanner scanner = new JSONScanner("2012-04-23T18:25:43.511Z");
|
|
Assert.assertTrue(scanner.scanISO8601DateIfMatch());
|
|
}
|
|
|
|
@Test
|
|
public void scanSingleComment() throws Throwable {
|
|
JSONScanner scanner = new JSONScanner("//dsajfklsjfk");
|
|
scanner.nextToken();
|
|
Assert.assertTrue(scanner.isEOF());
|
|
}
|
|
|
|
@Test
|
|
public void scanMultiComment() throws Throwable {
|
|
JSONScanner scanner = new JSONScanner("/*dsajfklsjfk*/");
|
|
scanner.nextToken();
|
|
Assert.assertTrue(scanner.isEOF());
|
|
}
|
|
|
|
@Test
|
|
public void scannerSkipWhitespace() throws Throwable {
|
|
JSONScanner scanner = new JSONScanner("\n\t\t \r\b\f232");
|
|
scanner.nextToken();
|
|
Assert.assertEquals(JSONToken.LITERAL_INT, scanner.token());
|
|
Assert.assertEquals(232, scanner.intValue());
|
|
}
|
|
|
|
@Test
|
|
public void inputTokenTest_set() throws Throwable {
|
|
JSONScanner scanner = new JSONScanner("Set[3,2,1]");
|
|
scanner.nextToken();
|
|
Assert.assertEquals(JSONToken.SET, scanner.token());
|
|
}
|
|
|
|
@Test
|
|
public void inputTokenTest_Undefined() throws Throwable {
|
|
JSONScanner scanner = new JSONScanner("undefined");
|
|
scanner.nextToken();
|
|
Assert.assertEquals(JSONToken.UNDEFINED, scanner.token());
|
|
}
|
|
|
|
@Test
|
|
public void stringWithIso8601Date_Invalid() throws Throwable {
|
|
Assert.assertTrue(new JSONScanner("2012-04-23T18:25:43.511Z").scanISO8601DateIfMatch());
|
|
Assert.assertFalse(new JSONScanner("2012-04-32T18:25:43.511Z").scanISO8601DateIfMatch());
|
|
Assert.assertFalse(new JSONScanner("2012-00-23T18:25:43.511Z").scanISO8601DateIfMatch());
|
|
Assert.assertFalse(new JSONScanner("2012-13-23T18:25:43.511Z").scanISO8601DateIfMatch());
|
|
}
|
|
|
|
@Test
|
|
public void inputTokenTest_ExplPosInt() throws Throwable {
|
|
JSONScanner scanner = new JSONScanner("+7531"); // explicit positive with sign
|
|
scanner.nextToken();
|
|
Assert.assertEquals(JSONToken.LITERAL_INT, scanner.token());
|
|
Assert.assertEquals(7531, scanner.intValue());
|
|
}
|
|
}
|