Skip to content

Instantly share code, notes, and snippets.

@undergroundmonorail
Created January 24, 2021 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save undergroundmonorail/f93779d2e14c9839b7d2d8292694b5a2 to your computer and use it in GitHub Desktop.
Save undergroundmonorail/f93779d2e14c9839b7d2d8292694b5a2 to your computer and use it in GitHub Desktop.
PropertyInfo[] allProps = typeof(Patient).GetProperties();
foreach (var property in allProps)
{
if (property.PropertyType == typeof(List<Address>))
{
// If the property is a list of addresses, use reflection on each address to compare all their properties
PropertyInfo[] addressProps = typeof(Address).GetProperties();
var addressPairs = ((List<Address>)property.GetValue(deserialized)).Zip((List<Address>)property.GetValue(expectedResult));
foreach ((Address deserializedAddress, Address expectedAddress) in addressPairs)
{
foreach (var addressProperty in addressProps)
{
Assert.AreEqual(addressProperty.GetValue(deserializedAddress), addressProperty.GetValue(expectedAddress),
$"Address Property {addressProperty.Name} did not match when deserialized");
}
}
}
else if (property.PropertyType == typeof(List<Identifier>))
{
// If the property is a list of identifiers, use reflection on each identifier to compare all their properties
PropertyInfo[] identifierProps = typeof(Identifier).GetProperties();
var identifierPairs = ((List<Identifier>)property.GetValue(deserialized)).Zip((List<Identifier>)property.GetValue(expectedResult));
foreach ((Identifier deserializedIdentifier, Identifier expectedIdentifier) in identifierPairs)
{
foreach (var identifierProperty in identifierProps)
{
Assert.AreEqual(identifierProperty.GetValue(deserializedIdentifier), identifierProperty.GetValue(expectedIdentifier),
$"Address Property {identifierProperty.Name} did not match when deserialized");
}
}
}
else
{
// Otherwise compare each property directly
Assert.AreEqual(property.GetValue(deserialized), property.GetValue(expectedResult), $"Property {property.Name} did not match when deserialized");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment