AttributeJsonConverterTests.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Text.Json;
  2. using Moq;
  3. using UnitTests;
  4. namespace ConfigurationTests;
  5. public class AttributeJsonConverterTests
  6. {
  7. public static readonly JsonSerializerOptions JsonOptions = new ()
  8. {
  9. Converters = { new AttributeJsonConverter (), new ColorJsonConverter () }
  10. };
  11. [Fact]
  12. public void TestDeserialize ()
  13. {
  14. // Test deserializing from human-readable color names
  15. var json = "{\"Foreground\":\"Blue\",\"Background\":\"Green\"}";
  16. var attribute = JsonSerializer.Deserialize<Attribute> (json, JsonOptions);
  17. Assert.Equal (Color.Blue, attribute.Foreground.GetClosestNamedColor16 ());
  18. Assert.Equal (Color.Green, attribute.Background.GetClosestNamedColor16 ());
  19. // Test deserializing from RGB values
  20. json = "{\"Foreground\":\"rgb(255,0,0)\",\"Background\":\"rgb(0,255,0)\"}";
  21. attribute = JsonSerializer.Deserialize<Attribute> (json, JsonOptions);
  22. Assert.Equal (Color.Red, attribute.Foreground.GetClosestNamedColor16 ());
  23. Assert.Equal (Color.BrightGreen, attribute.Background.GetClosestNamedColor16 ());
  24. }
  25. [Fact]
  26. public void Deserialize_TextStyle ()
  27. {
  28. var justStyleJson = "\"Bold\"";
  29. TextStyle textStyle = JsonSerializer.Deserialize<TextStyle> (justStyleJson, JsonOptions);
  30. Assert.Equal (TextStyle.Bold, textStyle);
  31. justStyleJson = "\"Bold,Underline\"";
  32. textStyle = JsonSerializer.Deserialize<TextStyle> (justStyleJson, JsonOptions);
  33. Assert.Equal (TextStyle.Bold | TextStyle.Underline, textStyle);
  34. var json = "{\"Foreground\":\"Blue\",\"Background\":\"Green\",\"Style\":\"Bold\"}";
  35. Attribute attribute = JsonSerializer.Deserialize<Attribute> (json, JsonOptions);
  36. Assert.Equal (TextStyle.Bold, attribute.Style);
  37. }
  38. [Fact]
  39. public void TestSerialize ()
  40. {
  41. // Test serializing to human-readable color names
  42. var attribute = new Attribute (Color.Blue, Color.Green);
  43. string json = JsonSerializer.Serialize (attribute, JsonOptions);
  44. Assert.Equal ("{\"Foreground\":\"Blue\",\"Background\":\"Green\"}", json);
  45. }
  46. [Fact]
  47. public void Serialize_TextStyle ()
  48. {
  49. // Test serializing to human-readable color names
  50. var attribute = new Attribute (Color.Blue, Color.Green, TextStyle.Bold);
  51. string json = JsonSerializer.Serialize (attribute, JsonOptions);
  52. Assert.Equal ("{\"Foreground\":\"Blue\",\"Background\":\"Green\",\"Style\":\"Bold\"}", json);
  53. attribute = new Attribute (Color.Blue, Color.Green, TextStyle.Bold | TextStyle.Italic);
  54. json = JsonSerializer.Serialize (attribute, JsonOptions);
  55. Assert.Equal ("{\"Foreground\":\"Blue\",\"Background\":\"Green\",\"Style\":\"Bold, Italic\"}", json);
  56. }
  57. [Fact]
  58. public void JsonRoundTrip_PreservesEquality ()
  59. {
  60. Attribute original = new (Color.Red, Color.Green, TextStyle.None);
  61. string json = JsonSerializer.Serialize (original, new JsonSerializerOptions
  62. {
  63. Converters = { new AttributeJsonConverter (), new ColorJsonConverter () }
  64. });
  65. Attribute roundTripped = JsonSerializer.Deserialize<Attribute> (json, new JsonSerializerOptions
  66. {
  67. Converters = { new AttributeJsonConverter (), new ColorJsonConverter () }
  68. })!;
  69. Assert.Equal (original, roundTripped); // ✅ This should pass if all fields are faithfully round-tripped
  70. }
  71. }