AttributeJsonConverterTests.cs 1.4 KB

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