Skip to content

Commit

Permalink
Harden primitive serializer (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus authored Oct 23, 2024
1 parent 01b77d1 commit 94bdac8
Showing 1 changed file with 50 additions and 15 deletions.
65 changes: 50 additions & 15 deletions src/Hyperion.Tests/PrimitivesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Xunit;

namespace Hyperion.Tests
Expand Down Expand Up @@ -85,28 +87,61 @@ public void CanSerializeGuid()
SerializeAndAssert(Guid.NewGuid());
}

[Fact]
public void CanSerializeDateTimeUtc()
[Theory]
[MemberData(nameof(DateTimeData))]
public void CanSerializeDateTime(DateTime expected)
{
SerializeAndAssert(DateTime.UtcNow);
SerializeAndAssert(expected);
}

[Fact]
public void CanSerializeDateTimeLocal()
public static IEnumerable<object[]> DateTimeData()
{
SerializeAndAssert(new DateTime(DateTime.Now.Ticks, DateTimeKind.Local));
yield return new object[] { DateTime.UtcNow };
yield return new object[] { DateTime.Now };
yield return new object[] { DateTime.MinValue };
yield return new object[] { DateTime.MaxValue };
var now = DateTime.Now;
yield return new object[] { DateTime.SpecifyKind(now, DateTimeKind.Unspecified) };
yield return new object[] { DateTime.SpecifyKind(now, DateTimeKind.Local) };
yield return new object[] { DateTime.SpecifyKind(now, DateTimeKind.Utc) };
}

[Fact]
public void CanSerializeDateTimeOffsetNow()
[Theory]
[MemberData(nameof(DateTimeOffsetData))]
public void CanSerializeDateTimeOffset(DateTimeOffset expected)
{
SerializeAndAssert(DateTimeOffset.Now);
SerializeAndAssert(expected);
}

[Fact]
public void CanSerializeDateTimeOffsetUtc()
public static IEnumerable<object[]> DateTimeOffsetData()
{
SerializeAndAssert(DateTimeOffset.UtcNow);
yield return new object[] { DateTimeOffset.UtcNow };
yield return new object[] { DateTimeOffset.Now };
yield return new object[] { DateTimeOffset.MinValue };
yield return new object[] { DateTimeOffset.MaxValue };
yield return new object[] { new DateTimeOffset(DateTime.MinValue, TimeSpan.Zero) };
yield return new object[] { new DateTimeOffset(DateTime.MaxValue, TimeSpan.Zero) };

var now = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
var increment = TimeSpan.FromMinutes(10);
var offset = TimeSpan.FromHours(-14) - increment;

// Over the whole possible timezone offsets (-14 to 14), incremented by 10 minutes
while (offset.TotalHours < 14.0)
{
offset += increment;
yield return new object[] { new DateTimeOffset(now, offset) };
}

increment = TimeSpan.FromMinutes(1);
offset = TimeSpan.FromHours(-1);

// Over the span of -1 to 1 hours, incremented by 1 minute
while (offset.TotalHours < 1.0)
{
offset += increment;
yield return new object[] { new DateTimeOffset(now, offset) };
}
}

[Fact]
Expand Down Expand Up @@ -182,12 +217,12 @@ public void CanSerializeString()
SerializeAndAssert("hello");
}

private void SerializeAndAssert(object expected)
private void SerializeAndAssert<T>(T expected)
{
Serialize(expected);
Reset();
var res = Deserialize<object>();
Assert.Equal(expected, res);
var res = Deserialize<T>();
expected.Should().Be(res);
AssertMemoryStreamConsumed();
}
}
Expand Down

0 comments on commit 94bdac8

Please sign in to comment.