Skip to content

Commit

Permalink
chore: Refactor unit tests (YAXLib#252)
Browse files Browse the repository at this point in the history
* Refactor unit tests
* Call independent Assert statements from inside an Assert.Multiple
* Use Assert.That(<collection>, Has.Count.EqualTo(<value>)
* Use Is.EqualTo constraint instead of direct comparison
* Define a constant instead of using literal multiple times
  • Loading branch information
axunonb authored Jul 13, 2024
1 parent 5cb9e90 commit 480d123
Show file tree
Hide file tree
Showing 25 changed files with 905 additions and 517 deletions.
22 changes: 14 additions & 8 deletions YAXLib/Deserialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ private bool TrySetValueForString(object obj, string? elemValue, XElement? xElem
// There's nothing to do for 'TextEmbedding.CData', it's deserialized transparently
if (member.TextEmbedding == TextEmbedding.Base64 && !string.IsNullOrEmpty(elemValue))
{
member.SetValue(obj, elemValue?.Trim().FromBase64(Encoding.UTF8));
member.SetValue(obj, elemValue!.Trim().FromBase64(Encoding.UTF8));
return true;
}

Expand Down Expand Up @@ -808,7 +808,7 @@ private void GetRealTypeIfSpecified(XElement? xElementValue, bool isRealTypeAttr
if (realType != null) memberType = realType;
}

private IList GetCollectionItemList(Type collectionItemType)
private static IList GetCollectionItemList(Type collectionItemType)
{
if (collectionItemType == typeof(object))
{
Expand Down Expand Up @@ -838,7 +838,7 @@ private IList GetCollectionItemList(Type collectionItemType)

var collItemType = ReflectionUtils.GetCollectionItemType(collType);

IList dataItems = GetCollectionItemList(collItemType); // this will hold the actual data items
var dataItems = GetCollectionItemList(collItemType); // this will hold the actual data items
var isPrimitive = ReflectionUtils.IsBasicType(collItemType);
if (isPrimitive && collAttrInstance is
{ SerializationType: YAXCollectionSerializationTypes.Serially })
Expand Down Expand Up @@ -877,7 +877,7 @@ private IList GetCollectionItemList(Type collectionItemType)
return null;
}

private bool TryDataItemListDirect(Type collType, IList dataItems, out object? result)
private static bool TryDataItemListDirect(Type collType, IList dataItems, out object? result)
{
if (collType.IsAssignableFrom(dataItems.GetType()))
{
Expand Down Expand Up @@ -983,15 +983,18 @@ private bool TryGetCollectionAsNonGenericDictionary(XElement xElement, Type coll
object? containerObj,
IList dataItems, out object? nonGenericDictionary)
{
const string keyPropName = "Key";
const string valuePropName = "Value";

nonGenericDictionary = containerObj;

if (!ReflectionUtils.IsNonGenericIDictionary(collType)) return false;

foreach (var lstItem in dataItems)
{
var key = lstItem?.GetType().GetProperty("Key", BindingFlags.Instance | BindingFlags.Public)
var key = lstItem?.GetType().GetProperty(keyPropName, BindingFlags.Instance | BindingFlags.Public)
?.GetValue(lstItem, null);
var value = lstItem?.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public)
var value = lstItem?.GetType().GetProperty(valuePropName, BindingFlags.Instance | BindingFlags.Public)
?.GetValue(lstItem, null);

try
Expand All @@ -1013,6 +1016,9 @@ private bool TryGetCollectionAsNonGenericDictionary(XElement xElement, Type coll
private bool TryGetCollectionAsDictionary(XElement xElement, Type collType, Type collItemType, XName memberAlias,
object? containerObj, IList dataItems, out object? dictionary)
{
const string keyPropName = "Key";
const string valuePropName = "Value";

dictionary = null;

if (!ReflectionUtils.IsIDictionary(collType, out _, out _)) return false;
Expand All @@ -1022,8 +1028,8 @@ private bool TryGetCollectionAsDictionary(XElement xElement, Type collType, Type

foreach (var dataItem in dataItems)
{
var key = collItemType.GetProperty("Key")?.GetValue(dataItem, null);
var value = collItemType.GetProperty("Value")?.GetValue(dataItem, null);
var key = collItemType.GetProperty(keyPropName)?.GetValue(dataItem, null);
var value = collItemType.GetProperty(valuePropName)?.GetValue(dataItem, null);
try
{
collType.InvokeMember("Add", BindingFlags.InvokeMethod, null, dict, new[] { key, value });
Expand Down
29 changes: 19 additions & 10 deletions YAXLibTests/Caching/MemberWrapperCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ public void SerializingShouldFillTheCache()
var s = new YAXSerializer<Book>();
_ = s.Serialize(Book.GetSampleInstance());

Assert.That(countAfterClear, Is.EqualTo(0));
Assert.That(MemberWrapperCache.Instance.CacheDictionary.Count, Is.GreaterThan(0));
Assert.That(MemberWrapperCache.Instance.CacheDictionary[(typeof(Book), s.Options)].Count, Is.GreaterThan(0));
Assert.Multiple(() =>
{
Assert.That(countAfterClear, Is.EqualTo(0));
Assert.That(MemberWrapperCache.Instance.CacheDictionary, Is.Not.Empty);
Assert.That(MemberWrapperCache.Instance.CacheDictionary[(typeof(Book), s.Options)], Is.Not.Empty);
});
}

[Test]
Expand All @@ -38,9 +41,12 @@ public void DeserializingShouldFillTheCache()
s = new YAXSerializer<Book>();
_ = s.Deserialize(xml);

Assert.That(countAfterClear, Is.EqualTo(0));
Assert.That(MemberWrapperCache.Instance.CacheDictionary.Count, Is.GreaterThan(0));
Assert.That(MemberWrapperCache.Instance.CacheDictionary[(typeof(Book), s.Options)].Count, Is.GreaterThan(0));
Assert.Multiple(() =>
{
Assert.That(countAfterClear, Is.EqualTo(0));
Assert.That(MemberWrapperCache.Instance.CacheDictionary, Is.Not.Empty);
Assert.That(MemberWrapperCache.Instance.CacheDictionary[(typeof(Book), s.Options)], Is.Not.Empty);
});
}

[Test]
Expand All @@ -66,10 +72,13 @@ public void CacheCannotExceedMaximumSize()
MemberWrapperCache.Instance.Add((typeof(ulong), so), new List<MemberWrapper>());
MemberWrapperCache.Instance.Add((typeof(char), so), new List<MemberWrapper>());

Assert.That(dupeAdded, Is.False);
Assert.That(MemberWrapperCache.Instance.CacheDictionary.Count, Is.EqualTo(5));
Assert.That(MemberWrapperCache.Instance.CacheDictionary.ContainsKey((typeof(string), so)), Is.False); // FIFO

Assert.Multiple(() =>
{
Assert.That(dupeAdded, Is.False);
Assert.That(MemberWrapperCache.Instance.CacheDictionary, Has.Count.EqualTo(5));
Assert.That(MemberWrapperCache.Instance.CacheDictionary.ContainsKey((typeof(string), so)), Is.False); // FIFO
});

MemberWrapperCache.Instance.MaxCacheSize = MemberWrapperCache.DefaultCacheSize;
}
}
16 changes: 11 additions & 5 deletions YAXLibTests/Caching/UdtWrapperCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ public void SerializingShouldFillTheCache()
var s = new YAXSerializer<Book>();
_ = s.Serialize(Book.GetSampleInstance());

Assert.That(countAfterClear, Is.EqualTo(0));
Assert.That(UdtWrapperCache.Instance.CacheDictionary, Contains.Key((typeof(Book), s.Options)));
Assert.Multiple(() =>
{
Assert.That(countAfterClear, Is.EqualTo(0));
Assert.That(UdtWrapperCache.Instance.CacheDictionary, Contains.Key((typeof(Book), s.Options)));
});
}

[Test]
Expand All @@ -36,8 +39,11 @@ public void DeserializingShouldFillTheCache()
s = new YAXSerializer<Book>();
_ = s.Deserialize(xml);

Assert.That(countAfterClear, Is.EqualTo(0));
Assert.That(UdtWrapperCache.Instance.CacheDictionary, Contains.Key((typeof(Book), s.Options)));
Assert.Multiple(() =>
{
Assert.That(countAfterClear, Is.EqualTo(0));
Assert.That(UdtWrapperCache.Instance.CacheDictionary, Contains.Key((typeof(Book), s.Options)));
});
}

[Test]
Expand All @@ -64,7 +70,7 @@ public void CacheCannotExceedMaximumSize()
UdtWrapperCache.Instance.GetOrAddItem(typeof(ulong), serializerOptions);
UdtWrapperCache.Instance.GetOrAddItem(typeof(char), serializerOptions);

Assert.That(UdtWrapperCache.Instance.CacheDictionary.Count, Is.EqualTo(5));
Assert.That(UdtWrapperCache.Instance.CacheDictionary, Has.Count.EqualTo(5));
Assert.That(UdtWrapperCache.Instance.CacheDictionary.ContainsKey((typeof(string), serializerOptions)), Is.False); // FIFO

UdtWrapperCache.Instance.MaxCacheSize = UdtWrapperCache.DefaultCacheSize;
Expand Down
101 changes: 67 additions & 34 deletions YAXLibTests/CustomSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,20 @@ public void Custom_Class_Level_Serializer_Element()
var xml = s.Serialize(original);
var deserialized = (ClassLevelSampleAsElement?) s.Deserialize(xml);

// " CUSTOM" is added by the ClassLevelSerializer
Assert.That(xml, Is.EqualTo("""
Assert.Multiple(() =>
{
// " CUSTOM" is added by the ClassLevelSerializer
Assert.That(xml, Is.EqualTo("""
<ClassLevelSampleAsElement>
<ClassLevelSample>
<Title>The Title CUSTOM</Title>
<MessageBody>The Message CUSTOM</MessageBody>
</ClassLevelSample>
</ClassLevelSampleAsElement>
"""));
// " CUSTOM" is removed by the ClassLevelSerializer
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
// " CUSTOM" is removed by the ClassLevelSerializer
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
});
}

[Test]
Expand All @@ -66,9 +69,12 @@ public void Custom_Class_Level_Serializer_Attribute()
var xml = s.Serialize(original);
var deserialized = (ClassLevelSampleAsAttribute?) s.Deserialize(xml);

Assert.That(xml,
Is.EqualTo("<ClassLevelSampleAsAttribute ClassLevelSample=\"ATTR|The Title|The Message\" />"));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
Assert.Multiple(() =>
{
Assert.That(xml,
Is.EqualTo("<ClassLevelSampleAsAttribute ClassLevelSample=\"ATTR|The Title|The Message\" />"));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
});
}

[Test]
Expand All @@ -82,9 +88,12 @@ public void Custom_Class_Level_Serializer_Value()
var xml = s.Serialize(original);
var deserialized = (ClassLevelSampleAsValue?) s.Deserialize(xml);

Assert.That(xml,
Is.EqualTo("<ClassLevelSampleAsValue>VAL|The Title|The Message</ClassLevelSampleAsValue>"));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
Assert.Multiple(() =>
{
Assert.That(xml,
Is.EqualTo("<ClassLevelSampleAsValue>VAL|The Title|The Message</ClassLevelSampleAsValue>"));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
});
}

[Test]
Expand All @@ -98,17 +107,20 @@ public void Custom_Class_Level_Ctx_Serializer_Element()
var xml = s.Serialize(original);
var deserialized = (ClassLevelCtxSampleAsElement?) s.Deserialize(xml);

// " CUSTOM" is added by the ClassLevelSerializer
Assert.That(xml, Is.EqualTo("""
Assert.Multiple(() =>
{
// " CUSTOM" is added by the ClassLevelSerializer
Assert.That(xml, Is.EqualTo("""
<ClassLevelCtxSampleAsElement>
<ClassLevelCtxSample>
<Title>The Title CUSTOM</Title>
<MessageBody>The Message CUSTOM</MessageBody>
</ClassLevelCtxSample>
</ClassLevelCtxSampleAsElement>
"""));
// " CUSTOM" is removed by the ClassLevelSerializer
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
// " CUSTOM" is removed by the ClassLevelSerializer
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
});
}

[Test]
Expand All @@ -120,9 +132,12 @@ public void Custom_Class_Level_Ctx_Serializer_Attribute()
var xml = s.Serialize(original);
var deserialized = (ClassLevelCtxSampleAsAttribute?) s.Deserialize(xml);

Assert.That(xml,
Is.EqualTo("<ClassLevelCtxSampleAsAttribute ClassLevelCtxSample=\"ATTR|The Title|The Message\" />"));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
Assert.Multiple(() =>
{
Assert.That(xml,
Is.EqualTo("<ClassLevelCtxSampleAsAttribute ClassLevelCtxSample=\"ATTR|The Title|The Message\" />"));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
});
}

[Test]
Expand All @@ -134,9 +149,12 @@ public void Custom_Class_Level_Ctx_Serializer_Value()
var xml = s.Serialize(original);
var deserialized = (ClassLevelCtxSampleAsValue?) s.Deserialize(xml);

Assert.That(xml,
Is.EqualTo("<ClassLevelCtxSampleAsValue>VAL|The Title|The Message</ClassLevelCtxSampleAsValue>"));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
Assert.Multiple(() =>
{
Assert.That(xml,
Is.EqualTo("<ClassLevelCtxSampleAsValue>VAL|The Title|The Message</ClassLevelCtxSampleAsValue>"));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
});
}

[Test]
Expand All @@ -161,8 +179,11 @@ public void Custom_Property_Serializer()
</PropertyLevelSample>
""";

Assert.That(xml, Is.EqualTo(expectedXml));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
Assert.Multiple(() =>
{
Assert.That(xml, Is.EqualTo(expectedXml));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
});
}

[Test]
Expand All @@ -188,8 +209,11 @@ public void Custom_Field_Serializer()
</FieldLevelSample>
""";

Assert.That(xml, Is.EqualTo(expectedXml));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
Assert.Multiple(() =>
{
Assert.That(xml, Is.EqualTo(expectedXml));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
});
}

[Test]
Expand All @@ -212,8 +236,11 @@ public void Custom_Field_Serializer_Combined()
<Title>ELE__This is the title</Title>VAL__Just a short message body</FieldLevelCombinedSample>
""";

Assert.That(xml, Is.EqualTo(expectedXml));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
Assert.Multiple(() =>
{
Assert.That(xml, Is.EqualTo(expectedXml));
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()));
});
}

[Test]
Expand All @@ -238,10 +265,13 @@ public void Custom_NonGenericClass_Interface_Serializer()
</ISampleInterface>
""";

// Note: Prefix "C_" is evidence for custom serializer was invoked
// during serialization and deserialization
Assert.That(xml, Does.Contain(expectedXmlPart), "Serialized XML");
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()), "Deserialized Object");
Assert.Multiple(() =>
{
// Note: Prefix "C_" is evidence for custom serializer was invoked
// during serialization and deserialization
Assert.That(xml, Does.Contain(expectedXmlPart), "Serialized XML");
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()), "Deserialized Object");
});
}

[Test]
Expand Down Expand Up @@ -272,9 +302,12 @@ public void Custom_GenericClass_Interface_Serializer()
</ISampleInterface>
""";

// Note: Prefix "C_" is evidence for custom serializer was invoked
// during serialization and deserialization
Assert.That(xml, Does.Contain(expectedXmlPart), "Serialized XML");
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()), "Deserialized Object");
Assert.Multiple(() =>
{
// Note: Prefix "C_" is evidence for custom serializer was invoked
// during serialization and deserialization
Assert.That(xml, Does.Contain(expectedXmlPart), "Serialized XML");
Assert.That(deserialized?.ToString(), Is.EqualTo(original.ToString()), "Deserialized Object");
});
}
}
Loading

0 comments on commit 480d123

Please sign in to comment.