JsonConverterTests.cs 7.0 KB

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