Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
clean tests
  • Loading branch information
SpocWeb committed Dec 11, 2022
commit f3dbdecea51d445b923c3da9e74cc8277b19bce3
35 changes: 11 additions & 24 deletions src/Ical.Net.CoreUnitTests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public static void CompareComponents(ICalendarComponent cb1, ICalendarComponent
var isMatch = false;
foreach (var p2 in cb2.Properties.AllOf(p1.Name))
{
try
{
Assert.AreEqual(p1, p2, "The properties '" + p1.Name + "' are not equal.");
if (p1.Value is IComparable comparable)
Expand All @@ -67,7 +66,6 @@ public static void CompareComponents(ICalendarComponent cb1, ICalendarComponent
isMatch = true;
break;
}
catch { }
}

Assert.IsTrue(isMatch, "Could not find a matching property - " + p1.Name + ":" + (p1.Value?.ToString() ?? string.Empty));
Expand Down Expand Up @@ -107,15 +105,18 @@ public static void CompareEnumerables(IEnumerable a1, IEnumerable a2, string val
}
}

public static string InspectSerializedSection(string serialized, string sectionName, params string[] elements)
=> InspectSerializedSection(serialized, sectionName, elements.AsEnumerable());

public static string InspectSerializedSection(string serialized, string sectionName, IEnumerable<string> elements)
{
const string notFound = "expected '{0}' not found";
var searchFor = "BEGIN:" + sectionName;
var begin = serialized.IndexOf(searchFor);
var begin = serialized.IndexOf(searchFor, StringComparison.Ordinal);
Assert.AreNotEqual(-1, begin, notFound, searchFor);

searchFor = "END:" + sectionName;
var end = serialized.IndexOf(searchFor, begin);
var end = serialized.IndexOf(searchFor, begin, StringComparison.Ordinal);
Assert.AreNotEqual(-1, end, notFound, searchFor);

var searchRegion = serialized.Substring(begin, end - begin + 1);
Expand Down Expand Up @@ -158,7 +159,7 @@ static Dictionary<string, string> GetValues(string serialized, string name, stri

[Test]
[Category("Serialization")]
//[Ignore("TODO: standard time, for NZ standard time (current example)")]
[Ignore("TODO: standard time, for NZ standard time (current example)")]
public void TimeZoneSerialize()
{
//ToDo: This test is broken as of 2016-07-13
Expand All @@ -183,17 +184,11 @@ public void TimeZoneSerialize()
var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(cal);

var vTimezone = InspectSerializedSection(serializedCalendar, "VTIMEZONE", new[] { "TZID:" + tz.TzId });
var vTimezone = InspectSerializedSection(serializedCalendar, "VTIMEZONE", "TZID:" + tz.TzId);
var o = tzi.BaseUtcOffset.ToString("hhmm", CultureInfo.InvariantCulture);

InspectSerializedSection(vTimezone, "STANDARD", new[] {"TZNAME:" + tzi.StandardName, "TZOFFSETTO:" + o
//"DTSTART:20150402T030000",
//"RRULE:FREQ=YEARLY;BYDAY=1SU;BYHOUR=3;BYMINUTE=0;BYMONTH=4",
//"TZOFFSETFROM:+1300"
});


InspectSerializedSection(vTimezone, "DAYLIGHT", new[] { "TZNAME:" + tzi.DaylightName, "TZOFFSETFROM:" + o });
InspectSerializedSection(vTimezone, "STANDARD", "TZNAME:" + tzi.StandardName, "TZOFFSETTO:" + o);
InspectSerializedSection(vTimezone, "DAYLIGHT", "TZNAME:" + tzi.DaylightName, "TZOFFSETFROM:" + o);
}

[Test, Category("Serialization")]
Expand Down Expand Up @@ -270,15 +265,7 @@ public void EventPropertiesSerialized()
Assert.IsTrue(serializedCalendar.Contains(SerializationConstants.LineBreak + p + SerializationConstants.LineBreak), "expected '" + p + "' not found");
}

InspectSerializedSection(serializedCalendar, "VEVENT",
new[]
{
"CLASS:" + evt.Class, "CREATED:" + CalDateString(evt.Created), "DTSTAMP:" + CalDateString(evt.DtStamp),
"LAST-MODIFIED:" + CalDateString(evt.LastModified), "SEQUENCE:" + evt.Sequence, "UID:" + evt.Uid, "PRIORITY:" + evt.Priority,
"LOCATION:" + evt.Location, "SUMMARY:" + evt.Summary, "DTSTART:" + CalDateString(evt.DtStart), "DTEND:" + CalDateString(evt.DtEnd)
//"TRANSPARENCY:" + TransparencyType.Opaque.ToString().ToUpperInvariant(),
//"STATUS:" + EventStatus.Confirmed.ToString().ToUpperInvariant()
});
InspectSerializedSection(serializedCalendar, "VEVENT", "CLASS:" + evt.Class, "CREATED:" + CalDateString(evt.Created), "DTSTAMP:" + CalDateString(evt.DtStamp), "LAST-MODIFIED:" + CalDateString(evt.LastModified), "SEQUENCE:" + evt.Sequence, "UID:" + evt.Uid, "PRIORITY:" + evt.Priority, "LOCATION:" + evt.Location, "SUMMARY:" + evt.Summary, "DTSTART:" + CalDateString(evt.DtStart), "DTEND:" + CalDateString(evt.DtEnd));
}

static readonly IList<Attendee> _attendees = new List<Attendee>
Expand Down Expand Up @@ -322,7 +309,7 @@ public void AttendeesSerialized()
var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(cal);

var vEvt = InspectSerializedSection(serializedCalendar, "VEVENT", new[] { "ORGANIZER:" + org });
var vEvt = InspectSerializedSection(serializedCalendar, "VEVENT", "ORGANIZER:" + org);

foreach (var a in evt.Attendees)
{
Expand Down
16 changes: 8 additions & 8 deletions src/Ical.Net/Serialization/CalendarSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ public CalendarSerializer(SerializationContext ctx) : base(ctx) {}

protected override IComparer<ICalendarProperty> PropertySorter => new CalendarPropertySorter();

public override string SerializeToString(object obj)
public override string SerializeToString(object? obj)
{
if (obj is Calendar calendar)
if (!(obj is Calendar calendar))
{
// If we're serializing a calendar, we should indicate that we're using ical.net to do the work
calendar.Version = LibraryMetadata.Version;
calendar.ProductId = LibraryMetadata.ProdId;

return base.SerializeToString(calendar);
return base.SerializeToString(obj);
}
// If we're serializing a calendar, we should indicate that we're using ical.net to do the work
calendar.Version = LibraryMetadata.Version;
calendar.ProductId = LibraryMetadata.ProdId;

return base.SerializeToString(calendar);

return base.SerializeToString(obj);
}

public override object Deserialize(TextReader tr) => null;
Expand Down
4 changes: 2 additions & 2 deletions src/Ical.Net/Serialization/ComponentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public ComponentSerializer(SerializationContext ctx) : base(ctx) { }

public override Type TargetType => typeof(CalendarComponent);

public override string? SerializeToString(object? obj) => obj is ICalendarComponent c ? SerializeToString(c) : null;
public override string SerializeToString(object obj) => Serialize((ICalendarComponent)obj);

public string SerializeToString(ICalendarComponent c)
public string Serialize(ICalendarComponent c)
{
var sb = new StringBuilder();
var upperName = c.Name.ToUpperInvariant();
Expand Down