Skip to content

Commit

Permalink
IsActive can be a property. Status should be a string, not an enum #318
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian Stockbower committed Nov 8, 2017
1 parent 37d49fb commit a3c3025
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void Event_Tests(CalendarEvent incoming, CalendarEvent expected)
Assert.AreEqual(incoming.DtEnd, expected.DtEnd);
Assert.AreEqual(incoming.Location, expected.Location);
Assert.AreEqual(incoming.Status, expected.Status);
Assert.AreEqual(incoming.IsActive(), expected.IsActive());
Assert.AreEqual(incoming.IsActive, expected.IsActive);
Assert.AreEqual(incoming.Duration, expected.Duration);
Assert.AreEqual(incoming.Transparency, expected.Transparency);
Assert.AreEqual(incoming.GetHashCode(), expected.GetHashCode());
Expand Down
15 changes: 15 additions & 0 deletions net-core/Ical.Net/Ical.Net.UnitTests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Ical.Net.Serialization.iCalendar.Serializers.Other;
using Ical.Net.UnitTests.ExtensionMethods;
using NUnit.Framework;
using Ical.Net.Utility;

namespace Ical.Net.UnitTests
{
Expand All @@ -22,6 +23,7 @@ public class SerializationTests
private static readonly DateTime _later = _nowTime.AddHours(1);
private static CalendarSerializer GetNewSerializer() => new CalendarSerializer();
private static string SerializeToString(Calendar c) => GetNewSerializer().SerializeToString(c);
private static string SerializeToString(CalendarEvent e) => SerializeToString(new Calendar { Events = { e } });
private static CalendarEvent GetSimpleEvent() => new CalendarEvent { DtStart = new CalDateTime(_nowTime), DtEnd = new CalDateTime(_later), Duration = _later - _nowTime };
private static Calendar UnserializeCalendar(string s) => Calendar.LoadFromStream(new StringReader(s)).Single();

Expand Down Expand Up @@ -367,5 +369,18 @@ public void DurationIsStable_Tests()
Assert.AreEqual(originalDuration, e.Duration);
Assert.IsTrue(!serialized.Contains("DURATION"));
}

[Test]
public void EventStatusAllCaps()
{
var e = GetSimpleEvent();
e.Status = EventStatus.Confirmed;
var serialized = SerializeToString(e);
Assert.IsTrue(serialized.Contains(EventStatus.Confirmed, EventStatus.Comparison));

var calendar = UnserializeCalendar(serialized);
var eventStatus = calendar.Events.First().Status;
Assert.IsTrue(string.Equals(EventStatus.Confirmed, eventStatus, EventStatus.Comparison));
}
}
}
39 changes: 20 additions & 19 deletions net-core/Ical.Net/Ical.Net/Components/CalendarEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ public virtual IList<string> Resources
/// <summary>
/// The status of the event.
/// </summary>
public EventStatus Status
public string Status
{
get => Properties.Get<EventStatus>("STATUS");
get => Properties.Get<string>("STATUS");
set => Properties.Set("STATUS", value);
}

Expand Down Expand Up @@ -207,7 +207,7 @@ public CalendarEvent()

