Skip to content

Commit

Permalink
Migrated Ical.Net.Tests project from NUnit3 to NUnit 4 (#613)
Browse files Browse the repository at this point in the history
* Migrated Ical.Net.Tests rpoject from NUnit3 to NUnit 4

* Migrate NUnit 3.14.0 to 4.2.2
* Add NUnit.Analyzers 4.3.0
* Convert Classic Assert to Constraint Model
* Introduce Assert.Multiple to group assertions, ensuring all are evaluated even if some fail.
* Simplify test case source return types to IEnumerable without a type argument
* Remove unused using directives and added necessary ones.

Test logic is left unchanged except for 7 tests in Exception context.
Here all try...catch blocks are replaced with `Throws.` assertions.

* Fix formatting in 2 files
  • Loading branch information
axunonb authored Oct 19, 2024
1 parent 00fd8c3 commit 9070096
Show file tree
Hide file tree
Showing 25 changed files with 1,078 additions and 827 deletions.
4 changes: 2 additions & 2 deletions Ical.Net.Tests/AlarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public void TestAlarm(string calendarString, List<IDateTime> dates, CalDateTime
//Only compare the UTC values here, since we care about the time coordinate when the alarm fires, and nothing else
foreach (var alarm in alarms.Select(a => a.DateTime.AsUtc))
{
Assert.IsTrue(utcDates.Contains(alarm), "Alarm triggers at " + alarm + ", but it should not.");
Assert.That(utcDates.Contains(alarm), Is.True, "Alarm triggers at " + alarm + ", but it should not.");
}
Assert.IsTrue(dates.Count == alarms.Count, "There were " + alarms.Count + " alarm occurrences; there should have been " + dates.Count + ".");
Assert.That(dates.Count == alarms.Count, Is.True, "There were " + alarms.Count + " alarm occurrences; there should have been " + dates.Count + ".");
}

[Test, Category("Alarm")]
Expand Down
27 changes: 15 additions & 12 deletions Ical.Net.Tests/AttendeeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,29 @@ public class AttendeeTest
public void Add1Attendee()
{
var evt = VEventFactory();
Assert.AreEqual(0, evt.Attendees.Count);
Assert.That(evt.Attendees.Count, Is.EqualTo(0));

evt.Attendees.Add(_attendees[0]);
Assert.AreEqual(1, evt.Attendees.Count);
Assert.That(evt.Attendees, Has.Count.EqualTo(1));

//the properties below had been set to null during the Attendees.Add operation in NuGet version 2.1.4
Assert.AreEqual(ParticipationRole.RequiredParticipant, evt.Attendees[0].Role);
Assert.AreEqual(EventParticipationStatus.Tentative, evt.Attendees[0].ParticipationStatus);
Assert.Multiple(() =>
{
//the properties below had been set to null during the Attendees.Add operation in NuGet version 2.1.4
Assert.That(evt.Attendees[0].Role, Is.EqualTo(ParticipationRole.RequiredParticipant));
Assert.That(evt.Attendees[0].ParticipationStatus, Is.EqualTo(EventParticipationStatus.Tentative));
});
}

[Test, Category("Attendee")]
public void Add2Attendees()
{
var evt = VEventFactory();
Assert.AreEqual(0, evt.Attendees.Count);
Assert.That(evt.Attendees.Count, Is.EqualTo(0));

evt.Attendees.Add(_attendees[0]);
evt.Attendees.Add(_attendees[1]);
Assert.AreEqual(2, evt.Attendees.Count);
Assert.AreEqual(ParticipationRole.RequiredParticipant, evt.Attendees[1].Role);
Assert.That(evt.Attendees, Has.Count.EqualTo(2));
Assert.That(evt.Attendees[1].Role, Is.EqualTo(ParticipationRole.RequiredParticipant));
}

/// <summary>
Expand All @@ -71,17 +74,17 @@ public void Add2Attendees()
public void Remove1Attendee()
{
var evt = VEventFactory();
Assert.AreEqual(0, evt.Attendees.Count);
Assert.That(evt.Attendees.Count, Is.EqualTo(0));

var attendee = _attendees.First();
evt.Attendees.Add(attendee);
Assert.AreEqual(1, evt.Attendees.Count);
Assert.That(evt.Attendees, Has.Count.EqualTo(1));

evt.Attendees.Remove(attendee);
Assert.AreEqual(0, evt.Attendees.Count);
Assert.That(evt.Attendees.Count, Is.EqualTo(0));

evt.Attendees.Remove(_attendees.Last());
Assert.AreEqual(0, evt.Attendees.Count);
Assert.That(evt.Attendees.Count, Is.EqualTo(0));
}
}
}
14 changes: 7 additions & 7 deletions Ical.Net.Tests/CalDateTimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Ical.Net.Tests
Expand Down Expand Up @@ -36,10 +36,10 @@ public void ToTimeZoneTests(CalendarEvent calendarEvent, string targetTimeZone)
var convertedStart = calendarEvent.Start.ToTimeZone(targetTimeZone);
var convertedAsUtc = convertedStart.AsUtc;

