Refactor and modernize JSON converter test code

Refactor test code in Jellyfin.Extensions.Tests for clarity and consistency:
- Use explicit object initializers and collection expressions
- Standardize field naming and use of this.
- Add/improve XML doc comments for test methods
- Use new(...) syntax for Guid/Version instantiation
- Convert file-scoped to block-scoped namespaces in key tests
- Nest test classes and use instance methods in enum tests
- Enable XML docs and suppress select warnings in csproj
- No changes to test or converter logic; style and maintainability only
This commit is contained in:
2026-02-22 10:31:23 -05:00
parent d5522c6fb3
commit dbeec2e9f0
17 changed files with 302 additions and 245 deletions
@@ -4,16 +4,18 @@
namespace Jellyfin.Extensions.Tests.Json.Converters
{
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Jellyfin.Extensions.Tests.Json.Models;
using MediaBrowser.Model.Session;
using Xunit;
/// <summary>
/// Tests for JSON comma delimited IReadOnlyList converter.
/// </summary>
public class JsonCommaDelimitedIReadOnlyListTests
{
private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions()
private readonly JsonSerializerOptions jsonOptions = new()
{
Converters =
{
@@ -21,94 +23,108 @@ namespace Jellyfin.Extensions.Tests.Json.Converters
},
};
/// <summary>
/// Tests that deserializing comma-delimited string succeeds.
/// </summary>
[Fact]
public void Deserialize_String_Valid_Success()
{
GenericBodyIReadOnlyListModel<string>
{ desiredValue = new GenericBodyIReadOnlyListModel<string>
GenericBodyIReadOnlyListModel<string> desiredValue = new()
{
Value = new[] { "a", "b", "c" },
Value = ["a", "b", "c"],
};
GenericBodyIReadOnlyListModel<string value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": ""a,b,c"" }", _jsonOptions);
GenericBodyIReadOnlyListModel<string>? value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": ""a,b,c"" }", this.jsonOptions);
Assert.Equal(desiredValue.Value, value?.Value);
}
/// <summary>
/// Tests that deserializing comma-delimited string with spaces succeeds.
/// </summary>
[Fact]
public void Deserialize_String_Space_Valid_Success()
{
GenericBodyIReadOnlyListModel<string>
{ desiredValue = new GenericBodyIReadOnlyListModel<string>
GenericBodyIReadOnlyListModel<string> desiredValue = new()
{
Value = new[] { "a", "b", "c" },
Value = ["a", "b", "c"],
};
GenericBodyIReadOnlyListModel<string value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": ""a, b, c"" }", _jsonOptions);
GenericBodyIReadOnlyListModel<string>? value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": ""a, b, c"" }", this.jsonOptions);
Assert.Equal(desiredValue.Value, value?.Value);
}
/// <summary>
/// Tests that deserializing comma-delimited enum string succeeds.
/// </summary>
[Fact]
public void Deserialize_GenericCommandType_Valid_Success()
{
GenericBodyIReadOnlyListModel<GeneralCommandType>
{ desiredValue = new GenericBodyIReadOnlyListModel<GeneralCommandType>
GenericBodyIReadOnlyListModel<GeneralCommandType> desiredValue = new()
{
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown },
Value = [GeneralCommandType.MoveUp, GeneralCommandType.MoveDown],
};
GenericBodyIReadOnlyListModel<GeneralCommandType value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", _jsonOptions);
GenericBodyIReadOnlyListModel<GeneralCommandType>? value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", this.jsonOptions);
Assert.Equal(desiredValue.Value, value?.Value);
}
/// <summary>
/// Tests that deserializing comma-delimited enum string with spaces succeeds.
/// </summary>
[Fact]
public void Deserialize_GenericCommandType_Space_Valid_Success()
{
GenericBodyIReadOnlyListModel<GeneralCommandType>
{ desiredValue = new GenericBodyIReadOnlyListModel<GeneralCommandType>
GenericBodyIReadOnlyListModel<GeneralCommandType> desiredValue = new()
{
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown },
Value = [GeneralCommandType.MoveUp, GeneralCommandType.MoveDown],
};
GenericBodyIReadOnlyListModel<GeneralCommandType value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp, MoveDown"" }", _jsonOptions);
GenericBodyIReadOnlyListModel<GeneralCommandType>? value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp, MoveDown"" }", this.jsonOptions);
Assert.Equal(desiredValue.Value, value?.Value);
}
/// <summary>
/// Tests that deserializing JSON array of strings succeeds.
/// </summary>
[Fact]
public void Deserialize_String_Array_Valid_Success()
{
GenericBodyIReadOnlyListModel<string>
{ desiredValue = new GenericBodyIReadOnlyListModel<string>
GenericBodyIReadOnlyListModel<string> desiredValue = new()
{
Value = new[] { "a", "b", "c" },
Value = ["a", "b", "c"],
};
GenericBodyIReadOnlyListModel<string value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": [""a"",""b"",""c""] }", _jsonOptions);
GenericBodyIReadOnlyListModel<string>? value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": [""a"",""b"",""c""] }", this.jsonOptions);
Assert.Equal(desiredValue.Value, value?.Value);
}
/// <summary>
/// Tests that deserializing JSON array of enums succeeds.
/// </summary>
[Fact]
public void Deserialize_GenericCommandType_Array_Valid_Success()
{
GenericBodyIReadOnlyListModel<GeneralCommandType>
{ desiredValue = new GenericBodyIReadOnlyListModel<GeneralCommandType>
GenericBodyIReadOnlyListModel<GeneralCommandType> desiredValue = new()
{
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown },
Value = [GeneralCommandType.MoveUp, GeneralCommandType.MoveDown],
};
GenericBodyIReadOnlyListModel<GeneralCommandType value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", _jsonOptions);
GenericBodyIReadOnlyListModel<GeneralCommandType>? value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", this.jsonOptions);
Assert.Equal(desiredValue.Value, value?.Value);
}
/// <summary>
/// Tests that serializing IReadOnlyList succeeds.
/// </summary>
[Fact]
public void Serialize_GenericCommandType_IReadOnlyList_Valid_Success()
{
GenericBodyIReadOnlyListModel<GeneralCommandType>
{ valueToSerialize = new GenericBodyIReadOnlyListModel<GeneralCommandType>
GenericBodyIReadOnlyListModel<GeneralCommandType> valueToSerialize = new()
{
Value = new List<GeneralCommandType> { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown },
Value = [GeneralCommandType.MoveUp, GeneralCommandType.MoveDown],
};
string value = JsonSerializer.Serialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(valueToSerialize, _jsonOptions);
string value = JsonSerializer.Serialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(valueToSerialize, this.jsonOptions);
Assert.Equal(@"{""Value"":[""MoveUp"",""MoveDown""]}", value);
}
}