示例#1
0
        private void VerifyEquality(EqualitySpecifications spec, BinaryCompareDelegate comparer, MethodInfo methodInfo, InstancePair pair)
        {
            bool actual   = comparer(pair.First, pair.Second);
            bool expected = !(pair.AreEquivalent ^ spec.ExpectedEquality);

            AssertionHelper.Explain(() =>
                                    Assert.AreEqual(expected, actual),
                                    innerFailures => new AssertionFailureBuilder(
                                        "The equality 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());
        }
示例#2
0
        private Test CreateEqualityTest(EqualitySpecifications 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.GetEqualityMethod(workingType);

                            if (MethodExists(methodInfo, spec.MethodFriendlyName, spec.IsEqualityMethodRequired(pair.First)))
                            {
                                BinaryCompareDelegate comparer = GetBinaryComparer(methodInfo);

                                if (pair.IsNewFirst)
                                {
                                    if (methodInfo.IsStatic && IsReferenceType(pair.First) && IsReferenceType(pair.Second))
                                    {
                                        VerifyEquality(spec, comparer, methodInfo, new InstancePair(null, null, true));
                                    }

                                    if (IsReferenceType(pair.Second))
                                    {
                                        VerifyEquality(spec, comparer, methodInfo, new InstancePair(pair.First, null, false));
                                    }
                                }

                                if (spec.IsSecondArgumentCompatible(pair.Second))
                                {
                                    VerifyEquality(spec, comparer, methodInfo, pair);

                                    if (methodInfo.IsStatic && IsReferenceType(pair.First))
                                    {
                                        VerifyEquality(spec, comparer, methodInfo, new InstancePair(null, pair.Second, false));
                                    }
                                }
                            }
                        }
                    }
                }
            })));
        }