diff --git a/Ical.Net.Tests/RecurrenceTests.cs b/Ical.Net.Tests/RecurrenceTests.cs index 675c16f5..273933a8 100644 --- a/Ical.Net.Tests/RecurrenceTests.cs +++ b/Ical.Net.Tests/RecurrenceTests.cs @@ -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; @@ -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; @@ -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 ) { @@ -70,7 +69,7 @@ private void EventOccurrenceTest( CalDateTime fromDate, CalDateTime toDate, Period[] expectedPeriods, - string[] timeZones + string[]? timeZones ) { EventOccurrenceTest(cal, fromDate, toDate, expectedPeriods, timeZones, 0); @@ -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) @@ -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 @@ -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++) @@ -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(); @@ -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))); @@ -3685,15 +3684,15 @@ 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 Instances { get; set; } + public IReadOnlyList? Instances { get; set; } - public string Exception { get; set; } + public string? Exception { get; set; } public override string ToString() => $"Line {LineNumber}: {DtStart}, {RRule}"; @@ -3701,7 +3700,7 @@ public override string ToString() private static IEnumerable ParseTestCaseFile(string fileContent) { - RecurrenceTestCase current = null; + RecurrenceTestCase? current = null; var rd = new StringReader(fileContent); var lineNo = 0; @@ -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) @@ -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 diff --git a/Ical.Net/DataTypes/RecurrencePattern.cs b/Ical.Net/DataTypes/RecurrencePattern.cs index df82a0b7..1794e4b4 100644 --- a/Ical.Net/DataTypes/RecurrencePattern.cs +++ b/Ical.Net/DataTypes/RecurrencePattern.cs @@ -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) { diff --git a/Ical.Net/Utility/CollectionHelpers.cs b/Ical.Net/Utility/CollectionHelpers.cs index 89712889..4bb2db50 100644 --- a/Ical.Net/Utility/CollectionHelpers.cs +++ b/Ical.Net/Utility/CollectionHelpers.cs @@ -261,7 +261,7 @@ public static IEnumerable OrderedDistinct(this IEnumerable items, IEqua foreach (var item in items) { - if (first || !comparer.Equals(prev, item)) + if (first || !comparer.Equals(prev!, item)) yield return item; prev = item;