Use a fixed timezone in tests

pull/1002/head
Tyrrrz 2 years ago
parent f73d07a7c9
commit 2a5e09bf07

@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="ReflectionMagic" Version="4.1.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="all" />

@ -1,6 +1,8 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using AngleSharp.Dom;
using DiscordChatExporter.Cli.Tests.Infra;
using DiscordChatExporter.Cli.Tests.Utils;
using DiscordChatExporter.Core.Discord;
using FluentAssertions;
using Xunit;
@ -11,6 +13,11 @@ public class HtmlMarkdownSpecs
{
[Fact]
public async Task Message_with_a_timestamp_is_rendered_correctly()
{
// Date formatting code relies on the local time zone, so we need to set it to a fixed value
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2));
try
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
@ -19,12 +26,22 @@ public class HtmlMarkdownSpecs
);
// Assert
message.Text().Should().Contain("Default timestamp: 12-Feb-23 03:36 PM");
message.Text().Should().Contain("Default timestamp: 02/12/2023 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
}
[Fact]
public async Task Message_with_a_short_time_timestamp_is_rendered_correctly()
{
// Date formatting code relies on the local time zone, so we need to set it to a fixed value
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2));
try
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
@ -36,9 +53,19 @@ public class HtmlMarkdownSpecs
message.Text().Should().Contain("Short time timestamp: 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
}
[Fact]
public async Task Message_with_a_long_time_timestamp_is_rendered_correctly()
{
// Date formatting code relies on the local time zone, so we need to set it to a fixed value
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2));
try
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
@ -50,9 +77,19 @@ public class HtmlMarkdownSpecs
message.Text().Should().Contain("Long time timestamp: 3:36:12 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
}
[Fact]
public async Task Message_with_a_short_date_timestamp_is_rendered_correctly()
{
// Date formatting code relies on the local time zone, so we need to set it to a fixed value
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2));
try
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
@ -64,9 +101,19 @@ public class HtmlMarkdownSpecs
message.Text().Should().Contain("Short date timestamp: 02/12/2023");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
}
[Fact]
public async Task Message_with_a_long_date_timestamp_is_rendered_correctly()
{
// Date formatting code relies on the local time zone, so we need to set it to a fixed value
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2));
try
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
@ -78,9 +125,19 @@ public class HtmlMarkdownSpecs
message.Text().Should().Contain("Long date timestamp: February 12, 2023");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
}
[Fact]
public async Task Message_with_a_full_timestamp_is_rendered_correctly()
{
// Date formatting code relies on the local time zone, so we need to set it to a fixed value
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2));
try
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
@ -92,9 +149,19 @@ public class HtmlMarkdownSpecs
message.Text().Should().Contain("Full timestamp: February 12, 2023 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
}
[Fact]
public async Task Message_with_a_full_long_timestamp_is_rendered_correctly()
{
// Date formatting code relies on the local time zone, so we need to set it to a fixed value
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2));
try
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
@ -106,9 +173,19 @@ public class HtmlMarkdownSpecs
message.Text().Should().Contain("Full long timestamp: Sunday, February 12, 2023 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
}
[Fact]
public async Task Message_with_a_relative_timestamp_is_rendered_as_the_default_timestamp()
{
// Date formatting code relies on the local time zone, so we need to set it to a fixed value
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2));
try
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
@ -117,12 +194,22 @@ public class HtmlMarkdownSpecs
);
// Assert
message.Text().Should().Contain("Relative timestamp: 12-Feb-23 03:36 PM");
message.Text().Should().Contain("Relative timestamp: 02/12/2023 3:36 PM");
message.InnerHtml.Should().Contain("Sunday, February 12, 2023 3:36 PM");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
}
[Fact]
public async Task Message_with_an_invalid_timestamp_is_rendered_correctly()
{
// Date formatting code relies on the local time zone, so we need to set it to a fixed value
TimeZoneInfoEx.SetLocal(TimeSpan.FromHours(+2));
try
{
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
@ -133,4 +220,9 @@ public class HtmlMarkdownSpecs
// Assert
message.Text().Should().Contain("Invalid timestamp: Invalid date");
}
finally
{
TimeZoneInfo.ClearCachedData();
}
}
}

@ -0,0 +1,14 @@
using System;
using ReflectionMagic;
namespace DiscordChatExporter.Cli.Tests.Utils;
internal static class TimeZoneInfoEx
{
// https://stackoverflow.com/a/63700512/2205454
public static void SetLocal(TimeZoneInfo timeZone) =>
typeof(TimeZoneInfo).AsDynamicType().s_cachedData._localTimeZone = timeZone;
public static void SetLocal(TimeSpan offset) =>
SetLocal(TimeZoneInfo.CreateCustomTimeZone("test-tz", offset, "test-tz", "test-tz"));
}
Loading…
Cancel
Save