AttributeJsonConverterTests.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. using System.Text.Json;
  2. namespace Terminal.Gui.ConfigurationTests;
  3. public class AttributeJsonConverterTests
  4. {
  5. [Fact]
  6. public void TestDeserialize ()
  7. {
  8. // Test deserializing from human-readable color names
  9. var json = "{\"Foreground\":\"Blue\",\"Background\":\"Green\"}";
  10. var attribute = JsonSerializer.Deserialize<Attribute> (json, ConfigurationManagerTests._jsonOptions);
  11. Assert.Equal (Color.Blue, attribute.Foreground.GetClosestNamedColor16 ());
  12. Assert.Equal (Color.Green, attribute.Background.GetClosestNamedColor16 ());
  13. // Test deserializing from RGB values
  14. json = "{\"Foreground\":\"rgb(255,0,0)\",\"Background\":\"rgb(0,255,0)\"}";
  15. attribute = JsonSerializer.Deserialize<Attribute> (json, ConfigurationManagerTests._jsonOptions);
  16. Assert.Equal (Color.Red, attribute.Foreground.GetClosestNamedColor16 ());
  17. Assert.Equal (Color.BrightGreen, attribute.Background.GetClosestNamedColor16 ());
  18. }
  19. [Fact]
  20. [AutoInitShutdown]
  21. public void TestSerialize ()
  22. {
  23. // Test serializing to human-readable color names
  24. var attribute = new Attribute (Color.Blue, Color.Green);
  25. string json = JsonSerializer.Serialize (attribute, ConfigurationManagerTests._jsonOptions);
  26. Assert.Equal ("{\"Foreground\":\"Blue\",\"Background\":\"Green\"}", json);
  27. }
  28. }