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 ProviderShouldThrowExceptionIfMetadataIsInvalid()
        {
            var provider        = new DynamicDataAnnotationsElementProvider(DisplayNameProvider);
            var validationClass = new InvalidaMetadataClass();

            ShouldThrow <MissingMemberException>(() => provider.GetValidationElements(validationClass));
        }
        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 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);
        }