JsonConverterTests.cs 13 KB

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