JsonConverterTests.cs 12 KB

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