public void ResolvesDuplicatedDynamicParameterName()
        {
            string[] parameters        = { "Name", "Location", "Mode" };
            string[] parameterSetNames = { "__AllParameterSets" };
            string   key = "Name";
            TemplateFileParameterV1 value = new TemplateFileParameterV1()
            {
                AllowedValues = new List <object>()
                {
                    "Mode1", "Mode2", "Mode3"
                },
                MaxLength = "5",
                MinLength = "1",
                Type      = "bool"
            };
            KeyValuePair <string, TemplateFileParameterV1> parameter = new KeyValuePair <string, TemplateFileParameterV1>(key, value);

            RuntimeDefinedParameter dynamicParameter = TemplateUtility.ConstructDynamicParameter(parameters, parameter);

            Assert.Equal(key + "FromTemplate", dynamicParameter.Name);
            Assert.Equal(value.DefaultValue, dynamicParameter.Value);
            Assert.Equal(typeof(bool), dynamicParameter.ParameterType);
            Assert.Equal(2, dynamicParameter.Attributes.Count);

            ParameterAttribute parameterAttribute = (ParameterAttribute)dynamicParameter.Attributes[0];

            Assert.True(parameterAttribute.Mandatory);
            Assert.True(parameterAttribute.ValueFromPipelineByPropertyName);
            Assert.Equal(parameterSetNames[0], parameterAttribute.ParameterSetName);

            ValidateLengthAttribute validateLengthAttribute = (ValidateLengthAttribute)dynamicParameter.Attributes[1];

            Assert.Equal(int.Parse(value.MinLength), validateLengthAttribute.MinLength);
            Assert.Equal(int.Parse(value.MaxLength), validateLengthAttribute.MaxLength);
        }
        public void IsValid_GetValidationResult_ReturnsValidationResultWhenValueDoesNotHaveSpecifiedLength(object value)
        {
            // Arrange
            ValidationContext       validationContext       = new ValidationContext(new { }, null, null);
            ValidateLengthAttribute validateLengthAttribute = new ValidateLengthAttribute(8, nameof(DummyStrings.Dummy), typeof(DummyStrings));

            // Act
            // IsValid is a protected function, the public function GetValidationResult calls it.
            ValidationResult validationResult = validateLengthAttribute.GetValidationResult(value, validationContext);

            // Assert
            Assert.Equal(DummyStrings.Dummy, validationResult.ErrorMessage);
        }
        public void IsValid_GetValidationResult_ReturnsNullWhenValueHasSpecifiedLengthOrIsNullOrAnEmptyString(object value)
        {
            // Arrange
            ValidationContext       validationContext       = new ValidationContext(new { }, null, null);
            ValidateLengthAttribute validateLengthAttribute = new ValidateLengthAttribute(8, nameof(DummyStrings.Dummy), typeof(DummyStrings));

            // Act
            // IsValid is a protected function, the public function GetValidationResult calls it.
            ValidationResult validationResult = validateLengthAttribute.GetValidationResult(value, validationContext);

            // Assert
            Assert.Null(validationResult);
        }
示例#4
0
        public void ConstructsDynamicParameter()
        {
            string[] parameters        = { "Name", "Location", "Mode" };
            string[] parameterSetNames = { "__AllParameterSets" };
            string   key = "computeMode";
            TemplateFileParameterV1 value = new TemplateFileParameterV1()
            {
                AllowedValues = new List <string>()
                {
                    "Mode1", "Mode2", "Mode3"
                },
                DefaultValue = "Mode1",
                MaxLength    = "5",
                MinLength    = "1",
                Type         = "string"
            };
            KeyValuePair <string, TemplateFileParameterV1> parameter = new KeyValuePair <string, TemplateFileParameterV1>(key, value);

            RuntimeDefinedParameter dynamicParameter = galleryTemplatesClient.ConstructDynamicParameter(parameters, parameter);

            Assert.Equal("computeMode", dynamicParameter.Name);
            Assert.Equal(value.DefaultValue, dynamicParameter.Value);
            Assert.Equal(typeof(string), dynamicParameter.ParameterType);
            Assert.Equal(3, dynamicParameter.Attributes.Count);

            ParameterAttribute parameterAttribute = (ParameterAttribute)dynamicParameter.Attributes[0];

            Assert.False(parameterAttribute.Mandatory);
            Assert.True(parameterAttribute.ValueFromPipelineByPropertyName);
            Assert.Equal(parameterSetNames[0], parameterAttribute.ParameterSetName);

            ValidateSetAttribute validateSetAttribute = (ValidateSetAttribute)dynamicParameter.Attributes[1];

            Assert.Equal(3, validateSetAttribute.ValidValues.Count);
            Assert.True(validateSetAttribute.IgnoreCase);
            Assert.True(value.AllowedValues.Contains(validateSetAttribute.ValidValues[0]));
            Assert.True(value.AllowedValues.Contains(validateSetAttribute.ValidValues[1]));
            Assert.True(value.AllowedValues.Contains(validateSetAttribute.ValidValues[2]));
            Assert.False(validateSetAttribute.ValidValues[0].Contains(' '));
            Assert.False(validateSetAttribute.ValidValues[1].Contains(' '));
            Assert.False(validateSetAttribute.ValidValues[2].Contains(' '));

            ValidateLengthAttribute validateLengthAttribute = (ValidateLengthAttribute)dynamicParameter.Attributes[2];

            Assert.Equal(int.Parse(value.MinLength), validateLengthAttribute.MinLength);
            Assert.Equal(int.Parse(value.MaxLength), validateLengthAttribute.MaxLength);
        }
        public void TestValidateLengthAttribute()
        {
            var attribute = new ValidateLengthAttribute();

            Assert.AreEqual(0, attribute.Min);
            Assert.AreEqual(Int32.MaxValue, attribute.Max);
            Assert.AreEqual(0, attribute.Is);
            Assert.IsFalse(attribute.IsRequiredLengthMode);
            Assert.IsNull(attribute.Within);
            Assert.IsNull(attribute.TooLong);
            Assert.IsNull(attribute.TooShort);
            Assert.IsNull(attribute.WrongLength);
        }
