ResourceManagerTests.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #nullable enable
  2. using System.Collections;
  3. using System.Globalization;
  4. using System.Resources;
  5. using Terminal.Gui.Resources;
  6. namespace Terminal.Gui.ResourcesTests;
  7. public class ResourceManagerTests
  8. {
  9. private const string DODGER_BLUE_COLOR_KEY = "DodgerBlue";
  10. private const string DODGER_BLUE_COLOR_NAME = "DodgerBlue";
  11. private const string NO_NAMED_COLOR_KEY = "#1E80FF";
  12. private const string NO_NAMED_COLOR_NAME = "#1E80FF";
  13. private const string EXISTENT_CULTURE = "pt-PT";
  14. private const string NO_EXISTENT_CULTURE = "de-DE";
  15. private const string NO_EXISTENT_KEY = "blabla";
  16. private const string NO_TRANSLATED_KEY = "fdDeleteTitle";
  17. private const string NO_TRANSLATED_VALUE = "Delete {0}";
  18. private const string TRANSLATED_KEY = "ctxSelectAll";
  19. private const string TRANSLATED_VALUE = "_Selecionar Tudo";
  20. private static readonly string _stringsNoTranslatedKey = Strings.fdDeleteTitle;
  21. private static readonly string _stringsTranslatedKey = Strings.ctxSelectAll;
  22. private static readonly CultureInfo _savedCulture = CultureInfo.CurrentCulture;
  23. private static readonly CultureInfo _savedUICulture = CultureInfo.CurrentUICulture;
  24. [Fact]
  25. public void GetObject_Does_Not_Overflows_If_Key_Does_Not_Exist () { Assert.Null (GlobalResources.GetObject (NO_EXISTENT_KEY, CultureInfo.CurrentCulture)); }
  26. [Fact]
  27. public void GetObject_FallBack_To_Default_For_No_Existent_Culture_File ()
  28. {
  29. CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
  30. CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
  31. Assert.Equal (NO_TRANSLATED_VALUE, GlobalResources.GetObject (NO_TRANSLATED_KEY, CultureInfo.CurrentCulture));
  32. RestoreCurrentCultures ();
  33. }
  34. [Fact]
  35. public void GetObject_FallBack_To_Default_For_Not_Translated_Existent_Culture_File ()
  36. {
  37. CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
  38. CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
  39. Assert.Equal (NO_TRANSLATED_VALUE, GlobalResources.GetObject (NO_TRANSLATED_KEY, CultureInfo.CurrentCulture));
  40. RestoreCurrentCultures ();
  41. }
  42. [Fact]
  43. public void GetResourceSet_FallBack_To_Default_For_No_Existent_Culture_File ()
  44. {
  45. CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
  46. CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
  47. // W3CColors.GetColorNames also calls ColorStrings.GetW3CColorNames
  48. string [] colorNames = new W3CColors ().GetColorNames ().ToArray ();
  49. Assert.Contains (DODGER_BLUE_COLOR_NAME, colorNames);
  50. Assert.DoesNotContain (NO_TRANSLATED_VALUE, colorNames);
  51. RestoreCurrentCultures ();
  52. }
  53. [Fact]
  54. public void GetResourceSet_FallBack_To_Default_For_Not_Translated_Existent_Culture_File ()
  55. {
  56. CultureInfo.CurrentCulture = new (EXISTENT_CULTURE);
  57. CultureInfo.CurrentUICulture = new (EXISTENT_CULTURE);
  58. // These aren't already translated
  59. // ColorStrings.GetW3CColorNames method uses GetResourceSet method to retrieve color names
  60. IEnumerable<string> colorNames = ColorStrings.GetW3CColorNames ();
  61. Assert.NotEmpty (colorNames);
  62. // W3CColors.GetColorNames also calls ColorStrings.GetW3CColorNames
  63. colorNames = new W3CColors ().GetColorNames ().ToArray ();
  64. Assert.Contains (DODGER_BLUE_COLOR_NAME, colorNames);
  65. Assert.DoesNotContain (NO_TRANSLATED_VALUE, colorNames);
  66. // ColorStrings.TryParseW3CColorName method uses GetResourceSet method to retrieve a color value
  67. Assert.True (ColorStrings.TryParseW3CColorName (DODGER_BLUE_COLOR_NAME, out Color color));
  68. Assert.Equal (DODGER_BLUE_COLOR_KEY, color.ToString ());
  69. // W3CColors.GetColorNames also calls ColorStrings.GetW3CColorNames for no-named colors
  70. colorNames = new W3CColors ().GetColorNames ().ToArray ();
  71. Assert.DoesNotContain (NO_NAMED_COLOR_NAME, colorNames);
  72. // ColorStrings.TryParseW3CColorName method uses GetResourceSet method to retrieve a color value for no-named colors
  73. Assert.True (ColorStrings.TryParseW3CColorName (NO_NAMED_COLOR_NAME, out color));
  74. Assert.Equal (NO_NAMED_COLOR_KEY, color.ToString ());
  75. RestoreCurrentCultures ();
  76. }
  77. [Fact]
  78. public void GetResourceSet_With_Filter_Does_Not_Overflows_If_Key_Does_Not_Exist ()
  79. {
  80. ResourceSet value = GlobalResources.GetResourceSet (CultureInfo.CurrentCulture, true, true, d => (string)d.Key == NO_EXISTENT_KEY)!;
  81. Assert.NotNull (value);
  82. Assert.Empty (value.Cast<DictionaryEntry> ());
  83. }
  84. [Fact]
  85. public void GetResourceSet_Without_Filter_Does_Not_Overflows_If_Key_Does_Not_Exist ()
  86. {
  87. ResourceSet value = GlobalResources.GetResourceSet (CultureInfo.CurrentCulture, true, true)!;
  88. Assert.NotNull (value);
  89. Assert.NotEmpty (value.Cast<DictionaryEntry> ());
  90. }
  91. [Fact]
  92. public void GetString_Does_Not_Overflows_If_Key_Does_Not_Exist () { Assert.Null (GlobalResources.GetString (NO_EXISTENT_KEY, CultureInfo.CurrentCulture)); }
  93. [Fact]
  94. public void GetString_FallBack_To_Default_For_No_Existent_Culture_File ()
  95. {
  96. CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
  97. CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
  98. Assert.Equal (NO_TRANSLATED_VALUE, GlobalResources.GetString (NO_TRANSLATED_KEY, CultureInfo.CurrentCulture));
  99. RestoreCurrentCultures ();
  100. }
  101. [Fact]
  102. public void GetString_FallBack_To_Default_For_Not_Translated_Existent_Culture_File ()
  103. {
  104. CultureInfo.CurrentCulture = new (EXISTENT_CULTURE);
  105. CultureInfo.CurrentUICulture = new (EXISTENT_CULTURE);
  106. // This is really already translated
  107. Assert.Equal (TRANSLATED_VALUE, GlobalResources.GetString (TRANSLATED_KEY, CultureInfo.CurrentCulture));
  108. // These aren't already translated
  109. // Calling Strings.fdDeleteBody return always the invariant culture
  110. Assert.Equal (NO_TRANSLATED_VALUE, GlobalResources.GetString (NO_TRANSLATED_KEY, CultureInfo.CurrentCulture));
  111. RestoreCurrentCultures ();
  112. }
  113. [Fact]
  114. public void Strings_Always_FallBack_To_Default_For_No_Existent_Culture_File ()
  115. {
  116. CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
  117. CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
  118. Assert.Equal (NO_TRANSLATED_VALUE, _stringsNoTranslatedKey);
  119. RestoreCurrentCultures ();
  120. }
  121. [Fact]
  122. public void Strings_Always_FallBack_To_Default_For_Not_Translated_Existent_Culture_File ()
  123. {
  124. CultureInfo.CurrentCulture = new (EXISTENT_CULTURE);
  125. CultureInfo.CurrentUICulture = new (EXISTENT_CULTURE);
  126. // This is really already translated
  127. Assert.Equal (TRANSLATED_VALUE, _stringsTranslatedKey);
  128. // This isn't already translated
  129. Assert.Equal (NO_TRANSLATED_VALUE, _stringsNoTranslatedKey);
  130. RestoreCurrentCultures ();
  131. }
  132. private void RestoreCurrentCultures ()
  133. {
  134. CultureInfo.CurrentCulture = _savedCulture;
  135. CultureInfo.CurrentUICulture = _savedUICulture;
  136. }
  137. }