From 2cf70453f821edb5d738eb4b09aab92994f3d531 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 9 May 2023 11:19:00 +0200 Subject: [PATCH] Add automated test for strings formatting and localization --- Source/Engine/Tests/TestLocalization.cpp | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Source/Engine/Tests/TestLocalization.cpp diff --git a/Source/Engine/Tests/TestLocalization.cpp b/Source/Engine/Tests/TestLocalization.cpp new file mode 100644 index 000000000..1807e52bf --- /dev/null +++ b/Source/Engine/Tests/TestLocalization.cpp @@ -0,0 +1,54 @@ +// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. + +#include "Engine/Core/Formatting.h" +#include "Engine/Core/Types/String.h" +#include "Engine/Core/Math/Vector2.h" +#include "Engine/Core/Math/Vector3.h" +#include "Engine/Localization/Localization.h" +#include + +struct MyStruct +{ + Vector2 Direction; + float Speed; +}; + +DEFINE_DEFAULT_FORMATTING(MyStruct, "Direction:{0} Speed:{1}", v.Direction, v.Speed); + +TEST_CASE("Localization") +{ + SECTION("Test Fallback Values") + { + String myStr = Localization::GetString(TEXT("localized_id"), TEXT("Fallback value")); + String myStrPlural = Localization::GetPluralString(TEXT("localized_id_n"), 2, TEXT("Count: {}")); + CHECK(myStr == TEXT("Fallback value")); + CHECK(myStrPlural == TEXT("Count: 2")); + } + SECTION("Test String Formatting") + { + // https://docs.flaxengine.com/manual/scripting/cpp/string-formatting.html + + // Values formatting + auto str1 = String::Format(TEXT("a: {0}, b: {1}, a: {0}"), TEXT("a"), TEXT("b")); + CHECK(str1 == TEXT("a: a, b: b, a: a")); + auto str2 = String::Format(TEXT("1: {}, 2: {}, 3: {}"), 1, 2, 3); + CHECK(str2 == TEXT("1: 1, 2: 2, 3: 3")); + auto str3 = String::Format(TEXT("vector: {0}"), Vector3(1, 2, 3)); + CHECK(str3 == TEXT("vector: X:1.0 Y:2.0 Z:3.0")); + String str = TEXT("hello"); + auto str4 = String::Format(TEXT("string: {0}"), str.ToString()); + CHECK(str4 == TEXT("string: hello")); + auto str5 = String::Format(TEXT("boolean: {0}"), true); + CHECK(str5 == TEXT("boolean: true")); + + // Custom type formatting + MyStruct data = { Vector2(1, 2), 10.0f }; + auto str6 = String::Format(TEXT("{0}"), data); + CHECK(str6 == TEXT("Direction:X:1.0 Y:2.0 Speed:10.0")); + + // Named arguments formatting + String text1 = String::Format(TEXT("text: {0}, {1}"), TEXT("one"), TEXT("two")); + String text2 = String::Format(TEXT("text: {arg0}, {arg1}"), fmt::arg(TEXT("arg0"), TEXT("one")), fmt::arg(TEXT("arg1"), TEXT("two"))); + CHECK(text1 == text2); + } +}