AppScopeTests.cs 2.7 KB

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