Assert.AreEqual(startAsUtc, convertedAsUtc);
Assert.That(convertedAsUtc, Is.EqualTo(startAsUtc));
}

public static IEnumerable<ITestCaseData> ToTimeZoneTestCases()
public static IEnumerable ToTimeZoneTestCases()
{
const string bclCst = "Central Standard Time";
const string bclEastern = "Eastern Standard Time";
Expand Down Expand Up @@ -70,7 +70,7 @@ public static IEnumerable<ITestCaseData> ToTimeZoneTestCases()
public DateTimeOffset AsDateTimeOffsetTests(CalDateTime incoming)
=> incoming.AsDateTimeOffset;

public static IEnumerable<ITestCaseData> AsDateTimeOffsetTestCases()
public static IEnumerable AsDateTimeOffsetTestCases()
{
const string nyTzId = "America/New_York";
var summerDate = DateTime.Parse("2018-05-15T11:00");
Expand Down Expand Up @@ -106,18 +106,18 @@ public void TestTzidChanges()

var someDt = new CalDateTime(someTime.DateTime) { TzId = "America/New_York" };
var firstUtc = someDt.AsUtc;
Assert.AreEqual(someTime.UtcDateTime, firstUtc);
Assert.That(firstUtc, Is.EqualTo(someTime.UtcDateTime));

someDt.TzId = "Europe/Berlin";
var berlinUtc = someDt.AsUtc;
Assert.AreNotEqual(firstUtc, berlinUtc);
Assert.That(berlinUtc, Is.Not.EqualTo(firstUtc));
}

[Test, TestCaseSource(nameof(DateTimeKindOverrideTestCases))]
public DateTimeKind DateTimeKindOverrideTests(DateTime dateTime, string tzId)
=> new CalDateTime(dateTime, tzId).Value.Kind;

public static IEnumerable<ITestCaseData> DateTimeKindOverrideTestCases()
public static IEnumerable DateTimeKindOverrideTestCases()
{
const string localTz = "America/New_York";
var localDt = DateTime.SpecifyKind(DateTime.Parse("2018-05-21T11:35:33"), DateTimeKind.Local);
Expand Down
120 changes: 73 additions & 47 deletions Ical.Net.Tests/CalendarEventTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Ical.Net.CalendarComponents;
using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
using Ical.Net.Serialization;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -32,8 +32,8 @@ public void Add1()
};

cal.Events.Add(evt);
Assert.AreEqual(1, cal.Children.Count);
Assert.AreSame(evt, cal.Children[0]);
Assert.That(cal.Children, Has.Count.EqualTo(1));
Assert.That(cal.Children[0], Is.SameAs(evt));
}

/// <summary>
Expand All @@ -52,12 +52,17 @@ public void Remove1()
};

cal.Events.Add(evt);
Assert.AreEqual(1, cal.Children.Count);
Assert.AreSame(evt, cal.Children[0]);

