af1152b001
Refactored all C# test files to use explicit namespace declarations and moved all using directives inside the namespace block for consistency. Updated assembly info and cache files to reflect the new build. Adjusted MvcTestingAppManifest.json to use fully qualified assembly names. No functional changes; all updates are related to code style, organization, and project metadata.
117 lines
4.4 KiB
C#
117 lines
4.4 KiB
C#
// <copyright file="JsonDefaultStringEnumConverterTests.cs" company="PlaceholderCompany">
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
// </copyright>
|
|
|
|
namespace Jellyfin.Extensions.Tests.Json.Converters;
|
|
|
|
using System.Text.Json;
|
|
using Jellyfin.Data.Enums;
|
|
using Jellyfin.Extensions.Json.Converters;
|
|
using Xunit;
|
|
|
|
public class JsonDefaultStringEnumConverterTests
|
|
{
|
|
private readonly JsonSerializerOptions _jsonOptions = new() { Converters = { new JsonDefaultStringEnumConverterFactory() } };
|
|
|
|
/// <summary>
|
|
/// Test to ensure that `null` and empty string are deserialized to the default value.
|
|
/// </summary>
|
|
/// <param name="input">The input string.</param>
|
|
/// <param name="output">The expected enum value.</param>
|
|
[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<MediaStreamProtocol>(input, _jsonOptions);
|
|
Assert.Equal(output, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test to ensure that `null` and empty string are deserialized to the default value.
|
|
/// </summary>
|
|
/// <param name="input">The input string.</param>
|
|
/// <param name="output">The expected enum value.</param>
|
|
[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<TestClass>(json, _jsonOptions);
|
|
Assert.NotNull(value);
|
|
Assert.Equal(output, value.EnumValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test to ensure that empty string is deserialized to the default value,
|
|
/// and `null` is deserialized to `null`.
|
|
/// </summary>
|
|
/// <param name="input">The input string.</param>
|
|
/// <param name="output">The expected enum value.</param>
|
|
[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<NullTestClass>(json, _jsonOptions);
|
|
Assert.NotNull(value);
|
|
Assert.Equal(output, value.EnumValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ensures that the roundtrip serialization & deserialization is successful.
|
|
/// </summary>
|
|
/// <param name="input">Input enum.</param>
|
|
/// <param name="output">Output enum.</param>
|
|
[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<TestClass>(JsonSerializer.Serialize(inputObj, _jsonOptions), _jsonOptions);
|
|
|
|
Assert.NotNull(outputObj);
|
|
Assert.Equal(output, outputObj.EnumValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ensures that the roundtrip serialization & deserialization is successful, including null.
|
|
/// </summary>
|
|
/// <param name="input">Input enum.</param>
|
|
/// <param name="output">Output enum.</param>
|
|
[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<NullTestClass>(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; }
|
|
}
|
|
}
|