Add test for ParseDuration

This commit is contained in:
Sijmen 2021-07-04 17:55:57 +02:00
parent 36f9fb6654
commit 024c8282b7
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
4 changed files with 75 additions and 2 deletions

View File

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BlazorApp\BlazorApp.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,41 @@
using System;
using Xunit;
namespace BlazorApp.Tests
{
public class CalendarTests
{
[Fact]
public void TestParseDuration()
{
// 1 week
Assert.Equal(Data.Calendar.ParseDuration("P1W"), TimeSpan.FromDays(7));
// 1 week and 2 days
Assert.Equal(Data.Calendar.ParseDuration("P1W2D"), TimeSpan.FromDays(7 + 2));
// 1 day
Assert.Equal(Data.Calendar.ParseDuration("P1D"), TimeSpan.FromDays(1));
Assert.Equal(Data.Calendar.ParseDuration("-P1D"), TimeSpan.FromDays(-1));
// 1 day, 5 hours
Assert.Equal(Data.Calendar.ParseDuration("P1DT5H"), TimeSpan.FromHours(24 + 5));
// 5 hours
Assert.Equal(Data.Calendar.ParseDuration("PT5H"), TimeSpan.FromHours(5));
// 1 hours, 3 minutes, 1 second
Assert.Equal(Data.Calendar.ParseDuration("PT1H03M01S"), TimeSpan.FromSeconds(3600 + 3 * 60 + 1));
// 3 weeks, 4 days, 1 hour, 3 minutes, 1 second
Assert.Equal(
Data.Calendar.ParseDuration("P3W4DT1H03M01S"),
TimeSpan.FromSeconds(3600 + 3 * 60 + 1) + TimeSpan.FromDays(3 * 7 + 4)
);
Assert.Equal(
Data.Calendar.ParseDuration("-P3W4DT1H03M01S"),
-(TimeSpan.FromSeconds(3600 + 3 * 60 + 1) + TimeSpan.FromDays(3 * 7 + 4))
);
}
}
}

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.31410.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp", "BlazorApp\BlazorApp.csproj", "{69DC6815-2B48-4DFF-9654-14990236E6E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp.Tests", "BlazorApp.Tests\BlazorApp.Tests.csproj", "{6AD55C0D-0C3A-4E5F-B8F7-4E01CC6D56E7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{69DC6815-2B48-4DFF-9654-14990236E6E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69DC6815-2B48-4DFF-9654-14990236E6E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69DC6815-2B48-4DFF-9654-14990236E6E2}.Release|Any CPU.Build.0 = Release|Any CPU
{6AD55C0D-0C3A-4E5F-B8F7-4E01CC6D56E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6AD55C0D-0C3A-4E5F-B8F7-4E01CC6D56E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AD55C0D-0C3A-4E5F-B8F7-4E01CC6D56E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AD55C0D-0C3A-4E5F-B8F7-4E01CC6D56E7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -47,7 +47,7 @@ namespace BlazorApp.Data
/// Very non-strict parser of durations as specified in
/// https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.6
/// </summary>
private static TimeSpan? _parseDuration(string s)
public static TimeSpan? ParseDuration(string s)
{
var timeSpan = new TimeSpan();
var positive = s[0] != '-';
@ -154,7 +154,7 @@ namespace BlazorApp.Data
@event.DtEnd = _parseDateTime(value);
break;
case "DURATION":
@event.Duration = _parseDuration(value);
@event.Duration = ParseDuration(value);
break;
}
}