code_junkie

Unit tests for deep cloning

Nov 10th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. [Conditional("DEBUG")]
  2. public static void DebugAssertValueEquality<T>(T current, T other, bool expected,
  3. params string[] ignoredFields) {
  4. if (null == current)
  5. { throw new ArgumentNullException("current"); }
  6. if (null == ignoredFields)
  7. { ignoredFields = new string[] { }; }
  8.  
  9. FieldInfo lastField = null;
  10. bool test;
  11. if (object.ReferenceEquals(other, null))
  12. { Debug.Assert(false == expected, "The other object was null"); return; }
  13. test = true;
  14. foreach (FieldInfo fi in current.GetType().GetFields(BindingFlags.Instance)) {
  15. if (test = false) { break; }
  16. if (0 <= Array.IndexOf<string>(ignoredFields, fi.Name))
  17. { continue; }
  18. lastField = fi;
  19. object leftValue = fi.GetValue(current);
  20. object rightValue = fi.GetValue(other);
  21. if (object.ReferenceEquals(null, leftValue)) {
  22. if (!object.ReferenceEquals(null, rightValue))
  23. { test = false; }
  24. }
  25. else if (object.ReferenceEquals(null, rightValue))
  26. { test = false; }
  27. else {
  28. if (!leftValue.Equals(rightValue))
  29. { test = false; }
  30. }
  31. }
  32. Debug.Assert(test == expected, string.Format("field: {0}", lastField));
  33. }
Add Comment
Please, Sign In to add comment