Files
Terminal.Gui/Tests/UnitTests/Resources/ResourceManagerTests.cs
Tig b0f32811eb Fixes #3930 - Splits tests to Tests/UnitTests, Tests/IntegrationTests, Tests/StressTests (#3954)
* Tons of API doc updates

* Removed stale test

* Removed stale tests

* Fixed Skipped Shadow test 1

* Fixed Skipped Shadow test 2

* Fixed Skipped Shadow test 3

* Removed stale test

* Removed stale test2

* Explicit unregister of event handler on Application.Driver!.ClearedContents

* Added Toplevels to dict

* code cleanup

* spelling error

* Removed stale test3

* Removed stale test4

* Removed stale test5

* added script

* tweaked script

* tweaked script

* Created StressTests project; moved some tests

* Created IntegrationTests project; moved some tests

* New yml

* made old yml just unit tests

* Tweaked Button_IsDefault_Raises_Accepted_Correctly

* tweaked script

* cleaned up ymls

* tweakled up ymls

* stress tests...

* stress tests on ubuntu only

* Fixed WindowsDriver in InvokeLeakTest

* Fixed WindowsDriver in InvokeLeakTest2

* Added Directory.Packages.props.
Added Directory.Build.props

* Shortened StressTest time

* Removed dupe file.

* DemoFiles

* Moved all tests to ./Tests dir.

* Fixed release build issue

* Fixed .sln file

* Fixed .sl* files

* Fixing ymls

* Fixing interation tests

* Create link to the file TestHelpers.

* Created Tests/UnitTestsParallelizable.
Moved all obviously parallelizable tests.
Updated yml.

* fixing logs

* fixing logs2

* fixing logs3

* don't require stress to pass for PRs

* Fix a failure?

* tweaked script

* Coudl this be it?

* Moved tons of tests to parallelizable

* Fixed some stuff

* Script to find duplicate tests

* Testing workflows

* Updated to v4

* Fix RelativeBasePath issue

* Replace powershell to pwsh

* Add ignore projects.

* Removed dupe unit tests

* Code cleanup of tests

* Cleaned up test warnings

* yml tweak

* Moved setter

* tweak ymls

* just randomly throwing spaghetti at a wall

* Enable runing 5 test runners in par

* Turned off DEBUG_DISPOSABLE for par tests

* RunningUnitTests=true

* code cleanup (forcing more Action runs)

* DISABLE_DEBUG_IDISPOSABLE

* Added View.DebugIDisposable. False by default.

* Remobed bogus tareet

* Remobed bogus tareet2

* fixed warning

* added api doc

* fixed warning

* fixed warning

* fixed warning2

* fixed warning3

* fixed warning4

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2025-03-05 23:44:27 -07:00

176 lines
7.0 KiB
C#

