JsonConverterTests.cs 8.2 KB

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