test_translation_server.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**************************************************************************/
  2. /* test_translation_server.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_TRANSLATION_SERVER_H
  31. #define TEST_TRANSLATION_SERVER_H
  32. #include "core/string/translation.h"
  33. #include "tests/test_macros.h"
  34. namespace TestTranslationServer {
  35. TEST_CASE("[TranslationServer] Translation operations") {
  36. Ref<Translation> t = memnew(Translation);
  37. t->set_locale("uk");
  38. t->add_message("Good Morning", String::utf8("Добрий ранок"));
  39. TranslationServer *ts = TranslationServer::get_singleton();
  40. int l_count_before = ts->get_loaded_locales().size();
  41. ts->add_translation(t);
  42. int l_count_after = ts->get_loaded_locales().size();
  43. // Newly created Translation object should be added to the list, so the counter should increase, too.
  44. CHECK(l_count_after > l_count_before);
  45. Ref<Translation> trans = ts->get_translation_object("uk");
  46. CHECK(trans.is_valid());
  47. ts->set_locale("uk");
  48. CHECK(ts->translate("Good Morning") == String::utf8("Добрий ранок"));
  49. ts->remove_translation(t);
  50. trans = ts->get_translation_object("uk");
  51. CHECK(trans.is_null());
  52. // If no suitable Translation object has been found - the original message should be returned.
  53. CHECK(ts->translate("Good Morning") == "Good Morning");
  54. }
  55. TEST_CASE("[TranslationServer] Locale operations") {
  56. TranslationServer *ts = TranslationServer::get_singleton();
  57. // Language variant test; we supplied the variant of Español and the result should be the same string.
  58. String loc = "es_Hani_ES_tradnl";
  59. String res = ts->standardize_locale(loc);
  60. CHECK(res == loc);
  61. // No such variant in variant_map; should return everything except the variant.
  62. loc = "es_Hani_ES_missing";
  63. res = ts->standardize_locale(loc);
  64. CHECK(res == "es_Hani_ES");
  65. // Non-ISO language name check (Windows issue).
  66. loc = "iw_Hani_IL";
  67. res = ts->standardize_locale(loc);
  68. CHECK(res == "he_Hani_IL");
  69. // Country rename check.
  70. loc = "uk_Hani_UK";
  71. res = ts->standardize_locale(loc);
  72. CHECK(res == "uk_Hani_GB");
  73. // Supplying a script name that is not in the list.
  74. loc = "de_Wrong_DE";
  75. res = ts->standardize_locale(loc);
  76. CHECK(res == "de_DE");
  77. }
  78. TEST_CASE("[TranslationServer] Comparing locales") {
  79. TranslationServer *ts = TranslationServer::get_singleton();
  80. String locale_a = "es";
  81. String locale_b = "es";
  82. // Exact match check.
  83. int res = ts->compare_locales(locale_a, locale_b);
  84. CHECK(res == 10);
  85. locale_a = "sr-Latn-CS";
  86. locale_b = "sr-Latn-RS";
  87. // Two elements from locales match.
  88. res = ts->compare_locales(locale_a, locale_b);
  89. CHECK(res == 2);
  90. locale_a = "uz-Cyrl-UZ";
  91. locale_b = "uz-Latn-UZ";
  92. // Two elements match, but they are not sequentual.
  93. res = ts->compare_locales(locale_a, locale_b);
  94. CHECK(res == 2);
  95. locale_a = "es-EC";
  96. locale_b = "fr-LU";
  97. // No match.
  98. res = ts->compare_locales(locale_a, locale_b);
  99. CHECK(res == 0);
  100. }
  101. } // namespace TestTranslationServer
  102. #endif // TEST_TRANSLATION_SERVER_H