#nullable enable
using System.Collections;
using System.Globalization;
using System.Resources;
using Terminal.Gui.Resources;
namespace Terminal.Gui.ResourcesTests;
public class ResourceManagerTests
{
private const string DODGER_BLUE_COLOR_KEY = "DodgerBlue";
private const string DODGER_BLUE_COLOR_NAME = "DodgerBlue";
private const string NO_NAMED_COLOR_KEY = "#1E80FF";
private const string NO_NAMED_COLOR_NAME = "#1E80FF";
private const string EXISTENT_CULTURE = "pt-PT";
private const string NO_EXISTENT_CULTURE = "de-DE";
private const string NO_EXISTENT_KEY = "blabla";
private const string NO_TRANSLATED_KEY = "fdDeleteTitle";
private const string NO_TRANSLATED_VALUE = "Delete {0}";
private const string TRANSLATED_KEY = "ctxSelectAll";
private const string TRANSLATED_VALUE = "_Selecionar Tudo";
private static readonly string _stringsNoTranslatedKey = Strings.fdDeleteTitle;
private static readonly string _stringsTranslatedKey = Strings.ctxSelectAll;
private static readonly CultureInfo _savedCulture = CultureInfo.CurrentCulture;
private static readonly CultureInfo _savedUICulture = CultureInfo.CurrentUICulture;
[Fact]
public void GetObject_Does_Not_Overflows_If_Key_Does_Not_Exist () { Assert.Null (GlobalResources.GetObject (NO_EXISTENT_KEY, CultureInfo.CurrentCulture)); }
[Fact]
public void GetObject_FallBack_To_Default_For_No_Existent_Culture_File ()
{
CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
Assert.Equal (NO_TRANSLATED_VALUE, GlobalResources.GetObject (NO_TRANSLATED_KEY, CultureInfo.CurrentCulture));
RestoreCurrentCultures ();
}
[Fact]
public void GetObject_FallBack_To_Default_For_Not_Translated_Existent_Culture_File ()
{
CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
Assert.Equal (NO_TRANSLATED_VALUE, GlobalResources.GetObject (NO_TRANSLATED_KEY, CultureInfo.CurrentCulture));
RestoreCurrentCultures ();
}
[Fact]
public void GetResourceSet_FallBack_To_Default_For_No_Existent_Culture_File ()
{
CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
// W3CColors.GetColorNames also calls ColorStrings.GetW3CColorNames
string [] colorNames = new W3CColors ().GetColorNames ().ToArray ();
Assert.Contains (DODGER_BLUE_COLOR_NAME, colorNames);
Assert.DoesNotContain (NO_TRANSLATED_VALUE, colorNames);
RestoreCurrentCultures ();
}
[Fact]
public void GetResourceSet_FallBack_To_Default_For_Not_Translated_Existent_Culture_File ()
{
CultureInfo.CurrentCulture = new (EXISTENT_CULTURE);
CultureInfo.CurrentUICulture = new (EXISTENT_CULTURE);
// These aren't already translated
// ColorStrings.GetW3CColorNames method uses GetResourceSet method to retrieve color names
IEnumerable<string> colorNames = ColorStrings.GetW3CColorNames ();
Assert.NotEmpty (colorNames);
// W3CColors.GetColorNames also calls ColorStrings.GetW3CColorNames
colorNames = new W3CColors ().GetColorNames ().ToArray ();
Assert.Contains (DODGER_BLUE_COLOR_NAME, colorNames);
Assert.DoesNotContain (NO_TRANSLATED_VALUE, colorNames);
// ColorStrings.TryParseW3CColorName method uses GetResourceSet method to retrieve a color value
Assert.True (ColorStrings.TryParseW3CColorName (DODGER_BLUE_COLOR_NAME, out Color color));
Assert.Equal (DODGER_BLUE_COLOR_KEY, color.ToString ());
// W3CColors.GetColorNames also calls ColorStrings.GetW3CColorNames for no-named colors
colorNames = new W3CColors ().GetColorNames ().ToArray ();
Assert.DoesNotContain (NO_NAMED_COLOR_NAME, colorNames);
// ColorStrings.TryParseW3CColorName method uses GetResourceSet method to retrieve a color value for no-named colors
Assert.True (ColorStrings.TryParseW3CColorName (NO_NAMED_COLOR_NAME, out color));
Assert.Equal (NO_NAMED_COLOR_KEY, color.ToString ());
RestoreCurrentCultures ();
}
[Fact]
public void GetResourceSet_With_Filter_Does_Not_Overflows_If_Key_Does_Not_Exist ()
{
ResourceSet value = GlobalResources.GetResourceSet (CultureInfo.CurrentCulture, true, true, d => (string)d.Key == NO_EXISTENT_KEY)!;
Assert.NotNull (value);
Assert.Empty (value.Cast<DictionaryEntry> ());
}
[Fact]
public void GetResourceSet_Without_Filter_Does_Not_Overflows_If_Key_Does_Not_Exist ()
{
ResourceSet value = GlobalResources.GetResourceSet (CultureInfo.CurrentCulture, true, true)!;
Assert.NotNull (value);
Assert.NotEmpty (value.Cast<DictionaryEntry> ());
}
[Fact]
public void GetString_Does_Not_Overflows_If_Key_Does_Not_Exist () { Assert.Null (GlobalResources.GetString (NO_EXISTENT_KEY, CultureInfo.CurrentCulture)); }
[Fact]
public void GetString_FallBack_To_Default_For_No_Existent_Culture_File ()
{
CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
Assert.Equal (NO_TRANSLATED_VALUE, GlobalResources.GetString (NO_TRANSLATED_KEY, CultureInfo.CurrentCulture));
RestoreCurrentCultures ();
}
[Fact]
public void GetString_FallBack_To_Default_For_Not_Translated_Existent_Culture_File ()
{
CultureInfo.CurrentCulture = new (EXISTENT_CULTURE);
CultureInfo.CurrentUICulture = new (EXISTENT_CULTURE);
// This is really already translated
Assert.Equal (TRANSLATED_VALUE, GlobalResources.GetString (TRANSLATED_KEY, CultureInfo.CurrentCulture));
// These aren't already translated
// Calling Strings.fdDeleteBody return always the invariant culture
Assert.Equal (NO_TRANSLATED_VALUE, GlobalResources.GetString (NO_TRANSLATED_KEY, CultureInfo.CurrentCulture));
RestoreCurrentCultures ();
}
[Fact]
public void Strings_Always_FallBack_To_Default_For_No_Existent_Culture_File ()
{
CultureInfo.CurrentCulture = new (NO_EXISTENT_CULTURE);
CultureInfo.CurrentUICulture = new (NO_EXISTENT_CULTURE);
Assert.Equal (NO_TRANSLATED_VALUE, _stringsNoTranslatedKey);
RestoreCurrentCultures ();
}
[Fact]
public void Strings_Always_FallBack_To_Default_For_Not_Translated_Existent_Culture_File ()
{
CultureInfo.CurrentCulture = new (EXISTENT_CULTURE);
CultureInfo.CurrentUICulture = new (EXISTENT_CULTURE);
// This is really already translated
Assert.Equal (TRANSLATED_VALUE, _stringsTranslatedKey);
// This isn't already translated
Assert.Equal (NO_TRANSLATED_VALUE, _stringsNoTranslatedKey);
RestoreCurrentCultures ();
}
private void RestoreCurrentCultures ()
{
CultureInfo.CurrentCulture = _savedCulture;
CultureInfo.CurrentUICulture = _savedUICulture;
}
}