2
0

AppScopeTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #nullable enable
  2. using System.Text.Json;
  3. using UnitTests;
  4. using static Terminal.Gui.Configuration.ConfigurationManager;
  5. namespace UnitTests.ConfigurationTests;
  6. public class AppSettingsScopeTests
  7. {
  8. [Fact]
  9. public void Empty_By_Default_Disabled ()
  10. {
  11. Assert.False (IsEnabled);
  12. Assert.NotNull (Settings! ["AppSettings"].PropertyValue);
  13. AppSettingsScope? appSettings = (Settings! ["AppSettings"].PropertyValue as AppSettingsScope);
  14. Assert.Equal (10, appSettings!.Count); // 10 test properties
  15. Assert.Equal ("test", (((AppSettingsScope)Settings! ["AppSettings"].PropertyValue!)!) ["AppSettingsTestClass.ReferenceProperty"].PropertyValue);
  16. }
  17. [Fact]
  18. public void Empty_By_Default_Enabled ()
  19. {
  20. Enable (ConfigLocations.HardCoded);
  21. Assert.NotNull (Settings! ["AppSettings"].PropertyValue);
  22. AppSettingsScope? appSettings = (Settings! ["AppSettings"].PropertyValue as AppSettingsScope);
  23. Assert.Equal (10, appSettings!.Count); // 10 test properties
  24. Assert.Equal ("test", (((AppSettingsScope)Settings! ["AppSettings"].PropertyValue!)!) ["AppSettingsTestClass.ReferenceProperty"].PropertyValue);
  25. Disable (resetToHardCodedDefaults: true);
  26. }
  27. [Fact]
  28. public void Apply_ShouldApplyUpdatedProperties ()
  29. {
  30. Enable (ConfigLocations.HardCoded);
  31. Assert.Null (AppSettingsTestClass.NullableValueProperty);
  32. Assert.NotEmpty (AppSettings!);
  33. Assert.Null (AppSettings! ["AppSettingsTestClass.NullableValueProperty"].PropertyValue);
  34. AppSettingsTestClass.NullableValueProperty = true;
  35. UpdateToCurrentValues ();
  36. Assert.True (AppSettingsTestClass.NullableValueProperty);
  37. Assert.NotEmpty (AppSettings);
  38. Assert.True (AppSettings ["AppSettingsTestClass.NullableValueProperty"].PropertyValue as bool?);
  39. AppSettings ["AppSettingsTestClass.NullableValueProperty"].PropertyValue = false;
  40. Assert.False (AppSettings ["AppSettingsTestClass.NullableValueProperty"].PropertyValue as bool?);
  41. // ConfigurationManager.Settings should NOT apply theme settings
  42. Settings!.Apply ();
  43. Assert.True (AppSettingsTestClass.NullableValueProperty);
  44. // ConfigurationManager.Themes should NOT apply theme settings
  45. ThemeManager.Themes! [ThemeManager.Theme]!.Apply ();
  46. Assert.True (AppSettingsTestClass.NullableValueProperty);
  47. // ConfigurationManager.AppSettings should NOT apply theme settings
  48. AppSettings.Apply ();
  49. Assert.False (AppSettingsTestClass.NullableValueProperty);
  50. Disable (resetToHardCodedDefaults: true);
  51. }
  52. [Fact]
  53. public void TestNullable ()
  54. {
  55. Enable (ConfigLocations.HardCoded);
  56. AppSettingsTestClass.NullableValueProperty = null;
  57. Assert.Null (AppSettingsTestClass.NullableValueProperty);
  58. ResetToHardCodedDefaults ();
  59. Assert.Null (AppSettings! ["AppSettingsTestClass.NullableValueProperty"].PropertyValue);
  60. Apply ();
  61. Assert.Null (AppSettings! ["AppSettingsTestClass.NullableValueProperty"].PropertyValue);
  62. Assert.Null (AppSettingsTestClass.NullableValueProperty);
  63. AppSettingsTestClass.NullableValueProperty = true;
  64. UpdateToCurrentValues ();
  65. Assert.True ((bool)AppSettings! ["AppSettingsTestClass.NullableValueProperty"].PropertyValue!);
  66. Assert.True (AppSettingsTestClass.NullableValueProperty);
  67. Assert.NotNull (AppSettingsTestClass.NullableValueProperty);
  68. Apply ();
  69. Assert.True (AppSettingsTestClass.NullableValueProperty);
  70. Assert.NotNull (AppSettingsTestClass.NullableValueProperty);
  71. Disable (resetToHardCodedDefaults: true);
  72. }
  73. [Fact]
  74. public void TestSerialize_RoundTrip ()
  75. {
  76. Enable (ConfigLocations.HardCoded);
  77. AppSettingsScope initial = AppSettings!;
  78. string serialized = JsonSerializer.Serialize (AppSettings, SerializerContext.Options);
  79. var deserialized = JsonSerializer.Deserialize<AppSettingsScope> (serialized, SerializerContext.Options);
  80. Assert.NotEqual (initial, deserialized);
  81. Assert.Equal (deserialized!.Count, initial.Count);
  82. Disable (resetToHardCodedDefaults: true);
  83. }
  84. public class AppSettingsTestClass
  85. {
  86. [ConfigurationProperty (Scope = typeof (AppSettingsScope))]
  87. public static bool ValueProperty { get; set; }
  88. [ConfigurationProperty (Scope = typeof (AppSettingsScope))]
  89. public static bool? NullableValueProperty { get; set; }
  90. [ConfigurationProperty (Scope = typeof (AppSettingsScope))]
  91. public static string ReferenceProperty { get; set; } = "test";
  92. [ConfigurationProperty (Scope = typeof (AppSettingsScope))]
  93. public static string? NullableReferenceProperty { get; set; }
  94. }
  95. }