ConfigPropertyTests.cs 5.2 KB

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