AppScopeTests.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Text.Json;
  2. using UnitTests;
  3. using static Terminal.Gui.ConfigurationManager;
  4. namespace Terminal.Gui.ConfigurationTests;
  5. public class AppScopeTests
  6. {
  7. public static readonly JsonSerializerOptions _jsonOptions = new ()
  8. {
  9. Converters =
  10. {
  11. //new AttributeJsonConverter (),
  12. //new ColorJsonConverter ()
  13. }
  14. };
  15. [Fact]
  16. [AutoInitShutdown (configLocation: ConfigLocations.Default)]
  17. public void Apply_ShouldApplyUpdatedProperties ()
  18. {
  19. Reset ();
  20. Assert.Null (AppSettingsTestClass.TestProperty);
  21. Assert.NotEmpty (AppSettings);
  22. Assert.Null (AppSettings ["AppSettingsTestClass.TestProperty"].PropertyValue);
  23. AppSettingsTestClass.TestProperty = true;
  24. Reset ();
  25. Assert.True (AppSettingsTestClass.TestProperty);
  26. Assert.NotEmpty (AppSettings);
  27. Assert.Null (AppSettings ["AppSettingsTestClass.TestProperty"].PropertyValue as bool?);
  28. AppSettings ["AppSettingsTestClass.TestProperty"].PropertyValue = false;
  29. Assert.False (AppSettings ["AppSettingsTestClass.TestProperty"].PropertyValue as bool?);
  30. // ConfigurationManager.Settings should NOT apply theme settings
  31. Settings.Apply ();
  32. Assert.True (AppSettingsTestClass.TestProperty);
  33. // ConfigurationManager.Themes should NOT apply theme settings
  34. ThemeManager.Themes! [ThemeManager.SelectedTheme]!.Apply ();
  35. Assert.True (AppSettingsTestClass.TestProperty);
  36. // ConfigurationManager.AppSettings should NOT apply theme settings
  37. AppSettings.Apply ();
  38. Assert.False (AppSettingsTestClass.TestProperty);
  39. }
  40. [Fact]
  41. public void TestNullable ()
  42. {
  43. AppSettingsTestClass.TestProperty = null;
  44. Assert.Null (AppSettingsTestClass.TestProperty);
  45. Initialize ();
  46. GetHardCodedDefaults ();
  47. Apply ();
  48. Assert.Null (AppSettingsTestClass.TestProperty);
  49. AppSettingsTestClass.TestProperty = true;
  50. Initialize ();
  51. GetHardCodedDefaults ();
  52. Assert.NotNull (AppSettingsTestClass.TestProperty);
  53. Apply ();
  54. Assert.NotNull (AppSettingsTestClass.TestProperty);
  55. }
  56. [Fact]
  57. public void TestSerialize_RoundTrip ()
  58. {
  59. Reset ();
  60. AppScope initial = AppSettings;
  61. string serialized = JsonSerializer.Serialize (AppSettings, _jsonOptions);
  62. var deserialized = JsonSerializer.Deserialize<AppScope> (serialized, _jsonOptions);
  63. Assert.NotEqual (initial, deserialized);
  64. Assert.Equal (deserialized.Count, initial.Count);
  65. }
  66. public class AppSettingsTestClass
  67. {
  68. [SerializableConfigurationProperty (Scope = typeof (AppScope))]
  69. public static bool? TestProperty { get; set; }
  70. }
  71. }