public static void Validate_DoesNotThrow(DataType dataType)
 {
     if (dataType != DataType.Custom)
     {
         DataTypeAttribute attribute = new DataTypeAttribute(dataType);
         attribute.Validate(new object(), s_testValidationContext);
     }
 }
 public static void GetDataTypeName_ReturnsExpectedName(DataType dataType)
 {
     if (dataType != DataType.Custom)
     {
         DataTypeAttribute attribute = new DataTypeAttribute(dataType);
         Assert.Equal(Enum.GetName(typeof(DataType), dataType), attribute.GetDataTypeName());
     }
 }
        private static IDataTypeFacet Create(DataTypeAttribute attribute, ISpecification holder) {
            if (attribute == null) {
                return null;
            }

            return attribute.DataType == DataType.Custom ? new DataTypeFacetAnnotation(attribute.CustomDataType, holder) :
                new DataTypeFacetAnnotation(attribute.DataType, holder);
        }
Esempio n. 4
0
 public static void GetDataTypeName_and_IsValid_on_empty_custom_DataTypeAttribute_throws_exception()
 {
     var attribute = new DataTypeAttribute(string.Empty);
     Assert.Equal(DataType.Custom, attribute.DataType); // Only throw when call GetDataTypeName() or Validate()
     AssertEx.Empty(attribute.CustomDataType); // Only throw when call GetDataTypeName() or Validate()
     Assert.Throws<InvalidOperationException>(() => attribute.GetDataTypeName());
     Assert.Throws<InvalidOperationException>(() => attribute.Validate(new object(), s_testValidationContext));
 }
        public static void Ctor_DataType(DataType dataType)
        {
            DataTypeAttribute attribute = new DataTypeAttribute(dataType);
            Assert.Equal(dataType, attribute.DataType);
            Assert.Null(attribute.CustomDataType);

            bool expectedNull = dataType != DataType.Date && dataType != DataType.Time && dataType != DataType.Currency;
            Assert.Equal(expectedNull, attribute.DisplayFormat == null);
        }
 public DataTypeAttributeAdapter(ModelMetadata metadata, ControllerContext context, DataTypeAttribute attribute, string ruleName)
     : base(metadata, context, attribute)
 {
     if (String.IsNullOrEmpty(ruleName))
     {
         throw new ArgumentException(MvcResources.Common_NullOrEmpty, "ruleName");
     }
     RuleName = ruleName;
 }
 public DataTypeAttributeAdapter(DataTypeAttribute attribute,
                                 [NotNull] string ruleName)
     : base(attribute)
 {
     if (string.IsNullOrEmpty(ruleName))
     {
         throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "ruleName");
     }
     RuleName = ruleName;
 }
        private static IDateOnlyFacet Create(DataTypeAttribute attribute, ConcurrencyCheckAttribute concurrencyCheckAttribute, ISpecification holder) {

            if (attribute?.DataType == DataType.Date) {
                return new DateOnlyFacet(holder);
            }

            if (concurrencyCheckAttribute != null) {
                return null;
            }

            return attribute?.DataType == DataType.DateTime ? null : new DateOnlyFacet(holder);
        }
Esempio n. 9
0
 public static void GetDataTypeName_and_Validate_successful_for_all_non_custom_DataTypes()
 {
     foreach (var enumValue in Enum.GetValues(typeof(DataType)))
     {
         var dataType = (DataType)enumValue;
         if (DataType.Custom != dataType)
         {
             var attribute = new DataTypeAttribute(dataType);
             Assert.Equal(Enum.GetName(typeof(DataType), enumValue), attribute.GetDataTypeName());
             AssertEx.DoesNotThrow(() => attribute.Validate(new object(), s_testValidationContext));
         }
     }
 }
Esempio n. 10
0
 public static void DataType_and_CustomDataType_assigned_correctly_for_all_non_custom_DataTypes()
 {
     foreach (var enumValue in Enum.GetValues(typeof(DataType)))
     {
         var dataType = (DataType)enumValue;
         if (DataType.Custom != dataType)
         {
             var attribute = new DataTypeAttribute(dataType);
             Assert.Equal(dataType, attribute.DataType);
             Assert.Null(attribute.CustomDataType);
         }
     }
 }
 public TypePropertyValidationRuleMetadata(DataTypeAttribute attribute)
     : this((ValidationAttribute) attribute)
 {
     switch (attribute.DataType) {
         case DataType.EmailAddress:
             Name = "email";
             break;
         case DataType.Url:
         default:
             Name = "url";
             break;
     }
     Value1 = true;
     _type = "boolean";
 }
        public static void Ctor_String(string customDataType)
        {
            DataTypeAttribute attribute = new DataTypeAttribute(customDataType);
            Assert.Equal(DataType.Custom, attribute.DataType);
            Assert.Equal(customDataType, attribute.CustomDataType);

            if (string.IsNullOrEmpty(customDataType))
            {
                Assert.Throws<InvalidOperationException>(() => attribute.GetDataTypeName());
                Assert.Throws<InvalidOperationException>(() => attribute.Validate(new object(), s_testValidationContext));
            }
            else
            {
                Assert.Equal(customDataType, attribute.GetDataTypeName());
                attribute.Validate(new object(), s_testValidationContext);
            }
        }
