From 024c8282b70bcf3eead419dda521f7e5f2e4ae4c Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sun, 4 Jul 2021 17:55:57 +0200 Subject: [PATCH] Add test for ParseDuration --- BlazorApp.Tests/BlazorApp.Tests.csproj | 26 ++++++++++++++++ BlazorApp.Tests/CalendarTests.cs | 41 ++++++++++++++++++++++++++ BlazorApp.sln | 6 ++++ BlazorApp/Data/Calendar.cs | 4 +-- 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 BlazorApp.Tests/BlazorApp.Tests.csproj create mode 100644 BlazorApp.Tests/CalendarTests.cs diff --git a/BlazorApp.Tests/BlazorApp.Tests.csproj b/BlazorApp.Tests/BlazorApp.Tests.csproj new file mode 100644 index 0000000..8b71a57 --- /dev/null +++ b/BlazorApp.Tests/BlazorApp.Tests.csproj @@ -0,0 +1,26 @@ + + + + net5.0 + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/BlazorApp.Tests/CalendarTests.cs b/BlazorApp.Tests/CalendarTests.cs new file mode 100644 index 0000000..8b3f4bd --- /dev/null +++ b/BlazorApp.Tests/CalendarTests.cs @@ -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)) + ); + } + } +} \ No newline at end of file diff --git a/BlazorApp.sln b/BlazorApp.sln index 1760963..64cf4da 100644 --- a/BlazorApp.sln +++ b/BlazorApp.sln @@ -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 diff --git a/BlazorApp/Data/Calendar.cs b/BlazorApp/Data/Calendar.cs index 20485c7..8dd9a3b 100644 --- a/BlazorApp/Data/Calendar.cs +++ b/BlazorApp/Data/Calendar.cs @@ -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 /// - 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; } }