Daniel Chalmers 1 year ago
parent
commit
e1ecc10229

+ 74 - 0
DesktopClock.Tests/DateTimeTests.cs

@@ -0,0 +1,74 @@
+using System;
+
+namespace DesktopClock.Tests;
+
+public class DateTimeTests
+{
+    [Fact]
+    public void TimeZones_ShouldContainSystemTimeZones()
+    {
+        // Act
+        var timeZones = DateTimeUtil.TimeZones;
+
+        // Assert
+        Assert.NotEmpty(timeZones);
+        Assert.Contains(timeZones, tz => tz.Id == "UTC");
+    }
+
+    [Theory]
+    [InlineData("UTC", true)]
+    [InlineData("Pacific Standard Time", true)]
+    [InlineData("NonExistentTimeZone", false)]
+    public void TryFindSystemTimeZoneById_ShouldReturnExpectedResult(string timeZoneId, bool expectedResult)
+    {
+        // Act
+        var result = DateTimeUtil.TryFindSystemTimeZoneById(timeZoneId, out var timeZoneInfo);
+
+        // Assert
+        Assert.Equal(expectedResult, result);
+        if (expectedResult)
+        {
+            Assert.NotNull(timeZoneInfo);
+            Assert.Equal(timeZoneId, timeZoneInfo.Id);
+        }
+        else
+        {
+            Assert.Null(timeZoneInfo);
+        }
+    }
+
+    [Theory]
+    [InlineData("2024-07-15T00:00:00Z", "00:00:00")]
+    [InlineData("2024-07-15T00:00:00Z", "01:00:00")]
+    [InlineData("0001-01-01T00:00:00Z", "00:00:00")]
+    public void ToDateTimeOffset_ShouldConvertDateTimeToExpectedOffset(string dateTimeString, string offsetString)
+    {
+        // Arrange
+        var dateTime = DateTime.Parse(dateTimeString);
+        var offset = TimeSpan.Parse(offsetString);
+
+        // Act
+        var dateTimeOffset = dateTime.ToDateTimeOffset(offset);
+
+        // Assert
+        Assert.Equal(new DateTimeOffset(dateTime.Ticks, offset), dateTimeOffset);
+    }
+
+    [Theory]
+    [InlineData("dddd, MMMM dd", "Monday, January 01")]
+    [InlineData("yyyy-MM-dd", "2024-01-01")]
+    [InlineData("HH:mm:ss", "00:00:00")]
+    [InlineData("MMMM dd, yyyy", "January 01, 2024")]
+    public void FromFormat_CreatesCorrectExample(string format, string expected)
+    {
+        // Arrange
+        var dateTimeOffset = new DateTime(2024, 01, 01);
+
+        // Act
+        var dateFormatExample = DateFormatExample.FromFormat(format, dateTimeOffset);
+
+        // Assert
+        Assert.Equal(format, dateFormatExample.Format);
+        Assert.Equal(expected, dateFormatExample.Example);
+    }
+}

+ 3 - 3
DesktopClock.Tests/DesktopClock.Tests.csproj

@@ -6,9 +6,9 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
-    <PackageReference Include="xunit" Version="2.6.4" />
-    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.6" PrivateAssets="all">
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
+    <PackageReference Include="xunit" Version="2.9.0" />
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="all">
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
     <ProjectReference Include="..\DesktopClock\DesktopClock.csproj" />

+ 2 - 2
DesktopClock/DateFormatExample.cs

@@ -27,7 +27,7 @@ public record DateFormatExample
     /// <summary>
     /// Creates a <see cref="DateFormatExample" /> from the given format.
     /// </summary>
-    public static DateFormatExample FromFormat(string format) => new(format, DateTimeOffset.Now.ToString(format));
+    public static DateFormatExample FromFormat(string format, DateTimeOffset dateTimeOffset) => new(format, dateTimeOffset.ToString(format));
 
     /// <summary>
     /// Common date time formatting strings and an example string for each.
@@ -55,5 +55,5 @@ public record DateFormatExample
         "t",
         "T",
         "O",
-    }.Select(FromFormat).Append(Tutorial).ToList();
+    }.Select(f => FromFormat(f, DateTimeOffset.Now)).Append(Tutorial).ToList();
 }