Skip to content

Commit

Permalink
Fix: Minor NRT warnings with Recurrence (ical-org#743)
Browse files Browse the repository at this point in the history
Enable NRT for RecurrenceTests
  • Loading branch information
axunonb authored Mar 4, 2025
1 parent 0151d03 commit ae7d699
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
39 changes: 19 additions & 20 deletions Ical.Net.Tests/RecurrenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand All @@ -15,7 +15,6 @@
using Ical.Net.Evaluation;
using Ical.Net.Serialization;
using Ical.Net.Serialization.DataTypes;
using Ical.Net.Utility;
using NUnit.Framework;

namespace Ical.Net.Tests;
Expand All @@ -27,10 +26,10 @@ public class RecurrenceTests

private void EventOccurrenceTest(
Calendar cal,
CalDateTime fromDate,
CalDateTime toDate,
CalDateTime? fromDate,
CalDateTime? toDate,
Period[] expectedPeriods,
string[] timeZones,
string[]? timeZones,
int eventIndex
)
{
Expand Down Expand Up @@ -70,7 +69,7 @@ private void EventOccurrenceTest(
CalDateTime fromDate,
CalDateTime toDate,
Period[] expectedPeriods,
string[] timeZones
string[]? timeZones
)
{
EventOccurrenceTest(cal, fromDate, toDate, expectedPeriods, timeZones, 0);
Expand Down Expand Up @@ -112,7 +111,7 @@ public void EventOccurrenceTest(

var periodSerializer = new PeriodSerializer();
var periods = expectedPeriods
.Select(p => (Period) periodSerializer.Deserialize(new StringReader(p)))
.Select(p => (Period) periodSerializer.Deserialize(new StringReader(p))!)
.Select(p =>
p.Duration is null
? new Period(p.StartTime.ToTimeZone(tzid), p.EndTime)
Expand Down Expand Up @@ -2578,7 +2577,7 @@ public void Bug3007244()
[TestCase("DTSTART;TZID=Europe/Vienna:20250316T023000", "DURATION:P1W", "20250316T023000/PT168H", "20250323T023000/PT168H", "20250330T033000/PT168H")]
[TestCase("DTSTART;TZID=Europe/Vienna:20250316T023000", "DURATION:P7D", "20250316T023000/PT168H", "20250323T023000/PT168H", "20250330T033000/PT168H")]

public void DurationOfRecurrencesOverDst(string dtStart, string dtEnd, string d1, string d2, string d3)
public void DurationOfRecurrencesOverDst(string dtStart, string dtEnd, string? d1, string? d2, string? d3)
{
var iCal = Calendar.Load($"""
BEGIN:VCALENDAR
Expand All @@ -2596,7 +2595,7 @@ public void DurationOfRecurrencesOverDst(string dtStart, string dtEnd, string d1
var expectedPeriods =
new[] { d1, d2, d3 }
.Where(x => x != null)
.Select(x => (Period)periodSerializer.Deserialize(new StringReader(x)))
.Select(x => (Period)periodSerializer.Deserialize(new StringReader(x!))!)
.ToArray();

for (var index = 0; index < expectedPeriods.Length; index++)
Expand Down Expand Up @@ -2708,7 +2707,7 @@ public void Bug3119920()
using var sr = new StringReader("FREQ=WEEKLY;UNTIL=20251126T120000;INTERVAL=1;BYDAY=MO");
var start = new CalDateTime(2010, 11, 27, 9, 0, 0);
var serializer = new RecurrencePatternSerializer();
var rp = (RecurrencePattern)serializer.Deserialize(sr);
var rp = (RecurrencePattern)serializer.Deserialize(sr)!;
var rpe = new RecurrencePatternEvaluator(rp);
var recurringPeriods = rpe.Evaluate(start, start, rp.Until, false).ToList();

Expand Down Expand Up @@ -2754,7 +2753,7 @@ public void Bug3292737()
{
using var sr = new StringReader("FREQ=WEEKLY;UNTIL=20251126");
var serializer = new RecurrencePatternSerializer();
var rp = (RecurrencePattern)serializer.Deserialize(sr);
var rp = (RecurrencePattern)serializer.Deserialize(sr)!;

Assert.That(rp, Is.Not.Null);
Assert.That(rp.Until, Is.EqualTo(new CalDateTime(2025, 11, 26)));
Expand Down Expand Up @@ -3685,23 +3684,23 @@ public class RecurrenceTestCase
{
public int LineNumber { get; set; }

public string RRule { get; set; }
public string? RRule { get; set; }

public CalDateTime DtStart { get; set; }
public CalDateTime? DtStart { get; set; }

public CalDateTime StartAt { get; set; }
public CalDateTime? StartAt { get; set; }

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

public string Exception { get; set; }
public string? Exception { get; set; }

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

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

var rd = new StringReader(fileContent);
var lineNo = 0;
Expand Down Expand Up @@ -3790,11 +3789,11 @@ public void ExecuteRecurrenceTestCase(RecurrenceTestCase testCase)
if (testCase.Exception != null)
{
var exceptionType = Type.GetType(testCase.Exception)!;
Assert.Throws(exceptionType, () => new RecurrencePattern(testCase.RRule));
Assert.Throws(exceptionType, () => new RecurrencePattern(testCase.RRule!));
return;
}

evt.RecurrenceRules.Add(new RecurrencePattern(testCase.RRule));
evt.RecurrenceRules.Add(new RecurrencePattern(testCase.RRule!));

var occurrences = evt.GetOccurrences(testCase.StartAt?.Value ?? DateTime.MinValue, DateTime.MaxValue)
.OrderBy(x => x)
Expand Down Expand Up @@ -3891,7 +3890,7 @@ public void TestGetOccurrenceIndefinite()
[TestCase("UTC")]
[TestCase("Europe/Vienna")]
[TestCase("America/New_York")]
public void TestDtStartTimezone(string tzId)
public void TestDtStartTimezone(string? tzId)
{
var icalText = """
BEGIN:VCALENDAR
Expand Down
33 changes: 17 additions & 16 deletions Ical.Net/DataTypes/RecurrencePattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,28 @@ public RecurrencePattern(string value) : this()
CopyFrom(deserialized);
}

public override string ToString()
public override string? ToString()
{
var serializer = new RecurrencePatternSerializer();
return serializer.SerializeToString(this);
}

protected bool Equals(RecurrencePattern? other) => other != null
&& Interval == other.Interval
&& Frequency == other.Frequency
&& (Until?.Equals(other.Until!) ?? other.Until == null)
&& Count == other.Count
&& FirstDayOfWeek == other.FirstDayOfWeek
&& CollectionEquals(BySecond, other.BySecond)
&& CollectionEquals(ByMinute, other.ByMinute)
&& CollectionEquals(ByHour, other.ByHour)
&& CollectionEquals(ByDay, other.ByDay)
&& CollectionEquals(ByMonthDay, other.ByMonthDay)
&& CollectionEquals(ByYearDay, other.ByYearDay)
&& CollectionEquals(ByWeekNo, other.ByWeekNo)
&& CollectionEquals(ByMonth, other.ByMonth)
&& CollectionEquals(BySetPosition, other.BySetPosition);
protected bool Equals(RecurrencePattern? other)
=> other != null
&& Interval == other.Interval
&& Frequency == other.Frequency
&& (Until?.Equals(other.Until!) ?? other.Until == null)
&& Count == other.Count
&& FirstDayOfWeek == other.FirstDayOfWeek
&& CollectionEquals(BySecond, other.BySecond)
&& CollectionEquals(ByMinute, other.ByMinute)
&& CollectionEquals(ByHour, other.ByHour)
&& CollectionEquals(ByDay, other.ByDay)
&& CollectionEquals(ByMonthDay, other.ByMonthDay)
&& CollectionEquals(ByYearDay, other.ByYearDay)
&& CollectionEquals(ByWeekNo, other.ByWeekNo)
&& CollectionEquals(ByMonth, other.ByMonth)
&& CollectionEquals(BySetPosition, other.BySetPosition);

public override bool Equals(object? obj)
{
Expand Down
2 changes: 1 addition & 1 deletion Ical.Net/Utility/CollectionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public static IEnumerable<T> OrderedDistinct<T>(this IEnumerable<T> items, IEqua

foreach (var item in items)
{
if (first || !comparer.Equals(prev, item))
if (first || !comparer.Equals(prev!, item))
yield return item;

prev = item;
Expand Down

0 comments on commit ae7d699

Please sign in to comment.