Path: blob/master/tests/core/string/test_translation_server.h
10278 views
/**************************************************************************/1/* test_translation_server.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/string/translation_server.h"3334#include "tests/test_macros.h"3536namespace TestTranslationServer {37TEST_CASE("[TranslationServer] Translation operations") {38Ref<Translation> t1 = memnew(Translation);39t1->set_locale("uk");40t1->add_message("Good Morning", String(U"Добрий ранок"));4142Ref<Translation> t2 = memnew(Translation);43t2->set_locale("uk");44t2->add_message("Hello Godot", String(U"你好戈多"));4546TranslationServer *ts = TranslationServer::get_singleton();4748// Adds translation for UK locale for the first time.49int l_count_before = ts->get_loaded_locales().size();50ts->add_translation(t1);51int l_count_after = ts->get_loaded_locales().size();52CHECK(l_count_after > l_count_before);5354// Adds translation for UK locale again.55ts->add_translation(t2);56CHECK_EQ(ts->get_loaded_locales().size(), l_count_after);5758// Removing that translation.59ts->remove_translation(t2);60CHECK_EQ(ts->get_loaded_locales().size(), l_count_after);6162CHECK(ts->get_translation_object("uk").is_valid());6364ts->set_locale("uk");65CHECK(ts->translate("Good Morning") == String::utf8("Добрий ранок"));6667ts->remove_translation(t1);68CHECK(ts->get_translation_object("uk").is_null());69// If no suitable Translation object has been found - the original message should be returned.70CHECK(ts->translate("Good Morning") == "Good Morning");71}7273TEST_CASE("[TranslationServer] Locale operations") {74TranslationServer *ts = TranslationServer::get_singleton();7576// Language variant test; we supplied the variant of Español and the result should be the same string.77String loc = "es_Hani_ES_tradnl";78String res = ts->standardize_locale(loc);7980CHECK(res == loc);8182// No such variant in variant_map; should return everything except the variant.83loc = "es_Hani_ES_missing";84res = ts->standardize_locale(loc);8586CHECK(res == "es_Hani_ES");8788// Non-ISO language name check (Windows issue).89loc = "iw_Hani_IL";90res = ts->standardize_locale(loc);9192CHECK(res == "he_Hani_IL");9394// Country rename check.95loc = "uk_Hani_UK";96res = ts->standardize_locale(loc);9798CHECK(res == "uk_Hani_GB");99100// Supplying a script name that is not in the list.101loc = "de_Wrong_DE";102res = ts->standardize_locale(loc);103104CHECK(res == "de_DE");105106// No added defaults.107loc = "es_ES";108res = ts->standardize_locale(loc, true);109110CHECK(res == "es_ES");111112// Add default script.113loc = "az_AZ";114res = ts->standardize_locale(loc, true);115116CHECK(res == "az_Latn_AZ");117118// Add default country.119loc = "pa_Arab";120res = ts->standardize_locale(loc, true);121122CHECK(res == "pa_Arab_PK");123124// Add default script and country.125loc = "zh";126res = ts->standardize_locale(loc, true);127128CHECK(res == "zh_Hans_CN");129130// Explicitly don't add defaults.131loc = "zh";132res = ts->standardize_locale(loc, false);133134CHECK(res == "zh");135}136137TEST_CASE("[TranslationServer] Comparing locales") {138TranslationServer *ts = TranslationServer::get_singleton();139140String locale_a = "es";141String locale_b = "es";142143// Exact match check.144int res = ts->compare_locales(locale_a, locale_b);145146CHECK(res == 10);147148locale_a = "sr-Latn-CS";149locale_b = "sr-Latn-RS";150151// Script matches (+1) but country doesn't (-1).152res = ts->compare_locales(locale_a, locale_b);153154CHECK(res == 5);155156locale_a = "uz-Cyrl-UZ";157locale_b = "uz-Latn-UZ";158159// Country matches (+1) but script doesn't (-1).160res = ts->compare_locales(locale_a, locale_b);161162CHECK(res == 5);163164locale_a = "aa-Latn-ER";165locale_b = "aa-Latn-ER-saaho";166167// Script and country match (+2) with variant on one locale (+0).168res = ts->compare_locales(locale_a, locale_b);169170CHECK(res == 7);171172locale_a = "uz-Cyrl-UZ";173locale_b = "uz-Latn-KG";174175// Both script and country mismatched (-2).176res = ts->compare_locales(locale_a, locale_b);177178CHECK(res == 3);179180locale_a = "es-ES";181locale_b = "es-AR";182183// Mismatched country (-1).184res = ts->compare_locales(locale_a, locale_b);185186CHECK(res == 4);187188locale_a = "es";189locale_b = "es-AR";190191// No country for one locale (+0).192res = ts->compare_locales(locale_a, locale_b);193194CHECK(res == 5);195196locale_a = "es-EC";197locale_b = "fr-LU";198199// No match.200res = ts->compare_locales(locale_a, locale_b);201202CHECK(res == 0);203204locale_a = "zh-HK";205locale_b = "zh";206207// In full standardization, zh-HK becomes zh_Hant_HK and zh becomes208// zh_Hans_CN. Both script and country mismatch (-2).209res = ts->compare_locales(locale_a, locale_b);210211CHECK(res == 3);212213locale_a = "zh-CN";214locale_b = "zh";215216// In full standardization, zh and zh-CN both become zh_Hans_CN for an217// exact match.218res = ts->compare_locales(locale_a, locale_b);219220CHECK(res == 10);221}222} // namespace TestTranslationServer223224225