ResourceManagerTests.cs 6.4 KB

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