public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext) { bool areEqual = true; var expectedEnumerable = (IEnumerable)expected; var actualEnumerable = (IEnumerable)actual; IEnumerator expectedEnumerator = expectedEnumerable.GetEnumerator(); IEnumerator actualEnumerator = actualEnumerable.GetEnumerator(); bool expectedHasValue = expectedEnumerator.MoveNext(); bool actualHasValue = actualEnumerator.MoveNext(); int yield = 0; while (expectedHasValue && actualHasValue) { areEqual = comparisonContext.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, "[" + yield++ + "]") && areEqual; expectedHasValue = expectedEnumerator.MoveNext(); actualHasValue = actualEnumerator.MoveNext(); } if (!expectedHasValue && actualHasValue) { areEqual = comparisonContext.AreEqual(null, new UnexpectedElement(actualEnumerator.Current), "[" + yield + "]") && areEqual; } else if (expectedHasValue) { areEqual = comparisonContext.AreEqual(expectedEnumerator.Current, new MissingElement(), "[" + yield + "]") && areEqual; } return areEqual; }
static bool CompareField(FieldInfo expectedFieldInfo, FieldInfo actualFieldInfo, object expected, object actual, IComparisonContext comparisonContext, bool equal) { object value1 = expectedFieldInfo.GetValue(expected); if (actualFieldInfo == null) { return comparisonContext .AreEqual(value1, Activator.CreateInstance(typeof(MissingMember<>) .MakeGenericType(expectedFieldInfo.FieldType)), expectedFieldInfo.Name); } object value2 = actualFieldInfo.GetValue(actual); return comparisonContext.AreEqual(value1, value2, expectedFieldInfo.Name); }
static bool CompareField(FieldInfo expectedFieldInfo, FieldInfo actualFieldInfo, object expected, object actual, IComparisonContext comparisonContext, bool equal) { object value1 = expectedFieldInfo.GetValue(expected); if (actualFieldInfo == null) { return(comparisonContext .AreEqual(value1, Activator.CreateInstance(typeof(MissingMember <>) .MakeGenericType(expectedFieldInfo.FieldType)), expectedFieldInfo.Name)); } object value2 = actualFieldInfo.GetValue(actual); return(comparisonContext.AreEqual(value1, value2, expectedFieldInfo.Name)); }
static bool CompareStandardProperty(PropertyInfo pi1, PropertyInfo pi2, object expected, object actual, IComparisonContext comparisonContext) { object value1 = pi1.GetValue(expected, null); if (pi2 == null) { return(comparisonContext .AreEqual(value1, Activator.CreateInstance(typeof(MissingMember <>) .MakeGenericType(pi1.PropertyType)), pi1.Name)); } object value2 = pi2.GetValue(actual, null); return(comparisonContext.AreEqual(value1, value2, pi1.Name)); }
static bool CompareIndexedProperty(PropertyInfo pi, object expected, object actual, ParameterInfo[] indexes, IComparisonContext comparisonContext) { bool areEqual = true; foreach (ParameterInfo index in indexes) { if (index.ParameterType == typeof(int)) { PropertyInfo expectedCountPropertyInfo = expected.GetType().GetProperty("Count"); PropertyInfo actualCountPropertyInfo = actual.GetType().GetProperty("Count"); if (expectedCountPropertyInfo != null) { var expectedCount = (int)expectedCountPropertyInfo.GetValue(expected, null); var actualCount = (int)actualCountPropertyInfo.GetValue(actual, null); if (expectedCount != actualCount) { areEqual = false; break; } for (int i = 0; i < expectedCount; i++) { object[] indexValues = { i }; object value1 = pi.GetValue(expected, indexValues); object value2 = pi.GetValue(actual, indexValues); if (!comparisonContext.AreEqual(value1, value2, pi.Name + "[" + i + "]")) { areEqual = false; } } } } } return(areEqual); }
static bool CompareIndexedProperty(PropertyInfo pi, object expected, object actual, ParameterInfo[] indexes, IComparisonContext comparisonContext) { bool areEqual = true; foreach (ParameterInfo index in indexes) { if (index.ParameterType == typeof (int)) { PropertyInfo expectedCountPropertyInfo = expected.GetType().GetProperty("Count"); PropertyInfo actualCountPropertyInfo = actual.GetType().GetProperty("Count"); if (expectedCountPropertyInfo != null) { var expectedCount = (int) expectedCountPropertyInfo.GetValue(expected, null); var actualCount = (int) actualCountPropertyInfo.GetValue(actual, null); if (expectedCount != actualCount) { areEqual = false; break; } for (int i = 0; i < expectedCount; i++) { object[] indexValues = {i}; object value1 = pi.GetValue(expected, indexValues); object value2 = pi.GetValue(actual, indexValues); if (!comparisonContext.AreEqual(value1, value2, pi.Name + "[" + i + "]")) { areEqual = false; } } } } } return areEqual; }
static bool CompareStandardProperty(PropertyInfo pi1, PropertyInfo pi2, object expected, object actual, IComparisonContext comparisonContext) { object value1 = pi1.GetValue(expected, null); if (pi2 == null) { return comparisonContext .AreEqual(value1, Activator.CreateInstance(typeof (MissingMember<>) .MakeGenericType(pi1.PropertyType)), pi1.Name); } object value2 = pi2.GetValue(actual, null); return comparisonContext.AreEqual(value1, value2, pi1.Name); }
public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext) { var areEqual = true; var expectedEnumerable = (IEnumerable)expected; var actualEnumerable = (IEnumerable)actual; var expectedList = expectedEnumerable.Cast <object>().ToList(); var actualList = actualEnumerable.Cast <object>().ToList(); var expectedDictionary = GetDictionary(expectedList); var actualDictionary = GetDictionary(actualList); Action emptyDelegate = () => { }; Action removeDelegate; foreach (var expectedEntry in expectedDictionary.ToList()) { removeDelegate = emptyDelegate; if (!actualDictionary.Any(actualEntry => { if (!comparisonContext.AreEqual(expectedEntry.Value, actualEntry.Value, $"[{expectedEntry.Key}]")) { return(false); } removeDelegate = () => actualDictionary.Remove(actualEntry.Key); return(true); })) { areEqual = comparisonContext.ReportEquality(expectedEntry.Value, new MissingElement(), $"[{expectedEntry.Key}]") && areEqual; } removeDelegate(); } var unexpectedValues = new List <object>(); for (var actualIndex = 0; actualIndex < actualList.Count; actualIndex++) { removeDelegate = emptyDelegate; if (!expectedDictionary.Any(expectedEntry => { var equal = comparisonContext.AreEqual(expectedEntry.Value, actualList[actualIndex], $"[{expectedEntry.Key}]"); if (equal) { removeDelegate = () => expectedDictionary.Remove(expectedEntry.Key); } return(equal); })) { unexpectedValues.Add(actualList[actualIndex].ToObjectString()); } removeDelegate(); } if (unexpectedValues.Any()) { var unexpectedElements = string.Join($",{Environment.NewLine}{Environment.NewLine}", unexpectedValues.Select(s => $"{s}")); comparisonContext.Report(false, $"The following elements were unexpected:{Environment.NewLine}{Environment.NewLine}{unexpectedElements}", null, null); areEqual = false; } return(areEqual); }