AppScopeTests.cs 3.0 KB

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