public void ValidatorShouldThrowExceptionIfMetadataIsInvalid()
        {
            DataAnnotationValidatior.GetValidationElementsDelegate = null;
            var validationClass = new InvalidaMetadataClass();

            ShouldThrow <MissingMemberException>(() => DataAnnotationValidatior.GetValidationElements(validationClass));
        }
        public void ValidatorShouldFindAllElements()
        {
            const int count       = 2;
            const int objectCount = 1;

            DataAnnotationValidatior.GetValidationElementsDelegate = null;
            var validationClass = new SpyValidationClass();
            var elements        = DataAnnotationValidatior.GetValidationElements(validationClass);

            elements.Count.ShouldEqual(count);
            elements[string.Empty].Count.ShouldEqual(objectCount);
            elements[SpyPropertyName].Count.ShouldEqual(1);
        }
        public void ProviderShouldValidateUsingDataAnnotationAttributesWithToolkitMetadata()
        {
            var result       = new ValidationResult("error", new[] { DisplayName });
            var dictionary   = new Dictionary <object, object>();
            var propertyInfo = typeof(MetadataValidationClassToolkit).GetPropertyEx(SpyPropertyName);

            DisplayNameProvider.GetNameDelegate = info =>
            {
                info.ShouldEqual(propertyInfo);
                return(DisplayName + propertyInfo.Name);
            };
            DataAnnotationValidatior.GetValidationElementsDelegate = null;
            DataAnnotationValidatior.ElementsCache.Clear();
            DataAnnotationValidatior.DisplayNameProvider = null;
            var validationClass = new MetadataValidationClassToolkit {
                SpyProperty = "Test"
            };
            var validationContext = new DataAnnotationValidatior.ValidationContext(validationClass, IocContainer, dictionary);

            SpyValidationAttribute.IsValidDelegate = (o, context) =>
            {
                o.ShouldEqual(validationClass.SpyProperty);
                context.DisplayName.ShouldEqual(DisplayName + propertyInfo.Name);
                context.Items[DataAnnotationValidatior.ServiceProviderKey].ShouldEqual(IocContainer);
                context.MemberName.ShouldEqual(SpyPropertyName);
                return(result);
            };

            var validationElements = DataAnnotationValidatior.GetValidationElements(validationClass)[SpyPropertyName];

            validationElements.Count.ShouldEqual(2);

            var validationResults = validationElements
                                    .SelectMany(element => element.Validate(validationContext))
                                    .ToArray();

            validationResults.Length.ShouldEqual(1);
            var validationResult = validationResults.OfType <ValidationResult>().Single();

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

            DisplayNameProvider.GetNameDelegate = info =>
            {
#if NETFX_CORE
                info.ShouldEqual(typeof(SpyValidationClass).GetTypeInfo());
#else
                info.ShouldEqual(typeof(SpyValidationClass));
#endif
                return(DisplayName);
            };

            DataAnnotationValidatior.GetValidationElementsDelegate = null;
            DataAnnotationValidatior.ElementsCache.Clear();
            DataAnnotationValidatior.DisplayNameProvider = null;
            var validationClass = new SpyValidationClass
            {
                ValidateSystem = context =>
                {
                    context.DisplayName.ShouldEqual(DisplayName);
                    context.Items[DataAnnotationValidatior.ServiceProviderKey].ShouldEqual(IocContainer);
                    context.MemberName.ShouldBeNull();
                    return(new[] { result });
                }
            };

            var validationContext = new DataAnnotationValidatior.ValidationContext(validationClass, IocContainer, dictionary);
            var elements          = DataAnnotationValidatior.GetValidationElements(validationClass);
            var validationResults = new List <object>();
            elements[string.Empty].ForEach(element => validationResults.AddRange(element.Validate(validationContext)));

            var validationResult = validationResults.OfType <ValidationResult>().Single();
            validationResult.MemberNames.SequenceEqual(result.MemberNames).ShouldBeTrue();
            validationResult.ErrorMessage.ShouldEqual(result.ErrorMessage);
        }