JsonConverterTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System.Text.Encodings.Web;
  2. using System.Text.Json;
  3. using System.Text.Unicode;
  4. namespace Terminal.Gui.ConfigurationTests;
  5. public class ColorJsonConverterTests {
  6. [Theory]
  7. [InlineData ("Black", Color.Black)]
  8. [InlineData ("Blue", Color.Blue)]
  9. [InlineData ("BrightBlue", Color.BrightBlue)]
  10. [InlineData ("BrightCyan", Color.BrightCyan)]
  11. [InlineData ("BrightGreen", Color.BrightGreen)]
  12. [InlineData ("BrightMagenta", Color.BrightMagenta)]
  13. [InlineData ("BrightRed", Color.BrightRed)]
  14. [InlineData ("BrightYellow", Color.BrightYellow)]
  15. [InlineData ("Yellow", Color.Yellow)]
  16. [InlineData ("Cyan", Color.Cyan)]
  17. [InlineData ("DarkGray", Color.DarkGray)]
  18. [InlineData ("Gray", Color.Gray)]
  19. [InlineData ("Green", Color.Green)]
  20. [InlineData ("Magenta", Color.Magenta)]
  21. [InlineData ("Red", Color.Red)]
  22. [InlineData ("White", Color.White)]
  23. public void TestColorDeserializationFromHumanReadableColorNames (string colorName, ColorName expectedColor)
  24. {
  25. // Arrange
  26. string json = $"\"{colorName}\"";
  27. // Act
  28. var actualColor = JsonSerializer.Deserialize<Color> (json, ConfigurationManagerTests._jsonOptions);
  29. // Assert
  30. Assert.Equal (new Color (expectedColor), actualColor);
  31. }
  32. [Theory]
  33. [InlineData (ColorName.Black, "Black")]
  34. [InlineData (ColorName.Blue, "Blue")]
  35. [InlineData (ColorName.Green, "Green")]
  36. [InlineData (ColorName.Cyan, "Cyan")]
  37. [InlineData (ColorName.Gray, "Gray")]
  38. [InlineData (ColorName.Red, "Red")]
  39. [InlineData (ColorName.Magenta, "Magenta")]
  40. [InlineData (ColorName.Yellow, "Yellow")]
  41. [InlineData (ColorName.DarkGray, "DarkGray")]
  42. [InlineData (ColorName.BrightBlue, "BrightBlue")]
  43. [InlineData (ColorName.BrightGreen, "BrightGreen")]
  44. [InlineData (ColorName.BrightCyan, "BrightCyan")]
  45. [InlineData (ColorName.BrightRed, "BrightRed")]
  46. [InlineData (ColorName.BrightMagenta, "BrightMagenta")]
  47. [InlineData (ColorName.BrightYellow, "BrightYellow")]
  48. [InlineData (ColorName.White, "White")]
  49. public void SerializesEnumValuesAsStrings (ColorName colorName, string expectedJson)
  50. {
  51. var converter = new ColorJsonConverter ();
  52. var options = new JsonSerializerOptions { Converters = { converter } };
  53. string serialized = JsonSerializer.Serialize<Color> (new Color (colorName), options);
  54. Assert.Equal ($"\"{expectedJson}\"", serialized);
  55. }
  56. [Fact]
  57. public void TestSerializeColor_Black ()
  58. {
  59. // Arrange
  60. string expectedJson = "\"Black\"";
  61. // Act
  62. string json = JsonSerializer.Serialize<Color> (new Color (Color.Black), 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. string expectedJson = "\"BrightRed\"";
  73. // Act
  74. string json = JsonSerializer.Serialize<Color> (new Color (Color.BrightRed), new JsonSerializerOptions {
  75. Converters = { new ColorJsonConverter () }
  76. });
  77. // Assert
  78. Assert.Equal (expectedJson, json);
  79. }
  80. [Fact]
  81. public void TestDeserializeColor_Black ()
  82. {
  83. // Arrange
  84. string json = "\"Black\"";
  85. var expectedColor = new Color (ColorName.Black);
  86. // Act
  87. var color = JsonSerializer.Deserialize<Color> (json, new JsonSerializerOptions {
  88. Converters = { new ColorJsonConverter () }
  89. });
  90. // Assert
  91. Assert.Equal (expectedColor, color);
  92. }
  93. [Fact]
  94. public void TestDeserializeColor_BrightRed ()
  95. {
  96. // Arrange
  97. string json = "\"BrightRed\"";
  98. var expectedColor = new Color (ColorName.BrightRed);
  99. // Act
  100. var color = JsonSerializer.Deserialize<Color> (json, new JsonSerializerOptions {
  101. Converters = { new ColorJsonConverter () }
  102. });
  103. // Assert
  104. Assert.Equal (expectedColor, color);
  105. }
  106. [Theory]
  107. [InlineData (0, 0, 0, "\"#000000\"")]
  108. [InlineData (0, 0, 1, "\"#000001\"")]
  109. public void SerializesToHexCode (int r, int g, int b, string expected)
  110. {
  111. // Arrange
  112. // Act
  113. string actual = JsonSerializer.Serialize (new Color (r, g, b), new JsonSerializerOptions {
  114. Converters = { new ColorJsonConverter () }
  115. });
  116. //Assert
  117. Assert.Equal (expected, actual);
  118. }
  119. [Theory]
  120. [InlineData ("\"#000000\"", 0, 0, 0)]
  121. public void DeserializesFromHexCode (string hexCode, int r, int g, int b)
  122. {
  123. // Arrange
  124. var expected = new Color (r, g, b);
  125. // Act
  126. var actual = JsonSerializer.Deserialize<Color> (hexCode, new JsonSerializerOptions {
  127. Converters = { new ColorJsonConverter () }
  128. });
  129. //Assert
  130. Assert.Equal (expected, actual);
  131. }
  132. [Theory]
  133. [InlineData ("\"rgb(0,0,0)\"", 0, 0, 0)]
  134. public void DeserializesFromRgb (string rgb, int r, int g, int b)
  135. {
  136. // Arrange
  137. var expected = new Color (r, g, b);
  138. // Act
  139. var actual = JsonSerializer.Deserialize<Color> (rgb, new JsonSerializerOptions {
  140. Converters = { new ColorJsonConverter () }
  141. });
  142. //Assert
  143. Assert.Equal (expected, actual);
  144. }
  145. }
  146. public class AttributeJsonConverterTests {
  147. [Fact]
  148. public void TestDeserialize ()
  149. {
  150. // Test deserializing from human-readable color names
  151. string json = "{\"Foreground\":\"Blue\",\"Background\":\"Green\"}";
  152. var attribute = JsonSerializer.Deserialize<Attribute> (json, ConfigurationManagerTests._jsonOptions);
  153. Assert.Equal (Color.Blue, attribute.Foreground.GetClosestNamedColor ());
  154. Assert.Equal (Color.Green, attribute.Background.GetClosestNamedColor ());
  155. // Test deserializing from RGB values
  156. json = "{\"Foreground\":\"rgb(255,0,0)\",\"Background\":\"rgb(0,255,0)\"}";
  157. attribute = JsonSerializer.Deserialize<Attribute> (json, ConfigurationManagerTests._jsonOptions);
  158. Assert.Equal (Color.Red, attribute.Foreground.GetClosestNamedColor ());
  159. Assert.Equal (Color.BrightGreen, attribute.Background.GetClosestNamedColor ());
  160. }
  161. [Fact, AutoInitShutdown]
  162. public void TestSerialize ()
  163. {
  164. // Test serializing to human-readable color names
  165. var attribute = new Attribute (Color.Blue, Color.Green);
  166. string json = JsonSerializer.Serialize<Attribute> (attribute, ConfigurationManagerTests._jsonOptions);
  167. Assert.Equal ("{\"Foreground\":\"Blue\",\"Background\":\"Green\"}", json);
  168. }
  169. }
  170. public class ColorSchemeJsonConverterTests {
  171. //string json = @"
  172. // {
  173. // ""ColorSchemes"": {
  174. // ""Base"": {
  175. // ""normal"": {
  176. // ""foreground"": ""White"",
  177. // ""background"": ""Blue""
  178. // },
  179. // ""focus"": {
  180. // ""foreground"": ""Black"",
  181. // ""background"": ""Gray""
  182. // },
  183. // ""hotNormal"": {
  184. // ""foreground"": ""BrightCyan"",
  185. // ""background"": ""Blue""
  186. // },
  187. // ""hotFocus"": {
  188. // ""foreground"": ""BrightBlue"",
  189. // ""background"": ""Gray""
  190. // },
  191. // ""disabled"": {
  192. // ""foreground"": ""DarkGray"",
  193. // ""background"": ""Blue""
  194. // }
  195. // }
  196. // }
  197. // }";
  198. [Fact, AutoInitShutdown]
  199. public void TestColorSchemesSerialization ()
  200. {
  201. // Arrange
  202. var expectedColorScheme = new ColorScheme {
  203. Normal = new Attribute (Color.White, Color.Blue),
  204. Focus = new Attribute (Color.Black, Color.Gray),
  205. HotNormal = new Attribute (Color.BrightCyan, Color.Blue),
  206. HotFocus = new Attribute (Color.BrightBlue, Color.Gray),
  207. Disabled = new Attribute (Color.DarkGray, Color.Blue)
  208. };
  209. string serializedColorScheme = JsonSerializer.Serialize<ColorScheme> (expectedColorScheme, ConfigurationManagerTests._jsonOptions);
  210. // Act
  211. var actualColorScheme = JsonSerializer.Deserialize<ColorScheme> (serializedColorScheme, ConfigurationManagerTests._jsonOptions);
  212. // Assert
  213. Assert.Equal (expectedColorScheme, actualColorScheme);
  214. }
  215. }
  216. public class KeyCodeJsonConverterTests {
  217. [Theory]
  218. [InlineData (KeyCode.A, "A")]
  219. [InlineData (KeyCode.A | KeyCode.ShiftMask, "A, ShiftMask")]
  220. [InlineData (KeyCode.A | KeyCode.CtrlMask, "A, CtrlMask")]
  221. [InlineData (KeyCode.A | KeyCode.AltMask | KeyCode.CtrlMask, "A, CtrlMask, AltMask")]
  222. [InlineData ((KeyCode)'a' | KeyCode.AltMask | KeyCode.CtrlMask, "Space, A, CtrlMask, AltMask")]
  223. [InlineData ((KeyCode)'a' | KeyCode.ShiftMask, "Space, A, ShiftMask")]
  224. [InlineData (KeyCode.Delete | KeyCode.AltMask | KeyCode.CtrlMask, "Delete, CtrlMask, AltMask")]
  225. [InlineData (KeyCode.D4, "D4")]
  226. [InlineData (KeyCode.Esc, "Esc")]
  227. public void TestKeyRoundTripConversion (KeyCode key, string expectedStringTo)
  228. {
  229. // Arrange
  230. var options = new JsonSerializerOptions ();
  231. options.Converters.Add (new KeyCodeJsonConverter ());
  232. // Act
  233. string json = JsonSerializer.Serialize (key, options);
  234. var deserializedKey = JsonSerializer.Deserialize<KeyCode> (json, options);
  235. // Assert
  236. Assert.Equal (expectedStringTo, deserializedKey.ToString ());
  237. }
  238. }
  239. public class KeyJsonConverterTests {
  240. [Theory]
  241. [InlineData (KeyCode.A, "\"a\"")]
  242. [InlineData ((KeyCode)'â', "\"â\"")]
  243. [InlineData (KeyCode.A | KeyCode.ShiftMask, "\"A\"")]
  244. [InlineData (KeyCode.A | KeyCode.CtrlMask, "\"Ctrl+A\"")]
  245. [InlineData (KeyCode.A | KeyCode.AltMask | KeyCode.CtrlMask, "\"Ctrl+Alt+A\"")]
  246. [InlineData ((KeyCode)'a' | KeyCode.AltMask | KeyCode.CtrlMask, "\"Ctrl+Alt+A\"")]
  247. [InlineData ((KeyCode)'a' | KeyCode.ShiftMask, "\"A\"")]
  248. [InlineData (KeyCode.Delete | KeyCode.AltMask | KeyCode.CtrlMask, "\"Ctrl+Alt+Delete\"")]
  249. [InlineData (KeyCode.D4, "\"4\"")]
  250. [InlineData (KeyCode.Esc, "\"Esc\"")]
  251. public void TestKey_Serialize (KeyCode key, string expected)
  252. {
  253. // Arrange
  254. var options = new JsonSerializerOptions ();
  255. options.Converters.Add (new KeyJsonConverter ());
  256. options.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
  257. // Act
  258. string json = JsonSerializer.Serialize ((Key)key, options);
  259. // Assert
  260. Assert.Equal (expected, json);
  261. }
  262. [Theory]
  263. [InlineData (KeyCode.A, "a")]
  264. [InlineData (KeyCode.A | KeyCode.ShiftMask, "A")]
  265. [InlineData (KeyCode.A | KeyCode.CtrlMask, "Ctrl+A")]
  266. [InlineData (KeyCode.A | KeyCode.AltMask | KeyCode.CtrlMask, "Ctrl+Alt+A")]
  267. [InlineData ((KeyCode)'a' | KeyCode.AltMask | KeyCode.CtrlMask, "Ctrl+Alt+A")]
  268. [InlineData ((KeyCode)'a' | KeyCode.ShiftMask, "A")]
  269. [InlineData (KeyCode.Delete | KeyCode.AltMask | KeyCode.CtrlMask, "Ctrl+Alt+Delete")]
  270. [InlineData (KeyCode.D4, "4")]
  271. [InlineData (KeyCode.Esc, "Esc")]
  272. public void TestKeyRoundTripConversion (KeyCode key, string expectedStringTo)
  273. {
  274. // Arrange
  275. var options = new JsonSerializerOptions ();
  276. options.Converters.Add (new KeyJsonConverter ());
  277. var encoderSettings = new TextEncoderSettings ();
  278. encoderSettings.AllowCharacters ('+', '-');
  279. encoderSettings.AllowRange (UnicodeRanges.BasicLatin);
  280. options.Encoder = JavaScriptEncoder.Create (encoderSettings);
  281. // Act
  282. string json = JsonSerializer.Serialize ((Key)key, options);
  283. var deserializedKey = JsonSerializer.Deserialize<Key> (json, options);
  284. // Assert
  285. Assert.Equal (expectedStringTo, deserializedKey.ToString ());
  286. }
  287. }