JsonConverterTests.cs 8.1 KB

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