using System.Text.Json;
using Jellyfin.Data.Enums;
using Jellyfin.Extensions.Json.Converters;
using Xunit;
namespace Jellyfin.Extensions.Tests.Json.Converters;
public class JsonDefaultStringEnumConverterTests
{
private readonly JsonSerializerOptions _jsonOptions = new() { Converters = { new JsonDefaultStringEnumConverterFactory() } };
///
/// Test to ensure that `null` and empty string are deserialized to the default value.
///
/// The input string.
/// The expected enum value.
[Theory]
[InlineData("\"\"", MediaStreamProtocol.http)]
[InlineData("\"Http\"", MediaStreamProtocol.http)]
[InlineData("\"Hls\"", MediaStreamProtocol.hls)]
public void Deserialize_Enum_Direct(string input, MediaStreamProtocol output)
{
var value = JsonSerializer.Deserialize(input, _jsonOptions);
Assert.Equal(output, value);
}
///
/// Test to ensure that `null` and empty string are deserialized to the default value.
///
/// The input string.
/// The expected enum value.
[Theory]
[InlineData(null, MediaStreamProtocol.http)]
[InlineData("\"\"", MediaStreamProtocol.http)]
[InlineData("\"Http\"", MediaStreamProtocol.http)]
[InlineData("\"Hls\"", MediaStreamProtocol.hls)]
public void Deserialize_Enum(string? input, MediaStreamProtocol output)
{
input ??= "null";
var json = $"{{ \"EnumValue\": {input} }}";
var value = JsonSerializer.Deserialize(json, _jsonOptions);
Assert.NotNull(value);
Assert.Equal(output, value.EnumValue);
}
///
/// Test to ensure that empty string is deserialized to the default value,
/// and `null` is deserialized to `null`.
///
/// The input string.
/// The expected enum value.
[Theory]
[InlineData(null, null)]
[InlineData("\"\"", MediaStreamProtocol.http)]
[InlineData("\"Http\"", MediaStreamProtocol.http)]
[InlineData("\"Hls\"", MediaStreamProtocol.hls)]
public void Deserialize_Enum_Nullable(string? input, MediaStreamProtocol? output)
{
input ??= "null";
var json = $"{{ \"EnumValue\": {input} }}";
var value = JsonSerializer.Deserialize(json, _jsonOptions);
Assert.NotNull(value);
Assert.Equal(output, value.EnumValue);
}
///
/// Ensures that the roundtrip serialization & deserialization is successful.
///
/// Input enum.
/// Output enum.
[Theory]
[InlineData(MediaStreamProtocol.http, MediaStreamProtocol.http)]
[InlineData(MediaStreamProtocol.hls, MediaStreamProtocol.hls)]
public void Enum_RoundTrip(MediaStreamProtocol input, MediaStreamProtocol output)
{
var inputObj = new TestClass { EnumValue = input };
var outputObj = JsonSerializer.Deserialize(JsonSerializer.Serialize(inputObj, _jsonOptions), _jsonOptions);
Assert.NotNull(outputObj);
Assert.Equal(output, outputObj.EnumValue);
}
///
/// Ensures that the roundtrip serialization & deserialization is successful, including null.
///
/// Input enum.
/// Output enum.
[Theory]
[InlineData(MediaStreamProtocol.http, MediaStreamProtocol.http)]
[InlineData(MediaStreamProtocol.hls, MediaStreamProtocol.hls)]
[InlineData(null, null)]
public void Enum_RoundTrip_Nullable(MediaStreamProtocol? input, MediaStreamProtocol? output)
{
var inputObj = new NullTestClass { EnumValue = input };
var outputObj = JsonSerializer.Deserialize(JsonSerializer.Serialize(inputObj, _jsonOptions), _jsonOptions);
Assert.NotNull(outputObj);
Assert.Equal(output, outputObj.EnumValue);
}
private sealed class TestClass
{
public MediaStreamProtocol EnumValue { get; set; }
}
private sealed class NullTestClass
{
public MediaStreamProtocol? EnumValue { get; set; }
}
}