DD.WellWorkover.Cloud/AsbCloudWebApi.IntegrationTests/MatchHelper.cs

35 lines
1.0 KiB
C#
Raw Normal View History

using System.Reflection;
using Xunit.Sdk;
namespace AsbCloudWebApi.IntegrationTests;
public static class MatchHelper
{
public static void Match<T>(T expected, T actual, IEnumerable<string>? excludeProps = null)
{
if (ReferenceEquals(expected, actual))
throw EqualException.ForMismatchedValues(expected, actual, "Reference are equals");
if (expected is null || actual is null)
2024-01-18 11:05:22 +05:00
throw EqualException.ForMismatchedValues(expected, actual);
var type = typeof(T);
var props = type.GetProperties(
BindingFlags.Public
| BindingFlags.Instance).Where(prop => prop.CanWrite);
if (excludeProps is not null && excludeProps.Any())
props = props.Where(prop => !excludeProps.Contains(prop.Name));
foreach (var prop in props)
{
var objValue = prop.GetValue(expected);
var anotherValue = prop.GetValue(actual);
if (objValue != null && !objValue.Equals(anotherValue))
{
var banner = $" of {type.Name} props {prop.Name} ";
throw EqualException.ForMismatchedValues(expected, actual, banner);
}
}
}
}