Exemple #1
0
        private void VerifyComparison <TResult>(ComparisonSpecifications <TResult> spec, ComparerDelegate <TResult> comparer, MethodInfo methodInfo, InstancePair pair)
        {
            string actual   = spec.FormatResult(comparer(pair.First, pair.Second));
            string expected = spec.FormatResult(spec.AdjustExpectedEquivalence(pair.Equivalence));

            AssertionHelper.Explain(() =>
                                    Assert.AreEqual(expected, actual),
                                    innerFailures => new AssertionFailureBuilder("The comparison between left and right values did not produce the expected result.")
                                    .AddRawLabeledValue("Left Value", pair.First)
                                    .AddRawLabeledValue("Right Value", pair.Second)
                                    .AddRawLabeledValue("Expected Result", expected)
                                    .AddRawLabeledValue("Actual Result", actual)
                                    .SetStackTrace(Context.GetStackTraceData())
                                    .AddInnerFailures(innerFailures)
                                    .ToAssertionFailure());
        }
Exemple #2
0
        private Test CreateComparisonTest <TResult>(ComparisonSpecifications <TResult> spec)
        {
            return(new TestCase(spec.Name, () => Assert.Multiple(() =>
            {
                foreach (InstancePair pair in GetAllPairs())
                {
                    if (IsTargetTypeOrDerived(pair.First))
                    {
                        Type workingType = spec.GetWorkingType(pair.First);

                        if (workingType != null)
                        {
                            MethodInfo methodInfo = spec.GetComparisonMethod(workingType);

                            if (MethodExists(methodInfo, spec.MethodFriendlyName, spec.IsComparisonMethodRequired(pair.First)))
                            {
                                ComparerDelegate <TResult> comparer = GetBinaryComparer <TResult>(methodInfo);

                                if (pair.IsNewFirst)
                                {
                                    if (methodInfo.IsStatic && IsReferenceType(pair.First) && IsReferenceType(pair.Second))
                                    {
                                        VerifyComparison <TResult>(spec, comparer, methodInfo, new InstancePair(null, null, 0));
                                    }

                                    if (IsReferenceType(pair.Second))
                                    {
                                        VerifyComparison <TResult>(spec, comparer, methodInfo, new InstancePair(pair.First, null, 1));
                                    }
                                }

                                if (spec.IsSecondArgumentCompatible(pair.Second))
                                {
                                    VerifyComparison <TResult>(spec, comparer, methodInfo, pair);

                                    if (methodInfo.IsStatic && IsReferenceType(pair.First))
                                    {
                                        VerifyComparison <TResult>(spec, comparer, methodInfo, new InstancePair(null, pair.Second, -1));
                                    }
                                }
                            }
                        }
                    }
                }
            })));
        }