Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Run recurrence test cases from the libical project and introduce generic file-based test cases. #617

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Ical.Net.Tests/Calendars/Recurrence/RecurrenceTestCases.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Monday and Thursday, 3 times.
RRULE:FREQ=WEEKLY;BYDAY=MO,TH;COUNT=3
DTSTART:20241024
INSTANCES:20241024,20241028,20241031
5 changes: 4 additions & 1 deletion Ical.Net.Tests/Ical.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<ProjectReference Include="..\Ical.Net\Ical.Net.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Calendars\**\*.ics" />
<EmbeddedResource Include="Calendars\**\*" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="contrib\libical\icalrecur_test.out" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
Expand Down
5 changes: 5 additions & 0 deletions Ical.Net.Tests/IcsFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,10 @@ internal static string ReadStream(string manifestResource)
internal static string YearlyCountByMonth1 => ReadStream("Ical.Net.Tests.Calendars.Recurrence.YearlyCountByMonth1.ics");
internal static string YearlyCountByYearDay1 => ReadStream("Ical.Net.Tests.Calendars.Recurrence.YearlyCountByYearDay1.ics");
internal static string YearlyInterval1 => ReadStream("Ical.Net.Tests.Calendars.Recurrence.YearlyInterval1.ics");

internal static string RecurrrenceTestCases => ReadStream("Ical.Net.Tests.Calendars.Recurrence.RecurrenceTestCases.txt");

internal static string LibicalIcalrecurTest => ReadStream("Ical.Net.Tests.contrib.libical.icalrecur_test.out");

}
}
111 changes: 111 additions & 0 deletions Ical.Net.Tests/RecurrenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3677,5 +3677,116 @@ public void InclusiveRruleUntil()
var occurrences = firstEvent.GetOccurrences(startSearch, endSearch);
Assert.That(occurrences, Has.Count.EqualTo(5));
}

public class RecurrenceTestCase
{
public int LineNumber { get; set; }

public string RRule { get; set; }

public CalDateTime DtStart { get; set; }

public CalDateTime StartAt { get; set; }

public IReadOnlyList<CalDateTime> Instances { get; set; }

public override string ToString()
=> $"Line {LineNumber}: {DtStart}, {RRule}";
}

private static IEnumerable<RecurrenceTestCase> ParseTestCaseFile(string fileContent)
{
RecurrenceTestCase current = null;

var rd = new StringReader(fileContent);
var lineNo = 0;

for (string line = rd.ReadLine(); line != null; line = rd.ReadLine())
{
lineNo++;

if (string.IsNullOrEmpty(line))
{
if (current != null)
{
yield return current;
current = null;
}
continue;
}

if (line.StartsWith("#"))
continue;

current = current ?? new RecurrenceTestCase();

var m = Regex.Match(line, @"^(?<h>[A-Z-]+):(?<v>.*)$");
if (!m.Success)
continue;

var hdr = m.Groups["h"].Value;
var val = m.Groups["v"].Value;

switch (hdr)
{
case "RRULE":
current.RRule = val;
current.LineNumber = lineNo;
break;

case "DTSTART":
current.DtStart = new CalDateTime(val) { TzId = "UTC" };
break;

case "START-AT":
current.StartAt = new CalDateTime(val) { TzId = "UTC" };
break;

case "INSTANCES":
current.Instances = val.Split(',').Select(dt => new CalDateTime(dt) { TzId = "UTC" }).ToList();
break;
}
}

if (current != null)
yield return current;
}

private static IEnumerable<RecurrenceTestCase> TestLibicalTestCasesSource
=> ParseTestCaseFile(IcsFiles.LibicalIcalrecurTest);

[TestCaseSource(nameof(TestLibicalTestCasesSource))]
public void TestLibicalTestCases(RecurrenceTestCase testCase)
=> ExecuteRecurrenceTestCase(testCase);

private static IEnumerable<RecurrenceTestCase> TestFileBasedRecurrenceTestCaseSource
=> ParseTestCaseFile(IcsFiles.RecurrrenceTestCases);

[TestCaseSource(nameof(TestFileBasedRecurrenceTestCaseSource))]
public void TestFileBasedRecurrenceTestCase(RecurrenceTestCase testCase)
=> ExecuteRecurrenceTestCase(testCase);

public void ExecuteRecurrenceTestCase(RecurrenceTestCase testCase)
{
Calendar cal = new Calendar();

CalendarEvent evt = cal.Create<CalendarEvent>();
evt.Summary = "Event summary";

// Start at midnight, UTC time
evt.Start = testCase.DtStart;
evt.RecurrenceRules.Add(new RecurrencePattern(testCase.RRule)
{
RestrictionType = RecurrenceRestrictionType.NoRestriction,
});

var occurrences = evt.GetOccurrences(testCase.StartAt?.Value ?? DateTime.MinValue, DateTime.MaxValue)
.OrderBy(x => x)
.ToList();

var startDates = occurrences.Select(x => x.Period.StartTime).ToList();

Assert.That(startDates, Is.EqualTo(testCase.Instances));
}
}
}
33 changes: 33 additions & 0 deletions Ical.Net.Tests/contrib/libical/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
libical is distributed under two licenses.
You may choose the terms of either:

* The Mozilla Public License (MPL) v2.0

or

* The GNU Lesser General Public License (LGPL) v2.1

----------------------------------------------------------------------

Software distributed under these licenses is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing rights
and limitations under the License.
Libical is distributed under both the LGPL and the MPL. The MPL
notice, reproduced below, covers the use of either of the licenses.

----------------------------------------------------------------------

The Original Code is libical.

The Initial Developer of the Original Code is Eric Busboom

All Rights Reserved.

Contributor(s): See individual source files.

Copyright: The libical developers


The project that contains this LICENSE.txt file (ICal.Net) uses the code under the
Mozilla Public License (MPL) v2.0.
Loading
Loading