software-testing/project_task_sheets/phase_03/project_phase03_tasks/JSONScannerTest2.java

83 lines
2.7 KiB
Java
Raw Permalink Normal View History

2022-05-19 08:07:46 +02:00
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 {
2022-05-19 09:34:34 +02:00
JSONScanner scanner = new JSONScanner("2012-04-23T18:25:43.511Z");
2022-05-19 08:07:46 +02:00
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
2022-05-19 09:34:34 +02:00
public void inputTokenTest_set() throws Throwable {
JSONScanner scanner = new JSONScanner("Set[3,2,1]");
2022-05-19 08:07:46 +02:00
scanner.nextToken();
2022-05-19 09:34:34 +02:00
Assert.assertEquals(JSONToken.SET, scanner.token());
2022-05-19 08:07:46 +02:00
}
@Test
public void inputTokenTest_Undefined() throws Throwable {
JSONScanner scanner = new JSONScanner("undefined");
scanner.nextToken();
Assert.assertEquals(JSONToken.UNDEFINED, scanner.token());
}
@Test
2022-05-19 09:34:34 +02:00
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());
2022-05-19 08:07:46 +02:00
}
@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());
}
}