Assert.Multiple(() =>
{
Assert.That(cal.Children, Has.Count.EqualTo(1));
Assert.That(cal.Children[0], Is.SameAs(evt));
});
cal.RemoveChild(evt);
Assert.AreEqual(0, cal.Children.Count);
Assert.AreEqual(0, cal.Events.Count);
Assert.Multiple(() =>
{
Assert.That(cal.Children.Count, Is.EqualTo(0));
Assert.That(cal.Events.Count, Is.EqualTo(0));
});
}

/// <summary>
Expand All @@ -76,12 +81,18 @@ public void Remove2()
};

cal.Events.Add(evt);
Assert.AreEqual(1, cal.Children.Count);
Assert.AreSame(evt, cal.Children[0]);
Assert.Multiple(() =>
{
Assert.That(cal.Children, Has.Count.EqualTo(1));
Assert.That(cal.Children[0], Is.SameAs(evt));
});

cal.Events.Remove(evt);
Assert.AreEqual(0, cal.Children.Count);
Assert.AreEqual(0, cal.Events.Count);
Assert.Multiple(() =>
{
Assert.That(cal.Children.Count, Is.EqualTo(0));
Assert.That(cal.Events.Count, Is.EqualTo(0));
});
}

/// <summary>
Expand All @@ -101,7 +112,7 @@ public void EnsureDTSTAMPisNotNull()
};

cal.Events.Add(evt);
Assert.IsNotNull(evt.DtStamp);
Assert.That(evt.DtStamp, Is.Not.Null);
}

/// <summary>
Expand All @@ -120,7 +131,7 @@ public void EnsureDTSTAMPisOfTypeUTC()
};

cal.Events.Add(evt);
Assert.IsTrue(evt.DtStamp.IsUtc, "DTSTAMP should always be of type UTC.");
Assert.That(evt.DtStamp.IsUtc, Is.True, "DTSTAMP should always be of type UTC.");
}

/// <summary>
Expand All @@ -135,7 +146,7 @@ public bool EnsureAutomaticallySetDTSTAMPisSerializedAsKindUTC(string serialized
return !result.Contains("TZID=") && result.EndsWith("Z");
}

public static IEnumerable<ITestCaseData> EnsureAutomaticallySetDtStampIsSerializedAsUtcKind_TestCases()
public static IEnumerable EnsureAutomaticallySetDtStampIsSerializedAsUtcKind_TestCases()
{
var emptyCalendar = new Calendar();
var evt = new CalendarEvent();
Expand Down Expand Up @@ -210,8 +221,8 @@ public void EventWithExDateShouldNotBeEqualToSameEventWithoutExDate()
var noException = Calendar.Load(icalNoException).Events.First();
var withException = Calendar.Load(icalWithException).Events.First();

Assert.AreNotEqual(noException, withException);
Assert.AreNotEqual(noException.GetHashCode(), withException.GetHashCode());
Assert.That(withException, Is.Not.EqualTo(noException));
Assert.That(withException.GetHashCode(), Is.Not.EqualTo(noException.GetHashCode()));
}

private static CalendarEvent GetSimpleEvent() => new CalendarEvent
Expand All @@ -229,13 +240,13 @@ public void RrulesAreSignificantTests()
testRrule.RecurrenceRules = new List<RecurrencePattern> { rrule };

var simpleEvent = GetSimpleEvent();
Assert.AreNotEqual(simpleEvent, testRrule);
Assert.AreNotEqual(simpleEvent.GetHashCode(), testRrule.GetHashCode());
Assert.That(testRrule, Is.Not.EqualTo(simpleEvent));
Assert.That(testRrule.GetHashCode(), Is.Not.EqualTo(simpleEvent.GetHashCode()));

var testRdate = GetSimpleEvent();
testRdate.RecurrenceDates = new List<PeriodList> { new PeriodList { new Period(new CalDateTime(_now)) } };
Assert.AreNotEqual(simpleEvent, testRdate);
Assert.AreNotEqual(simpleEvent.GetHashCode(), testRdate.GetHashCode());
Assert.That(testRdate, Is.Not.EqualTo(simpleEvent));
Assert.That(testRdate.GetHashCode(), Is.Not.EqualTo(simpleEvent.GetHashCode()));
}

