Last active
December 11, 2017 21:34
-
-
Save janosorcsik/8e3d3868bef86af2e85b to your computer and use it in GitHub Desktop.
Unit test helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace UnitTest.Helpers | |
{ | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Reflection; | |
using System.Linq; | |
public static class UnitTestHelper | |
{ | |
public static bool PublicInstancePropertiesEqual<T>(T self, T to, params string[] ignore) where T : class | |
{ | |
if (self != null && to != null) | |
{ | |
bool end_result = true; | |
Type type = typeof(T); | |
List<string> ignoreList = new List<string>(ignore); | |
foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)) | |
try | |
{ | |
if (ignoreList.Contains(pi.Name)) | |
continue; | |
object selfValue = type.GetProperty(pi.Name).GetValue(self, null); | |
object toValue = type.GetProperty(pi.Name).GetValue(to, null); | |
if (pi.PropertyType.IsGenericType && typeof(IEnumerable).IsAssignableFrom(pi.PropertyType)) | |
{ | |
MethodInfo mi = miPublicEnumerableInstancePropertiesEqual.MakeGenericMethod(pi.PropertyType.GetGenericArguments()[0]); | |
bool result = (bool)mi.Invoke(null, new object[] { selfValue, toValue, new string[0] }); | |
if (!result) | |
{ | |
Console.Out.WriteLine("Value mismatch at: {0} value: {1} <-> {2}", pi.Name, selfValue, toValue); | |
end_result = false; | |
} | |
continue; | |
} | |
if (pi.PropertyType.IsGenericType && pi.PropertyType.Name.Contains("HolderSingleDTO")) | |
{ | |
continue; | |
} | |
if (pi.PropertyType != typeof(IEnumerable)) | |
{ | |
if (pi.PropertyType.IsClass && pi.PropertyType != typeof(string)) | |
{ | |
MethodInfo mi = miPublicInstancePropertiesEqual.MakeGenericMethod(pi.PropertyType); | |
bool result = (bool)mi.Invoke(null, new object[] { selfValue, toValue, new string[0] }); | |
if (!result) | |
{ | |
Console.Out.WriteLine("Value mismatch at: {0} value: {1} <-> {2}", pi.Name, selfValue, toValue); | |
end_result = false; | |
} | |
} | |
else if (pi.PropertyType == typeof(DateTime)) | |
{ | |
bool result = Math.Abs(((DateTime)selfValue - (DateTime)toValue).TotalMilliseconds) < 100; | |
if (!result) | |
{ | |
Console.Out.WriteLine("Value mismatch at: {0} value: {1} <-> {2}", pi.Name, selfValue, toValue); | |
end_result = false; | |
} | |
} | |
else if (selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue))) | |
{ | |
Console.Out.WriteLine("Value mismatch at: {0} value: {1} <-> {2}", pi.Name, selfValue, toValue); | |
end_result = false; | |
} | |
} | |
} | |
catch (Exception exc) | |
{ | |
end_result = false; | |
} | |
return end_result; | |
} | |
return self == to; | |
} | |
private static MethodInfo _miPublicEnumerableInstancePropertiesEqual = null; | |
private static MethodInfo miPublicEnumerableInstancePropertiesEqual | |
{ | |
get | |
{ | |
if (_miPublicEnumerableInstancePropertiesEqual == null) | |
_miPublicEnumerableInstancePropertiesEqual = typeof(UnitTestHelper).GetMethods().FirstOrDefault(mi => mi.IsStatic && mi.IsPublic && mi.IsGenericMethodDefinition && mi.Name.Contains("PublicEnumerableInstancePropertiesEqual")); | |
return _miPublicEnumerableInstancePropertiesEqual; | |
} | |
} | |
private static MethodInfo _miPublicInstancePropertiesEqual = null; | |
private static MethodInfo miPublicInstancePropertiesEqual | |
{ | |
get | |
{ | |
if (_miPublicInstancePropertiesEqual == null) | |
_miPublicInstancePropertiesEqual = typeof(UnitTestHelper).GetMethods().FirstOrDefault(mi => mi.IsStatic && mi.IsPublic && mi.IsGenericMethodDefinition && mi.Name.Contains("PublicInstancePropertiesEqual")); | |
return _miPublicInstancePropertiesEqual; | |
} | |
} | |
public static bool PublicEnumerableInstancePropertiesEqual<T>( | |
IEnumerable<T> self, IEnumerable<T> to, params string[] ignore) where T : class | |
{ | |
if (self != null && to != null) | |
{ | |
if (self.Count() != to.Count()) | |
{ | |
return false; | |
} | |
int idx = 0; | |
foreach (var item in self) | |
{ | |
var obj = to.ElementAt(idx++); | |
if (!PublicInstancePropertiesEqual(item, obj, ignore)) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
return self == to; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment