Files
MonoGame.Extended/tests/MonoGame.Extended.Tests/Serialization/ColorJsonConverterTests.cs
T
4ecb74f6b6 Fna.Extended (#893)
* FNA projects

* update actions

fixes MSBUILD : error MSB1011: Specify which project or solution file to
use because this folder contains more than one project or solution file.

* build tests

* remove test for issue #633

* FNA/XNA compatible

* GraphicsDevice Extensions

* remove MockGameWindow

---------

Co-authored-by: Christopher Whitley <103014489+AristurtleDev@users.noreply.github.com>
2024-06-23 22:49:58 -04:00

67 lines
1.9 KiB
C#

using System;
using System.IO;
using System.Text;
using System.Text.Json;
using Microsoft.Xna.Framework;
using MonoGame.Extended.Serialization;
namespace MonoGame.Extended.Tests.Serialization;
public sealed class ColorJsonConverterTests
{
private readonly ColorJsonConverter _converter = new ColorJsonConverter();
[Fact]
public void CanConvert_ColorType_ReturnsTrue()
{
var colorType = typeof(Color);
var result = _converter.CanConvert(colorType);
Assert.True(result);
}
[Fact]
public void CanConvert_NonColorType_ReturnsFalse()
{
var otherType = typeof(string);
var result = _converter.CanConvert(otherType);
Assert.False(result);
}
[Theory]
[InlineData("Red", 255, 0, 0, 255)]
[InlineData("#FF0000FF", 255, 0, 0, 255)]
public void Read_ValidColorJson_ReturnsExpectedColor(string jsonValue, byte r, byte g, byte b, byte a)
{
var json = $"\"{jsonValue}\"";
var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
reader.Read();
var actual = _converter.Read(ref reader, typeof(Color), new JsonSerializerOptions());
var expected = new Color(r, g, b, a);
Assert.Equal(expected, actual);
}
[Fact]
public void Write_ValidColor_WritesExpectedJson()
{
var expected = "#ff000000";
var color = ColorHelper.FromHex(expected);
using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream);
_converter.Write(writer, color, new JsonSerializerOptions());
writer.Flush();
var actual = Encoding.UTF8.GetString(stream.ToArray());
Assert.Equal($"\"{expected}\"", actual);
}
[Fact]
public void Write_NullWrier_ThrowArgumentNullException()
{
var color = Color.DarkOrange;
Assert.Throws<ArgumentNullException>(() => _converter.Write(null, color, new JsonSerializerOptions()));
}
}