Esempio n. 13
0
        public static void DisplayFormat_set_correctly_for_date_time_and_currency()
        {
            var dateAttribute = new DataTypeAttribute(DataType.Date);
            Assert.NotNull(dateAttribute.DisplayFormat);
            Assert.Equal("{0:d}", dateAttribute.DisplayFormat.DataFormatString);
            Assert.True(dateAttribute.DisplayFormat.ApplyFormatInEditMode);

            var timeAttribute = new DataTypeAttribute(DataType.Time);
            Assert.NotNull(timeAttribute.DisplayFormat);
            Assert.Equal("{0:t}", timeAttribute.DisplayFormat.DataFormatString);
            Assert.True(timeAttribute.DisplayFormat.ApplyFormatInEditMode);

            var currencyAttribute = new DataTypeAttribute(DataType.Currency);
            Assert.NotNull(currencyAttribute.DisplayFormat);
            Assert.Equal("{0:C}", currencyAttribute.DisplayFormat.DataFormatString);
            Assert.False(currencyAttribute.DisplayFormat.ApplyFormatInEditMode);
        }
        public void GetDisplayMetadata_FindsDisplayFormat_FromDataType()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var dataType = new DataTypeAttribute(DataType.Currency);
            var displayFormat = dataType.DisplayFormat; // Non-null for DataType.Currency.

            var attributes = new[] { dataType, };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString);
        }
        private static void SetFromDataTypeAndDisplayAttributes(DataAnnotationsModelMetadata result,
            DataTypeAttribute dataTypeAttribute, DisplayFormatAttribute displayFormatAttribute)
        {
            if (dataTypeAttribute != null)
            {
                result.DataTypeName = dataTypeAttribute.ToDataTypeName();
            }

            if (displayFormatAttribute == null && dataTypeAttribute != null)
            {
                displayFormatAttribute = dataTypeAttribute.DisplayFormat;

                // If DisplayFormat value was non-null and this [DataType] is of a subclass, assume the [DataType]
                // constructor used the protected DisplayFormat setter to override its default. Note deriving from
                // [DataType] but preserving DataFormatString and ApplyFormatInEditMode results in
                // HasNonDefaultEditFormat==true.
                if (displayFormatAttribute != null && dataTypeAttribute.GetType() != typeof(DataTypeAttribute))
                {
                    result.HasNonDefaultEditFormat = true;
                }
            }
            else if (displayFormatAttribute != null)
            {
                result.HasNonDefaultEditFormat = true;
            }

            if (displayFormatAttribute != null)
            {
                result.NullDisplayText = displayFormatAttribute.NullDisplayText;
                result.DisplayFormatString = displayFormatAttribute.DataFormatString;
                result.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;
                result.HtmlEncode = displayFormatAttribute.HtmlEncode;

                if (displayFormatAttribute.ApplyFormatInEditMode)
                {
                    result.EditFormatString = displayFormatAttribute.DataFormatString;
                }

                if (!displayFormatAttribute.HtmlEncode && String.IsNullOrWhiteSpace(result.DataTypeName))
                {
                    result.DataTypeName = DataTypeUtil.HtmlTypeName;
                }

                // Regardless of HasNonDefaultEditFormat calculation above, treat missing EditFormatString as the
                // default.  Note the corner case of a [DataType] subclass overriding a non-empty default to apply a
                // [DisplayFormat] lacking DataFormatString or with ApplyFormatInEditMode==false results in
                // HasNonDefaultEditFormat==false.
                if (String.IsNullOrEmpty(result.EditFormatString))
                {
                    result.HasNonDefaultEditFormat = false;
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="LocalizedDataTypeAttributeAdapter"/> class.
 /// </summary>
 /// <param name="metadata">The model metadata.</param>
 /// <param name="context">The controller context.</param>
 /// <param name="attribute">The <see cref="DataTypeAttribute"/> attribute.</param>
 /// <param name="ruleName">The name of the client validation rule.</param>
 public LocalizedDataTypeAttributeAdapter(ModelMetadata metadata, ControllerContext context, DataTypeAttribute attribute, string ruleName) : base(metadata, context, attribute)
 {
     mRuleName = ruleName;
 }
 public static void GetDataTypeName_InvalidDataType_ThrowsIndexOutOfRangeException(DataType dataType)
 {
     DataTypeAttribute attribute = new DataTypeAttribute(dataType);
     Assert.Throws<IndexOutOfRangeException>(() => attribute.GetDataTypeName());
 }
Esempio n. 18
0
        private ValidationAttribute CopyAttribute(ValidationAttribute attribute)
        {
            ValidationAttribute result = null;

            if (attribute is RangeAttribute)
            {
                var attr = (RangeAttribute)attribute;
                result = (attr.Minimum is double)
                    ?  new RangeAttribute((double)attr.Minimum, (double)attr.Maximum)
                    :  new RangeAttribute((int)attr.Minimum, (int)attr.Maximum);
            }

            if (attribute is RegularExpressionAttribute)
            {
                var attr = (RegularExpressionAttribute)attribute;
                result = new RegularExpressionAttribute(attr.Pattern);
            }

            if (attribute is RequiredAttribute)
                result = new RequiredAttribute();

            if (attribute is StringLengthAttribute)
            {
                var attr = (StringLengthAttribute)attribute;
                result = new StringLengthAttribute(attr.MaximumLength)
                {
                    MinimumLength = attr.MinimumLength
                };
            }

            if (attribute is DA.CompareAttribute)
            {
                var attr = (DA.CompareAttribute)attribute;
                result = new DA.CompareAttribute(attr.OtherProperty);
            }

            if (attribute is DataTypeAttribute)
            {
                var attr = (DataTypeAttribute)attribute;
                result = new DataTypeAttribute(attr.DataType);
            }

            if (result == null && attribute.GetType().GetInterfaces().Contains(typeof(ICloneable)))
                result = ((ICloneable)attribute).Clone() as ValidationAttribute;

            return result;
        }
        public void DataTypeName_AttributesHaveExpectedPrecedence()
        {
            // Arrange
            var expected = "MultilineText";
            var dataType = new DataTypeAttribute(DataType.MultilineText);
            var displayFormat = new DisplayFormatAttribute { HtmlEncode = false, };
            var provider = CreateProvider(new object[] { dataType, displayFormat });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.DataTypeName;

            // Assert
            Assert.Equal(expected, result);
        }
Esempio n. 20
0
 public static void DataType_and_CustomDataType_assigned_correctly_for_custom_DataType()
 {
     var attribute = new DataTypeAttribute("CustomValue");
     Assert.Equal(DataType.Custom, attribute.DataType);
     Assert.Equal("CustomValue", attribute.CustomDataType);
 }
 private static IPasswordFacet Create(DataTypeAttribute attribute, IFacetHolder holder) {
     return attribute != null && attribute.DataType == DataType.Password ? new PasswordFacetAnnotation(holder) : null;
 }
        public void GetDisplayMetadata_FindsDisplayFormat_OverridingDataType()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var dataType = new DataTypeAttribute(DataType.Time); // Has a non-null DisplayFormat.
            var displayFormat = new DisplayFormatAttribute() // But these values override the values from DataType
            {
                DataFormatString = "Cool {0}",
            };

            var attributes = new Attribute[] { dataType, displayFormat, };
            var key = ModelMetadataIdentity.ForType(typeof(string));
            var context = new DisplayMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.GetDisplayMetadata(context);

            // Assert
            Assert.Same(displayFormat.DataFormatString, context.DisplayMetadata.DisplayFormatString);
        }
Esempio n. 23
0
 public static void DisplayFormat_null_for_non_date_time_and_currency()
 {
     foreach (var enumValue in Enum.GetValues(typeof(DataType)))
     {
         var dataType = (DataType)enumValue;
         if (DataType.Date != dataType
             && DataType.Time != dataType
             && DataType.Currency != dataType)
         {
             var attribute = new DataTypeAttribute(dataType);
             Assert.Null(attribute.DisplayFormat);
         }
     }
 }
Esempio n. 24
0
 //: this(attribute)
 /// <summary>
 /// Initializes a new instance of the <see cref="TypePropertyValidationRuleMetadata" /> class.
 /// </summary>
 /// <param name="attribute">The attribute.</param>
 public TypePropertyValidationRuleMetadata(DataTypeAttribute attribute)
 {
     switch (attribute.DataType)
     {
         case DataType.EmailAddress:
             this.Name = "email";
             break;
         case DataType.Url:
             this.Name = "url";
             break;
     }
     this.Value1 = true;
     this.type = "boolean";
 }
 public static void DisplayFormat_ReturnsExpected(DataType dataType, string dataFormatString, bool applyFormatInEditMode)
 {
     DataTypeAttribute attribute = new DataTypeAttribute(dataType);
     Assert.Equal(dataFormatString, attribute.DisplayFormat.DataFormatString);
     Assert.Equal(applyFormatInEditMode, attribute.DisplayFormat.ApplyFormatInEditMode);
 }
        public void EditFormatString_AttributesHaveExpectedPrecedence()
        {
            // Arrange
            var expected = "custom format";
            var dataType = new DataTypeAttribute(DataType.Currency);
            var displayFormat = new DisplayFormatAttribute
            {
                ApplyFormatInEditMode = true,
                DataFormatString = expected,
            };

            var provider = CreateProvider(new object[] { displayFormat, dataType, });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.EditFormatString;

            // Assert
            Assert.Equal(expected, result);
        }
Esempio n. 27
0
 public static void GetDataTypeName_and_IsValid_on_non_null_custom_DataTypeAttribute_is_successful()
 {
     var attribute = new DataTypeAttribute("TestCustomDataType");
     Assert.Equal("TestCustomDataType", attribute.GetDataTypeName());
     AssertEx.DoesNotThrow(() => attribute.Validate(new object(), s_testValidationContext));
 }