diff options
Diffstat (limited to 'tests/test_string.h')
-rw-r--r-- | tests/test_string.h | 107 |
1 files changed, 71 insertions, 36 deletions
diff --git a/tests/test_string.h b/tests/test_string.h index b041cb2f49..6febf22765 100644 --- a/tests/test_string.h +++ b/tests/test_string.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -38,11 +38,7 @@ #include "core/io/ip_address.h" #include "core/os/main_loop.h" #include "core/os/os.h" -#include "core/ustring.h" - -#ifdef MODULE_REGEX_ENABLED -#include "modules/regex/regex.h" -#endif +#include "core/string/ustring.h" #include "tests/test_macros.h" @@ -248,11 +244,11 @@ TEST_CASE("[String] Testing size and length of string") { } TEST_CASE("[String] Testing for empty string") { - CHECK(!String("Mellon").empty()); + CHECK(!String("Mellon").is_empty()); // do this more than once, to check for string corruption - CHECK(String("").empty()); - CHECK(String("").empty()); - CHECK(String("").empty()); + CHECK(String("").is_empty()); + CHECK(String("").is_empty()); + CHECK(String("").is_empty()); } TEST_CASE("[String] Test chr") { @@ -270,7 +266,7 @@ TEST_CASE("[String] Operator []") { a[6] = 'C'; CHECK(a == "Sugar Cane"); CHECK(a[1] == 'u'); - CHECK(a.ord_at(1) == 'u'); + CHECK(a.unicode_at(1) == 'u'); } TEST_CASE("[String] Case function test") { @@ -374,12 +370,9 @@ TEST_CASE("[String] String to integer") { TEST_CASE("[String] Hex to integer") { static const char *nums[4] = { "0xFFAE", "22", "0", "AADDAD" }; static const int64_t num[4] = { 0xFFAE, 0x22, 0, 0xAADDAD }; - static const bool wo_prefix[4] = { false, true, true, true }; - static const bool w_prefix[4] = { true, false, true, false }; for (int i = 0; i < 4; i++) { - CHECK((String(nums[i]).hex_to_int(true) == num[i]) == w_prefix[i]); - CHECK((String(nums[i]).hex_to_int(false) == num[i]) == wo_prefix[i]); + CHECK(String(nums[i]).hex_to_int() == num[i]); } } @@ -475,15 +468,6 @@ TEST_CASE("[String] Erasing") { CHECK(s == "Josephine is such a girl!"); } -#ifdef MODULE_REGEX_ENABLED -TEST_CASE("[String] Regex substitution") { - String s = "Double all the vowels."; - RegEx re("(?<vowel>[aeiou])"); - s = re.sub(s, "$0$vowel", true); - CHECK(s == "Doouublee aall thee vooweels."); -} -#endif - struct test_27_data { char const *data; char const *part; @@ -1168,20 +1152,12 @@ TEST_CASE("[String] hash") { CHECK(a.hash64() != c.hash64()); } -TEST_CASE("[String] http_escape/unescape") { +TEST_CASE("[String] uri_encode/unescape") { String s = "Godot Engine:'docs'"; String t = "Godot%20Engine%3A%27docs%27"; - CHECK(s.http_escape() == t); - CHECK(t.http_unescape() == s); -} - -TEST_CASE("[String] percent_encode/decode") { // Note: is it redundant? Seems to be same as http_escape/unescape but in lower case. - String s = "Godot Engine:'docs'"; - String t = "Godot%20Engine%3a%27docs%27"; - - CHECK(s.percent_encode() == t); - CHECK(t.percent_decode() == s); + CHECK(s.uri_encode() == t); + CHECK(t.uri_decode() == s); } TEST_CASE("[String] xml_escape/unescape") { @@ -1190,6 +1166,52 @@ TEST_CASE("[String] xml_escape/unescape") { CHECK(s.xml_escape(false).xml_unescape() == s); } +TEST_CASE("[String] xml_unescape") { + // Named entities + String input = ""&'<>"; + CHECK(input.xml_unescape() == "\"&\'<>"); + + // Numeric entities + input = "AB"; + CHECK(input.xml_unescape() == "AB"); + + input = "�&x#0;More text"; + String result = input.xml_unescape(); + // Didn't put in a leading NUL and terminate the string + CHECK(input.length() > 0); + CHECK(input[0] != '\0'); + // Entity should be left as-is if invalid + CHECK(input.xml_unescape() == input); + + // Check near char32_t range + input = "�"; + result = input.xml_unescape(); + CHECK(result.length() == 1); + CHECK(result[0] == 0xFFFFFFFF); + input = "�"; + result = input.xml_unescape(); + CHECK(result.length() == 1); + CHECK(result[0] == 0xFFFFFFFF); + + // Check out of range of char32_t + input = "�"; + CHECK(input.xml_unescape() == input); + input = "�"; + CHECK(input.xml_unescape() == input); + + // Shouldn't consume without ending in a ';' + input = "B"; + CHECK(input.xml_unescape() == input); + input = "A"; + CHECK(input.xml_unescape() == input); + + // Invalid characters should make the entity ignored + input = "ASomeIrrelevantText;"; + CHECK(input.xml_unescape() == input); + input = "BSomeIrrelevantText;"; + CHECK(input.xml_unescape() == input); +} + TEST_CASE("[String] Strip escapes") { String s = "\t\tTest Test\r\n Test"; CHECK(s.strip_escapes() == "Test Test Test"); @@ -1297,6 +1319,19 @@ TEST_CASE("[String] humanize_size") { CHECK(String::humanize_size(5345555000) == "4.97 GiB"); } +TEST_CASE("[String] validate_node_name") { + String numeric_only = "12345"; + CHECK(numeric_only.validate_node_name() == "12345"); + + String name_with_spaces = "Name with spaces"; + CHECK(name_with_spaces.validate_node_name() == "Name with spaces"); + + String name_with_kana = "Name with kana ゴドツ"; + CHECK(name_with_kana.validate_node_name() == "Name with kana ゴドツ"); + + String name_with_invalid_chars = "Name with invalid characters :.@removed!"; + CHECK(name_with_invalid_chars.validate_node_name() == "Name with invalid characters removed!"); +} } // namespace TestString #endif // TEST_STRING_H |