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:
+86
-51
@@ -7,16 +7,18 @@ namespace Jellyfin.Extensions.Tests.Json.Converters
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
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 collection converter.
|
||||
/// </summary>
|
||||
public class JsonCommaDelimitedCollectionTests
|
||||
{
|
||||
private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions()
|
||||
private readonly JsonSerializerOptions jsonOptions = new()
|
||||
{
|
||||
Converters =
|
||||
{
|
||||
@@ -24,203 +26,236 @@ namespace Jellyfin.Extensions.Tests.Json.Converters
|
||||
},
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Tests that deserializing null string value succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Deserialize_String_Null_Success()
|
||||
{
|
||||
GenericBodyArrayModel<string value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": null }", _jsonOptions);
|
||||
GenericBodyArrayModel<string>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": null }", this.jsonOptions);
|
||||
Assert.Null(value?.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that deserializing empty string succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Deserialize_Empty_Success()
|
||||
{
|
||||
GenericBodyArrayModel<string>
|
||||
{ desiredValue = new GenericBodyArrayModel<string>
|
||||
GenericBodyArrayModel<string> desiredValue = new()
|
||||
{
|
||||
Value = Array.Empty<string>(),
|
||||
Value = [],
|
||||
};
|
||||
|
||||
GenericBodyArrayModel<string value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": """" }", _jsonOptions);
|
||||
GenericBodyArrayModel<string>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": """" }", this.jsonOptions);
|
||||
Assert.Equal(desiredValue.Value, value?.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that deserializing empty string to list throws exception.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Deserialize_EmptyList_Success()
|
||||
{
|
||||
GenericBodyListModel<string>
|
||||
{ desiredValue = new GenericBodyListModel<string>
|
||||
GenericBodyListModel<string> desiredValue = new()
|
||||
{
|
||||
Value = [],
|
||||
};
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Deserialize<GenericBodyListModel<string>>(@"{ ""Value"": """" }", _jsonOptions));
|
||||
_ = Assert.Throws<InvalidOperationException>(() => JsonSerializer.Deserialize<GenericBodyListModel<string>>(@"{ ""Value"": """" }", this.jsonOptions));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that deserializing empty string to IReadOnlyList succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Deserialize_EmptyIReadOnlyList_Success()
|
||||
{
|
||||
GenericBodyIReadOnlyListModel<string>
|
||||
{ desiredValue = new GenericBodyIReadOnlyListModel<string>
|
||||
GenericBodyIReadOnlyListModel<string> desiredValue = new()
|
||||
{
|
||||
Value = [],
|
||||
};
|
||||
|
||||
GenericBodyIReadOnlyListModel<string value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": """" }", _jsonOptions);
|
||||
GenericBodyIReadOnlyListModel<string>? value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": """" }", this.jsonOptions);
|
||||
Assert.Equal(desiredValue.Value, value?.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that deserializing comma-delimited string succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Deserialize_String_Valid_Success()
|
||||
{
|
||||
GenericBodyArrayModel<string>
|
||||
{ desiredValue = new GenericBodyArrayModel<string>
|
||||
GenericBodyArrayModel<string> desiredValue = new()
|
||||
{
|
||||
Value = ["a", "b", "c"],
|
||||
};
|
||||
|
||||
GenericBodyArrayModel<string value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": ""a,b,c"" }", _jsonOptions);
|
||||
GenericBodyArrayModel<string>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": ""a,b,c"" }", this.jsonOptions);
|
||||
Assert.Equal(desiredValue.Value, value?.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that deserializing comma-delimited string to list throws exception.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Deserialize_StringList_Valid_Success()
|
||||
{
|
||||
GenericBodyListModel<string>
|
||||
{ desiredValue = new GenericBodyListModel<string>
|
||||
GenericBodyListModel<string> desiredValue = new()
|
||||
{
|
||||
Value = ["a", "b", "c"],
|
||||
};
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Deserialize<GenericBodyListModel<string>>(@"{ ""Value"": ""a,b,c"" }", _jsonOptions));
|
||||
_ = Assert.Throws<InvalidOperationException>(() => JsonSerializer.Deserialize<GenericBodyListModel<string>>(@"{ ""Value"": ""a,b,c"" }", this.jsonOptions));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that deserializing comma-delimited string with spaces succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Deserialize_String_Space_Valid_Success()
|
||||
{
|
||||
GenericBodyArrayModel<string>
|
||||
{ desiredValue = new GenericBodyArrayModel<string>
|
||||
GenericBodyArrayModel<string> desiredValue = new()
|
||||
{
|
||||
Value = ["a", "b", "c"],
|
||||
};
|
||||
|
||||
GenericBodyArrayModel<string value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": ""a, b, c"" }", _jsonOptions);
|
||||
GenericBodyArrayModel<string>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<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()
|
||||
{
|
||||
GenericBodyArrayModel<GeneralCommandType>
|
||||
{ desiredValue = new GenericBodyArrayModel<GeneralCommandType>
|
||||
GenericBodyArrayModel<GeneralCommandType> desiredValue = new()
|
||||
{
|
||||
Value = [GeneralCommandType.MoveUp, GeneralCommandType.MoveDown],
|
||||
};
|
||||
|
||||
GenericBodyArrayModel<GeneralCommandType value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", _jsonOptions);
|
||||
GenericBodyArrayModel<GeneralCommandType>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", this.jsonOptions);
|
||||
Assert.Equal(desiredValue.Value, value?.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that deserializing comma-delimited enum string with empty entry succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Deserialize_GenericCommandType_EmptyEntry_Success()
|
||||
{
|
||||
GenericBodyArrayModel<GeneralCommandType>
|
||||
{ desiredValue = new GenericBodyArrayModel<GeneralCommandType>
|
||||
GenericBodyArrayModel<GeneralCommandType> desiredValue = new()
|
||||
{
|
||||
Value = [GeneralCommandType.MoveUp, GeneralCommandType.MoveDown],
|
||||
};
|
||||
|
||||
GenericBodyArrayModel<GeneralCommandType value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,,MoveDown"" }", _jsonOptions);
|
||||
GenericBodyArrayModel<GeneralCommandType>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,,MoveDown"" }", this.jsonOptions);
|
||||
Assert.Equal(desiredValue.Value, value?.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that deserializing comma-delimited enum string with invalid value succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Deserialize_GenericCommandType_Invalid_Success()
|
||||
{
|
||||
GenericBodyArrayModel<GeneralCommandType>
|
||||
{ desiredValue = new GenericBodyArrayModel<GeneralCommandType>
|
||||
GenericBodyArrayModel<GeneralCommandType> desiredValue = new()
|
||||
{
|
||||
Value = [GeneralCommandType.MoveUp, GeneralCommandType.MoveDown],
|
||||
};
|
||||
|
||||
GenericBodyArrayModel<GeneralCommandType value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,TotallyNotAValidCommand,MoveDown"" }", _jsonOptions);
|
||||
GenericBodyArrayModel<GeneralCommandType>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,TotallyNotAValidCommand,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()
|
||||
{
|
||||
GenericBodyArrayModel<GeneralCommandType>
|
||||
{ desiredValue = new GenericBodyArrayModel<GeneralCommandType>
|
||||
GenericBodyArrayModel<GeneralCommandType> desiredValue = new()
|
||||
{
|
||||
Value = [GeneralCommandType.MoveUp, GeneralCommandType.MoveDown],
|
||||
};
|
||||
|
||||
GenericBodyArrayModel<GeneralCommandType value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp, MoveDown"" }", _jsonOptions);
|
||||
GenericBodyArrayModel<GeneralCommandType>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<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()
|
||||
{
|
||||
GenericBodyArrayModel<string>
|
||||
{ desiredValue = new GenericBodyArrayModel<string>
|
||||
GenericBodyArrayModel<string> desiredValue = new()
|
||||
{
|
||||
Value = ["a", "b", "c"],
|
||||
};
|
||||
|
||||
GenericBodyArrayModel<string value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": [""a"",""b"",""c""] }", _jsonOptions);
|
||||
GenericBodyArrayModel<string>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<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()
|
||||
{
|
||||
GenericBodyArrayModel<GeneralCommandType>
|
||||
{ desiredValue = new GenericBodyArrayModel<GeneralCommandType>
|
||||
GenericBodyArrayModel<GeneralCommandType> desiredValue = new()
|
||||
{
|
||||
Value = [GeneralCommandType.MoveUp, GeneralCommandType.MoveDown],
|
||||
};
|
||||
|
||||
GenericBodyArrayModel<GeneralCommandType value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", _jsonOptions);
|
||||
GenericBodyArrayModel<GeneralCommandType>? value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", this.jsonOptions);
|
||||
Assert.Equal(desiredValue.Value, value?.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that serializing readonly collection succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Serialize_GenericCommandType_ReadOnlyArray_Valid_Success()
|
||||
{
|
||||
GenericBodyIReadOnlyCollectionModel<GeneralCommandType>
|
||||
{ valueToSerialize = new GenericBodyIReadOnlyCollectionModel<GeneralCommandType>
|
||||
GenericBodyIReadOnlyCollectionModel<GeneralCommandType> valueToSerialize = new()
|
||||
{
|
||||
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }.AsReadOnly(),
|
||||
};
|
||||
|
||||
string value = JsonSerializer.Serialize<GenericBodyIReadOnlyCollectionModel<GeneralCommandType>>(valueToSerialize, _jsonOptions);
|
||||
string value = JsonSerializer.Serialize<GenericBodyIReadOnlyCollectionModel<GeneralCommandType>>(valueToSerialize, this.jsonOptions);
|
||||
Assert.Equal(@"{""Value"":[""MoveUp"",""MoveDown""]}", value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that serializing immutable array succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Serialize_GenericCommandType_ImmutableArrayArray_Valid_Success()
|
||||
{
|
||||
GenericBodyIReadOnlyCollectionModel<GeneralCommandType>
|
||||
{ valueToSerialize = new GenericBodyIReadOnlyCollectionModel<GeneralCommandType>
|
||||
GenericBodyIReadOnlyCollectionModel<GeneralCommandType> valueToSerialize = new()
|
||||
{
|
||||
Value = ImmutableArray.Create(new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }),
|
||||
Value = ImmutableArray.Create([GeneralCommandType.MoveUp, GeneralCommandType.MoveDown]),
|
||||
};
|
||||
|
||||
string value = JsonSerializer.Serialize<GenericBodyIReadOnlyCollectionModel<GeneralCommandType>>(valueToSerialize, _jsonOptions);
|
||||
string value = JsonSerializer.Serialize<GenericBodyIReadOnlyCollectionModel<GeneralCommandType>>(valueToSerialize, this.jsonOptions);
|
||||
Assert.Equal(@"{""Value"":[""MoveUp"",""MoveDown""]}", value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests that serializing list succeeds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Serialize_GenericCommandType_List_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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user