Skip to content

Commit 796018e

Browse files
authored
Enhance CalDateTime and DateTimeSerializer (#638)
* Enhance CalDateTime and DateTimeSerializer * Introduced new methods and test cases in `CalDateTimeTests.cs` to ensure correct behavior of `CalDateTime` properties and methods. * Updated various tests in `DeserializationTests.cs` and `RecurrenceTests.cs` to handle `CalDateTime` without time components and improve clarity. * Refined `IsAllDay` property in `CalendarEvent.cs` and updated methods in `UniqueComponent.cs` and `VTimeZone.cs` to use `CalDateTime` with time components. * Removed outdated comments and improved code formatting. * Enabled nullable reference types and updated `IDateTime` interface. * Added new methods and properties to `CalDateTime.cs` for better date and time management. * Refactored methods in `Period`, `RecurrencePatternEvaluator`, and `RecurringEvaluator` classes to handle `HasTime` property correctly. * Improved `DateTimeSerializer` class for better performance and handling of nullable types. * Added XML documentation and marked obsolete methods. Resolves #630 Resolves #633 Resolves #635 Resolves #636 Resolves #637 * Fix occasual failing unit test Depending on the time offset from local time to UTC the tests failed. Changed all date kinds to UTC. * Make AlmostZeroEpsilon private const * Replace DateTimeKind.Local with DateTimeKind.Unspecified Affected classes: * `CalDateTime` * `DateUtil` * `DateTimeSerializer` * SOC: Move getting "TZID" from `IParameterCollection` to `DateTimeSerializer` `CalDateTime.TzId` does no more read or write `CalDateTime.Parameters` * Use backing fields `DateOnly` and `TimeOnly` to create the `CalDateTime.Value` * Depending on `HasTime`, `Value` contains only the `DateTime.Date`, or the value including `DateTime.TimeOfDay`. * The timezone dermines whether `DateTimeKind.Utc`or `DateTimeKind.Unspecified`will be used. * Remove the redundant `Calendar` argument to CTORs * Implement review comments * `if (Math.Abs(right.TotalDays % 1) > AlmostZeroEpsilon)` is now `if ((right.Ticks % TimeSpan.TicksPerDay) != 0)` * `DateUtil.DateTime GetSimpleDateTimeData(IDateTime dt)` returns `dt.Value` * Move internal `DateUtil.SimpleDateTimeToMatch` as private to `RecurrenceTests` * Move internal `DateUtil.MatchTimeZone` as private to `RecurrencePatternEvaluator` * Create tasks in #646 and #647 for new PRs * Remove xmldoc for non-existing parameter "kind" Also refactor for SonarCloud complaints
1 parent 1e4962e commit 796018e

29 files changed

+1198
-1004
lines changed

.editorconfig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ csharp_prefer_braces = false:suggestion
8585

8686
# Expression-bodied members
8787
csharp_style_expression_bodied_methods = false:suggestion
88-
csharp_style_expression_bodied_properties = true:suggestion
88+
csharp_style_expression_bodied_properties = true
8989
csharp_style_expression_bodied_properties = when_on_single_line:suggestion
90+
csharp_style_expression_bodied_indexers = false:warning
9091
#prefer block bodies for constructors
91-
csharp_style_expression_bodied_constructors = false:suggestion
92+
csharp_style_expression_bodied_constructors = true
9293
#prefer block bodies for accessors
93-
csharp_style_expression_bodied_accessors = false:suggestion
94+
csharp_style_expression_bodied_accessors = true
9495

9596

9697
#Style - expression level options

Ical.Net.Benchmarks/ApplicationWorkflows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// Licensed under the MIT license.
44
//
55

6+
using BenchmarkDotNet.Attributes;
7+
using Ical.Net.DataTypes;
68
using System;
79
using System.Collections.Generic;
810
using System.IO;
911
using System.Linq;
10-
using BenchmarkDotNet.Attributes;
11-
using Ical.Net.DataTypes;
1212

1313
namespace Ical.Net.Benchmarks;
1414

Ical.Net.Benchmarks/CalDateTimePerfTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// Licensed under the MIT license.
44
//
55

6-
using System;
76
using BenchmarkDotNet.Attributes;
87
using Ical.Net.DataTypes;
8+
using System;
99

1010
namespace Ical.Net.Benchmarks;
1111

Ical.Net.Benchmarks/OccurencePerfTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// Licensed under the MIT license.
44
//
55

6-
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
96
using BenchmarkDotNet.Attributes;
107
using Ical.Net.CalendarComponents;
118
using Ical.Net.DataTypes;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
1212

1313
namespace Ical.Net.Benchmarks;
1414

Ical.Net.Benchmarks/Runner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// Licensed under the MIT license.
44
//
55

6-
using System.IO;
76
using BenchmarkDotNet.Configs;
87
using BenchmarkDotNet.Running;
8+
using System.IO;
99

1010
namespace Ical.Net.Benchmarks;
1111

@@ -15,7 +15,7 @@ private static void Main(string[] args)
1515
{
1616
#if DEBUG
1717
BenchmarkSwitcher.FromAssembly(typeof(ApplicationWorkflows).Assembly).Run(args, new DebugInProcessConfig());
18-
#else
18+
#else
1919
#region * ApplicationWorkflows results *
2020
/*
2121
// * Summary *

Ical.Net.Benchmarks/SerializationPerfTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
// Licensed under the MIT license.
44
//
55

6-
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
96
using BenchmarkDotNet.Attributes;
107
using Ical.Net.CalendarComponents;
118
using Ical.Net.DataTypes;
129
using Ical.Net.Serialization;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
1313

1414
namespace Ical.Net.Benchmarks;
1515

Ical.Net.Benchmarks/ThroughputTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// Licensed under the MIT license.
44
//
55

6+
using BenchmarkDotNet.Attributes;
67
using System;
78
using System.Linq;
8-
using BenchmarkDotNet.Attributes;
99

1010
namespace Ical.Net.Benchmarks;
1111

Ical.Net.Tests/AlarmTest.cs

Lines changed: 60 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,94 @@
1-
//
1+
//
22
// Copyright ical.net project maintainers and contributors.
33
// Licensed under the MIT license.
44
//
55

6-
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
6+
using Ical.Net.CalendarComponents;
97
using Ical.Net.DataTypes;
108
using NUnit.Framework;
9+
using System.Collections.Generic;
10+
using System.Linq;
1111

1212
namespace Ical.Net.Tests;
1313

1414
[TestFixture]
15-
public class AlarmTest
15+
public class AttendeeTest
1616
{
17-
private const string _tzid = "US-Eastern";
18-
19-
public void TestAlarm(string calendarString, List<IDateTime> dates, CalDateTime start, CalDateTime end)
17+
internal static CalendarEvent VEventFactory() => new CalendarEvent
2018
{
21-
var iCal = Calendar.Load(calendarString);
22-
ProgramTest.TestCal(iCal);
23-
var evt = iCal.Events.First();
24-
25-
// Poll all alarms that occurred between Start and End
26-
var alarms = evt.PollAlarms(start, end);
19+
Summary = "Testing",
20+
Start = new CalDateTime(2010, 3, 25),
21+
End = new CalDateTime(2010, 3, 26)
22+
};
2723

28-
var utcDates = new HashSet<DateTime>(dates.Select(d => d.AsUtc));
29-
30-
//Only compare the UTC values here, since we care about the time coordinate when the alarm fires, and nothing else
31-
foreach (var alarm in alarms.Select(a => a.DateTime.AsUtc))
32-
{
33-
Assert.That(utcDates.Contains(alarm), Is.True, "Alarm triggers at " + alarm + ", but it should not.");
34-
}
35-
Assert.That(dates.Count == alarms.Count, Is.True, "There were " + alarms.Count + " alarm occurrences; there should have been " + dates.Count + ".");
36-
}
37-
38-
[Test, Category("Alarm")]
39-
public void Alarm1()
24+
private static readonly IList<Attendee> _attendees = new List<Attendee>
4025
{
41-
var dateTimes = new List<IDateTime>();
42-
dateTimes.AddRange(new[]
26+
new Attendee("MAILTO:[email protected]")
4327
{
44-
new CalDateTime(2006, 7, 18, 9, 30, 0, _tzid)
45-
});
46-
47-
var content = IcsFiles.Alarm1;
48-
TestAlarm(content, dateTimes, new CalDateTime(2006, 7, 1, _tzid), new CalDateTime(2006, 9, 1, _tzid));
49-
}
50-
51-
[Test, Category("Alarm")]
52-
public void Alarm2()
53-
{
54-
var dateTimes = new List<IDateTime>();
55-
dateTimes.AddRange(new[]
28+
CommonName = "James James",
29+
Role = ParticipationRole.RequiredParticipant,
30+
Rsvp = true,
31+
ParticipationStatus = EventParticipationStatus.Tentative
32+
},
33+
new Attendee("MAILTO:[email protected]")
5634
{
57-
new CalDateTime(2006, 7, 18, 9, 30, 0, _tzid),
58-
new CalDateTime(2006, 7, 20, 9, 30, 0, _tzid),
59-
new CalDateTime(2006, 7, 22, 9, 30, 0, _tzid),
60-
new CalDateTime(2006, 7, 24, 9, 30, 0, _tzid),
61-
new CalDateTime(2006, 7, 26, 9, 30, 0, _tzid),
62-
new CalDateTime(2006, 7, 28, 9, 30, 0, _tzid),
63-
new CalDateTime(2006, 7, 30, 9, 30, 0, _tzid),
64-
new CalDateTime(2006, 8, 1, 9, 30, 0, _tzid),
65-
new CalDateTime(2006, 8, 3, 9, 30, 0, _tzid),
66-
new CalDateTime(2006, 8, 5, 9, 30, 0, _tzid)
67-
});
35+
CommonName = "Mary Mary",
36+
Role = ParticipationRole.RequiredParticipant,
37+
Rsvp = true,
38+
ParticipationStatus = EventParticipationStatus.Accepted
39+
}
40+
}.AsReadOnly();
6841

69-
var content = IcsFiles.Alarm2;
70-
TestAlarm(content, dateTimes, new CalDateTime(2006, 7, 1, _tzid), new CalDateTime(2006, 9, 1, _tzid));
71-
}
7242

73-
[Test, Category("Alarm")]
74-
public void Alarm3()
43+
/// <summary>
44+
/// Ensures that attendees can be properly added to an event.
45+
/// </summary>
46+
[Test, Category("Attendee")]
47+
public void Add1Attendee()
7548
{
76-
var dateTimes = new List<IDateTime>();
77-
dateTimes.AddRange(new[]
78-
{
79-
new CalDateTime(1998, 2, 11, 9, 0, 0, _tzid),
80-
new CalDateTime(1998, 3, 11, 9, 0, 0, _tzid),
81-
new CalDateTime(1998, 11, 11, 9, 0, 0, _tzid),
82-
new CalDateTime(1999, 8, 11, 9, 0, 0, _tzid),
83-
new CalDateTime(2000, 10, 11, 9, 0, 0, _tzid)
84-
});
49+
var evt = VEventFactory();
50+
Assert.That(evt.Attendees.Count, Is.EqualTo(0));
8551

86-
var content = IcsFiles.Alarm3;
87-
TestAlarm(content, dateTimes, new CalDateTime(1997, 1, 1, _tzid), new CalDateTime(2000, 12, 31, _tzid));
88-
}
52+
evt.Attendees.Add(_attendees[0]);
53+
Assert.That(evt.Attendees, Has.Count.EqualTo(1));
8954

90-
[Test, Category("Alarm")]
91-
public void Alarm4()
92-
{
93-
var dateTimes = new List<IDateTime>();
94-
dateTimes.AddRange(new[]
55+
Assert.Multiple(() =>
9556
{
96-
new CalDateTime(1998, 2, 11, 9, 0, 0, _tzid),
97-
new CalDateTime(1998, 2, 11, 11, 0, 0, _tzid),
98-
new CalDateTime(1998, 2, 11, 13, 0, 0, _tzid),
99-
new CalDateTime(1998, 2, 11, 15, 0, 0, _tzid),
100-
new CalDateTime(1998, 3, 11, 9, 0, 0, _tzid),
101-
new CalDateTime(1998, 3, 11, 11, 0, 0, _tzid),
102-
new CalDateTime(1998, 3, 11, 13, 0, 0, _tzid),
103-
new CalDateTime(1998, 3, 11, 15, 0, 0, _tzid),
104-
new CalDateTime(1998, 11, 11, 9, 0, 0, _tzid),
105-
new CalDateTime(1998, 11, 11, 11, 0, 0, _tzid),
106-
new CalDateTime(1998, 11, 11, 13, 0, 0, _tzid),
107-
new CalDateTime(1998, 11, 11, 15, 0, 0, _tzid),
108-
new CalDateTime(1999, 8, 11, 9, 0, 0, _tzid),
109-
new CalDateTime(1999, 8, 11, 11, 0, 0, _tzid),
110-
new CalDateTime(1999, 8, 11, 13, 0, 0, _tzid),
111-
new CalDateTime(1999, 8, 11, 15, 0, 0, _tzid),
112-
new CalDateTime(2000, 10, 11, 9, 0, 0, _tzid),
113-
new CalDateTime(2000, 10, 11, 11, 0, 0, _tzid),
114-
new CalDateTime(2000, 10, 11, 13, 0, 0, _tzid),
115-
new CalDateTime(2000, 10, 11, 15, 0, 0, _tzid)
57+
//the properties below had been set to null during the Attendees.Add operation in NuGet version 2.1.4
58+
Assert.That(evt.Attendees[0].Role, Is.EqualTo(ParticipationRole.RequiredParticipant));
59+
Assert.That(evt.Attendees[0].ParticipationStatus, Is.EqualTo(EventParticipationStatus.Tentative));
11660
});
117-
118-
var content = IcsFiles.Alarm4;
119-
TestAlarm(content, dateTimes, new CalDateTime(1997, 1, 1, _tzid), new CalDateTime(2000, 12, 31, _tzid));
12061
}
12162

122-
[Test, Category("Alarm")]
123-
public void Alarm5()
63+
[Test, Category("Attendee")]
64+
public void Add2Attendees()
12465
{
125-
var dateTimes = new List<IDateTime>();
126-
dateTimes.AddRange(new[]
127-
{
128-
new CalDateTime(1998, 1, 2, 8, 0, 0, _tzid)
129-
});
66+
var evt = VEventFactory();
67+
Assert.That(evt.Attendees.Count, Is.EqualTo(0));
13068

131-
var content = IcsFiles.Alarm5;
132-
TestAlarm(content, dateTimes, new CalDateTime(1997, 7, 1, _tzid), new CalDateTime(2000, 12, 31, _tzid));
69+
evt.Attendees.Add(_attendees[0]);
70+
evt.Attendees.Add(_attendees[1]);
71+
Assert.That(evt.Attendees, Has.Count.EqualTo(2));
72+
Assert.That(evt.Attendees[1].Role, Is.EqualTo(ParticipationRole.RequiredParticipant));
13373
}
13474

135-
[Test, Category("Alarm")]
136-
public void Alarm6()
75+
/// <summary>
76+
/// Ensures that attendees can be properly removed from an event.
77+
/// </summary>
78+
[Test, Category("Attendee")]
79+
public void Remove1Attendee()
13780
{
138-
var dateTimes = new List<IDateTime>();
139-
dateTimes.AddRange(new[]
140-
{
141-
new CalDateTime(1998, 1, 2, 8, 0, 0, _tzid),
142-
new CalDateTime(1998, 1, 5, 8, 0, 0, _tzid),
143-
new CalDateTime(1998, 1, 8, 8, 0, 0, _tzid),
144-
new CalDateTime(1998, 1, 11, 8, 0, 0, _tzid),
145-
new CalDateTime(1998, 1, 14, 8, 0, 0, _tzid),
146-
new CalDateTime(1998, 1, 17, 8, 0, 0, _tzid)
147-
});
81+
var evt = VEventFactory();
82+
Assert.That(evt.Attendees.Count, Is.EqualTo(0));
14883

149-
var content = IcsFiles.Alarm6;
150-
TestAlarm(content, dateTimes, new CalDateTime(1997, 7, 1, _tzid), new CalDateTime(2000, 12, 31, _tzid));
151-
}
84+
var attendee = _attendees.First();
85+
evt.Attendees.Add(attendee);
86+
Assert.That(evt.Attendees, Has.Count.EqualTo(1));
15287

153-
[Test, Category("Alarm")]
154-
public void Alarm7()
155-
{
156-
var dateTimes = new List<IDateTime>();
157-
dateTimes.AddRange(new[]
158-
{
159-
new CalDateTime(2006, 7, 18, 10, 30, 0, _tzid),
160-
new CalDateTime(2006, 7, 20, 10, 30, 0, _tzid),
161-
new CalDateTime(2006, 7, 22, 10, 30, 0, _tzid),
162-
new CalDateTime(2006, 7, 24, 10, 30, 0, _tzid),
163-
new CalDateTime(2006, 7, 26, 10, 30, 0, _tzid),
164-
new CalDateTime(2006, 7, 28, 10, 30, 0, _tzid),
165-
new CalDateTime(2006, 7, 30, 10, 30, 0, _tzid),
166-
new CalDateTime(2006, 8, 1, 10, 30, 0, _tzid),
167-
new CalDateTime(2006, 8, 3, 10, 30, 0, _tzid),
168-
new CalDateTime(2006, 8, 5, 10, 30, 0, _tzid)
169-
});
88+
evt.Attendees.Remove(attendee);
89+
Assert.That(evt.Attendees.Count, Is.EqualTo(0));
17090

171-
var content = IcsFiles.Alarm7;
172-
TestAlarm(content, dateTimes, new CalDateTime(2006, 7, 1, _tzid), new CalDateTime(2006, 9, 1, _tzid));
91+
evt.Attendees.Remove(_attendees.Last());
92+
Assert.That(evt.Attendees.Count, Is.EqualTo(0));
17393
}
174-
}
94+
}

Ical.Net.Tests/AttendeeTest.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
// Licensed under the MIT license.
44
//
55

6-
using System.Collections.Generic;
7-
using System.Linq;
6+
using Ical.Net;
87
using Ical.Net.CalendarComponents;
98
using Ical.Net.DataTypes;
109
using NUnit.Framework;
11-
12-
namespace Ical.Net.Tests;
10+
using System.Collections.Generic;
11+
using System.Linq;
1312

1413
[TestFixture]
1514
public class AttendeeTest
@@ -91,4 +90,4 @@ public void Remove1Attendee()
9190
evt.Attendees.Remove(_attendees.Last());
9291
Assert.That(evt.Attendees.Count, Is.EqualTo(0));
9392
}
94-
}
93+
}

0 commit comments

Comments
 (0)