From 9eb04414c0a7f201d039cdbf9a9ebc4144990d3f Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Mon, 8 Aug 2022 01:00:35 +0200 Subject: [PATCH] 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 --- .../functional/InternationalizationTest.java | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/InternationalizationTest.java b/gson/src/test/java/com/google/gson/functional/InternationalizationTest.java index 169c37a5..bdf6ea6e 100644 --- a/gson/src/test/java/com/google/gson/functional/InternationalizationTest.java +++ b/gson/src/test/java/com/google/gson/functional/InternationalizationTest.java @@ -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); + } }