Improve InternationalizationTest (#1705)

* Improve InternationalizationTest

- Remove "raw" tests since after compiling they are the same as the one with
  escape sequences
- Add tests for supplementary code points (> \uFFFF)

* Improve variable names, fix incorrect escape sequences
This commit is contained in:
Marcono1234 2022-08-08 01:00:35 +02:00 committed by GitHub
parent 76c78f5925
commit 9eb04414c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,7 +17,6 @@
package com.google.gson.functional;
import com.google.gson.Gson;
import junit.framework.TestCase;
/**
@ -34,32 +33,16 @@ public class InternationalizationTest extends TestCase {
gson = new Gson();
}
/*
public void testStringsWithRawChineseCharactersSerialization() throws Exception {
String target = "好好好";
String json = gson.toJson(target);
String expected = "\"\\u597d\\u597d\\u597d\"";
assertEquals(expected, json);
}
*/
public void testStringsWithRawChineseCharactersDeserialization() throws Exception {
String expected = "好好好";
String json = "\"" + expected + "\"";
String actual = gson.fromJson(json, String.class);
assertEquals(expected, actual);
}
public void testStringsWithUnicodeChineseCharactersSerialization() throws Exception {
String target = "\u597d\u597d\u597d";
String json = gson.toJson(target);
String expected = "\"\u597d\u597d\u597d\"";
String expected = '"' + target + '"';
assertEquals(expected, json);
}
public void testStringsWithUnicodeChineseCharactersDeserialization() throws Exception {
String expected = "\u597d\u597d\u597d";
String json = "\"" + expected + "\"";
String json = '"' + expected + '"';
String actual = gson.fromJson(json, String.class);
assertEquals(expected, actual);
}
@ -68,4 +51,25 @@ public class InternationalizationTest extends TestCase {
String actual = gson.fromJson("'\\u597d\\u597d\\u597d'", String.class);
assertEquals("\u597d\u597d\u597d", actual);
}
public void testSupplementaryUnicodeSerialization() throws Exception {
// Supplementary code point U+1F60A
String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1);
String json = gson.toJson(supplementaryCodePoint);
assertEquals('"' + supplementaryCodePoint + '"', json);
}
public void testSupplementaryUnicodeDeserialization() throws Exception {
// Supplementary code point U+1F60A
String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1);
String actual = gson.fromJson('"' + supplementaryCodePoint + '"', String.class);
assertEquals(supplementaryCodePoint, actual);
}
public void testSupplementaryUnicodeEscapedDeserialization() throws Exception {
// Supplementary code point U+1F60A
String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1);
String actual = gson.fromJson("\"\\uD83D\\uDE0A\"", String.class);
assertEquals(supplementaryCodePoint, actual);
}
}