private void Initialize()
{
Name = Components.Event;
Name = EventStatus.Name;

_mEvaluator = new EventEvaluator(this);
SetService(_mEvaluator);
Expand Down Expand Up @@ -245,7 +245,7 @@ public virtual bool OccursAt(IDateTime dateTime)
/// as an upcoming or occurred event.
/// </summary>
/// <returns>True if the event has not been cancelled, False otherwise.</returns>
public virtual bool IsActive() => (Status != EventStatus.Cancelled);
public virtual bool IsActive => string.Equals(Status, EventStatus.Cancelled, EventStatus.Comparison);

protected override bool EvaluationIncludesReferenceDate => true;

Expand Down Expand Up @@ -284,19 +284,20 @@ protected bool Equals(CalendarEvent other)
var resourcesSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
resourcesSet.UnionWith(Resources);

var result = Equals(DtStart, other.DtStart)
&& string.Equals(Summary, other.Summary, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Description, other.Description, StringComparison.OrdinalIgnoreCase)
&& Equals(DtEnd, other.DtEnd)
&& string.Equals(Location, other.Location, StringComparison.OrdinalIgnoreCase)
&& resourcesSet.SetEquals(other.Resources)
&& Status.Equals(other.Status)
&& IsActive() == other.IsActive()
&& Transparency.Equals(other.Transparency)
&& EvaluationIncludesReferenceDate == other.EvaluationIncludesReferenceDate
&& Attachments.SequenceEqual(other.Attachments)
&& CollectionHelpers.Equals(ExceptionRules, other.ExceptionRules)
&& CollectionHelpers.Equals(RecurrenceRules, other.RecurrenceRules);
var result =
Equals(DtStart, other.DtStart)
&& string.Equals(Summary, other.Summary, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Description, other.Description, StringComparison.OrdinalIgnoreCase)
&& Equals(DtEnd, other.DtEnd)
&& string.Equals(Location, other.Location, StringComparison.OrdinalIgnoreCase)
&& resourcesSet.SetEquals(other.Resources)
&& string.Equals(Status, other.Status, StringComparison.Ordinal)
&& IsActive == other.IsActive
&& Transparency.Equals(other.Transparency)
&& EvaluationIncludesReferenceDate == other.EvaluationIncludesReferenceDate
&& Attachments.SequenceEqual(other.Attachments)
&& CollectionHelpers.Equals(ExceptionRules, other.ExceptionRules)
&& CollectionHelpers.Equals(RecurrenceRules, other.RecurrenceRules);

if (!result)
{
Expand Down Expand Up @@ -349,8 +350,8 @@ public override int GetHashCode()
hashCode = (hashCode * 397) ^ (Description?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ (DtEnd?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ (Location?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ Status.GetHashCode();
hashCode = (hashCode * 397) ^ IsActive().GetHashCode();
hashCode = (hashCode * 397) ^ Status?.GetHashCode() ?? 0;
hashCode = (hashCode * 397) ^ IsActive.GetHashCode();
hashCode = (hashCode * 397) ^ Transparency.GetHashCode();
hashCode = (hashCode * 397) ^ CollectionHelpers.GetHashCode(Attachments);
hashCode = (hashCode * 397) ^ CollectionHelpers.GetHashCode(Resources);
Expand Down
11 changes: 5 additions & 6 deletions net-core/Ical.Net/Ical.Net/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ public enum TriggerRelation
End
}

public class Components
public static class Components
{
public const string Alarm = "VALARM";
public const string Calendar = "VCALENDAR";
public const string Event = "VEVENT";
public const string Freebusy = "VFREEBUSY";
public const string Todo = "VTODO";
public const string Journal = "VJOURNAL";
Expand Down Expand Up @@ -112,11 +111,11 @@ public class SerializationConstants
/// <summary>
/// Status codes available to an <see cref="Components.Event"/> item
/// </summary>
public enum EventStatus
public static class EventStatus
{
Tentative,
Confirmed,
Cancelled
public const string Tentative = "TENTATIVE";
public const string Confirmed = "CONFIRMED";
public const string Cancelled = "CANCELLED";
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public virtual ICalendarComponent Build(string objectName)
case Components.Alarm:
c = new Alarm();
break;
case Components.Event:
case EventStatus.Name:
c = new CalendarEvent();
break;
case Components.Freebusy:
Expand Down
3 changes: 3 additions & 0 deletions net-core/Ical.Net/Ical.Net/Utility/TextUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@ public static TextReader Normalize(string s, SerializationContext ctx)

/// <summary> Unwraps lines from the RFC 5545 "line folding" technique. </summary>
public static string UnwrapLines(string s) => NewLineMatch.Replace(s, string.Empty);

public static bool Contains(this string haystack, string needle, StringComparison stringComparison)
=> haystack.IndexOf(needle, stringComparison) >= 0;
}
}

0 comments on commit a3c3025

Please sign in to comment.