wabbajack/Wabbajack.DTOs.Test/ValueTests.cs

60 lines
1.6 KiB
C#
Raw Permalink Normal View History

2021-10-23 16:51:17 +00:00
using System.Text.Json.Serialization;
2021-09-27 12:42:46 +00:00
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.Hashing.xxHash64;
using Xunit;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.DTOs.Test;
public class ValueTests
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private readonly DTOSerializer _dtos;
public ValueTests()
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_dtos = new DTOSerializer(new JsonConverter[] {new HashJsonConverter(), new HashRelativePathConverter()});
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void TestHash()
{
var a = new HashData {Value = Hash.FromULong(int.MaxValue)};
var b = _dtos.Deserialize<HashData>(_dtos.Serialize(a));
Assert.Equal(a.Value, b.Value);
}
[Fact]
public void TestHashRelative()
{
var a = new HashDataRelative {Value = new HashRelativePath(Hash.FromULong(int.MaxValue))};
var b = _dtos.Deserialize<HashDataRelative>(_dtos.Serialize(a));
Assert.Equal(a.Value.Hash, b.Value.Hash);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void TestToFromJsonHash()
{
for (ulong hash = 0; hash < 1024 * 1024; hash++)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var a = new BoxedHash {Hash = Hash.FromULong(hash)};
var b = _dtos.Deserialize<BoxedHash>(_dtos.Serialize(a))!;
Assert.Equal($"{{\"Hash\":\"{a.Hash.ToString()}\"}}", _dtos.Serialize(b));
Assert.Equal(a.Hash, Hash.FromBase64(Hash.FromULong(hash).ToBase64()));
Assert.Equal(a.Hash, b.Hash);
2021-09-27 12:42:46 +00:00
}
}
2021-10-23 16:51:17 +00:00
public class HashData
{
public Hash Value { get; set; }
}
public class HashDataRelative
{
public HashRelativePath Value { get; set; }
}
private class BoxedHash
{
[JsonPropertyName("Hash")] public Hash Hash { get; set; }
}
2021-09-27 12:42:46 +00:00
}