JsonConverterTests.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using Xunit;
  2. using Terminal.Gui.Configuration;
  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. namespace Terminal.Gui.ConfigurationTests {
  10. public class ColorJsonConverterTests {
  11. [Theory]
  12. [InlineData ("Black", Color.Black)]
  13. [InlineData ("Blue", Color.Blue)]
  14. [InlineData ("BrightBlue", Color.BrightBlue)]
  15. [InlineData ("BrightCyan", Color.BrightCyan)]
  16. [InlineData ("BrightGreen", Color.BrightGreen)]
  17. [InlineData ("BrightMagenta", Color.BrightMagenta)]
  18. [InlineData ("BrightRed", Color.BrightRed)]
  19. [InlineData ("BrightYellow", Color.BrightYellow)]
  20. [InlineData ("Brown", Color.Brown)]
  21. [InlineData ("Cyan", Color.Cyan)]
  22. [InlineData ("DarkGray", Color.DarkGray)]
  23. [InlineData ("Gray", Color.Gray)]
  24. [InlineData ("Green", Color.Green)]
  25. [InlineData ("Magenta", Color.Magenta)]
  26. [InlineData ("Red", Color.Red)]
  27. [InlineData ("White", Color.White)]
  28. public void TestColorDeserializationFromHumanReadableColorNames (string colorName, Color expectedColor)
  29. {
  30. // Arrange
  31. string json = $"\"{colorName}\"";
  32. // Act
  33. Color actualColor = JsonSerializer.Deserialize<Color> (json, ConfigurationManagerTests._jsonOptions);
  34. // Assert
  35. Assert.Equal (expectedColor, actualColor);
  36. }
  37. [Theory]
  38. [InlineData (Color.Black, "Black")]
  39. [InlineData (Color.Blue, "Blue")]
  40. [InlineData (Color.Green, "Green")]
  41. [InlineData (Color.Cyan, "Cyan")]
  42. [InlineData (Color.Gray, "Gray")]
  43. [InlineData (Color.Red, "Red")]
  44. [InlineData (Color.Magenta, "Magenta")]
  45. [InlineData (Color.Brown, "Brown")]
  46. [InlineData (Color.DarkGray, "DarkGray")]
  47. [InlineData (Color.BrightBlue, "BrightBlue")]
  48. [InlineData (Color.BrightGreen, "BrightGreen")]
  49. [InlineData (Color.BrightCyan, "BrightCyan")]
  50. [InlineData (Color.BrightRed, "BrightRed")]
  51. [InlineData (Color.BrightMagenta, "BrightMagenta")]
  52. [InlineData (Color.BrightYellow, "BrightYellow")]
  53. [InlineData (Color.White, "White")]
  54. public void SerializesEnumValuesAsStrings (Color color, string expectedJson)
  55. {
  56. var converter = new ColorJsonConverter ();
  57. var options = new JsonSerializerOptions { Converters = { converter } };
  58. var serialized = JsonSerializer.Serialize (color, options);
  59. Assert.Equal ($"\"{expectedJson}\"", serialized);
  60. }
  61. [Fact]
  62. public void TestSerializeColor_Black ()
  63. {
  64. // Arrange
  65. var color = Color.Black;
  66. var expectedJson = "\"Black\"";
  67. // Act
  68. var json = JsonSerializer.Serialize (color, new JsonSerializerOptions {
  69. Converters = { new ColorJsonConverter () }
  70. });
  71. // Assert
  72. Assert.Equal (expectedJson, json);
  73. }
  74. [Fact]
  75. public void TestSerializeColor_BrightRed ()
  76. {
  77. // Arrange
  78. var color = Color.BrightRed;
  79. var expectedJson = "\"BrightRed\"";
  80. // Act
  81. var json = JsonSerializer.Serialize (color, new JsonSerializerOptions {
  82. Converters = { new ColorJsonConverter () }
  83. });
  84. // Assert
  85. Assert.Equal (expectedJson, json);
  86. }
  87. [Fact]
  88. public void TestDeserializeColor_Black ()
  89. {
  90. // Arrange
  91. var json = "\"Black\"";
  92. var expectedColor = Color.Black;
  93. // Act
  94. var color = JsonSerializer.Deserialize<Color> (json, new JsonSerializerOptions {
  95. Converters = { new ColorJsonConverter () }
  96. });
  97. // Assert
  98. Assert.Equal (expectedColor, color);
  99. }
  100. [Fact]
  101. public void TestDeserializeColor_BrightRed ()
  102. {
  103. // Arrange
  104. var json = "\"BrightRed\"";
  105. var expectedColor = Color.BrightRed;
  106. // Act
  107. var color = JsonSerializer.Deserialize<Color> (json, new JsonSerializerOptions {
  108. Converters = { new ColorJsonConverter () }
  109. });
  110. // Assert
  111. Assert.Equal (expectedColor, color);
  112. }
  113. }
  114. public class AttributeJsonConverterTests {
  115. [Fact, AutoInitShutdown]
  116. public void TestDeserialize ()
  117. {
  118. // Test deserializing from human-readable color names
  119. var json = "{\"Foreground\":\"Blue\",\"Background\":\"Green\"}";
  120. var attribute = JsonSerializer.Deserialize<Attribute> (json, ConfigurationManagerTests._jsonOptions);
  121. Assert.Equal (Color.Blue, attribute.Foreground);
  122. Assert.Equal (Color.Green, attribute.Background);
  123. // Test deserializing from RGB values
  124. json = "{\"Foreground\":\"rgb(255,0,0)\",\"Background\":\"rgb(0,255,0)\"}";
  125. attribute = JsonSerializer.Deserialize<Attribute> (json, ConfigurationManagerTests._jsonOptions);
  126. Assert.Equal (Color.BrightRed, attribute.Foreground);
  127. Assert.Equal (Color.BrightGreen, attribute.Background);
  128. }
  129. [Fact, AutoInitShutdown]
  130. public void TestSerialize ()
  131. {
  132. // Test serializing to human-readable color names
  133. var attribute = new Attribute (Color.Blue, Color.Green);
  134. var json = JsonSerializer.Serialize<Attribute> (attribute, ConfigurationManagerTests._jsonOptions);
  135. Assert.Equal ("{\"Foreground\":\"Blue\",\"Background\":\"Green\"}", json);
  136. }
  137. }
  138. public class ColorSchemeJsonConverterTests {
  139. //string json = @"
  140. // {
  141. // ""ColorSchemes"": {
  142. // ""Base"": {
  143. // ""normal"": {
  144. // ""foreground"": ""White"",
  145. // ""background"": ""Blue""
  146. // },
  147. // ""focus"": {
  148. // ""foreground"": ""Black"",
  149. // ""background"": ""Gray""
  150. // },
  151. // ""hotNormal"": {
  152. // ""foreground"": ""BrightCyan"",
  153. // ""background"": ""Blue""
  154. // },
  155. // ""hotFocus"": {
  156. // ""foreground"": ""BrightBlue"",
  157. // ""background"": ""Gray""
  158. // },
  159. // ""disabled"": {
  160. // ""foreground"": ""DarkGray"",
  161. // ""background"": ""Blue""
  162. // }
  163. // }
  164. // }
  165. // }";
  166. [Fact, AutoInitShutdown]
  167. public void TestColorSchemesSerialization ()
  168. {
  169. // Arrange
  170. var expectedColorScheme = new ColorScheme {
  171. Normal = Attribute.Make (Color.White, Color.Blue),
  172. Focus = Attribute.Make (Color.Black, Color.Gray),
  173. HotNormal = Attribute.Make (Color.BrightCyan, Color.Blue),
  174. HotFocus = Attribute.Make (Color.BrightBlue, Color.Gray),
  175. Disabled = Attribute.Make (Color.DarkGray, Color.Blue)
  176. };
  177. var serializedColorScheme = JsonSerializer.Serialize<ColorScheme> (expectedColorScheme, ConfigurationManagerTests._jsonOptions);
  178. // Act
  179. var actualColorScheme = JsonSerializer.Deserialize<ColorScheme> (serializedColorScheme, ConfigurationManagerTests._jsonOptions);
  180. // Assert
  181. Assert.Equal (expectedColorScheme, actualColorScheme);
  182. }
  183. }
  184. public class KeyJsonConverterTests {
  185. [Theory, AutoInitShutdown]
  186. [InlineData (Key.A, "A")]
  187. [InlineData (Key.a | Key.ShiftMask, "a, ShiftMask")]
  188. [InlineData (Key.A | Key.CtrlMask, "A, CtrlMask")]
  189. [InlineData (Key.a | Key.AltMask | Key.CtrlMask, "a, CtrlMask, AltMask")]
  190. [InlineData (Key.Delete | Key.AltMask | Key.CtrlMask, "Delete, CtrlMask, AltMask")]
  191. [InlineData (Key.D4, "D4")]
  192. [InlineData (Key.Esc, "Esc")]
  193. public void TestKeyRoundTripConversion (Key key, string expectedStringTo)
  194. {
  195. // Arrange
  196. var options = new JsonSerializerOptions ();
  197. options.Converters.Add (new KeyJsonConverter ());
  198. // Act
  199. var json = JsonSerializer.Serialize (key, options);
  200. var deserializedKey = JsonSerializer.Deserialize<Key> (json, options);
  201. // Assert
  202. Assert.Equal (expectedStringTo, deserializedKey.ToString ());
  203. }
  204. }
  205. }