Skip to content

Commit 2375512

Browse files
committed
Update CalDateTimeTests.cs
1 parent d7f3ecb commit 2375512

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

Ical.Net.Tests/CalDateTimeTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,11 @@ public static IEnumerable DateTimeKindOverrideTestCases()
146146
public void ToStringTest()
147147
{
148148
var calDateTime = new CalDateTime(2024, 8, 30, 10, 30, 0, tzId: "Pacific/Auckland");
149-
var expected = "08/30/2024 10:30:00 Pacific/Auckland";
150-
Assert.That(calDateTime.ToString(), Is.EqualTo(expected));
149+
Assert.That(calDateTime.ToString(),
150+
Is.EqualTo("08/30/2024 10:30:00 Pacific/Auckland"));
151151
#pragma warning disable CA1305
152-
Assert.That(calDateTime.ToString("yyyy-MM-dd HH:mm:ss"), Is.EqualTo(expected));
152+
Assert.That(calDateTime.ToString("yyyy-MM-dd HH:mm:ss"),
153+
Is.EqualTo("2024-08-30 10:30:00 Pacific/Auckland"));
153154
#pragma warning restore CA1305
154155
}
155156

Ical.Net.Tests/CalDateTimeZonedTests.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace Ical.Net.Tests;
1414

1515
[TestFixture]
16-
public class CalDateTimeZonedTests
16+
internal class CalDateTimeZonedTests
1717
{
1818
#pragma warning disable NUnit2043 // Use ComparisonConstraint for better assertion messages in case of failure
1919

@@ -229,11 +229,18 @@ public void CompareTo_Throws_WhenNoTimeZoneExists()
229229
{
230230
var zonedDt1 = new CalDateTime(2024, 1, 1).AsZoned();
231231
var zonedDt2 = new CalDateTime(2024, 1, 2).AsZoned();
232-
var zonedDt3 = new CalDateTime(2024, 1, 2, 10, 11, 12).AsZoned();
233232

234233
Assert.Throws<InvalidOperationException>(() => _ = zonedDt1.CompareTo(zonedDt2)); // Both have no zone
235-
Assert.Throws<InvalidOperationException>(() => _ = zonedDt1.CompareTo(zonedDt3)); // Self has no zone
236-
Assert.Throws<InvalidOperationException>(() => _ = zonedDt3.CompareTo(zonedDt1)); // Other has no zone
234+
}
235+
236+
[Test]
237+
public void CompareTo_Throws_WhenOnlyOneHasTimeZone()
238+
{
239+
var withZone = new CalDateTime(2024, 1, 2, 10, 11, 12, "UTC").AsZoned();
240+
var noZone = new CalDateTime(2024, 1, 2, 10, 11, 12).AsZoned();
241+
242+
Assert.Throws<InvalidOperationException>(() => _ = withZone.CompareTo(noZone));
243+
Assert.Throws<InvalidOperationException>(() => _ = noZone.CompareTo(withZone));
237244
}
238245

239246
[Test]
@@ -310,13 +317,27 @@ public static System.Collections.IEnumerable LessThanTestCases
310317
true
311318
).SetName("LessThan_SameTimeZone");
312319

320+
// Timezone: same
321+
yield return new TestCaseData(
322+
new CalDateTime(2025, 1, 2, 12, 0, 0, "UTC").AsZoned(),
323+
new CalDateTime(2025, 1, 1, 12, 0, 0, "UTC").AsZoned(),
324+
false
325+
).SetName("NotLessThan_SameTimeZone");
326+
313327
// Timezone: different
314328
yield return new TestCaseData(
315329
new CalDateTime(2025, 1, 1, 12, 0, 0, "UTC").AsZoned(),
316330
new CalDateTime(2025, 1, 2, 12, 0, 0, "Europe/Vienna").AsZoned(),
317331
true
318332
).SetName("LessThan_DifferentTimeZone");
319333

334+
// Timezone: different
335+
yield return new TestCaseData(
336+
new CalDateTime(2025, 1, 2, 12, 0, 0, "Europe/Vienna").AsZoned(),
337+
new CalDateTime(2025, 1, 1, 12, 0, 0, "UTC").AsZoned(),
338+
false
339+
).SetName("NotLessThan_DifferentTimeZone");
340+
320341
// self is null, other is not
321342
yield return new TestCaseData(
322343
null,
@@ -387,13 +408,27 @@ public static System.Collections.IEnumerable GreaterThanTestCases
387408
true
388409
).SetName("GreaterThan_SameTimeZone");
389410

