/// <summary> /// Applies a step as part of the task to compare two objects for structural equality. /// </summary> /// <value> /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions /// have to be executed. Should return <c>false</c> otherwise. /// </value> /// <remarks> /// May throw when preconditions are not met or if it detects mismatching data. /// </remarks> public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config) { if ((context.Expectation is null) || (context.Subject is null)) { return(false); } Type subjectType = context.Subject.GetType(); Type expectationType = context.Expectation.GetType(); if (subjectType.IsSameOrInherits(expectationType)) { return(false); } if (TryChangeType(context.Subject, expectationType, out object convertedSubject)) { context.TraceSingle(path => $"Converted subject {context.Subject} at {path} to {expectationType}"); var newContext = context.CreateWithDifferentSubject(convertedSubject, expectationType); structuralEqualityValidator.AssertEqualityUsing(newContext); return(true); } context.TraceSingle(path => $"Subject {context.Subject} at {path} could not be converted to {expectationType}"); return(false); }
/// <summary> /// Applies a step as part of the task to compare two objects for structural equality. /// </summary> /// <value> /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions /// have to be executed. Should return <c>false</c> otherwise. /// </value> /// <remarks> /// May throw when preconditions are not met or if it detects mismatching data. /// </remarks> public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config) { if (!ReferenceEquals(context.Expectation, null) && !ReferenceEquals(context.Subject, null) && !context.Subject.GetType().IsSameOrInherits(context.Expectation.GetType())) { Type expectationType = context.Expectation.GetType(); object convertedSubject; if (TryChangeType(context.Subject, expectationType, out convertedSubject)) { var newContext = context.CreateWithDifferentSubject(convertedSubject, expectationType); structuralEqualityValidator.AssertEqualityUsing(newContext); return true; } } return false; }
/// <summary> /// Applies a step as part of the task to compare two objects for structural equality. /// </summary> /// <value> /// Should return <c>true</c> if the subject matches the expectation or if no additional assertions /// have to be executed. Should return <c>false</c> otherwise. /// </value> /// <remarks> /// May throw when preconditions are not met or if it detects mismatching data. /// </remarks> public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator structuralEqualityValidator, IEquivalencyAssertionOptions config) { if (!ReferenceEquals(context.Expectation, null) && !ReferenceEquals(context.Subject, null) && !context.Subject.GetType().IsSameOrInherits(context.Expectation.GetType())) { Type expectationType = context.Expectation.GetType(); object convertedSubject; if (TryChangeType(context.Subject, expectationType, out convertedSubject)) { var newContext = context.CreateWithDifferentSubject(convertedSubject, expectationType); structuralEqualityValidator.AssertEqualityUsing(newContext); return(true); } } return(false); }