public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext) { var areEqual = true; var expectedEnumerable = (IEnumerable)expected; var actualEnumerable = (IEnumerable)actual; var expectedEnumerator = expectedEnumerable.GetEnumerator(); var actualEnumerator = actualEnumerable.GetEnumerator(); var expectedHasValue = expectedEnumerator.MoveNext(); var actualHasValue = actualEnumerator.MoveNext(); var yield = 0; while (expectedHasValue && actualHasValue) { areEqual = comparisonContext.ReportEquality(expectedEnumerator.Current, actualEnumerator.Current, $"[{yield++}]") && areEqual; expectedHasValue = expectedEnumerator.MoveNext(); actualHasValue = actualEnumerator.MoveNext(); } if (!expectedHasValue && actualHasValue) { areEqual = comparisonContext.ReportEquality(null, new UnexpectedElement(actualEnumerator.Current), $"[{yield}]") && areEqual; } else if (expectedHasValue) { areEqual = comparisonContext.ReportEquality(expectedEnumerator.Current, new MissingElement(), $"[{yield}]") && areEqual; } return(areEqual); }
static bool CompareField(FieldInfo expectedFieldInfo, FieldInfo actualFieldInfo, object expected, object actual, IComparisonContext comparisonContext) { var value1 = expectedFieldInfo.GetValue(expected); if (actualFieldInfo == null) { return(comparisonContext .ReportEquality(value1, Activator.CreateInstance(typeof(MissingMember <>) .MakeGenericType(expectedFieldInfo.FieldType)), expectedFieldInfo.Name)); } var value2 = actualFieldInfo.GetValue(actual); return(comparisonContext.ReportEquality(value1, value2, expectedFieldInfo.Name)); }
static bool CompareStandardProperty(PropertyInfo pi1, PropertyInfo pi2, object expected, object actual, IComparisonContext comparisonContext) { var value1 = pi1.GetValue(expected, null); if (pi2 == null) { return(comparisonContext .ReportEquality(value1, Activator.CreateInstance(typeof(MissingMember <>) .MakeGenericType(pi1.PropertyType)), pi1.Name)); } var value2 = pi2.GetValue(actual, null); return(comparisonContext.ReportEquality(value1, value2, pi1.Name)); }
public bool AreEqual(object expected, object actual, IComparisonContext comparisonContext) { var genericTypes = expected.GetType().GetGenericArguments(); var getKey = GetMethodInfo("GetKey", genericTypes); var key1 = getKey.Invoke(this, new[] { expected }); var key2 = getKey.Invoke(this, new[] { actual }); var areEqual = comparisonContext.ReportEquality(key1, key2, "Key"); var getValue = GetMethodInfo("GetValue", genericTypes); var value1 = getValue.Invoke(this, new[] { expected }); var value2 = getValue.Invoke(this, new[] { actual }); areEqual = comparisonContext.ReportEquality(value1, value2, "Value") && areEqual; return(areEqual); }
static bool CompareIndexedProperty(PropertyInfo pi, object expected, object actual, ParameterInfo[] indexes, IComparisonContext comparisonContext) { var areEqual = true; foreach (var index in indexes) { if (index.ParameterType == typeof(int)) { var expectedCountPropertyInfo = expected.GetType().GetProperty("Count"); var 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 (var i = 0; i < expectedCount; i++) { object[] indexValues = { i }; var value1 = pi.GetValue(expected, indexValues); var value2 = pi.GetValue(actual, indexValues); if (!comparisonContext.ReportEquality(value1, value2, pi.Name + "[" + i + "]")) { areEqual = false; } } } } } return(areEqual); }
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); }