diff --git a/Ical.Net.Tests/CalendarEventTest.cs b/Ical.Net.Tests/CalendarEventTest.cs index 01157066..61f5ade7 100644 --- a/Ical.Net.Tests/CalendarEventTest.cs +++ b/Ical.Net.Tests/CalendarEventTest.cs @@ -417,8 +417,8 @@ public void EventResourcesCanBeZeroedOut() Assert.That(e.Resources.Any(r => resources.Contains(r)), Is.False); }); - e.Resources = null; - //See https://github.com/rianjs/ical.net/issues/208 -- this should be changed later so the collection is really null + //See https://github.com/rianjs/ical.net/issues/208 + e.Resources = Array.Empty(); Assert.That(e.Resources?.Count, Is.EqualTo(0)); } diff --git a/Ical.Net.Tests/ComponentTest.cs b/Ical.Net.Tests/ComponentTest.cs index 8dcf0b90..33090c7d 100644 --- a/Ical.Net.Tests/ComponentTest.cs +++ b/Ical.Net.Tests/ComponentTest.cs @@ -3,9 +3,8 @@ // Licensed under the MIT license. // -using System; +#nullable enable using Ical.Net.CalendarComponents; -using Ical.Net.DataTypes; using NUnit.Framework; namespace Ical.Net.Tests; @@ -18,8 +17,11 @@ public void UniqueComponent1() var iCal = new Calendar(); var evt = iCal.Create(); - Assert.That(evt.Uid, Is.Not.Null); - Assert.That(evt.Created, Is.Null); // We don't want this to be set automatically - Assert.That(evt.DtStamp, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(evt.Uid, Is.Not.Null); + Assert.That(evt.Created, Is.Null); // We don't want this to be set automatically + Assert.That(evt.DtStamp, Is.Not.Null); + }); } } diff --git a/Ical.Net.Tests/CopyComponentTests.cs b/Ical.Net.Tests/CopyComponentTests.cs index c4df1d63..1be5799f 100644 --- a/Ical.Net.Tests/CopyComponentTests.cs +++ b/Ical.Net.Tests/CopyComponentTests.cs @@ -23,7 +23,7 @@ public class CopyComponentTests [Test, TestCaseSource(nameof(CopyCalendarTest_TestCases)), Category("Copy tests")] public void CopyCalendarTest(string calendarString) { - var iCal1 = Calendar.Load(calendarString); + var iCal1 = Calendar.Load(calendarString)!; var iCal2 = iCal1.Copy(); SerializationTests.CompareCalendars(iCal1, iCal2); } diff --git a/Ical.Net.Tests/FreeBusyTest.cs b/Ical.Net.Tests/FreeBusyTest.cs index 2371c8ab..261a7ee5 100644 --- a/Ical.Net.Tests/FreeBusyTest.cs +++ b/Ical.Net.Tests/FreeBusyTest.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using Ical.Net.CalendarComponents; using Ical.Net.DataTypes; using NUnit.Framework; @@ -18,14 +19,14 @@ public class FreeBusyTest [Test, Category("FreeBusy")] public void GetFreeBusyStatus1() { - Calendar cal = new Calendar(); + var cal = new Calendar(); - CalendarEvent evt = cal.Create(); + var evt = cal.Create(); evt.Summary = "Test event"; evt.Start = new CalDateTime(2010, 10, 1, 8, 0, 0); evt.End = new CalDateTime(2010, 10, 1, 9, 0, 0); - var freeBusy = cal.GetFreeBusy(new CalDateTime(2010, 10, 1, 0, 0, 0), new CalDateTime(2010, 10, 7, 11, 59, 59)); + var freeBusy = cal.GetFreeBusy(new CalDateTime(2010, 10, 1, 0, 0, 0), new CalDateTime(2010, 10, 7, 11, 59, 59))!; Assert.Multiple(() => { Assert.That(freeBusy.GetFreeBusyStatus(new CalDateTime(2010, 10, 1, 7, 59, 59)), Is.EqualTo(FreeBusyStatus.Free)); @@ -34,4 +35,4 @@ public void GetFreeBusyStatus1() Assert.That(freeBusy.GetFreeBusyStatus(new CalDateTime(2010, 10, 1, 9, 0, 0)), Is.EqualTo(FreeBusyStatus.Free)); }); } -} \ No newline at end of file +} diff --git a/Ical.Net.Tests/PeriodListWrapperTests.cs b/Ical.Net.Tests/PeriodListWrapperTests.cs index 4d506235..d6f5c8a0 100644 --- a/Ical.Net.Tests/PeriodListWrapperTests.cs +++ b/Ical.Net.Tests/PeriodListWrapperTests.cs @@ -187,14 +187,14 @@ public void AddRPeriod_ShouldCreate_DedicatePeriodList() var serializer = new CalendarSerializer(cal); var serialized = serializer.SerializeToString(); // Assign the deserialized event - cal = Calendar.Load(serialized); + cal = Calendar.Load(serialized)!; evt = cal.Events[0]; // Assert the serialized string and the deserialized event Assert.Multiple(() => { // 2 dedicate PeriodList objects - Assert.That(evt.RecurrenceDatesPeriodLists, Has.Count.EqualTo(3)); + Assert.That(evt!.RecurrenceDatesPeriodLists, Has.Count.EqualTo(3)); // First PeriodList has date-only periods Assert.That(evt.RecurrenceDatesPeriodLists[0], Has.Count.EqualTo(3)); diff --git a/Ical.Net.Tests/ProgramTest.cs b/Ical.Net.Tests/ProgramTest.cs index b15c6108..4cac77a9 100644 --- a/Ical.Net.Tests/ProgramTest.cs +++ b/Ical.Net.Tests/ProgramTest.cs @@ -40,11 +40,11 @@ public static void TestCal(Calendar cal) [Test] public void Merge1() { - var iCal1 = Calendar.Load(IcsFiles.MonthlyCountByMonthDay3); - var iCal2 = Calendar.Load(IcsFiles.MonthlyByDay1); + var iCal1 = Calendar.Load(IcsFiles.MonthlyCountByMonthDay3)!; + var iCal2 = Calendar.Load(IcsFiles.MonthlyByDay1)!; // Change the UID of the 2nd event to make sure it's different - iCal2.Events[iCal1.Events[0].Uid].Uid = "1234567890"; + iCal2.Events[iCal1.Events[0].Uid!].Uid = "1234567890"; iCal1.MergeWith(iCal2); var evt1 = iCal1.Events.First(); diff --git a/Ical.Net.Tests/RecurrenceTests.cs b/Ical.Net.Tests/RecurrenceTests.cs index 0b5a4900..10c5ce25 100644 --- a/Ical.Net.Tests/RecurrenceTests.cs +++ b/Ical.Net.Tests/RecurrenceTests.cs @@ -107,7 +107,7 @@ public void EventOccurrenceTest( END:VCALENDAR """; - var cal = Calendar.Load(calendarIcalStr); + var cal = Calendar.Load(calendarIcalStr)!; var tzid = cal.Events.Single().Start!.TzId; var periodSerializer = new PeriodSerializer(); @@ -128,7 +128,7 @@ p.Duration is null [Test, Category("Recurrence")] public void YearlyComplex1() { - var iCal = Calendar.Load(IcsFiles.YearlyComplex1); + var iCal = Calendar.Load(IcsFiles.YearlyComplex1)!; ProgramTest.TestCal(iCal); var evt = iCal.Events.First(); var occurrences = evt.GetOccurrences( @@ -164,7 +164,7 @@ public void YearlyComplex1() [Test, Category("Recurrence")] public void DailyCount1() { - var iCal = Calendar.Load(IcsFiles.DailyCount1); + var iCal = Calendar.Load(IcsFiles.DailyCount1)!; EventOccurrenceTest( iCal, new CalDateTime(2006, 7, 1), @@ -191,7 +191,7 @@ public void DailyCount1() [Test, Category("Recurrence")] public void DailyUntil1() { - var iCal = Calendar.Load(IcsFiles.DailyUntil1); + var iCal = Calendar.Load(IcsFiles.DailyUntil1)!; ProgramTest.TestCal(iCal); var evt = iCal.Events.First(); @@ -228,7 +228,7 @@ public void DailyUntil1() [Test, Category("Recurrence")] public void Daily1() { - var iCal = Calendar.Load(IcsFiles.Daily1); + var iCal = Calendar.Load(IcsFiles.Daily1)!; EventOccurrenceTest( iCal, new CalDateTime(1997, 9, 1), @@ -341,7 +341,7 @@ public void Daily1() [Test, Category("Recurrence")] public void DailyCount2() { - var iCal = Calendar.Load(IcsFiles.DailyCount2); + var iCal = Calendar.Load(IcsFiles.DailyCount2)!; EventOccurrenceTest( iCal, new CalDateTime(1997, 9, 1), @@ -363,7 +363,7 @@ public void DailyCount2() [Test, Category("Recurrence")] public void ByMonth1() { - var iCal = Calendar.Load(IcsFiles.ByMonth1); + var iCal = Calendar.Load(IcsFiles.ByMonth1)!; ProgramTest.TestCal(iCal); var evt = iCal.Events.First(); @@ -398,8 +398,8 @@ public void ByMonth1() [Test, Category("Recurrence")] public void ByMonth2() { - var iCal1 = Calendar.Load(IcsFiles.ByMonth1); - var iCal2 = Calendar.Load(IcsFiles.ByMonth2); + var iCal1 = Calendar.Load(IcsFiles.ByMonth1)!; + var iCal2 = Calendar.Load(IcsFiles.ByMonth2)!; ProgramTest.TestCal(iCal1); ProgramTest.TestCal(iCal2); var evt1 = iCal1.Events.First(); @@ -418,7 +418,7 @@ public void ByMonth2() [Test, Category("Recurrence")] public void WeeklyCount1() { - var iCal = Calendar.Load(IcsFiles.WeeklyCount1); + var iCal = Calendar.Load(IcsFiles.WeeklyCount1)!; EventOccurrenceTest( iCal, new CalDateTime(1997, 9, 1), @@ -457,7 +457,7 @@ public void WeeklyCount1() [Test, Category("Recurrence")] public void WeeklyUntil1() { - var iCal = Calendar.Load(IcsFiles.WeeklyUntil1); + var iCal = Calendar.Load(IcsFiles.WeeklyUntil1)!; EventOccurrenceTest( iCal, new CalDateTime(1997, 9, 1), @@ -510,7 +510,7 @@ public void WeeklyUntil1() [Test, Category("Recurrence")] public void WeeklyWkst1() { - var iCal = Calendar.Load(IcsFiles.WeeklyWkst1); + var iCal = Calendar.Load(IcsFiles.WeeklyWkst1)!; EventOccurrenceTest( iCal, new CalDateTime(1997, 9, 1), @@ -552,7 +552,7 @@ public void WeeklyWkst1() [Test, Category("Recurrence")] public void WeeklyUntilWkst1() { - var iCal = Calendar.Load(IcsFiles.WeeklyUntilWkst1); + var iCal = Calendar.Load(IcsFiles.WeeklyUntilWkst1)!; EventOccurrenceTest( iCal, new CalDateTime(1997, 9, 1), @@ -580,8 +580,8 @@ public void WeeklyUntilWkst1() [Test, Category("Recurrence")] public void WeeklyCountWkst1() { - var iCal1 = Calendar.Load(IcsFiles.WeeklyUntilWkst1); - var iCal2 = Calendar.Load(IcsFiles.WeeklyCountWkst1); + var iCal1 = Calendar.Load(IcsFiles.WeeklyUntilWkst1)!; + var iCal2 = Calendar.Load(IcsFiles.WeeklyCountWkst1)!; ProgramTest.TestCal(iCal1); ProgramTest.TestCal(iCal2); var evt1 = iCal1.Events.First(); @@ -602,7 +602,7 @@ public void WeeklyCountWkst1() [Test, Category("Recurrence")] public void WeeklyUntilWkst2() { - var iCal = Calendar.Load(IcsFiles.WeeklyUntilWkst2); + var iCal = Calendar.Load(IcsFiles.WeeklyUntilWkst2)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -671,7 +671,7 @@ public void WeeklyUntilWkst2() [Test, Category("Recurrence")] public void WeeklyUntilWkst2_1() { - var iCal = Calendar.Load(IcsFiles.WeeklyUntilWkst2); + var iCal = Calendar.Load(IcsFiles.WeeklyUntilWkst2)!; EventOccurrenceTest( iCal, new CalDateTime(1997, 9, 9), @@ -735,7 +735,7 @@ public void WeeklyUntilWkst2_1() [Test, Category("Recurrence")] public void WeeklyCountWkst2() { - var iCal = Calendar.Load(IcsFiles.WeeklyCountWkst2); + var iCal = Calendar.Load(IcsFiles.WeeklyCountWkst2)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -761,7 +761,7 @@ public void WeeklyCountWkst2() [Test, Category("Recurrence")] public void MonthlyCountByDay1() { - var iCal = Calendar.Load(IcsFiles.MonthlyCountByDay1); + var iCal = Calendar.Load(IcsFiles.MonthlyCountByDay1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -801,7 +801,7 @@ public void MonthlyCountByDay1() [Test, Category("Recurrence")] public void MonthlyUntilByDay1() { - var iCal = Calendar.Load(IcsFiles.MonthlyUntilByDay1); + var iCal = Calendar.Load(IcsFiles.MonthlyUntilByDay1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -829,7 +829,7 @@ public void MonthlyUntilByDay1() [Test, Category("Recurrence")] public void MonthlyCountByDay2() { - var iCal = Calendar.Load(IcsFiles.MonthlyCountByDay2); + var iCal = Calendar.Load(IcsFiles.MonthlyCountByDay2)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -869,7 +869,7 @@ public void MonthlyCountByDay2() [Test, Category("Recurrence")] public void MonthlyCountByDay3() { - var iCal = Calendar.Load(IcsFiles.MonthlyCountByDay3); + var iCal = Calendar.Load(IcsFiles.MonthlyCountByDay3)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -901,7 +901,7 @@ public void MonthlyCountByDay3() [Test, Category("Recurrence")] public void ByMonthDay1() { - var iCal = Calendar.Load(IcsFiles.ByMonthDay1); + var iCal = Calendar.Load(IcsFiles.ByMonthDay1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -933,7 +933,7 @@ public void ByMonthDay1() [Test, Category("Recurrence")] public void MonthlyCountByMonthDay1() { - var iCal = Calendar.Load(IcsFiles.MonthlyCountByMonthDay1); + var iCal = Calendar.Load(IcsFiles.MonthlyCountByMonthDay1)!; EventOccurrenceTest( iCal, @@ -974,7 +974,7 @@ public void MonthlyCountByMonthDay1() [Test, Category("Recurrence")] public void MonthlyCountByMonthDay2() { - var iCal = Calendar.Load(IcsFiles.MonthlyCountByMonthDay2); + var iCal = Calendar.Load(IcsFiles.MonthlyCountByMonthDay2)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1014,7 +1014,7 @@ public void MonthlyCountByMonthDay2() [Test, Category("Recurrence")] public void MonthlyCountByMonthDay3() { - var iCal = Calendar.Load(IcsFiles.MonthlyCountByMonthDay3); + var iCal = Calendar.Load(IcsFiles.MonthlyCountByMonthDay3)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1054,7 +1054,7 @@ public void MonthlyCountByMonthDay3() [Test, Category("Recurrence")] public void MonthlyByDay1() { - var iCal = Calendar.Load(IcsFiles.MonthlyByDay1); + var iCal = Calendar.Load(IcsFiles.MonthlyByDay1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1110,7 +1110,7 @@ public void MonthlyByDay1() [Test, Category("Recurrence")] public void YearlyByMonth1() { - var iCal = Calendar.Load(IcsFiles.YearlyByMonth1); + var iCal = Calendar.Load(IcsFiles.YearlyByMonth1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1138,7 +1138,7 @@ public void YearlyByMonth1() [Test, Category("Recurrence")] public void YearlyCountByMonth1() { - var iCal = Calendar.Load(IcsFiles.YearlyCountByMonth1); + var iCal = Calendar.Load(IcsFiles.YearlyCountByMonth1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1166,7 +1166,7 @@ public void YearlyCountByMonth1() [Test, Category("Recurrence")] public void YearlyCountByYearDay1() { - var iCal = Calendar.Load(IcsFiles.YearlyCountByYearDay1); + var iCal = Calendar.Load(IcsFiles.YearlyCountByYearDay1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1206,7 +1206,7 @@ public void YearlyCountByYearDay1() [Test, Category("Recurrence")] public void YearlyByDay1() { - var iCal = Calendar.Load(IcsFiles.YearlyByDay1); + var iCal = Calendar.Load(IcsFiles.YearlyByDay1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1244,7 +1244,7 @@ public void WeekNoOrderingShouldNotMatter() [Test, Category("Recurrence")] public void YearlyByWeekNo1() { - var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo1); + var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1270,7 +1270,7 @@ public void YearlyByWeekNo1() [Test, Category("Recurrence")] public void YearlyByWeekNo2() { - var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo2); + var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo2)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1295,7 +1295,7 @@ public void YearlyByWeekNo2() [Test, Category("Recurrence")] public void YearlyByWeekNo3() { - var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo3); + var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo3)!; EventOccurrenceTest( iCal, new CalDateTime(2001, 1, 1), @@ -1318,7 +1318,7 @@ public void YearlyByWeekNo3() [Test, Category("Recurrence")] public void YearlyByWeekNo4() { - var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo4); + var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo4)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1362,7 +1362,7 @@ public void YearlyByWeekNo4() [Test, Category("Recurrence")] public void YearlyByWeekNo5() { - var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo5); + var iCal = Calendar.Load(IcsFiles.YearlyByWeekNo5)!; EventOccurrenceTest( iCal, new CalDateTime(2001, 1, 1), @@ -1393,7 +1393,7 @@ public void YearlyByWeekNo5() [Test, Category("Recurrence")] public void YearlyByMonth2() { - var iCal = Calendar.Load(IcsFiles.YearlyByMonth2); + var iCal = Calendar.Load(IcsFiles.YearlyByMonth2)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1422,7 +1422,7 @@ public void YearlyByMonth2() [Test, Category("Recurrence")] public void YearlyByMonth3() { - var iCal = Calendar.Load(IcsFiles.YearlyByMonth3); + var iCal = Calendar.Load(IcsFiles.YearlyByMonth3)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1481,7 +1481,7 @@ public void YearlyByMonth3() [Test, Category("Recurrence")] public void MonthlyByMonthDay1() { - var iCal = Calendar.Load(IcsFiles.MonthlyByMonthDay1); + var iCal = Calendar.Load(IcsFiles.MonthlyByMonthDay1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1511,7 +1511,7 @@ public void MonthlyByMonthDay1() [Test, Category("Recurrence")] public void MonthlyByMonthDay2() { - var iCal = Calendar.Load(IcsFiles.MonthlyByMonthDay2); + var iCal = Calendar.Load(IcsFiles.MonthlyByMonthDay2)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1551,7 +1551,7 @@ public void MonthlyByMonthDay2() [Test, Category("Recurrence")] public void YearlyByMonthDay1() { - var iCal = Calendar.Load(IcsFiles.YearlyByMonthDay1); + var iCal = Calendar.Load(IcsFiles.YearlyByMonthDay1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1572,7 +1572,7 @@ public void YearlyByMonthDay1() [Test, Category("Recurrence")] public void MonthlyBySetPos1() { - var iCal = Calendar.Load(IcsFiles.MonthlyBySetPos1); + var iCal = Calendar.Load(IcsFiles.MonthlyBySetPos1)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1598,7 +1598,7 @@ public void MonthlyBySetPos1() [Test, Category("Recurrence")] public void MonthlyBySetPos2() { - var iCal = Calendar.Load(IcsFiles.MonthlyBySetPos2); + var iCal = Calendar.Load(IcsFiles.MonthlyBySetPos2)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1634,7 +1634,7 @@ public void MonthlyBySetPos2() [Test, Category("Recurrence")] public void HourlyUntil1() { - var iCal = Calendar.Load(IcsFiles.HourlyUntil1); + var iCal = Calendar.Load(IcsFiles.HourlyUntil1)!; EventOccurrenceTest( iCal, fromDate: new CalDateTime(1996, 1, 1), @@ -1656,7 +1656,7 @@ public void HourlyUntil1() [Test, Category("Recurrence")] public void MinutelyCount1() { - var iCal = Calendar.Load(IcsFiles.MinutelyCount1); + var iCal = Calendar.Load(IcsFiles.MinutelyCount1)!; EventOccurrenceTest( iCal, new CalDateTime(1997, 9, 2), @@ -1680,7 +1680,7 @@ public void MinutelyCount1() [Test, Category("Recurrence")] public void MinutelyCount2() { - var iCal = Calendar.Load(IcsFiles.MinutelyCount2); + var iCal = Calendar.Load(IcsFiles.MinutelyCount2)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1702,7 +1702,7 @@ public void MinutelyCount2() [Test, Category("Recurrence")] public void MinutelyCount3() { - var iCal = Calendar.Load(IcsFiles.MinutelyCount3); + var iCal = Calendar.Load(IcsFiles.MinutelyCount3)!; EventOccurrenceTest( iCal, new CalDateTime(2010, 8, 27), @@ -1730,7 +1730,7 @@ public void MinutelyCount3() [Test, Category("Recurrence")] public void MinutelyCount4() { - var iCal = Calendar.Load(IcsFiles.MinutelyCount4); + var iCal = Calendar.Load(IcsFiles.MinutelyCount4)!; EventOccurrenceTest( iCal, new CalDateTime(2010, 8, 27), @@ -1758,7 +1758,7 @@ public void MinutelyCount4() [Test, Category("Recurrence")] public void DailyByHourMinute1() { - var iCal = Calendar.Load(IcsFiles.DailyByHourMinute1); + var iCal = Calendar.Load(IcsFiles.DailyByHourMinute1)!; EventOccurrenceTest( iCal, new CalDateTime(1997, 9, 2), @@ -1824,8 +1824,8 @@ public void DailyByHourMinute1() [Test, Category("Recurrence")] public void MinutelyByHour1() { - var iCal1 = Calendar.Load(IcsFiles.DailyByHourMinute1); - var iCal2 = Calendar.Load(IcsFiles.MinutelyByHour1); + var iCal1 = Calendar.Load(IcsFiles.DailyByHourMinute1)!; + var iCal2 = Calendar.Load(IcsFiles.MinutelyByHour1)!; ProgramTest.TestCal(iCal1); ProgramTest.TestCal(iCal2); var evt1 = iCal1.Events.First(); @@ -1844,7 +1844,7 @@ public void MinutelyByHour1() [Test, Category("Recurrence")] public void WeeklyCountWkst3() { - var iCal = Calendar.Load(IcsFiles.WeeklyCountWkst3); + var iCal = Calendar.Load(IcsFiles.WeeklyCountWkst3)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1867,7 +1867,7 @@ public void WeeklyCountWkst3() [Test, Category("Recurrence")] public void WeeklyCountWkst4() { - var iCal = Calendar.Load(IcsFiles.WeeklyCountWkst4); + var iCal = Calendar.Load(IcsFiles.WeeklyCountWkst4)!; EventOccurrenceTest( iCal, new CalDateTime(1996, 1, 1), @@ -1890,7 +1890,7 @@ public void WeeklyCountWkst4() [Test, Category("Recurrence")] public void Bug1741093() { - var iCal = Calendar.Load(IcsFiles.Bug1741093); + var iCal = Calendar.Load(IcsFiles.Bug1741093)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 7, 1), @@ -1917,7 +1917,7 @@ public void Bug1741093() [Test, Category("Recurrence")] public void Secondly_DefinedNumberOfOccurrences_ShouldSucceed() { - var iCal = Calendar.Load(IcsFiles.Secondly1); + var iCal = Calendar.Load(IcsFiles.Secondly1)!; var start = new CalDateTime(2007, 6, 21, 8, 0, 0, _tzid); var end = new CalDateTime(2007, 6, 21, 8, 1, 0, _tzid); // End period is exclusive, not inclusive. @@ -1934,7 +1934,7 @@ public void Secondly_DefinedNumberOfOccurrences_ShouldSucceed() [Test, Category("Recurrence")] public void Minutely_DefinedNumberOfOccurrences_ShouldSucceed() { - var iCal = Calendar.Load(IcsFiles.Minutely1); + var iCal = Calendar.Load(IcsFiles.Minutely1)!; var start = new CalDateTime(2007, 6, 21, 8, 0, 0, _tzid); var end = new CalDateTime(2007, 6, 21, 12, 0, 1, _tzid); // End period is exclusive, not inclusive. @@ -1951,7 +1951,7 @@ public void Minutely_DefinedNumberOfOccurrences_ShouldSucceed() [Test, Category("Recurrence")] public void Hourly_DefinedNumberOfOccurrences_ShouldSucceed() { - var iCal = Calendar.Load(IcsFiles.Hourly1); + var iCal = Calendar.Load(IcsFiles.Hourly1)!; var start = new CalDateTime(2007, 6, 21, 8, 0, 0, _tzid); var end = new CalDateTime(2007, 6, 25, 8, 0, 1, _tzid); // End period is exclusive, not inclusive. @@ -1971,7 +1971,7 @@ public void Hourly_DefinedNumberOfOccurrences_ShouldSucceed() [Test, Category("Recurrence")] public void MonthlyInterval1() { - var iCal = Calendar.Load(IcsFiles.MonthlyInterval1); + var iCal = Calendar.Load(IcsFiles.MonthlyInterval1)!; EventOccurrenceTest( iCal, new CalDateTime(2008, 1, 1, 7, 0, 0, _tzid), @@ -1991,7 +1991,7 @@ public void MonthlyInterval1() [Test, Category("Recurrence")] public void YearlyInterval1() { - var iCal = Calendar.Load(IcsFiles.YearlyInterval1); + var iCal = Calendar.Load(IcsFiles.YearlyInterval1)!; EventOccurrenceTest( iCal, new CalDateTime(2006, 1, 1, 7, 0, 0, _tzid), @@ -2011,7 +2011,7 @@ public void YearlyInterval1() [Test, Category("Recurrence")] public void DailyInterval1() { - var iCal = Calendar.Load(IcsFiles.DailyInterval1); + var iCal = Calendar.Load(IcsFiles.DailyInterval1)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 4, 11, 7, 0, 0, _tzid), @@ -2031,7 +2031,7 @@ public void DailyInterval1() [Test, Category("Recurrence")] public void HourlyInterval1() { - var iCal = Calendar.Load(IcsFiles.HourlyInterval1); + var iCal = Calendar.Load(IcsFiles.HourlyInterval1)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 4, 9, 10, 0, 0, _tzid), @@ -2058,7 +2058,7 @@ public void HourlyInterval1() [Test, Category("Recurrence")] public void YearlyBySetPos1() { - var iCal = Calendar.Load(IcsFiles.YearlyBySetPos1); + var iCal = Calendar.Load(IcsFiles.YearlyBySetPos1)!; EventOccurrenceTest( iCal, new CalDateTime(2009, 1, 1, 0, 0, 0, _tzid), @@ -2087,7 +2087,7 @@ public void YearlyBySetPos1() [Test, Category("Recurrence")] public void Empty1() { - var iCal = Calendar.Load(IcsFiles.Empty1); + var iCal = Calendar.Load(IcsFiles.Empty1)!; EventOccurrenceTest( iCal, new CalDateTime(2009, 1, 1, 0, 0, 0, _tzid), @@ -2106,7 +2106,7 @@ public void Empty1() [Test, Category("Recurrence")] public void HourlyInterval2() { - var iCal = Calendar.Load(IcsFiles.HourlyInterval2); + var iCal = Calendar.Load(IcsFiles.HourlyInterval2)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 4, 9, 7, 0, 0), @@ -2135,7 +2135,7 @@ public void HourlyInterval2() [Test, Category("Recurrence")] public void MinutelyInterval1() { - var iCal = Calendar.Load(IcsFiles.MinutelyInterval1); + var iCal = Calendar.Load(IcsFiles.MinutelyInterval1)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 4, 9, 7, 0, 0), @@ -2164,7 +2164,7 @@ public void MinutelyInterval1() [Test, Category("Recurrence")] public void DailyInterval2() { - var iCal = Calendar.Load(IcsFiles.DailyInterval2); + var iCal = Calendar.Load(IcsFiles.DailyInterval2)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 4, 9, 7, 0, 0), @@ -2192,7 +2192,7 @@ public void DailyInterval2() [Test, Category("Recurrence")] public void DailyByDay1() { - var iCal = Calendar.Load(IcsFiles.DailyByDay1); + var iCal = Calendar.Load(IcsFiles.DailyByDay1)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 9, 10, 7, 0, 0), @@ -2216,7 +2216,7 @@ public void DailyByDay1() [Test, Category("Recurrence")] public void WeeklyWeekStartsLastYear() { - var iCal = Calendar.Load(IcsFiles.WeeklyWeekStartsLastYear); + var iCal = Calendar.Load(IcsFiles.WeeklyWeekStartsLastYear)!; EventOccurrenceTest( iCal, new CalDateTime(2012, 1, 1, 7, 0, 0), @@ -2244,7 +2244,7 @@ public void WeeklyWeekStartsLastYear() [Test, Category("Recurrence")] public void WeeklyInterval1() { - var iCal = Calendar.Load(IcsFiles.WeeklyInterval1); + var iCal = Calendar.Load(IcsFiles.WeeklyInterval1)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 9, 10, 7, 0, 0), @@ -2271,7 +2271,7 @@ public void WeeklyInterval1() [Test, Category("Recurrence")] public void Monthly1() { - var iCal = Calendar.Load(IcsFiles.Monthly1); + var iCal = Calendar.Load(IcsFiles.Monthly1)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 9, 10, 7, 0, 0), @@ -2302,7 +2302,7 @@ public void Monthly1() [Test, Category("Recurrence")] public void Yearly1() { - var iCal = Calendar.Load(IcsFiles.Yearly1); + var iCal = Calendar.Load(IcsFiles.Yearly1)!; EventOccurrenceTest( iCal, new CalDateTime(2007, 9, 10, 7, 0, 0), @@ -2336,7 +2336,7 @@ public void Yearly1() [Test, Category("Recurrence")] public void Bug2912657() { - var iCal = Calendar.Load(IcsFiles.Bug2912657); + var iCal = Calendar.Load(IcsFiles.Bug2912657)!; var localTzid = iCal.Events.First().Start!.TzId; // Daily recurrence @@ -2394,8 +2394,8 @@ public void Bug2912657() [Test, Category("Recurrence")] public void Bug2916581() { - var iCal = Calendar.Load(IcsFiles.Bug2916581); - var localTzid = iCal.TimeZones[0].TzId; + var iCal = Calendar.Load(IcsFiles.Bug2916581)!; + var localTzid = iCal.TimeZones[0]!.TzId!; // Weekly across year boundary EventOccurrenceTest( @@ -2434,8 +2434,8 @@ public void Bug2916581() [Test, Category("Recurrence")] public void Bug2959692() { - var iCal = Calendar.Load(IcsFiles.Bug2959692); - var localTzid = iCal.TimeZones[0].TzId; + var iCal = Calendar.Load(IcsFiles.Bug2959692)!; + var localTzid = iCal.TimeZones[0]!.TzId!; EventOccurrenceTest( iCal, @@ -2464,8 +2464,8 @@ public void Bug2959692() [Test, Category("Recurrence")] public void Bug2966236() { - var iCal = Calendar.Load(IcsFiles.Bug2966236); - var localTzid = iCal.TimeZones[0].TzId; + var iCal = Calendar.Load(IcsFiles.Bug2966236)!; + var localTzid = iCal.TimeZones[0]!.TzId; EventOccurrenceTest( iCal, @@ -2508,7 +2508,7 @@ public void Bug2966236() [Test, Category("Recurrence")] public void Bug3007244() { - var iCal = Calendar.Load(IcsFiles.Bug3007244); + var iCal = Calendar.Load(IcsFiles.Bug3007244)!; // date only cannot have a time zone EventOccurrenceTest( @@ -2588,7 +2588,7 @@ public void DurationOfRecurrencesOverDst(string dtStart, string dtEnd, string? d RRULE:FREQ=WEEKLY;COUNT=3 END:VEVENT END:VCALENDAR - """); + """)!; var start = iCal.Events.First().Start; @@ -3104,7 +3104,7 @@ public void ExDateShouldFilterOutAllPeriods() TRANSP:TRANSPARENT END:VEVENT END:VCALENDAR"; - var calendar = Calendar.Load(ical); + var calendar = Calendar.Load(ical)!; var firstEvent = calendar.Events.First(); var startSearch = new CalDateTime(2010, 1, 1); var endSearch = new CalDateTime(2016, 12, 31); @@ -3133,7 +3133,7 @@ public void RDateShouldBeUnionedWithRecurrenceSet() END:VEVENT END:VCALENDAR"; - var calendar = Calendar.Load(ical); + var calendar = Calendar.Load(ical)!; var firstEvent = calendar.Events.First(); var startSearch = new CalDateTime(DateTime.Parse("2015-08-28T07:00:00"), _tzid); var endSearch = new CalDateTime(DateTime.Parse("2016-08-28T07:00:00").AddDays(7), _tzid); @@ -3188,7 +3188,7 @@ public void OccurrenceMustBeCompletelyContainedWithinSearchRange() Uid = "abab717c-1786-4efc-87dd-6859c2b48eb6", }; - var deserializedCalendar = Calendar.Load(ical); + var deserializedCalendar = Calendar.Load(ical)!; var firstEvent = deserializedCalendar.Events.First(); var calendar = new Calendar(); calendar.Events.Add(e); @@ -3276,7 +3276,7 @@ public void EventsWithShareUidsShouldGenerateASingleRecurrenceSet() END:VCALENDAR """; - var calendar = Calendar.Load(ical); + var calendar = Calendar.Load(ical)!; //The API should be something like: var orderedOccurrences = calendar.GetOccurrences() @@ -3519,10 +3519,10 @@ public void RecurrenceRuleTests() END:VCALENDAR """; - var simpleA = Calendar.Load(aString); - var normalA = Calendar.Load(aString); - var simpleB = Calendar.Load(bString); - var normalB = Calendar.Load(bString); + var simpleA = Calendar.Load(aString)!; + var normalA = Calendar.Load(aString)!; + var simpleB = Calendar.Load(bString)!; + var normalB = Calendar.Load(bString)!; var calendarList = new List { simpleA, normalA, simpleB, normalB }; var eventList = new List @@ -3665,7 +3665,7 @@ public void InclusiveRruleUntil() END:VCALENDAR """; const string timeZoneId = @"Eastern Standard Time"; - var calendar = Calendar.Load(icalText); + var calendar = Calendar.Load(icalText)!; var firstEvent = calendar.Events.First(); var startSearch = new CalDateTime(DateTime.Parse("2017-07-01T00:00:00"), timeZoneId); var endSearch = new CalDateTime(DateTime.Parse("2018-07-01T00:00:00"), timeZoneId); @@ -3848,7 +3848,7 @@ public void GetOccurrenceShouldExcludeDtEndFloating() END:VCALENDAR """; - var calendar = Calendar.Load(ical); + var calendar = Calendar.Load(ical)!; // Set start date for occurrences to search to the end date of the event var occurrences = calendar.GetOccurrences(new CalDateTime(2024, 12, 2), new CalDateTime(2024, 12, 3)); @@ -3871,7 +3871,7 @@ public void TestGetOccurrenceIndefinite() END:VCALENDAR """; - var calendar = Calendar.Load(ical); + var calendar = Calendar.Load(ical)!; // Although the occurrences are unbounded, we can still call GetOccurrences without // specifying bounds, because the instances are only generated on enumeration. @@ -3901,7 +3901,7 @@ public void TestDtStartTimezone(string? tzId) END:VCALENDAR """; - var cal = Calendar.Load(icalText); + var cal = Calendar.Load(icalText)!; var evt = cal.Events.First(); var ev = new EventEvaluator(evt); var occurrences = ev.Evaluate(evt.DtStart!, evt.DtStart!.ToTimeZone(tzId), evt.DtStart.AddMinutes(61).ToTimeZone(tzId), null); diff --git a/Ical.Net.Tests/RecurrenceTests_From_Issues.cs b/Ical.Net.Tests/RecurrenceTests_From_Issues.cs index b38f92c5..774325f9 100644 --- a/Ical.Net.Tests/RecurrenceTests_From_Issues.cs +++ b/Ical.Net.Tests/RecurrenceTests_From_Issues.cs @@ -46,7 +46,7 @@ public void GetOccurrence_DtEnd_ShouldBeExcluded() END:VCALENDAR """; - var calendar = Calendar.Load(ical); + var calendar = Calendar.Load(ical)!; // Event ends on 2024-10-27, at 02:00:00 GMT (when DST ends). The end time is excluded by RFC 5545 definition. var occurrences = calendar.GetOccurrences(endDate, new CalDateTime("20250101T000000", "Europe/London")).ToList(); diff --git a/Ical.Net.Tests/RecurrenceWithExDateTests.cs b/Ical.Net.Tests/RecurrenceWithExDateTests.cs index 92dca0d3..160bfce0 100644 --- a/Ical.Net.Tests/RecurrenceWithExDateTests.cs +++ b/Ical.Net.Tests/RecurrenceWithExDateTests.cs @@ -57,7 +57,7 @@ public void ShouldNotOccurOnLocalExceptionDate(bool useExDateWithTime) var serializer = new CalendarSerializer(); var ics = serializer.SerializeToString(calendar); - var deserializedCalendar = Calendar.Load(ics); + var deserializedCalendar = Calendar.Load(ics)!; var occurrences = deserializedCalendar.GetOccurrences().ToList(); Assert.Multiple(() => @@ -96,13 +96,13 @@ public void ShouldNotOccurOnUtcExceptionDate() END:VCALENDAR """; - var cal = Calendar.Load(ics); + var cal = Calendar.Load(ics)!; var occurrences = cal.GetOccurrences().ToList(); var serializer = new CalendarSerializer(); ics = serializer.SerializeToString(cal); // serialize and deserialize to ensure the exclusion dates de/serialized - cal = Calendar.Load(new CalendarSerializer(cal).SerializeToString()); + cal = Calendar.Load(new CalendarSerializer(cal).SerializeToString())!; // Start date: 2024-10-19 at 18:00 (GMT Standard Time) // Recurrence: Every hour, 4 occurrences @@ -118,7 +118,7 @@ public void ShouldNotOccurOnUtcExceptionDate() Assert.That( occurrences.All( o => !cal - .Events[0] + .Events[0]! .ExceptionDates.GetAllDates() .Any(ex => ex.Equals(o.Period.StartTime))), Is.True); Assert.That(ics, Does.Contain("EXDATE:20241019T190000Z")); @@ -144,13 +144,13 @@ public void MultipleExclusionDatesSameTimeZoneShouldBeExcluded() END:VCALENDAR """; - var cal = Calendar.Load(ics); + var cal = Calendar.Load(ics)!; var occurrences = cal.GetOccurrences().ToList(); var serializer = new CalendarSerializer(); ics = serializer.SerializeToString(cal); // serialize and deserialize to ensure the exclusion dates de/serialized - cal = Calendar.Load(new CalendarSerializer(cal).SerializeToString()); + cal = Calendar.Load(new CalendarSerializer(cal).SerializeToString())!; // Occurrences: // 2023-10-25 09:00 (UTC Offset: +0200) @@ -173,11 +173,11 @@ public void MultipleExclusionDatesSameTimeZoneShouldBeExcluded() Assert.Multiple(() => { Assert.That(occurrences.Count, Is.EqualTo(10)); - Assert.That(cal.Events[0].ExceptionDates.GetAllDates().Count(), Is.EqualTo(3)); + Assert.That(cal.Events[0]!.ExceptionDates.GetAllDates().Count(), Is.EqualTo(3)); Assert.That( occurrences.All( o => !cal - .Events[0] + .Events[0]! .ExceptionDates.GetAllDates() .Any(ex => ex.Equals(o.Period.StartTime))), Is.True); Assert.That(ics, Does.Contain("EXDATE;TZID=Europe/Berlin:20231029T090000,20231105T090000,20231112T090000")); @@ -204,9 +204,9 @@ public void MultipleExclusionDatesDifferentZoneShouldBeExcluded() END:VCALENDAR """; - var cal = Calendar.Load(ics); + var cal = Calendar.Load(ics)!; // serialize and deserialize to ensure the exclusion dates de/serialized - cal = Calendar.Load(new CalendarSerializer(cal).SerializeToString()); + cal = Calendar.Load(new CalendarSerializer(cal).SerializeToString())!; var occurrences = cal.GetOccurrences().ToList(); // Occurrences: @@ -226,11 +226,11 @@ public void MultipleExclusionDatesDifferentZoneShouldBeExcluded() Assert.Multiple(() => { Assert.That(occurrences.Count, Is.EqualTo(9)); - Assert.That(cal.Events[0].ExceptionDates.GetAllDates().Count(), Is.EqualTo(2)); + Assert.That(cal.Events[0]!.ExceptionDates.GetAllDates().Count(), Is.EqualTo(2)); Assert.That( occurrences.All( o => !cal - .Events[0] + .Events[0]! .ExceptionDates.GetAllDates() .Any(ex => ex.Equals(o.Period.StartTime))), Is.True); }); diff --git a/Ical.Net.Tests/RecurrenceWithRDateTests.cs b/Ical.Net.Tests/RecurrenceWithRDateTests.cs index 25785a1d..fe072e98 100644 --- a/Ical.Net.Tests/RecurrenceWithRDateTests.cs +++ b/Ical.Net.Tests/RecurrenceWithRDateTests.cs @@ -158,7 +158,7 @@ public void RDate_PeriodsWithTimezone_AreProcessedCorrectly() // Deserialization - cal = Calendar.Load(ics); + cal = Calendar.Load(ics)!; occurrences = cal.Events.First().GetOccurrences().ToList(); Assert.Multiple(() => diff --git a/Ical.Net/Calendar.cs b/Ical.Net/Calendar.cs index f19d010c..5644fdf2 100644 --- a/Ical.Net/Calendar.cs +++ b/Ical.Net/Calendar.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System; using System.Collections.Generic; using System.IO; @@ -20,7 +21,7 @@ namespace Ical.Net; public class Calendar : CalendarComponent, IGetOccurrencesTyped, IGetFreeBusy, IMergeable { - public static Calendar Load(string iCalendarString) + public static Calendar? Load(string iCalendarString) => CalendarCollection.Load(new StringReader(iCalendarString)).SingleOrDefault(); /// @@ -28,11 +29,11 @@ public static Calendar Load(string iCalendarString) /// /// The stream from which to load the object /// An object - public static Calendar Load(Stream s) + public static Calendar? Load(Stream s) => CalendarCollection.Load(new StreamReader(s, Encoding.UTF8)).SingleOrDefault(); - public static Calendar Load(TextReader tr) - => CalendarCollection.Load(tr)?.SingleOrDefault(); + public static Calendar? Load(TextReader tr) + => CalendarCollection.Load(tr).SingleOrDefault(); public static IList Load(Stream s, Encoding e) => Load(new StreamReader(s, e)); @@ -65,6 +66,14 @@ public Calendar() ProductId = LibraryMetadata.ProdId; Version = LibraryMetadata.Version; + // Initialize the members to make the compiler happy. + _mUniqueComponents = null!; + _mEvents = null!; + _mTodos = null!; + _mJournals = null!; + _mFreeBusy = null!; + _mTimeZones = null!; + Initialize(); } @@ -95,7 +104,7 @@ protected bool Equals(Calendar other) && CollectionHelpers.Equals(FreeBusy, other.FreeBusy) && CollectionHelpers.Equals(TimeZones, other.TimeZones); - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (ReferenceEquals(null, obj)) { @@ -163,7 +172,7 @@ public override int GetHashCode() /// /// The default value does not apply to deserialized objects. /// - public virtual string Version + public virtual string? Version { get => Properties.Get("VERSION"); set => Properties.Set("VERSION", value); @@ -178,19 +187,19 @@ public virtual string Version /// /// The default value does not apply to deserialized objects. /// - public virtual string ProductId + public virtual string? ProductId { get => Properties.Get("PRODID"); set => Properties.Set("PRODID", value); } - public virtual string Scale + public virtual string? Scale { get => Properties.Get("CALSCALE"); set => Properties.Set("CALSCALE", value); } - public virtual string Method + public virtual string? Method { get => Properties.Get("METHOD"); set => Properties.Set("METHOD", value); @@ -214,8 +223,9 @@ public VTimeZone AddTimeZone(VTimeZone tz) /// /// The beginning date/time of the range. /// The end date/time of the range. + /// /// A list of occurrences that fall between the date/time arguments provided. - public virtual IEnumerable GetOccurrences(CalDateTime startTime = null, CalDateTime endTime = null, EvaluationOptions options = default) + public virtual IEnumerable GetOccurrences(CalDateTime? startTime = null, CalDateTime? endTime = null, EvaluationOptions? options = null) => GetOccurrences(startTime, endTime, options); /// @@ -225,12 +235,13 @@ public virtual IEnumerable GetOccurrences(CalDateTime startTime = nu /// /// The starting date range /// The ending date range - public virtual IEnumerable GetOccurrences(CalDateTime startTime = null, CalDateTime endTime = null, EvaluationOptions options = default) where T : IRecurringComponent + /// + public virtual IEnumerable GetOccurrences(CalDateTime? startTime = null, CalDateTime? endTime = null, EvaluationOptions? options = null) where T : IRecurringComponent { // These are the UID/RECURRENCE-ID combinations that replace other occurrences. var recurrenceIdsAndUids = this.Children.OfType() .Where(r => r.RecurrenceId != null) - .Select(r => new { (r as IUniqueComponent)?.Uid, Dt = r.RecurrenceId.Value }) + .Select(r => new { (r as IUniqueComponent).Uid, Dt = r.RecurrenceId!.Value }) .Where(r => r.Uid != null) .ToDictionary(x => x); @@ -283,37 +294,33 @@ public T Create() where T : ICalendarComponent this.AddChild(cal); return (T) cal; } - return default(T); + throw new ArgumentException($"Creating {typeof(T).FullName} failed."); } public virtual void MergeWith(IMergeable obj) { - var c = obj as Calendar; - if (c == null) + if (obj is not Calendar c) { return; } - if (Name == null) - { - Name = c.Name; - } + Name ??= c.Name; Method = c.Method; Version = c.Version; ProductId = c.ProductId; Scale = c.Scale; - foreach (var p in c.Properties.Where(p => !Properties.ContainsKey(p.Name))) + foreach (var p in c.Properties.Where(p => p.Name != null && !Properties.ContainsKey(p.Name))) { Properties.Add(p); } foreach (var child in c.Children) { - if (child is IUniqueComponent) + if (child is IUniqueComponent component) { - if (!UniqueComponents.ContainsKey(((IUniqueComponent) child).Uid)) + if (component.Uid != null && !UniqueComponents.ContainsKey(component.Uid)) { this.AddChild(child); } @@ -325,20 +332,20 @@ public virtual void MergeWith(IMergeable obj) } } - public virtual FreeBusy GetFreeBusy(FreeBusy freeBusyRequest) => CalendarComponents.FreeBusy.Create(this, freeBusyRequest); + public virtual FreeBusy? GetFreeBusy(FreeBusy freeBusyRequest) => CalendarComponents.FreeBusy.Create(this, freeBusyRequest); - public virtual FreeBusy GetFreeBusy(CalDateTime fromInclusive, CalDateTime toExclusive) + public virtual FreeBusy? GetFreeBusy(CalDateTime fromInclusive, CalDateTime toExclusive) => CalendarComponents.FreeBusy.Create(this, CalendarComponents.FreeBusy.CreateRequest(fromInclusive, toExclusive, null, null)); - public virtual FreeBusy GetFreeBusy(Organizer organizer, IEnumerable contacts, CalDateTime fromInclusive, CalDateTime toExclusive) + public virtual FreeBusy? GetFreeBusy(Organizer organizer, IEnumerable contacts, CalDateTime fromInclusive, CalDateTime toExclusive) => CalendarComponents.FreeBusy.Create(this, CalendarComponents.FreeBusy.CreateRequest(fromInclusive, toExclusive, organizer, contacts)); /// - /// Adds a system time zone to the iCalendar. This time zone may + /// Adds a system time zone to the iCalendar. This time zone may /// then be used in date/time objects contained in the /// calendar. /// - /// A System.TimeZoneInfo object to add to the calendar. + /// A object to add to the calendar. /// The time zone added to the calendar. public VTimeZone AddTimeZone(TimeZoneInfo tzi) { diff --git a/Ical.Net/CalendarCollection.cs b/Ical.Net/CalendarCollection.cs index a54e24b9..ed7b1285 100644 --- a/Ical.Net/CalendarCollection.cs +++ b/Ical.Net/CalendarCollection.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System; using System.Collections.Generic; using System.IO; @@ -13,7 +14,6 @@ using Ical.Net.Evaluation; using Ical.Net.Serialization; using Ical.Net.Utility; -using static NodaTime.TimeZones.ZoneEqualityComparer; namespace Ical.Net; @@ -57,38 +57,41 @@ private IEnumerable GetOccurrences(Func GetOccurrences(CalDateTime startTime = null, CalDateTime endTime = null, EvaluationOptions options = default) + public IEnumerable GetOccurrences(CalDateTime? startTime = null, CalDateTime? endTime = null, EvaluationOptions? options = null) => GetOccurrences(iCal => iCal.GetOccurrences(startTime, endTime, options)); - public IEnumerable GetOccurrences(CalDateTime startTime = null, CalDateTime endTime = null, EvaluationOptions options = default) where T : IRecurringComponent + public IEnumerable GetOccurrences(CalDateTime? startTime = null, CalDateTime? endTime = null, EvaluationOptions? options = null) where T : IRecurringComponent => GetOccurrences(iCal => iCal.GetOccurrences(startTime, endTime, options)); - private FreeBusy CombineFreeBusy(FreeBusy main, FreeBusy current) + private FreeBusy CombineFreeBusy(FreeBusy? main, FreeBusy current) { main?.MergeWith(current); return current; } - public FreeBusy GetFreeBusy(FreeBusy freeBusyRequest) + public FreeBusy? GetFreeBusy(FreeBusy freeBusyRequest) { - return this.Aggregate(null, (current, iCal) => CombineFreeBusy(current, iCal.GetFreeBusy(freeBusyRequest))); + return this.Aggregate(null, (current, iCal) => + { + var freeBusy = iCal.GetFreeBusy(freeBusyRequest); + return current is null ? freeBusy : CombineFreeBusy(current, freeBusy); + }); } - public FreeBusy GetFreeBusy(CalDateTime fromInclusive, CalDateTime toExclusive) + public FreeBusy? GetFreeBusy(Organizer organizer, IEnumerable contacts, CalDateTime fromInclusive, CalDateTime toExclusive) { - return this.Aggregate(null, (current, iCal) => CombineFreeBusy(current, iCal.GetFreeBusy(fromInclusive, toExclusive))); - } - - public FreeBusy GetFreeBusy(Organizer organizer, IEnumerable contacts, CalDateTime fromInclusive, CalDateTime toExclusive) - { - return this.Aggregate(null, (current, iCal) => CombineFreeBusy(current, iCal.GetFreeBusy(organizer, contacts, fromInclusive, toExclusive))); + return this.Aggregate(null, (current, iCal) => + { + var freeBusy = iCal.GetFreeBusy(organizer, contacts, fromInclusive, toExclusive); + return current is null ? freeBusy : CombineFreeBusy(current, freeBusy); + }); } public override int GetHashCode() => CollectionHelpers.GetHashCode(this); protected bool Equals(CalendarCollection obj) => CollectionHelpers.Equals(this, obj); - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; diff --git a/Ical.Net/CalendarComponents/FreeBusy.cs b/Ical.Net/CalendarComponents/FreeBusy.cs index 9142edcd..6e02bd05 100644 --- a/Ical.Net/CalendarComponents/FreeBusy.cs +++ b/Ical.Net/CalendarComponents/FreeBusy.cs @@ -65,8 +65,9 @@ public class FreeBusy : UniqueComponent, IMergeable var participatingAttendeeQuery = uc.Attendees .Where(attendee => attendee.Value != null + && attendee.ParticipationStatus != null && contacts.Contains(attendee.Value.OriginalString.Trim())) - .Select(pa => pa.ParticipationStatus.ToUpperInvariant()); + .Select(pa => pa.ParticipationStatus!.ToUpperInvariant()); foreach (var participatingAttendee in participatingAttendeeQuery) { diff --git a/Ical.Net/CalendarExtensions.cs b/Ical.Net/CalendarExtensions.cs index 1da2c3ae..6f936c86 100644 --- a/Ical.Net/CalendarExtensions.cs +++ b/Ical.Net/CalendarExtensions.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System; using System.Globalization; using Ical.Net.DataTypes; diff --git a/Ical.Net/CalendarObject.cs b/Ical.Net/CalendarObject.cs index 0ec2fe0f..fb639a28 100644 --- a/Ical.Net/CalendarObject.cs +++ b/Ical.Net/CalendarObject.cs @@ -20,6 +20,7 @@ public class CalendarObject : CalendarObjectBase, ICalendarObject internal CalendarObject() { + Name = string.Empty; Initialize(); } @@ -61,7 +62,7 @@ public override bool Equals(object? obj) return obj.GetType() == GetType() && Equals((CalendarObject) obj); } - public override int GetHashCode() => Name?.GetHashCode() ?? 0; + public override int GetHashCode() => Name.GetHashCode(); /// public override void CopyFrom(ICopyable c) @@ -82,7 +83,7 @@ public override void CopyFrom(ICopyable c) foreach (var child in obj.Children) { // Add a deep copy of the child instead of the child itself - this.AddChild(child.Copy()); + this.AddChild(child.Copy()!); } } @@ -97,9 +98,9 @@ public override void CopyFrom(ICopyable c) public virtual ICalendarObjectList Children => _children; /// - /// Gets or sets the name of the iCalObject. For iCalendar components, this is the RFC 5545 name of the component. + /// Gets or sets the name of the iCalObject. For iCalendar components, this is the RFC 5545 name of the component. /// - public virtual string? Name { get; set; } + public virtual string Name { get; set; } /// /// Gets the object. @@ -124,7 +125,7 @@ public virtual Calendar? Calendar public virtual int Column { get; set; } - public virtual string? Group + public virtual string Group { get => Name; set => Name = value; diff --git a/Ical.Net/CalendarObjectExtensions.cs b/Ical.Net/CalendarObjectExtensions.cs index 2c7d50ca..acf9670e 100644 --- a/Ical.Net/CalendarObjectExtensions.cs +++ b/Ical.Net/CalendarObjectExtensions.cs @@ -3,17 +3,14 @@ // Licensed under the MIT license. // +#nullable enable namespace Ical.Net; public static class CalendarObjectExtensions { public static void AddChild(this ICalendarObject obj, TItem child) where TItem : ICalendarObject - { - obj.Children.Add(child); - } + => obj.Children.Add(child); public static void RemoveChild(this ICalendarObject obj, TItem child) where TItem : ICalendarObject - { - obj.Children.Remove(child); - } -} \ No newline at end of file + => obj.Children.Remove(child); +} diff --git a/Ical.Net/CalendarObjectList.cs b/Ical.Net/CalendarObjectList.cs index 165cc9b8..d15300e7 100644 --- a/Ical.Net/CalendarObjectList.cs +++ b/Ical.Net/CalendarObjectList.cs @@ -3,7 +3,7 @@ // Licensed under the MIT license. // -using System.Diagnostics.CodeAnalysis; +#nullable enable using Ical.Net.Collections; namespace Ical.Net; @@ -11,7 +11,5 @@ namespace Ical.Net; /// /// A collection of calendar objects. /// -[ExcludeFromCodeCoverage] public class CalendarObjectList : GroupedList, ICalendarObjectList -{ -} \ No newline at end of file +{} diff --git a/Ical.Net/CalendarParameter.cs b/Ical.Net/CalendarParameter.cs index 9cfa9ea4..46189d65 100644 --- a/Ical.Net/CalendarParameter.cs +++ b/Ical.Net/CalendarParameter.cs @@ -16,7 +16,7 @@ namespace Ical.Net; [DebuggerDisplay("{Name}={string.Join(\",\", Values)}")] public class CalendarParameter : CalendarObject, IValueObject { - private HashSet _values = new(); + private HashSet _values = new(); public CalendarParameter() { @@ -34,7 +34,7 @@ public CalendarParameter(string name, string? value) : base(name) AddValue(value); } - public CalendarParameter(string name, IEnumerable values) : base(name) + public CalendarParameter(string name, IEnumerable values) : base(name) { Initialize(); foreach (var v in values) @@ -45,7 +45,7 @@ public CalendarParameter(string name, IEnumerable values) : base(name) private void Initialize() { - _values = new HashSet(StringComparer.OrdinalIgnoreCase); + _values = new HashSet(StringComparer.OrdinalIgnoreCase); } protected override void OnDeserializing(StreamingContext context) @@ -66,22 +66,22 @@ public override void CopyFrom(ICopyable c) return; } - _values = new HashSet(p.Values.Where(IsValidValue), StringComparer.OrdinalIgnoreCase); + _values = new HashSet(p.Values.Where(IsValidValue), StringComparer.OrdinalIgnoreCase); } - public virtual IEnumerable Values => _values; + public virtual IEnumerable Values => _values; public virtual bool ContainsValue(string value) => _values.Contains(value); public virtual int ValueCount => _values.Count; - public virtual void SetValue(string value) + public virtual void SetValue(string? value) { _values.Clear(); _values.Add(value); } - public virtual void SetValue(IEnumerable values) + public virtual void SetValue(IEnumerable values) { // Remove all previous values _values.Clear(); @@ -90,16 +90,16 @@ public virtual void SetValue(IEnumerable values) private static bool IsValidValue(string? value) => !string.IsNullOrWhiteSpace(value); - public virtual void AddValue(string? value) + public virtual bool AddValue(string? value) { if (!IsValidValue(value)) { - return; + return false; } - _values.Add(value!); + return _values.Add(value!); } - public virtual void RemoveValue(string value) => _values.Remove(value); + public virtual bool RemoveValue(string? value) => _values.Remove(value); public virtual string? Value { diff --git a/Ical.Net/CalendarProperty.cs b/Ical.Net/CalendarProperty.cs index 5f939c4f..a9025ef3 100644 --- a/Ical.Net/CalendarProperty.cs +++ b/Ical.Net/CalendarProperty.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -31,7 +32,7 @@ namespace Ical.Net; [DebuggerDisplay("{Name}:{Value}")] public class CalendarProperty : CalendarObject, ICalendarProperty { - private List _values = new List(); + private readonly List _values = new List(); /// /// Returns a list of parameters that are associated with the iCalendar object. @@ -77,36 +78,40 @@ public override void CopyFrom(ICopyable obj) SetValue(p.Values); } - public virtual IEnumerable Values => _values; + public virtual IEnumerable Values => _values; - public object Value + /// + /// Gets or sets the first value in the list of values. + /// Using for will clear all values. + /// + public object? Value { - get => _values?.FirstOrDefault(); + get => _values.FirstOrDefault(); set { if (value == null) { - _values = null; + _values.Clear(); return; } - if (_values != null && _values.Count > 0) + if (_values.Count > 0) { _values[0] = value; } else { - _values?.Clear(); - _values?.Add(value); + // collection is known to be empty here + _values.Add(value); } } } - public virtual bool ContainsValue(object value) => _values.Contains(value); + public virtual bool ContainsValue(object? value) => _values.Contains(value); - public virtual int ValueCount => _values?.Count ?? 0; + public virtual int ValueCount => _values.Count; - public virtual void SetValue(object value) + public virtual void SetValue(object? value) { if (_values.Count == 0) { @@ -114,7 +119,7 @@ public virtual void SetValue(object value) } else if (value != null) { - // Our list contains values. Let's set the first value! + // Our list contains values. Let's set the first value! _values[0] = value; } else @@ -123,32 +128,26 @@ public virtual void SetValue(object value) } } - public virtual void SetValue(IEnumerable values) + /// + /// Sets the value of the property to the specified . + /// Using for will clear all values. + /// + /// + public virtual void SetValue(IEnumerable? values) { // Remove all previous values _values.Clear(); // If the values are ICopyable, create a deep copy of each value, // otherwise just add the value - var toAdd = values?.Select(x => (x as ICopyable)?.Copy() ?? x) ?? Enumerable.Empty(); + var toAdd = values?.Select(x => (x as ICopyable)?.Copy() ?? x) ?? Enumerable.Empty(); _values.AddRange(toAdd); } - public virtual void AddValue(object value) + public virtual bool AddValue(object? value) { - if (value == null) - { - return; - } - _values.Add(value); + return true; } - public virtual void RemoveValue(object value) - { - if (value == null) - { - return; - } - _values.Remove(value); - } + public virtual bool RemoveValue(object? value) => _values.Remove(value); } diff --git a/Ical.Net/CalendarPropertyList.cs b/Ical.Net/CalendarPropertyList.cs index c93cbb84..cc8e4389 100644 --- a/Ical.Net/CalendarPropertyList.cs +++ b/Ical.Net/CalendarPropertyList.cs @@ -3,6 +3,8 @@ // Licensed under the MIT license. // +#nullable enable +using System; using System.Linq; using Ical.Net.Collections; @@ -10,7 +12,7 @@ namespace Ical.Net; public class CalendarPropertyList : GroupedValueList { - private readonly ICalendarObject _mParent; + private readonly ICalendarObject? _mParent; public CalendarPropertyList() { } @@ -20,12 +22,10 @@ public CalendarPropertyList(ICalendarObject parent) ItemAdded += CalendarPropertyList_ItemAdded; } - private void CalendarPropertyList_ItemAdded(object sender, ObjectEventArgs e) - { - e.First.Parent = _mParent; - } + private void CalendarPropertyList_ItemAdded(object? sender, ObjectEventArgs e) + => e.First.Parent = _mParent; - public ICalendarProperty this[string name] => ContainsKey(name) + public ICalendarProperty? this[string name] => ContainsKey(name) ? AllOf(name).FirstOrDefault() : null; -} \ No newline at end of file +} diff --git a/Ical.Net/Collections/GroupedList.cs b/Ical.Net/Collections/GroupedList.cs index e4158b7e..d9c74ad7 100644 --- a/Ical.Net/Collections/GroupedList.cs +++ b/Ical.Net/Collections/GroupedList.cs @@ -22,7 +22,7 @@ public class GroupedList : private readonly List> _lists = new(); private readonly Dictionary> _dictionary = new(); - private IMultiLinkedList? EnsureList(TGroup group) + private IMultiLinkedList EnsureList(TGroup group) { if (_dictionary.TryGetValue(group, out var existingList)) { @@ -62,10 +62,6 @@ public virtual void Add(TItem? item) var group = item.Group; var list = EnsureList(group); - if (list == null) - { - return; - } var index = list.Count; list.Add(item); @@ -184,12 +180,8 @@ public virtual void Insert(int index, TItem? item) public virtual void RemoveAt(int index) { var list = ListForIndex(index, out var relativeIndex); - if (list == null) - { - return; - } - list.RemoveAt(relativeIndex); + list?.RemoveAt(relativeIndex); } /// diff --git a/Ical.Net/Collections/GroupedValueList.cs b/Ical.Net/Collections/GroupedValueList.cs index 3815a7e0..d46890ac 100644 --- a/Ical.Net/Collections/GroupedValueList.cs +++ b/Ical.Net/Collections/GroupedValueList.cs @@ -18,16 +18,14 @@ public class GroupedValueList : where TInterface : class, IGroupedObject, IValueObject where TItem : new() { - public virtual void Set(TGroup group, TValueType value) - { - Set(group, new[] { value }); - } + public virtual void Set(TGroup group, TValueType? value) + => Set(group, new[] { value }); - public virtual void Set(TGroup group, IEnumerable values) + public virtual void Set(TGroup group, IEnumerable values) { if (ContainsKey(group)) { - AllOf(group)?.FirstOrDefault()?.SetValue(values); + AllOf(group).FirstOrDefault()?.SetValue(values); return; } diff --git a/Ical.Net/Collections/Interfaces/IValueObject.cs b/Ical.Net/Collections/Interfaces/IValueObject.cs index 65725626..d4d398e9 100644 --- a/Ical.Net/Collections/Interfaces/IValueObject.cs +++ b/Ical.Net/Collections/Interfaces/IValueObject.cs @@ -10,12 +10,12 @@ namespace Ical.Net.Collections.Interfaces; public interface IValueObject { - IEnumerable? Values { get; } + IEnumerable Values { get; } bool ContainsValue(T value); - void SetValue(T value); - void SetValue(IEnumerable values); - void AddValue(T value); - void RemoveValue(T value); + void SetValue(T? value); + void SetValue(IEnumerable values); + bool AddValue(T? value); + bool RemoveValue(T? value); int ValueCount { get; } } diff --git a/Ical.Net/DataTypes/Attachment.cs b/Ical.Net/DataTypes/Attachment.cs index 22c56734..6cb6ba93 100644 --- a/Ical.Net/DataTypes/Attachment.cs +++ b/Ical.Net/DataTypes/Attachment.cs @@ -23,20 +23,13 @@ public class Attachment : EncodableDataType public virtual byte[]? Data { get; private set; } // private set for CopyFrom private Encoding _valueEncoding = System.Text.Encoding.UTF8; - public virtual Encoding ValueEncoding + public virtual Encoding ValueEncoding //NOSONAR { get => _valueEncoding; - set - { - if (value == null) - { - return; - } - _valueEncoding = value; - } + set => _valueEncoding = value; } - public virtual string FormatType + public virtual string? FormatType { get => Parameters.Get("FMTTYPE"); set => Parameters.Set("FMTTYPE", value); @@ -66,7 +59,7 @@ public Attachment(string value) : this() throw new ArgumentException($"{value} is not a valid ATTACH component"); } - ValueEncoding = a.ValueEncoding; + _valueEncoding = a.ValueEncoding; Data = a.Data; Uri = a.Uri; diff --git a/Ical.Net/DataTypes/Attendee.cs b/Ical.Net/DataTypes/Attendee.cs index f4a1b4a9..1c45f060 100644 --- a/Ical.Net/DataTypes/Attendee.cs +++ b/Ical.Net/DataTypes/Attendee.cs @@ -40,8 +40,10 @@ public virtual Uri? SentBy } private string? _commonName; - /// CN: to show the common or displayable name associated with the calendar address - public virtual string CommonName + /// + /// CN: to show the common or displayable name associated with the calendar address. + /// + public virtual string? CommonName { get { @@ -63,7 +65,9 @@ public virtual string CommonName } private Uri? _directoryEntry; - /// DIR, to indicate the URI that points to the directory information corresponding to the attendee + /// + /// DIR, to indicate the URI that points to the directory information corresponding to the attendee. + /// public virtual Uri? DirectoryEntry { get @@ -89,8 +93,10 @@ public virtual Uri? DirectoryEntry } private string? _type; - /// CUTYPE: the type of calendar user - public virtual string Type + /// + /// CUTYPE: the type of calendar user. + /// + public virtual string? Type { get { @@ -113,7 +119,9 @@ public virtual string Type } private List? _members; - /// MEMBER: the groups the user belongs to + /// + /// MEMBER: the groups the user belongs to. + /// public virtual IList Members { get => _members ?? (_members = new List(Parameters.GetMany("MEMBER"))); @@ -125,8 +133,10 @@ public virtual IList Members } private string? _role; - /// ROLE: the intended role the attendee will have - public virtual string Role + /// + /// ROLE: the intended role the attendee will have. + /// + public virtual string? Role { get { @@ -148,7 +158,7 @@ public virtual string Role } private string? _participationStatus; - public virtual string ParticipationStatus + public virtual string? ParticipationStatus { get { @@ -170,7 +180,9 @@ public virtual string ParticipationStatus } private bool? _rsvp; - /// RSVP, to indicate whether a reply is requested + /// + /// RSVP, to indicate whether a reply is requested. + /// public virtual bool Rsvp { get @@ -197,7 +209,9 @@ public virtual bool Rsvp } private List? _delegatedTo; - /// DELEGATED-TO, to indicate the calendar users that the original request was delegated to + /// + /// DELEGATED-TO, to indicate the calendar users that the original request was delegated to. + /// public virtual IList DelegatedTo { get => _delegatedTo ?? (_delegatedTo = new List(Parameters.GetMany("DELEGATED-TO"))); @@ -209,7 +223,9 @@ public virtual IList DelegatedTo } private List? _delegatedFrom; - /// DELEGATED-FROM, to indicate whom the request was delegated from + /// + /// DELEGATED-FROM, to indicate whom the request was delegated from. + /// public virtual IList DelegatedFrom { get => _delegatedFrom ?? (_delegatedFrom = new List(Parameters.GetMany("DELEGATED-FROM"))); @@ -220,7 +236,9 @@ public virtual IList DelegatedFrom } } - /// Uri associated with the attendee, typically an email address + /// + /// Uri associated with the attendee, typically an email address. + /// public virtual Uri? Value { get; set; } public Attendee() { } @@ -259,17 +277,20 @@ public override void CopyFrom(ICopyable obj) DirectoryEntry = atn.DirectoryEntry; } - protected bool Equals(Attendee other) => Equals(SentBy, other.SentBy) - && string.Equals(CommonName, other.CommonName, StringComparison.OrdinalIgnoreCase) - && Equals(DirectoryEntry, other.DirectoryEntry) - && string.Equals(Type, other.Type, StringComparison.OrdinalIgnoreCase) - && string.Equals(Role, other.Role) - && string.Equals(ParticipationStatus, other.ParticipationStatus, StringComparison.OrdinalIgnoreCase) - && Rsvp == other.Rsvp - && Equals(Value, other.Value) - && Members.SequenceEqual(other.Members) - && DelegatedTo.SequenceEqual(other.DelegatedTo) - && DelegatedFrom.SequenceEqual(other.DelegatedFrom); + protected bool Equals(Attendee other) + => Equals(SentBy, other.SentBy) + && string.Equals(CommonName, other.CommonName, + StringComparison.OrdinalIgnoreCase) + && Equals(DirectoryEntry, other.DirectoryEntry) + && string.Equals(Type, other.Type, StringComparison.OrdinalIgnoreCase) + && string.Equals(Role, other.Role) + && string.Equals(ParticipationStatus, other.ParticipationStatus, + StringComparison.OrdinalIgnoreCase) + && Rsvp == other.Rsvp + && Equals(Value, other.Value) + && Members.SequenceEqual(other.Members) + && DelegatedTo.SequenceEqual(other.DelegatedTo) + && DelegatedFrom.SequenceEqual(other.DelegatedFrom); public override bool Equals(object? obj) { if (ReferenceEquals(null, obj)) return false; diff --git a/Ical.Net/DataTypes/Organizer.cs b/Ical.Net/DataTypes/Organizer.cs index 4273eb65..52137d73 100644 --- a/Ical.Net/DataTypes/Organizer.cs +++ b/Ical.Net/DataTypes/Organizer.cs @@ -41,7 +41,7 @@ public virtual Uri? SentBy } } - public virtual string CommonName + public virtual string? CommonName { get => Parameters.Get("CN"); set => Parameters.Set("CN", value); @@ -51,7 +51,7 @@ public virtual Uri? DirectoryEntry { get { - string dir = Parameters.Get("DIR"); + var dir = Parameters.Get("DIR"); if (!string.IsNullOrWhiteSpace(dir)) { return new Uri(dir); diff --git a/Ical.Net/DefaultTimeZoneResolver.cs b/Ical.Net/DefaultTimeZoneResolver.cs index e78bde3c..ed90b05a 100644 --- a/Ical.Net/DefaultTimeZoneResolver.cs +++ b/Ical.Net/DefaultTimeZoneResolver.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System; using System.Collections.Generic; using System.Linq; @@ -30,7 +31,7 @@ private static readonly Lazy> _windowsMapping /// A BCL, IANA, or serialization time zone identifier /// Processing failed /// The DateTimeZone if found or null otherwise. - public static DateTimeZone GetZone(string tzId) + public static DateTimeZone? GetZone(string? tzId) { var exMsg = $"Unrecognized time zone id {tzId}"; diff --git a/Ical.Net/ICalendarObject.cs b/Ical.Net/ICalendarObject.cs index 9d84e4e3..853ef10b 100644 --- a/Ical.Net/ICalendarObject.cs +++ b/Ical.Net/ICalendarObject.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using Ical.Net.Collections; namespace Ical.Net; @@ -19,7 +20,7 @@ public interface ICalendarObject : IGroupedObject, ILoadable, ICopyable /// /// Returns the parent of this object. /// - ICalendarObject Parent { get; set; } + ICalendarObject? Parent { get; set; } /// /// Returns a collection of children of this object. @@ -30,7 +31,7 @@ public interface ICalendarObject : IGroupedObject, ILoadable, ICopyable /// Returns the iCalendar that this object /// is associated with. /// - Calendar Calendar { get; } + Calendar? Calendar { get; } /// /// Returns the line number where this calendar diff --git a/Ical.Net/ICalendarObjectList.cs b/Ical.Net/ICalendarObjectList.cs index 26f775a5..20e5b634 100644 --- a/Ical.Net/ICalendarObjectList.cs +++ b/Ical.Net/ICalendarObjectList.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using Ical.Net.Collections; namespace Ical.Net; @@ -10,5 +11,5 @@ namespace Ical.Net; public interface ICalendarObjectList : IGroupedCollection where TType : class, ICalendarObject { - TType this[int index] { get; } -} \ No newline at end of file + TType? this[int index] { get; } +} diff --git a/Ical.Net/ICalendarProperty.cs b/Ical.Net/ICalendarProperty.cs index 968962d6..be9556b4 100644 --- a/Ical.Net/ICalendarProperty.cs +++ b/Ical.Net/ICalendarProperty.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using Ical.Net.Collections.Interfaces; using Ical.Net.DataTypes; @@ -10,5 +11,5 @@ namespace Ical.Net; public interface ICalendarProperty : ICalendarParameterCollectionContainer, ICalendarObject, IValueObject { - object Value { get; set; } -} \ No newline at end of file + object? Value { get; set; } +} diff --git a/Ical.Net/ICalendarPropertyListContainer.cs b/Ical.Net/ICalendarPropertyListContainer.cs index cfa4e6f1..6c263a31 100644 --- a/Ical.Net/ICalendarPropertyListContainer.cs +++ b/Ical.Net/ICalendarPropertyListContainer.cs @@ -3,9 +3,10 @@ // Licensed under the MIT license. // +#nullable enable namespace Ical.Net; public interface ICalendarPropertyListContainer : ICalendarObject { CalendarPropertyList Properties { get; } -} \ No newline at end of file +} diff --git a/Ical.Net/ICopyable.cs b/Ical.Net/ICopyable.cs index f93dc366..01e5767e 100644 --- a/Ical.Net/ICopyable.cs +++ b/Ical.Net/ICopyable.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable namespace Ical.Net; public interface ICopyable @@ -21,5 +22,5 @@ public interface ICopyable /// of the object when it is overridden, otherwise is used the implementation of the base class. /// This is necessary when working with mutable reference types. /// - T Copy(); -} \ No newline at end of file + T? Copy(); +} diff --git a/Ical.Net/IGetFreeBusy.cs b/Ical.Net/IGetFreeBusy.cs index 67cae4df..0ed497cc 100644 --- a/Ical.Net/IGetFreeBusy.cs +++ b/Ical.Net/IGetFreeBusy.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System.Collections.Generic; using Ical.Net.CalendarComponents; using Ical.Net.DataTypes; @@ -11,7 +12,7 @@ namespace Ical.Net; public interface IGetFreeBusy { - FreeBusy GetFreeBusy(FreeBusy freeBusyRequest); - FreeBusy GetFreeBusy(CalDateTime fromInclusive, CalDateTime toExclusive); - FreeBusy GetFreeBusy(Organizer organizer, IEnumerable contacts, CalDateTime fromInclusive, CalDateTime toExclusive); -} \ No newline at end of file + FreeBusy? GetFreeBusy(FreeBusy freeBusyRequest); + FreeBusy? GetFreeBusy(CalDateTime fromInclusive, CalDateTime toExclusive); + FreeBusy? GetFreeBusy(Organizer organizer, IEnumerable contacts, CalDateTime fromInclusive, CalDateTime toExclusive); +} diff --git a/Ical.Net/ILoadable.cs b/Ical.Net/ILoadable.cs index da19f509..9c17ecd8 100644 --- a/Ical.Net/ILoadable.cs +++ b/Ical.Net/ILoadable.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System; namespace Ical.Net; diff --git a/Ical.Net/IMergeable.cs b/Ical.Net/IMergeable.cs index 61aab5be..a395a5de 100644 --- a/Ical.Net/IMergeable.cs +++ b/Ical.Net/IMergeable.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable namespace Ical.Net; public interface IMergeable @@ -11,4 +12,4 @@ public interface IMergeable /// Merges this object with another. /// void MergeWith(IMergeable obj); -} \ No newline at end of file +} diff --git a/Ical.Net/IParameterCollection.cs b/Ical.Net/IParameterCollection.cs index f23edc94..c1fc1bc2 100644 --- a/Ical.Net/IParameterCollection.cs +++ b/Ical.Net/IParameterCollection.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System.Collections.Generic; using Ical.Net.Collections; @@ -12,8 +13,8 @@ public interface IParameterCollection : IGroupedList { void SetParent(ICalendarObject parent); void Add(string name, string value); - string Get(string name); + string? Get(string name); IList GetMany(string name); - void Set(string name, string value); - void Set(string name, IEnumerable values); -} \ No newline at end of file + void Set(string name, string? value); + void Set(string name, IEnumerable values); +} diff --git a/Ical.Net/ParameterList.cs b/Ical.Net/ParameterList.cs index b9afb90d..f306640f 100644 --- a/Ical.Net/ParameterList.cs +++ b/Ical.Net/ParameterList.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System.Collections.Generic; using Ical.Net.Collections; @@ -10,7 +11,7 @@ namespace Ical.Net; public class ParameterList : GroupedValueList, IParameterCollection { - public virtual void SetParent(ICalendarObject parent) + public virtual void SetParent(ICalendarObject? parent) { foreach (var parameter in this) { @@ -19,11 +20,9 @@ public virtual void SetParent(ICalendarObject parent) } public virtual void Add(string name, string value) - { - Add(new CalendarParameter(name, value)); - } + => Add(new CalendarParameter(name, value)); - public virtual string Get(string name) => Get(name); + public virtual string? Get(string name) => Get(name); public virtual IList GetMany(string name) => GetMany(name); -} \ No newline at end of file +} diff --git a/Ical.Net/Proxies/CalendarObjectListProxy.cs b/Ical.Net/Proxies/CalendarObjectListProxy.cs index 8218f551..5cf5916d 100644 --- a/Ical.Net/Proxies/CalendarObjectListProxy.cs +++ b/Ical.Net/Proxies/CalendarObjectListProxy.cs @@ -31,5 +31,5 @@ public CalendarObjectListProxy(IGroupedCollection list) /// /// /// - TType ICalendarObjectList.this[int index] => this[index] ?? throw new ArgumentOutOfRangeException(nameof(index)); + TType ICalendarObjectList.this[int index] => this.Skip(index).FirstOrDefault() ?? throw new ArgumentOutOfRangeException(nameof(index)); } diff --git a/Ical.Net/Proxies/ParameterCollectionProxy.cs b/Ical.Net/Proxies/ParameterCollectionProxy.cs index ad06b15e..98dbc2a3 100644 --- a/Ical.Net/Proxies/ParameterCollectionProxy.cs +++ b/Ical.Net/Proxies/ParameterCollectionProxy.cs @@ -39,7 +39,7 @@ public virtual void Add(string name, string value) public virtual IList GetMany(string name) => new GroupedValueListProxy(Parameters, name); - public virtual void Set(string name, string value) + public virtual void Set(string name, string? value) { var parameter = RealObject.FirstOrDefault(o => string.Equals(o.Name, name, StringComparison.Ordinal)); @@ -53,7 +53,7 @@ public virtual void Set(string name, string value) } } - public virtual void Set(string name, IEnumerable values) + public virtual void Set(string name, IEnumerable values) { var parameter = RealObject.FirstOrDefault(o => string.Equals(o.Name, name, StringComparison.Ordinal)); diff --git a/Ical.Net/Serialization/PropertySerializer.cs b/Ical.Net/Serialization/PropertySerializer.cs index d426f6ab..ba56c828 100644 --- a/Ical.Net/Serialization/PropertySerializer.cs +++ b/Ical.Net/Serialization/PropertySerializer.cs @@ -40,7 +40,7 @@ public PropertySerializer(SerializationContext ctx) : base(ctx) { } var result = new StringBuilder(); foreach (var v in prop.Values.Where(value => value != null)) { - SerializeValue(result, prop, v, sf); + SerializeValue(result, prop, v!, sf); } // Pop the object off the serialization context. diff --git a/Ical.Net/ServiceProvider.cs b/Ical.Net/ServiceProvider.cs index 05136327..7a7542ec 100644 --- a/Ical.Net/ServiceProvider.cs +++ b/Ical.Net/ServiceProvider.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System; using System.Collections.Generic; using System.Linq; @@ -14,43 +15,41 @@ public class ServiceProvider private readonly IDictionary _mTypedServices = new Dictionary(); private readonly IDictionary _mNamedServices = new Dictionary(); - public virtual object GetService(Type serviceType) + public virtual object? GetService(Type serviceType) { - object service; - _mTypedServices.TryGetValue(serviceType, out service); + _mTypedServices.TryGetValue(serviceType, out var service); return service; } - public virtual object GetService(string name) + public virtual object? GetService(string name) { - object service; - _mNamedServices.TryGetValue(name, out service); + _mNamedServices.TryGetValue(name, out var service); return service; } - public virtual T GetService() + public virtual T? GetService() { var service = GetService(typeof(T)); - if (service is T) + if (service is T svc) { - return (T) service; + return svc; } return default(T); } - public virtual T GetService(string name) + public virtual T? GetService(string name) { var service = GetService(name); - if (service is T) + if (service is T svc) { - return (T) service; + return svc; } return default(T); } public virtual void SetService(string name, object obj) { - if (!string.IsNullOrEmpty(name) && obj != null) + if (!string.IsNullOrEmpty(name)) { _mNamedServices[name] = obj; } @@ -58,33 +57,26 @@ public virtual void SetService(string name, object obj) public virtual void SetService(object obj) { - if (obj != null) - { - var type = obj.GetType(); - _mTypedServices[type] = obj; + var type = obj.GetType(); + _mTypedServices[type] = obj; - // Get interfaces for the given type - foreach (var iface in type.GetInterfaces()) - { - _mTypedServices[iface] = obj; - } + // Get interfaces for the given type + foreach (var interfaceType in type.GetInterfaces()) + { + _mTypedServices[interfaceType] = obj; } } public virtual void RemoveService(Type type) { - if (type != null) + if (_mTypedServices.ContainsKey(type)) { - if (_mTypedServices.ContainsKey(type)) - { - _mTypedServices.Remove(type); - } + _mTypedServices.Remove(type); + } - // Get interfaces for the given type - foreach (var iface in type.GetInterfaces().Where(iface => _mTypedServices.ContainsKey(iface))) - { - _mTypedServices.Remove(iface); - } + foreach (var interfaceType in type.GetInterfaces().Where(interfaceType => _mTypedServices.ContainsKey(interfaceType))) + { + _mTypedServices.Remove(interfaceType); } } @@ -95,4 +87,4 @@ public virtual void RemoveService(string name) _mNamedServices.Remove(name); } } -} \ No newline at end of file +} diff --git a/Ical.Net/TimeZoneResolvers.cs b/Ical.Net/TimeZoneResolvers.cs index 669e37d2..a64b2383 100644 --- a/Ical.Net/TimeZoneResolvers.cs +++ b/Ical.Net/TimeZoneResolvers.cs @@ -3,6 +3,7 @@ // Licensed under the MIT license. // +#nullable enable using System; using NodaTime; @@ -13,7 +14,8 @@ public static class TimeZoneResolvers /// /// The default time zone resolver. /// - public static Func Default => tzId => DefaultTimeZoneResolver.GetZone(tzId); + public static Func Default + => tzId => DefaultTimeZoneResolver.GetZone(tzId) ?? throw new ArgumentException($"Unrecognized time zone id {tzId}", nameof(tzId)); /// /// Gets or sets a function that returns the NodaTime DateTimeZone for the given TZID. diff --git a/Ical.Net/Utility/DateUtil.cs b/Ical.Net/Utility/DateUtil.cs index 7bf3ab48..984de63f 100644 --- a/Ical.Net/Utility/DateUtil.cs +++ b/Ical.Net/Utility/DateUtil.cs @@ -44,8 +44,7 @@ public static CalDateTime FirstDayOfWeek(CalDateTime dt, DayOfWeek firstDayOfWee /// A time zone identifier /// Unrecognized time zone id public static DateTimeZone GetZone(string tzId) - => (TimeZoneResolvers.TimeZoneResolver ?? throw new InvalidOperationException())(tzId) - ?? throw new ArgumentException($"Unrecognized time zone id {tzId}"); + => TimeZoneResolvers.TimeZoneResolver(tzId) ?? throw new ArgumentException($"Unrecognized time zone id {tzId}", nameof(tzId)); public static ZonedDateTime AddYears(ZonedDateTime zonedDateTime, int years) {