private static List<RecurrencePattern> GetSimpleRecurrenceList()
Expand Down Expand Up @@ -263,13 +274,16 @@ public void EventWithRecurrenceAndExceptionComparison()
var eventA = calendar.Events.First();
var eventB = cal2.Events.First();

Assert.AreEqual(eventA.RecurrenceRules.First(), eventB.RecurrenceRules.First());
Assert.AreEqual(eventA.RecurrenceRules.First().GetHashCode(), eventB.RecurrenceRules.First().GetHashCode());
Assert.AreEqual(eventA.ExceptionDates.First(), eventB.ExceptionDates.First());
Assert.AreEqual(eventA.ExceptionDates.First().GetHashCode(), eventB.ExceptionDates.First().GetHashCode());
Assert.AreEqual(eventA.GetHashCode(), eventB.GetHashCode());
Assert.AreEqual(eventA, eventB);
Assert.AreEqual(calendar, cal2);
Assert.Multiple(() =>
{
Assert.That(eventB.RecurrenceRules.First(), Is.EqualTo(eventA.RecurrenceRules.First()));
Assert.That(eventB.RecurrenceRules.First().GetHashCode(), Is.EqualTo(eventA.RecurrenceRules.First().GetHashCode()));
Assert.That(eventB.ExceptionDates.First(), Is.EqualTo(eventA.ExceptionDates.First()));
Assert.That(eventB.ExceptionDates.First().GetHashCode(), Is.EqualTo(eventA.ExceptionDates.First().GetHashCode()));
Assert.That(eventB.GetHashCode(), Is.EqualTo(eventA.GetHashCode()));
Assert.That(eventB, Is.EqualTo(eventA));
Assert.That(cal2, Is.EqualTo(calendar));
});
}

[Test]
Expand All @@ -288,15 +302,18 @@ public void AddingExdateToEventShouldNotBeEqualToOriginal()
cal1.Events.Add(vEvent);
var serialized = serializer.SerializeToString(cal1);
var deserializedNoExDate = Calendar.Load(serialized);
Assert.AreEqual(cal1, deserializedNoExDate);
Assert.That(deserializedNoExDate, Is.EqualTo(cal1));

vEvent.ExceptionDates = GetExceptionDates();
serialized = serializer.SerializeToString(cal1);
var deserializedWithExDate = Calendar.Load(serialized);

Assert.AreNotEqual(deserializedNoExDate.Events.First(), deserializedWithExDate.Events.First());
Assert.AreNotEqual(deserializedNoExDate.Events.First().GetHashCode(), deserializedWithExDate.Events.First().GetHashCode());
Assert.AreNotEqual(deserializedNoExDate, deserializedWithExDate);
Assert.Multiple(() =>
{
Assert.That(deserializedWithExDate.Events.First(), Is.Not.EqualTo(deserializedNoExDate.Events.First()));
Assert.That(deserializedWithExDate.Events.First().GetHashCode(), Is.Not.EqualTo(deserializedNoExDate.Events.First().GetHashCode()));
Assert.That(deserializedWithExDate, Is.Not.EqualTo(deserializedNoExDate));
});
}

[Test]
Expand All @@ -307,14 +324,17 @@ public void ChangingRrulesShouldNotBeEqualToOriginalEvent()

var eventB = GetSimpleEvent();
eventB.RecurrenceRules = GetSimpleRecurrenceList();
Assert.IsFalse(ReferenceEquals(eventA, eventB));
Assert.AreEqual(eventA, eventB);
Assert.Multiple(() =>
{
Assert.That(ReferenceEquals(eventA, eventB), Is.False);
Assert.That(eventB, Is.EqualTo(eventA));
});

var foreverDailyRule = new RecurrencePattern(FrequencyType.Daily, 1);
eventB.RecurrenceRules = new List<RecurrencePattern> { foreverDailyRule };

