2020-10-02 17:43:34 +00:00
|
|
|
using System;
|
|
|
|
using System.Text.Json;
|
2021-06-19 16:02:33 +00:00
|
|
|
using Jellyfin.Extensions.Json.Converters;
|
2020-10-02 17:43:34 +00:00
|
|
|
using Xunit;
|
|
|
|
|
2021-06-19 16:02:33 +00:00
|
|
|
namespace Jellyfin.Extensions.Tests.Json.Converters
|
2020-10-02 17:43:34 +00:00
|
|
|
{
|
2020-11-28 15:33:57 +00:00
|
|
|
public class JsonGuidConverterTests
|
2020-10-02 17:43:34 +00:00
|
|
|
{
|
2020-11-28 15:33:57 +00:00
|
|
|
private readonly JsonSerializerOptions _options;
|
|
|
|
|
|
|
|
public JsonGuidConverterTests()
|
|
|
|
{
|
|
|
|
_options = new JsonSerializerOptions();
|
|
|
|
_options.Converters.Add(new JsonGuidConverter());
|
|
|
|
}
|
|
|
|
|
2020-10-02 17:43:34 +00:00
|
|
|
[Fact]
|
2020-11-28 15:33:57 +00:00
|
|
|
public void Deserialize_Valid_Success()
|
2020-10-02 17:43:34 +00:00
|
|
|
{
|
2020-11-28 15:33:57 +00:00
|
|
|
Guid value = JsonSerializer.Deserialize<Guid>(@"""a852a27afe324084ae66db579ee3ee18""", _options);
|
2020-10-02 17:43:34 +00:00
|
|
|
Assert.Equal(new Guid("a852a27afe324084ae66db579ee3ee18"), value);
|
2020-11-28 15:33:57 +00:00
|
|
|
}
|
2020-10-02 17:43:34 +00:00
|
|
|
|
2020-11-28 15:33:57 +00:00
|
|
|
[Fact]
|
|
|
|
public void Deserialize_ValidDashed_Success()
|
|
|
|
{
|
|
|
|
Guid value = JsonSerializer.Deserialize<Guid>(@"""e9b2dcaa-529c-426e-9433-5e9981f27f2e""", _options);
|
2020-10-02 17:43:34 +00:00
|
|
|
Assert.Equal(new Guid("e9b2dcaa-529c-426e-9433-5e9981f27f2e"), value);
|
|
|
|
}
|
2020-10-04 14:26:43 +00:00
|
|
|
|
|
|
|
[Fact]
|
2020-11-28 15:33:57 +00:00
|
|
|
public void Roundtrip_Valid_Success()
|
2020-10-04 14:26:43 +00:00
|
|
|
{
|
|
|
|
Guid guid = new Guid("a852a27afe324084ae66db579ee3ee18");
|
2020-11-28 15:33:57 +00:00
|
|
|
string value = JsonSerializer.Serialize(guid, _options);
|
|
|
|
Assert.Equal(guid, JsonSerializer.Deserialize<Guid>(value, _options));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void Deserialize_Null_EmptyGuid()
|
|
|
|
{
|
|
|
|
Assert.Equal(Guid.Empty, JsonSerializer.Deserialize<Guid>("null", _options));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2020-12-07 22:23:40 +00:00
|
|
|
public void Serialize_EmptyGuid_EmptyGuid()
|
2020-11-28 15:33:57 +00:00
|
|
|
{
|
2020-12-09 02:33:26 +00:00
|
|
|
Assert.Equal($"\"{Guid.Empty:N}\"", JsonSerializer.Serialize(Guid.Empty, _options));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void Serialize_Valid_NoDash_Success()
|
|
|
|
{
|
|
|
|
var guid = new Guid("531797E9-9457-40E0-88BC-B1D6D38752FA");
|
|
|
|
var str = JsonSerializer.Serialize(guid, _options);
|
|
|
|
Assert.Equal($"\"{guid:N}\"", str);
|
2020-10-04 14:26:43 +00:00
|
|
|
}
|
2020-12-10 02:57:25 +00:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void Serialize_Nullable_Success()
|
|
|
|
{
|
|
|
|
Guid? guid = new Guid("531797E9-9457-40E0-88BC-B1D6D38752FA");
|
|
|
|
var str = JsonSerializer.Serialize(guid, _options);
|
|
|
|
Assert.Equal($"\"{guid:N}\"", str);
|
|
|
|
}
|
2020-10-02 17:43:34 +00:00
|
|
|
}
|
|
|
|
}
|