Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 23 additions & 3 deletions Ical.Net.Tests/SymmetricSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,37 @@ public static IEnumerable UriAttachment_TestCases()
yield return new TestCaseData("\\\\uncPath\\to\\resource.txt", new Uri("\\\\uncPath\\to\\resource.txt")).SetName("UNC path URL");
}

[Test, Ignore("TODO: Fix CATEGORIES multiple serializations")]
public void CategoriesTest()
[TestCase("Foo", "Bar", "Baz")]
[TestCase("Hello", "World", null)]
public void CategoriesTest(string cat1, string cat2, string cat3)
{
var vEvent = GetSimpleEvent();
vEvent.Categories = new List<string> { "Foo", "Bar", "Baz" };
vEvent.Categories = [cat1, cat2, cat3];
var c = new Calendar();
c.Events.Add(vEvent);

var serialized = SerializeToString(c);
var categoriesCount = Regex.Matches(serialized, "CATEGORIES").Count;
Assert.That(categoriesCount, Is.EqualTo(1));
Assert.That(serialized, Does.Contain($"CATEGORIES:{string.Join(",", vEvent.Categories)}"));

var deserialized = UnserializeCalendar(serialized);
Assert.That(deserialized, Is.EqualTo(c));
}

[TestCase("Foo", "Bar", "Baz")]
[TestCase("Hello", "World", null)]
public void ResourceTest(string cat1, string cat2, string cat3)
{
var vEvent = GetSimpleEvent();
vEvent.Resources = [cat1, cat2, cat3];
var c = new Calendar();
c.Events.Add(vEvent);

var serialized = SerializeToString(c);
var categoriesCount = Regex.Matches(serialized, "RESOURCES").Count;
Assert.That(categoriesCount, Is.EqualTo(1));
Assert.That(serialized, Does.Contain($"RESOURCES:{string.Join(",", vEvent.Categories)}"));

var deserialized = UnserializeCalendar(serialized);
Assert.That(deserialized, Is.EqualTo(c));
Expand Down
26 changes: 23 additions & 3 deletions Ical.Net/Serialization/PropertySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,35 @@ public PropertySerializer(SerializationContext ctx) : base(ctx) { }
// the property and parameter values
var sf = GetService<ISerializerFactory>();

// TODO: Exhaust this list with all properties which can be displayed in one line.
var stringBuilder = prop.Name switch
{
"CATEGORIES" or "RESOURCES" => ToOneLine(prop, sf),
_ => ToMultipleLines(prop, sf),
};

// Pop the object off the serialization context.
SerializationContext.Pop();
return stringBuilder.ToString();
}

private StringBuilder ToOneLine(ICalendarProperty prop, ISerializerFactory sf)
{
var result = new StringBuilder();
SerializeValue(result, prop, prop.Values.Where(e => e is not null), sf);

return result;
}

private StringBuilder ToMultipleLines(ICalendarProperty prop, ISerializerFactory sf)
{
var result = new StringBuilder();
foreach (var v in prop.Values.Where(value => value != null))
{
SerializeValue(result, prop, v!, sf);
}

// Pop the object off the serialization context.
SerializationContext.Pop();
return result.ToString();
return result;
}

private void SerializeValue(StringBuilder result, ICalendarProperty prop, object value, ISerializerFactory sf)
Expand Down
Loading