Assert.AreNotEqual(eventA, eventB);
Assert.AreNotEqual(eventA.GetHashCode(), eventB.GetHashCode());
Assert.That(eventB, Is.Not.EqualTo(eventA));
Assert.That(eventB.GetHashCode(), Is.Not.EqualTo(eventA.GetHashCode()));
}

[Test]
Expand Down Expand Up @@ -367,10 +387,13 @@ public void EventsDifferingByDtStampAreEqual()
var calendarA = Calendar.Load(eventA);
var calendarB = Calendar.Load(eventB);

Assert.AreEqual(calendarA.Events.First().GetHashCode(), calendarB.Events.First().GetHashCode());
Assert.AreEqual(calendarA.Events.First(), calendarB.Events.First());
Assert.AreEqual(calendarA.GetHashCode(), calendarB.GetHashCode());
Assert.AreEqual(calendarA, calendarB);
Assert.Multiple(() =>
{
Assert.That(calendarB.Events.First().GetHashCode(), Is.EqualTo(calendarA.Events.First().GetHashCode()));
Assert.That(calendarB.Events.First(), Is.EqualTo(calendarA.Events.First()));
Assert.That(calendarB.GetHashCode(), Is.EqualTo(calendarA.GetHashCode()));
Assert.That(calendarB, Is.EqualTo(calendarA));
});
}

[Test]
Expand All @@ -380,16 +403,19 @@ public void EventResourcesCanBeZeroedOut()
var resources = new[] { "Foo", "Bar", "Baz" };

e.Resources = new List<string>(resources);
CollectionAssert.AreEquivalent(e.Resources, resources);
Assert.That(resources, Is.EquivalentTo(e.Resources));

var newResources = new[] { "Hello", "Goodbye" };
e.Resources = new List<string>(newResources);
CollectionAssert.AreEquivalent(e.Resources, newResources);
Assert.IsFalse(e.Resources.Any(r => resources.Contains(r)));
Assert.Multiple(() =>
{
Assert.That(newResources, Is.EquivalentTo(e.Resources));
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
Assert.AreEqual(0, e.Resources?.Count);
Assert.That(e.Resources?.Count, Is.EqualTo(0));
}

[Test]
Expand Down Expand Up @@ -429,14 +455,14 @@ public void HourMinuteSecondOffsetParsingTest()
.Skip(1).Take(1).First()
.Properties.First().Value as UtcOffset;
var expectedPositive = TimeSpan.FromMinutes(17.5);
Assert.AreEqual(expectedPositive, positiveOffset?.Offset);
Assert.That(positiveOffset?.Offset, Is.EqualTo(expectedPositive));

var negativeOffset = timezones
.First()
.Properties.First().Value as UtcOffset;

var expectedNegative = TimeSpan.FromMinutes(-17.5);
Assert.AreEqual(expectedNegative, negativeOffset?.Offset);
Assert.That(negativeOffset?.Offset, Is.EqualTo(expectedNegative));
}
}
}
4 changes: 2 additions & 2 deletions Ical.Net.Tests/CalendarPropertiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void AddPropertyShouldNotIncludePropertyNameInValue()

var lines = result.Split(new [] { SerializationConstants.LineBreak }, StringSplitOptions.None);
var propLine = lines.FirstOrDefault(x => x.StartsWith("X-WR-CALNAME:"));
Assert.AreEqual($"{propName}:{propValue}", propLine);
Assert.That(propLine, Is.EqualTo($"{propName}:{propValue}"));
}

[Test]
Expand All @@ -47,7 +47,7 @@ public void PropertySerialization_Tests()
calendar.Events.Add(@event);

var serialized = new CalendarSerializer().SerializeToString(calendar);
Assert.IsTrue(serialized.Contains("X-ALT-DESC;"));
Assert.That(serialized.Contains("X-ALT-DESC;"), Is.True);
}

[Test]
Expand Down
Loading

0 comments on commit 9070096

Please sign in to comment.