コード例 #1
0
        /// <summary>
        /// Checks if an object is an instance of a specified type.
        /// </summary>
        /// <param name="check">checker logic object</param>
        /// <param name="type">expected type</param>
        /// <typeparam name="T">type of checked object</typeparam>
        /// <returns>A check link</returns>
        public static ICheckLink <ICheck <T> > IsInstanceOfType <T>(this ICheck <T> check, Type type)
        {
            ExtensibilityHelper.BeginCheck(check)
            .Analyze((sut, test) =>
            {
                var reflectionSut = sut as ReflectionWrapper;
                if (reflectionSut != null)
                {
                    var expectedWrapper = ReflectionWrapper.BuildFromType(type, reflectionSut.Criteria);
                    expectedWrapper.MapFields(reflectionSut, 1, (expected, actual, depth) =>
                    {
                        if (actual != null && expected != null && actual.ValueType != expected.ValueType)
                        {
                            if (actual.ValueType.IsPrimitive() || expected.ValueType.IsPrimitive())
                            {
                                test.GetSutProperty(_ => actual.Value, actual.MemberLabel)
                                .Fails("The {0} is of a different type than the {1}.")
                                .ExpectingType(expected.ValueType, "", "");
                                return(false);
                            }

                            return(true);
                        }

                        if (actual != null && expected != null && expected.ValueType == actual.ValueType)
                        {
                            return(false);
                        }

                        if (actual == null)
                        {
                            test.GetSutProperty(_ => expectedWrapper.Value, expected.MemberLabel)
                            .Expecting(expected)
                            .Fails("The {1} is absent from the {0}.", MessageOption.NoCheckedBlock);
                        }
                        else
                        {
                            test.GetSutProperty(_ => actual, actual.MemberLabel.DoubleCurlyBraces())
                            .Fails("The {0} is absent from the {1}.");
                        }
                        return(false);
                    });
                }
                else if (typeof(T).IsNullable())
                {
                    test.FailsIf(sut2 => typeof(T) != type || (sut2 == null && !typeof(T).IsNullable()),
                                 $"The {{0}} is not an instance of [{type.ToStringProperlyFormatted()}].", MessageOption.WithType);
                }
                else
                {
                    test.FailsIf(sut2 => sut2.GetTypeWithoutThrowingException() != type,
                                 $"The {{0}} is not an instance of [{type.ToStringProperlyFormatted()}].", sut != null ? MessageOption.WithType : MessageOption.None);
                }
            })
            .ExpectingType(type, "", "different from")
            .Negates($"The {{0}} is an instance of [{type.ToStringProperlyFormatted()}] whereas it must not.", MessageOption.WithType)
            .EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
コード例 #2
0
        /// <summary>
        /// Checks if an object is an instance of a specified type.
        /// </summary>
        /// <param name="check">checker logic object</param>
        /// <param name="type">expected type</param>
        /// <typeparam name="T">type of checked object</typeparam>
        /// <returns>A check link</returns>
        public static ICheckLink <ICheck <T> > IsInstanceOfType <T>(this ICheck <T> check, Type type)
        {
            ExtensibilityHelper.BeginCheck(check).
            FailWhen(sut => sut == null && typeof(T) == type && typeof(T).IsNullable(), $"The {{checked}} does not have a value.").
            FailWhen(sut => sut == null, $"The {{checked}} is not an instance of [{type.ToStringProperlyFormatted()}].").
            Analyze((sut, test) =>
            {
                if (sut is ReflectionWrapper reflectionSut)
                {
                    var expectedWrapper = ReflectionWrapper.BuildFromType(type, reflectionSut.Criteria);
                    expectedWrapper.MapFields(reflectionSut, 1, (expected, actual, depth) =>
                    {
                        if (actual == null)
                        {
                            test.CheckSutAttributes(_ => expectedWrapper.Value, expected.MemberLabel)
                            .DefineExpectedValue(expected)
                            .Fail("The {1} is absent from the {0}.", MessageOption.NoCheckedBlock);
                        }
                        else if (expected == null)
                        {
                            test.CheckSutAttributes(_ => actual, actual.MemberLabel.DoubleCurlyBraces())
                            .Fail("The {0} is absent from the {1}.");
                        }
                        else if (actual.ValueType != expected.ValueType)
                        {
                            if (!actual.ValueType.IsPrimitive() && !expected.ValueType.IsPrimitive())
                            {
                                return(true);
                            }

                            test.CheckSutAttributes(_ => actual.Value, actual.MemberLabel)
                            .Fail("The {0} is of a different type than the {1}.")
                            .DefineExpectedType(expected.ValueType);
                        }

                        return(false);
                    });
                }
                else
                {
                    test.FailWhen(sut2 => sut2.GetTypeWithoutThrowingException() != type,
                                  $"The {{0}} is not an instance of [{type.ToStringProperlyFormatted()}].",
                                  MessageOption.WithType);
                }
            })
            .DefineExpectedType(type)
            .OnNegate($"The {{0}} is an instance of [{type.ToStringProperlyFormatted()}] whereas it must not.", MessageOption.WithType)
            .EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }