public void ProviderShouldValidateUsingDataAnnotationAttributes()
        {
            var result       = new ValidationResult("error", new[] { DisplayName });
            var dictionary   = new Dictionary <object, object>();
            var propertyInfo = typeof(SpyValidationClass).GetProperty(SpyPropertyName);

            DisplayNameProvider.GetNameDelegate = info =>
            {
                info.ShouldEqual(propertyInfo);
                return(DisplayName + propertyInfo.Name);
            };
            var provider        = new DynamicDataAnnotationsElementProvider(DisplayNameProvider);
            var validationClass = new SpyValidationClass {
                SpyProperty = "Test"
            };
            var validationContext = new MugenMvvmToolkit.Models.Validation.ValidationContext(validationClass, IocContainer, dictionary);

            SpyValidationAttribute.IsValidDelegate = (o, context) =>
            {
                o.ShouldEqual(validationClass.SpyProperty);
                context.DisplayName.ShouldEqual(DisplayName + propertyInfo.Name);
                context.Items[DynamicDataAnnotationsElementProvider.ServiceProviderKey].ShouldEqual(IocContainer);
                context.MemberName.ShouldEqual(SpyPropertyName);
                return(result);
            };
            var validationResult = provider.GetValidationElements(validationClass)[SpyPropertyName]
                                   .Single()
                                   .Validate(validationContext)
                                   .Single();

            validationResult.MemberNames.SequenceEqual(result.MemberNames).ShouldBeTrue();
            validationResult.ErrorMessage.ShouldEqual(result.ErrorMessage);
        }
        public void ProviderShouldValidateItemUsingSystemValidatableObject()
        {
            var result     = new ValidationResult("error", new[] { DisplayName });
            var dictionary = new Dictionary <object, object>();

            DisplayNameProvider.GetNameDelegate = info =>
            {
                info.ShouldEqual(typeof(SpyValidationClass));
                return(DisplayName);
            };
            var provider        = new DynamicDataAnnotationsElementProvider(DisplayNameProvider);
            var validationClass = new SpyValidationClass
            {
                ValidateSystem = context =>
                {
                    context.DisplayName.ShouldEqual(DisplayName);
                    context.Items[DynamicDataAnnotationsElementProvider.ServiceProviderKey].ShouldEqual(IocContainer);
                    context.MemberName.ShouldBeNull();
                    return(new[] { result });
                },
                Validate = context => Enumerable.Empty <IValidationResult>()
            };

            var validationContext = new MugenMvvmToolkit.Models.Validation.ValidationContext(validationClass, IocContainer, dictionary);
            var elements          = provider.GetValidationElements(validationClass);
            var validationResults = new List <IValidationResult>();

            elements[string.Empty].ForEach(element => validationResults.AddRange(element.Validate(validationContext)));

            var validationResult = validationResults.Single();

            validationResult.MemberNames.SequenceEqual(result.MemberNames).ShouldBeTrue();
            validationResult.ErrorMessage.ShouldEqual(result.ErrorMessage);
        }
        public void ProviderShouldThrowExceptionIfMetadataIsInvalid()
        {
            var provider        = new DynamicDataAnnotationsElementProvider(DisplayNameProvider);
            var validationClass = new InvalidaMetadataClass();

            ShouldThrow <MissingMemberException>(() => provider.GetValidationElements(validationClass));
        }
        /// <summary>
        ///     Gets a display name for the specified type using the specified member.
        /// </summary>
        /// <param name="memberInfo">The specified member.</param>
        protected virtual Func <string> GetDisplayNameInternal(MemberInfo memberInfo)
        {
#if PCL_WINRT
            var typeInfo = memberInfo as TypeInfo;
#else
            var typeInfo = memberInfo as Type;
#endif

            if (typeInfo != null)
            {
#if PCL_WINRT
                var type = typeInfo.AsType();
#else
                var type = typeInfo;
#endif
                var metadataTypes = new List <Type>(DynamicDataAnnotationsElementProvider.GetMetadataTypes(type));
                metadataTypes.Insert(0, type);
                for (int index = 0; index < metadataTypes.Count; index++)
                {
#if PCL_WINRT
                    var result = TryGetDisplayNameAttributeAccessor(metadataTypes[index].GetTypeInfo());
#else
                    var result = TryGetDisplayNameAttributeAccessor(metadataTypes[index]);
#endif

                    if (result != null)
                    {
                        return(result);
                    }
                }
                return(() => string.Empty);
            }
            Func <string> accessor = TryGetDisplayAttributeAccessor(memberInfo);
            if (accessor != null)
            {
                return(accessor);
            }

            ICollection <Type> types = DynamicDataAnnotationsElementProvider.GetMetadataTypes(ExpressionReflectionManager.GetDeclaringType(memberInfo));
            foreach (Type metaType in types)
            {
                MemberInfo metaMemberInfo = TryFindMetaMemberInfo(metaType, memberInfo);
                if (metaMemberInfo == null)
                {
                    continue;
                }
                accessor = TryGetDisplayAttributeAccessor(metaMemberInfo);
                if (accessor != null)
                {
                    return(accessor);
                }
            }
            string s = memberInfo.Name;
            return(() => s);
        }
        public void ProviderShouldFindAllElements()
        {
#if WPF
            const int count       = 2;
            const int objectCount = 2;
#else
            const int count       = 2;
            const int objectCount = 1;
#endif
            var provider        = new DynamicDataAnnotationsElementProvider(DisplayNameProvider);
            var validationClass = new SpyValidationClass();
            var elements        = provider.GetValidationElements(validationClass);
            elements.Count.ShouldEqual(count);
            elements[string.Empty].Count.ShouldEqual(objectCount);
            elements[SpyPropertyName].Count.ShouldEqual(1);
        }