411+
// Timezone: same
412+
yield return new TestCaseData(
413+
new CalDateTime(2025, 1, 1, 12, 0, 0, "UTC").AsZoned(),
414+
new CalDateTime(2025, 1, 2, 12, 0, 0, "UTC").AsZoned(),
415+
false
416+
).SetName("NotGreaterThan_SameTimeZone");
417+
390418
// Timezone: different
391419
yield return new TestCaseData(
392420
new CalDateTime(2025, 1, 2, 12, 0, 0, "Europe/Vienna").AsZoned(),
393421
new CalDateTime(2025, 1, 1, 12, 0, 0, "UTC").AsZoned(),
394422
true
395423
).SetName("GreaterThan_DifferentTimeZone");
396424

425+
// Timezone: different
426+
yield return new TestCaseData(
427+
new CalDateTime(2025, 1, 1, 12, 0, 0, "UTC").AsZoned(),
428+
new CalDateTime(2025, 1, 2, 12, 0, 0, "Europe/Vienna").AsZoned(),
429+
false
430+
).SetName("NotGreaterThan_DifferentTimeZone");
431+
397432
yield return new TestCaseData(
398433
null,
399434
new CalDateTime(2025, 1, 2, 12, 0, 0, "Europe/Vienna").AsZoned(),

Ical.Net/Evaluation/CalDateTimeEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Ical.Net.Evaluation;
1616
/// and <see cref="CalDateTimeZoned"/> objects, including conversions between timezones,
1717
/// adding durations, and formatting.
1818
/// </summary>
19-
public static class CalDateTimeEvaluator
19+
internal static class CalDateTimeEvaluator
2020
{
2121
private static ZonedDateTime? ResolveZonedDateTime(CalDateTime calDateTime)
2222
{

Ical.Net/Evaluation/CalDateTimeZoned.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Ical.Net.Evaluation;
1515
/// can be evaluated to timezone information.
1616
/// Test for <see cref="HasZone"/> to determine if the timezone information is available.
1717
/// </summary>
18-
public readonly struct CalDateTimeZoned : IEquatable<CalDateTimeZoned?>, IComparable<CalDateTimeZoned>, IFormattable
18+
internal readonly struct CalDateTimeZoned : IEquatable<CalDateTimeZoned?>, IComparable<CalDateTimeZoned>, IFormattable
1919
{
2020
public CalDateTimeZoned(CalDateTime calDateTime, ZonedDateTime? zonedDateTime)
2121
{
@@ -134,7 +134,7 @@ public bool LessThan(CalDateTimeZoned? zoned)
134134
return
135135
(self.IsFloating || other.IsFloating || self.TzId == other.TzId)
136136
? self.Value < other.Value
137-
: Utc < zoned.Value.Utc;
137+
: Utc!.Value < zoned.Value.Utc!.Value; // Both have timezones, so UTC is not null
138138
}
139139

140140
/// <summary>
@@ -152,7 +152,7 @@ public bool LessThanOrEqual(CalDateTimeZoned? zoned)
152152
return
153153
(self.IsFloating || other.IsFloating || self.TzId == other.TzId)
154154
? self.Value <= other.Value
155-
: Utc <= zoned.Value.Utc;
155+
: Utc!.Value <= zoned.Value.Utc!.Value; // Both have timezones, so UTC is not null
156156
}
157157

158158
/// <summary>
@@ -170,7 +170,7 @@ public bool GreaterThan(CalDateTimeZoned? zoned)
170170
return
171171
(self.IsFloating || other.IsFloating || self.TzId == other.TzId)
172172
? self.Value > other.Value
173-
: Utc > zoned.Value.Utc;
173+
: Utc!.Value > zoned.Value.Utc!.Value; // Both have timezones, so UTC is not null
174174
}
175175

176176
/// <summary>
@@ -188,7 +188,7 @@ public bool GreaterThanOrEqual(CalDateTimeZoned? zoned)
188188
return
189189
(self.IsFloating || other.IsFloating || self.TzId == other.TzId)
190190
? self.Value >= other.Value
191-
: Utc >= zoned.Value.Utc;
191+
: Utc!.Value >= zoned.Value.Utc!.Value; // Both have timezones, so UTC is not null
192192
}
193193

194194
#endregion

0 commit comments

Comments
 (0)