Skip to content

Commit

Permalink
REBASE - work on attendee serialization and deserialization. HashCode…
Browse files Browse the repository at this point in the history
…s and Equality working as expected #45
  • Loading branch information
rianjs committed Jul 20, 2016
1 parent 18a0af8 commit 5851fe2
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 78 deletions.
9 changes: 4 additions & 5 deletions ical.NET.UnitTests/AttendeeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ internal static Event VEventFactory()
};
}

private const string _requiredParticipant = "REQ-PARTICIPANT"; //this string may be added to the api in the future
private static readonly IList<Attendee> _attendees = new List<Attendee>
{
new Attendee("MAILTO:[email protected]")
{
CommonName = "James James",
Role = _requiredParticipant,
Role = ParticipationRole.RequiredParticipant,
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Tentative
},
new Attendee("MAILTO:[email protected]")
{
CommonName = "Mary Mary",
Role = _requiredParticipant,
Role = ParticipationRole.RequiredParticipant,
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Accepted
}
Expand All @@ -54,7 +53,7 @@ public void Add1Attendee()
Assert.AreEqual(1, evt.Attendees.Count);

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

Expand All @@ -67,7 +66,7 @@ public void Add2Attendees()
evt.Attendees.Add(_attendees[0]);
evt.Attendees.Add(_attendees[1]);
Assert.AreEqual(2, evt.Attendees.Count);
Assert.AreEqual(_requiredParticipant, evt.Attendees[1].Role);
Assert.AreEqual(ParticipationRole.RequiredParticipant, evt.Attendees[1].Role);

var cal = new Calendar();
cal.Events.Add(evt);
Expand Down
74 changes: 53 additions & 21 deletions ical.NET.UnitTests/EqualityAndHashingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,58 @@ public static IEnumerable<ITestCaseData> VTimeZone_TestCases()
yield return new TestCaseData(first, second);
}

//ToDo: Tests for:
//private IUniqueComponentList<IUniqueComponent> _mUniqueComponents;
//private IUniqueComponentList<IEvent> _mEvents;
//private IUniqueComponentList<ITodo> _mTodos;
//private ICalendarObjectList<IJournal> _mJournals;
//private IUniqueComponentList<IFreeBusy> _mFreeBusy;
//private ICalendarObjectList<ITimeZone> _mTimeZones;

// FreeBusy
//VAlarm
//Journal
//RecurringComponent?
//Todo
//VTimeZone?
//IAttachment
//GeographicLocation
//Organizer
//StatusCode
//Trigger
//UTCOffset -- perhaps try to retire this entirely first
//WeekDay
[Test, TestCaseSource(nameof(Attendees_TestCases))]
public void Attendees_Tests(Attendee actual, Attendee expected)
{
Assert.AreEqual(expected.GetHashCode(), actual.GetHashCode());
Assert.AreEqual(expected, actual);
}

public static IEnumerable<ITestCaseData> Attendees_TestCases()
{
var tentative1 = new Attendee("MAILTO:[email protected]")
{
CommonName = "James Tentative",
Role = ParticipationRole.RequiredParticipant,
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Tentative
};
var tentative2 = new Attendee("MAILTO:[email protected]")
{
CommonName = "James Tentative",
Role = ParticipationRole.RequiredParticipant,
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Tentative
};
yield return new TestCaseData(tentative1, tentative2).SetName("Simple attendee test case");

var complex1 = new Attendee("MAILTO:[email protected]")
{
CommonName = "Mary Accepted",
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Accepted,
SentBy = new Uri("mailto:[email protected]"),
DirectoryEntry = new Uri("ldap://example.com:6666/o=eDABC Industries,c=3DUS??(cn=3DBMary Accepted)"),
Type = "CuType",
Members = new List<string> { "Group A", "Group B"},
Role = ParticipationRole.Chair,
DelegatedTo = new List<string> { "Peon A", "Peon B"},
DelegatedFrom = new List<string> { "Bigwig A", "Bigwig B"}
};
var complex2 = new Attendee("MAILTO:[email protected]")
{
CommonName = "Mary Accepted",
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Accepted,
SentBy = new Uri("mailto:[email protected]"),
DirectoryEntry = new Uri("ldap://example.com:6666/o=eDABC Industries,c=3DUS??(cn=3DBMary Accepted)"),
Type = "CuType",
Members = new List<string> { "Group A", "Group B" },
Role = ParticipationRole.Chair,
DelegatedTo = new List<string> { "Peon A", "Peon B" },
DelegatedFrom = new List<string> { "Bigwig A", "Bigwig B" }
};
yield return new TestCaseData(complex1, complex2).SetName("Complex attendee test");
}
}
}
5 changes: 2 additions & 3 deletions ical.NET.UnitTests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,19 @@ public void EventPropertiesSerialized()
});
}

