2
0

ConfigPropertyTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Reflection;
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. using Terminal.Gui;
  6. using Xunit;
  7. namespace Terminal.Gui.ConfigurationTests;
  8. public class ConfigPropertyTests
  9. {
  10. [Fact]
  11. public void Apply_PropertyValueIsAppliedToStatic_String_Property()
  12. {
  13. // Arrange
  14. TestConfiguration.Reset ();
  15. var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
  16. var configProperty = new ConfigProperty
  17. {
  18. PropertyInfo = propertyInfo,
  19. PropertyValue = "UpdatedValue"
  20. };
  21. // Act
  22. var result = configProperty.Apply();
  23. // Assert
  24. Assert.Equal (1, TestConfiguration.TestStringPropertySetCount);
  25. Assert.True(result);
  26. Assert.Equal("UpdatedValue", TestConfiguration.TestStringProperty);
  27. TestConfiguration.Reset ();
  28. }
  29. [Fact]
  30. public void Apply_PropertyValueIsAppliedToStatic_Key_Property ()
  31. {
  32. // Arrange
  33. TestConfiguration.Reset ();
  34. var propertyInfo = typeof (TestConfiguration).GetProperty (nameof (TestConfiguration.TestKeyProperty));
  35. var configProperty = new ConfigProperty
  36. {
  37. PropertyInfo = propertyInfo,
  38. PropertyValue = Key.Q.WithCtrl
  39. };
  40. // Act
  41. var result = configProperty.Apply ();
  42. // Assert
  43. Assert.Equal(1, TestConfiguration.TestKeyPropertySetCount);
  44. Assert.True (result);
  45. Assert.Equal (Key.Q.WithCtrl, TestConfiguration.TestKeyProperty);
  46. TestConfiguration.Reset ();
  47. }
  48. [Fact]
  49. public void RetrieveValue_GetsCurrentValueOfStaticProperty()
  50. {
  51. // Arrange
  52. TestConfiguration.TestStringProperty = "CurrentValue";
  53. var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
  54. var configProperty = new ConfigProperty
  55. {
  56. PropertyInfo = propertyInfo
  57. };
  58. // Act
  59. var value = configProperty.RetrieveValue();
  60. // Assert
  61. Assert.Equal("CurrentValue", value);
  62. Assert.Equal("CurrentValue", configProperty.PropertyValue);
  63. }
  64. [Fact]
  65. public void UpdateValueFrom_Updates_String_Property_Value ()
  66. {
  67. // Arrange
  68. TestConfiguration.Reset ();
  69. var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
  70. var configProperty = new ConfigProperty
  71. {
  72. PropertyInfo = propertyInfo,
  73. PropertyValue = "InitialValue"
  74. };
  75. // Act
  76. var updatedValue = configProperty.UpdateValueFrom("NewValue");
  77. // Assert
  78. Assert.Equal (0, TestConfiguration.TestStringPropertySetCount);
  79. Assert.Equal("NewValue", updatedValue);
  80. Assert.Equal("NewValue", configProperty.PropertyValue);
  81. TestConfiguration.Reset ();
  82. }
  83. //[Fact]
  84. //public void UpdateValueFrom_InvalidType_ThrowsArgumentException()
  85. //{
  86. // // Arrange
  87. // var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
  88. // var configProperty = new ConfigProperty
  89. // {
  90. // PropertyInfo = propertyInfo
  91. // };
  92. // // Act & Assert
  93. // Assert.Throws<ArgumentException>(() => configProperty.UpdateValueFrom(123));
  94. //}
  95. [Fact]
  96. public void Apply_TargetInvocationException_ThrowsJsonException()
  97. {
  98. // Arrange
  99. var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
  100. var configProperty = new ConfigProperty
  101. {
  102. PropertyInfo = propertyInfo,
  103. PropertyValue = null // This will cause ArgumentNullException in the set accessor
  104. };
  105. // Act & Assert
  106. var exception = Assert.Throws<JsonException> (() => configProperty.Apply());
  107. }
  108. [Fact]
  109. public void GetJsonPropertyName_ReturnsJsonPropertyNameAttributeValue()
  110. {
  111. // Arrange
  112. var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
  113. // Act
  114. var jsonPropertyName = ConfigProperty.GetJsonPropertyName(propertyInfo);
  115. // Assert
  116. Assert.Equal("TestStringProperty", jsonPropertyName);
  117. }
  118. }
  119. public class TestConfiguration
  120. {
  121. private static string _testStringProperty = "Default";
  122. public static int TestStringPropertySetCount { get; set; }
  123. [SerializableConfigurationProperty]
  124. public static string TestStringProperty
  125. {
  126. get => _testStringProperty;
  127. set
  128. {
  129. TestStringPropertySetCount++;
  130. _testStringProperty = value ?? throw new ArgumentNullException (nameof (value));
  131. }
  132. }
  133. private static Key _testKeyProperty = Key.Esc;
  134. public static int TestKeyPropertySetCount { get; set; }
  135. [SerializableConfigurationProperty]
  136. public static Key TestKeyProperty
  137. {
  138. get => _testKeyProperty;
  139. set
  140. {
  141. TestKeyPropertySetCount++;
  142. _testKeyProperty = value ?? throw new ArgumentNullException (nameof (value));
  143. }
  144. }
  145. public static void Reset ()
  146. {
  147. TestStringPropertySetCount = 0;
  148. TestKeyPropertySetCount = 0;
  149. }
  150. }