示例#6
0
        public void CanGenerateValidators()
        {
            Type         type     = Assembly.GetExecutingAssembly().GetType("Debugging.Tests.ClassWithProperties");
            PropertyInfo property = type.GetProperty("PropertyWithValidators");

            ValidateCreditCardAttribute ccAttribute = property.GetCustomAttributes(typeof(ValidateCreditCardAttribute), false)[0] as ValidateCreditCardAttribute;

            Assert.IsNotNull(ccAttribute);

            ValidateEmailAttribute emailAttribute = property.GetCustomAttributes(typeof(ValidateEmailAttribute), false)[0] as ValidateEmailAttribute;

            Assert.IsNotNull(emailAttribute);

            ValidateRegExpAttribute regExpAttribute = property.GetCustomAttributes(typeof(ValidateRegExpAttribute), false)[0] as ValidateRegExpAttribute;

            Assert.IsNotNull(regExpAttribute);

            ValidateLengthAttribute lengthAttribute = property.GetCustomAttributes(typeof(ValidateLengthAttribute), false)[0] as ValidateLengthAttribute;

            Assert.IsNotNull(lengthAttribute);

            ValidateDateAttribute dateAttribute = property.GetCustomAttributes(typeof(ValidateDateAttribute), false)[0] as ValidateDateAttribute;

            Assert.IsNotNull(dateAttribute);

            ValidateDateTimeAttribute dateTimeAttribute = property.GetCustomAttributes(typeof(ValidateDateTimeAttribute), false)[0] as ValidateDateTimeAttribute;

            Assert.IsNotNull(dateTimeAttribute);

            ValidateDecimalAttribute decimalAttribute = property.GetCustomAttributes(typeof(ValidateDecimalAttribute), false)[0] as ValidateDecimalAttribute;

            Assert.IsNotNull(decimalAttribute);

            ValidateDoubleAttribute doubleAttribute = property.GetCustomAttributes(typeof(ValidateDoubleAttribute), false)[0] as ValidateDoubleAttribute;

            Assert.IsNotNull(doubleAttribute);

            ValidateIntegerAttribute integerAttribute = property.GetCustomAttributes(typeof(ValidateIntegerAttribute), false)[0] as ValidateIntegerAttribute;

            Assert.IsNotNull(integerAttribute);

            ValidateNonEmptyAttribute nonEmptyAttribute = property.GetCustomAttributes(typeof(ValidateNonEmptyAttribute), false)[0] as ValidateNonEmptyAttribute;

            Assert.IsNotNull(nonEmptyAttribute);

            ValidateRangeAttribute rangeAttribute = property.GetCustomAttributes(typeof(ValidateRangeAttribute), false)[0] as ValidateRangeAttribute;

            Assert.IsNotNull(rangeAttribute);

            ValidateSameAsAttribute sameAsAttribute = property.GetCustomAttributes(typeof(ValidateSameAsAttribute), false)[0] as ValidateSameAsAttribute;

            Assert.IsNotNull(sameAsAttribute);

            ValidateSetAttribute setAttribute = property.GetCustomAttributes(typeof(ValidateSetAttribute), false)[0] as ValidateSetAttribute;

            Assert.IsNotNull(setAttribute);

            ValidateSingleAttribute singleAttribute = property.GetCustomAttributes(typeof(ValidateSingleAttribute), false)[0] as ValidateSingleAttribute;

            Assert.IsNotNull(singleAttribute);
        }