public void GetValidators_ReturnsExpectedValidatorTypes() {
            // Arrange
            ClientDataTypeModelValidatorProvider validatorProvider = new ClientDataTypeModelValidatorProvider();

            Action<string, bool> checkPropertyIsNumeric = (propertyName, shouldBeNumeric) => {
                ModelValidator[] validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForProperty(null, typeof(SampleModel), propertyName), new ControllerContext()).ToArray();

                bool isNumeric = validators.OfType<ClientDataTypeModelValidatorProvider.NumericModelValidator>().Any();
                if (isNumeric && !shouldBeNumeric) {
                    Assert.Fail("Property '{0}' shouldn't have returned a numeric validator but did.", propertyName);
                }
                else if (!isNumeric && shouldBeNumeric) {
                    Assert.Fail("Property '{0}' should've returned a numeric validator but didn't.", propertyName);
                }
            };

            // Act & assert
            checkPropertyIsNumeric("Byte", true);
            checkPropertyIsNumeric("SByte", true);
            checkPropertyIsNumeric("Int16", true);
            checkPropertyIsNumeric("UInt16", true);
            checkPropertyIsNumeric("Int32", true);
            checkPropertyIsNumeric("UInt32", true);
            checkPropertyIsNumeric("Int64", true);
            checkPropertyIsNumeric("UInt64", true);
            checkPropertyIsNumeric("Single", true);
            checkPropertyIsNumeric("Double", true);
            checkPropertyIsNumeric("Decimal", true);
            checkPropertyIsNumeric("NullableInt32", true);
            checkPropertyIsNumeric("String", false);
            checkPropertyIsNumeric("Object", false);
        }
        public void GetValidators_ThrowsIfMetadataIsNull() {
            // Arrange
            ClientDataTypeModelValidatorProvider validatorProvider = new ClientDataTypeModelValidatorProvider();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    validatorProvider.GetValidators(null, new ControllerContext());
                }, "metadata");
        }
        public void GetValidators_ThrowsIfContextIsNull() {
            // Arrange
            ClientDataTypeModelValidatorProvider validatorProvider = new ClientDataTypeModelValidatorProvider();
            ModelMetadata metadata = _metadataProvider.GetMetadataForType(null, typeof(SampleModel));

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    validatorProvider.GetValidators(metadata, null);
                }, "context");
        }
        public void RegisterDataTypeValidator_RemovesClientDataTypeModelValidatorProvider()
        {
            ClientDataTypeModelValidatorProvider provider = new ClientDataTypeModelValidatorProvider();
            ModelValidatorProviders.Providers.Add(provider);

            application.RegisterDataTypeValidator();

            Assert.False(ModelValidatorProviders.Providers.Contains(provider));
        }
        public void NumericValidator_Validate_DoesNotReadPropertyValue() {
            // Arrange
            ObservableModel model = new ObservableModel();
            ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => model.TheProperty, typeof(ObservableModel), "TheProperty");
            ControllerContext controllerContext = new ControllerContext();

            // Act
            ModelValidator[] validators = new ClientDataTypeModelValidatorProvider().GetValidators(metadata, controllerContext).ToArray();
            ModelValidationResult[] results = validators.SelectMany(o => o.Validate(model)).ToArray();

            // Assert
            CollectionAssert.AreEqual(new Type[] { typeof(ClientDataTypeModelValidatorProvider.NumericModelValidator) }, Array.ConvertAll(validators, o => o.GetType()), "Provider did not return expected validator.");
            Assert.AreEqual(0, results.Length);
            Assert.IsFalse(model.PropertyWasRead(), "Property should not have been read by provider or validator.");
        }