Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 40c93ee

Browse files
committedOct 5, 2023
Added FakeLocalTimeZone helper class.
The class is used to change the time zone of the tests upon initialization, and resets it back when disposed.
1 parent 7913fd0 commit 40c93ee

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace Todoist.Net.Tests.Helpers;
5+
6+
/// <summary>
7+
/// A helper class that changes the local timezone to a fake timezone provided
8+
/// at initialization, and resets the original local timezone when disposed.
9+
/// </summary>
10+
/// <remarks>
11+
/// See this <see href="https://stackoverflow.com/questions/44413407/mock-the-country-timezone-you-are-running-unit-test-from">SO question</see> for more details.
12+
/// </remarks>
13+
public sealed class FakeLocalTimeZone : IDisposable
14+
{
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="FakeLocalTimeZone"/> class
18+
/// and changes the local time zone to the given <paramref name="fakeTimeZoneInfo"/>
19+
/// until it's disposed.
20+
/// </summary>
21+
/// <param name="fakeTimeZoneInfo">The time zone to set as local until disposal.</param>
22+
public FakeLocalTimeZone(TimeZoneInfo fakeTimeZoneInfo)
23+
{
24+
var info = typeof(TimeZoneInfo).GetField("s_cachedData", BindingFlags.NonPublic | BindingFlags.Static);
25+
var cachedData = info.GetValue(null);
26+
27+
var field = cachedData.GetType().GetField("_localTimeZone",
28+
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Instance);
29+
30+
field.SetValue(cachedData, fakeTimeZoneInfo);
31+
}
32+
33+
public void Dispose()
34+
{
35+
TimeZoneInfo.ClearCachedData();
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Linq;
3+
4+
using Todoist.Net.Tests.Extensions;
5+
6+
using Xunit;
7+
8+
namespace Todoist.Net.Tests.Helpers;
9+
10+
[Trait(Constants.TraitName, Constants.UnitTraitValue)]
11+
public class FakeLocalTimeZoneTests
12+
{
13+
14+
[Fact]
15+
public void FakeLocalTimeZone_ShouldChangeLocalTimeZoneWithinScope_AndResetItBackOutsideScope()
16+
{
17+
var actualTimeZoneInfo = TimeZoneInfo.Local;
18+
19+
var timeZoneCollection = TimeZoneInfo
20+
.GetSystemTimeZones()
21+
.Where(t => !actualTimeZoneInfo.Equals(t))
22+
.ToArray();
23+
24+
var randomIndex = new Random().Next(timeZoneCollection.Length);
25+
var fakeTimeZoneInfo = timeZoneCollection.ElementAt(randomIndex);
26+
27+
28+
Assert.NotEqual(fakeTimeZoneInfo, actualTimeZoneInfo);
29+
30+
using (var fakeLocalTimeZone = new FakeLocalTimeZone(fakeTimeZoneInfo))
31+
{
32+
Assert.Equal(fakeTimeZoneInfo, TimeZoneInfo.Local);
33+
}
34+
Assert.Equal(actualTimeZoneInfo, TimeZoneInfo.Local);
35+
}
36+
37+
}

‎src/Todoist.Net.Tests/Models/DueDateTests.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,35 @@
22

33
using Todoist.Net.Models;
44
using Todoist.Net.Tests.Extensions;
5+
using Todoist.Net.Tests.Helpers;
6+
57
using Xunit;
68

79
namespace Todoist.Net.Tests.Models
810
{
911
[Trait(Constants.TraitName, Constants.UnitTraitValue)]
10-
public class DueDateTests
12+
public class DueDateTests : IDisposable
1113
{
14+
15+
private readonly FakeLocalTimeZone _fakeLocalTimeZone;
16+
17+
public DueDateTests()
18+
{
19+
var timeZoneCollection = System.TimeZoneInfo.GetSystemTimeZones();
20+
21+
var randomIndex = new Random().Next(timeZoneCollection.Count);
22+
var fakeTimeZoneInfo = timeZoneCollection[randomIndex];
23+
24+
_fakeLocalTimeZone = new FakeLocalTimeZone(fakeTimeZoneInfo);
25+
}
26+
27+
public void Dispose()
28+
{
29+
_fakeLocalTimeZone.Dispose();
30+
GC.SuppressFinalize(this);
31+
}
32+
33+
1234
[Fact]
1335
public void DateTimeAssignment_FullDayEvent_Success()
1436
{

0 commit comments

Comments
 (0)
Please sign in to comment.