private const string _requiredParticipant = "REQ-PARTICIPANT"; //this string may be added to the api in the future
private static readonly IList<Attendee> _attendees = new List<Attendee>
{
new Attendee("MAILTO:[email protected]")
{
CommonName = "James James",
Role = _requiredParticipant,
Role = ParticipationRole.RequiredParticipant,
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Tentative
},
new Attendee("MAILTO:[email protected]")
{
CommonName = "Mary Mary",
Role = _requiredParticipant,
Role = ParticipationRole.RequiredParticipant,
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Accepted
}
Expand Down
75 changes: 75 additions & 0 deletions ical.NET.UnitTests/SymmetricSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Ical.Net;
using Ical.Net.DataTypes;
using Ical.Net.Interfaces;
using Ical.Net.Interfaces.DataTypes;
using Ical.Net.Serialization;
using Ical.Net.Serialization.iCalendar.Serializers;
using Ical.Net.Serialization.iCalendar.Serializers.DataTypes;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

namespace ical.Net.UnitTests
{
public class SymmetricSerializationTests
{
private static readonly DateTime _nowTime = DateTime.Now;
private static readonly DateTime _later = _nowTime.AddHours(1);
private static CalendarSerializer GetNewSerializer() => new CalendarSerializer(new SerializationContext());
private static string SerializeToString(Calendar c) => GetNewSerializer().SerializeToString(c);
private static Event GetSimpleEvent() => new Event {DtStart = new CalDateTime(_nowTime), DtEnd = new CalDateTime(_later),Duration = _later - _nowTime};
private static ICalendar UnserializeCalendar(string s) => Calendar.LoadFromStream(new StringReader(s)).Single();

[Test]
public void SimpleEvent_Test()
{
Expand Down Expand Up @@ -66,5 +77,69 @@ public void VTimeZoneSerialization_Test()
Assert.AreEqual(originalCalendar, unserializedCalendar);
Assert.AreEqual(originalCalendar.GetHashCode(), unserializedCalendar.GetHashCode());
}

[Test, TestCaseSource(nameof(AttendeeSerialization_TestCases))]
public void AttendeeSerialization_Test(Attendee attendee)
{
var calendar = new Calendar();
calendar.AddTimeZone(new VTimeZone("America/Los_Angeles"));
var someEvent = GetSimpleEvent();
someEvent.Attendees = new List<IAttendee> {attendee};
calendar.Events.Add(someEvent);

var serialized = SerializeToString(calendar);
var unserialized = UnserializeCalendar(serialized);

Assert.AreEqual(calendar.GetHashCode(), unserialized.GetHashCode());
Assert.IsTrue(calendar.Events.SequenceEqual(unserialized.Events));
Assert.AreEqual(calendar, unserialized);
}

public static IEnumerable<ITestCaseData> AttendeeSerialization_TestCases()
{
//TODO: Fix this. It appears to be non-deterministic. E.g. you can have one of the Delegated* properties uncommented, and it works, but not both
var complex1 = new Attendee("MAILTO:[email protected]")
{
CommonName = "Mary Accepted",
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Accepted,
//SentBy = new Uri("mailto:[email protected]"), //Broken
//DirectoryEntry = new Uri("ldap://example.com:6666/o=eDABC Industries,c=3DUS??(cn=3DBMary Accepted)"), //Broken
//Type = "CuType",
//Members = new List<string> { "Group A", "Group B" },
//Role = ParticipationRole.Chair,
//DelegatedTo = new List<string> { "Peon A", "Peon B" },
//DelegatedFrom = new List<string> { "Bigwig A", "Bigwig B" }
};
yield return new TestCaseData(complex1).SetName("Complex attendee");

var simple = new Attendee("MAILTO:[email protected]")
{
CommonName = "James James",
Role = ParticipationRole.RequiredParticipant,
Rsvp = true,
ParticipationStatus = EventParticipationStatus.Tentative
};
yield return new TestCaseData(simple).SetName("Simple attendee");
}

[Test]
public void Attachment_Test()
{
const string someString = "This is a string.";
var asBytes = Encoding.UTF8.GetBytes(someString);
var attachment = new Attachment { Data = asBytes, ValueEncoding = Encoding.UTF8};

var calendar = new Calendar();
var vEvent = GetSimpleEvent();
vEvent.Attachments = new List<IAttachment> {attachment};
calendar.Events.Add(vEvent);

var serialized = SerializeToString(calendar);
var unserialized = UnserializeCalendar(serialized);

Assert.AreEqual(calendar.GetHashCode(), unserialized.GetHashCode());
Assert.AreEqual(calendar, unserialized);
}
}
}
3 changes: 2 additions & 1 deletion ical.NET/Components/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ protected bool Equals(Event other)
&& Status.Equals(other.Status)
&& IsActive() == other.IsActive()
&& Transparency.Equals(other.Transparency)
&& EvaluationIncludesReferenceDate == other.EvaluationIncludesReferenceDate;
&& EvaluationIncludesReferenceDate == other.EvaluationIncludesReferenceDate
&& Attachments.SequenceEqual(other.Attachments);
}

public override bool Equals(object obj)
Expand Down
6 changes: 4 additions & 2 deletions ical.NET/DataTypes/Attachment.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Linq;
using System.Text;
using Ical.Net.Interfaces.DataTypes;
using Ical.Net.Interfaces.General;
using Ical.Net.Serialization.iCalendar.Serializers.DataTypes;
using Utility;

namespace Ical.Net.DataTypes
{
Expand Down Expand Up @@ -55,7 +57,7 @@ public override void CopyFrom(ICopyable obj) { }

protected bool Equals(Attachment other)
{
return Equals(Uri, other.Uri) && Equals(Data, other.Data) && Equals(ValueEncoding, other.ValueEncoding);
return Equals(Uri, other.Uri) && ValueEncoding.Equals(other.ValueEncoding) && Data.SequenceEqual(other.Data);
}

public override bool Equals(object obj)
Expand All @@ -71,7 +73,7 @@ public override int GetHashCode()
unchecked
{
var hashCode = Uri?.GetHashCode() ?? 0;
hashCode = (hashCode * 397) ^ (Data?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ (CollectionHelpers.GetHashCode(Data));
hashCode = (hashCode * 397) ^ (ValueEncoding?.GetHashCode() ?? 0);
return hashCode;
}
Expand Down
Loading

0 comments on commit 5851fe2

Please sign in to comment.