Beispiel #1
0
        protected override IMatchResult Matches <T1>(T1 actual)
        {
            if (ExplicitTypesFlag && ReferenceEquals(actual.GetType(), Expected.GetType()) == false)
            {
                return(MatchResult.Fail($"was of type {Expected.GetType()} (ExplicitTypesFlag was set)", $"was of type {actual.GetType()}"));
            }

            if (primitive != null)
            {
                return(new ConstraintResultMatchResult(primitive.ApplyTo(actual)));
            }

            PropertiesMatchResult result = new PropertiesMatchResult();

            foreach (Property property in propertyMap.Values)
            {
                try
                {
                    property.Actual   = property.Info.GetValue(actual, null);
                    property.Expected = property.Info.GetValue(Expected, null);
                    ConstraintResult pr = property.Apply();
                    if (!pr.IsSuccess)
                    {
                        result.Failure(property.Info, new ConstraintResultMatchResult(pr));
                    }
                }
                catch (TargetException)
                {
                    result.Failure(property.Info, MatchResult.Fail(
                                       expectedMessage: $"[{Expected.GetType().Name}] contained the property '{property.Info.Name}'.",
                                       actualMessage: $"[{actual.GetType().Name}] did not."
                                       ));
                }
            }

            return(result);
        }
        protected override IMatchResult Matches <T1>(T1 actual)
        {
            if (base.Matches(actual).Matches)
            {
                return(MatchResult.Fail("any property of the object to not be equal", "all was equal"));
            }

            if (!strict)
            {
                return(MatchResult.Success());
            }

            PropertiesMatchResult result = new PropertiesMatchResult();

            foreach (Property property in propertyMap.Values)
            {
                try
                {
                    property.Actual   = property.Info.GetValue(actual, null);
                    property.Expected = property.Info.GetValue(Expected, null);
                    ConstraintResult pr = property.Apply();
                    if (pr.IsSuccess)
                    {
                        result.Failure(property.Info, new ConstraintResultMatchResult(pr));
                    }
                }
                catch (TargetException)
                {
                    result.Failure(property.Info, MatchResult.Fail(
                                       expectedMessage: $"[{Expected.GetType().Name}] contained the property '{property.Info.Name}'.",
                                       actualMessage: $"[{actual.GetType().Name}] did not."
                                       ));
                }
            }
            return(result);
        }