public void MaxValueAttribute_ForInteger()
        {
            Decimal actual   = new MaxValueAttribute(10).Maximum;
            Decimal expected = 10M;

            Assert.Equal(expected, actual);
        }
Exemple #2
0
        public void MaxValueAttribute_SetsMaximum()
        {
            Decimal actual   = new MaxValueAttribute(12.56).Maximum;
            Decimal expected = 12.56M;

            Assert.Equal(expected, actual);
        }
        public override void ValidateProperty(SerializedProperty property)
        {
            MaxValueAttribute maxValueAttribute = PropertyUtility.GetAttributes <MaxValueAttribute>(property)[0];

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                if (property.intValue > maxValueAttribute.MaxValue)
                {
                    property.intValue = (int)maxValueAttribute.MaxValue;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                if (property.floatValue > maxValueAttribute.MaxValue)
                {
                    property.floatValue = maxValueAttribute.MaxValue;
                }
            }
            else
            {
                string warning = maxValueAttribute.GetType().Name + " can be used only on int or float fields";
                EditorGUILayout.HelpBox(warning, MessageType.Warning);
                Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property));
            }
        }
Exemple #4
0
        public void FormatErrorMessage_ForName()
        {
            attribute = new MaxValueAttribute(13.44);

            String expected = String.Format(Validations.MaxValue, "Sum", attribute.Maximum);
            String actual   = attribute.FormatErrorMessage("Sum");

            Assert.Equal(expected, actual);
        }
        public void FormatErrorMessage_FormatsErrorMessageForDouble()
        {
            attribute = new MaxValueAttribute(13.44);

            String expected = String.Format(Validations.FieldMustBeLessOrEqualTo, "Sum", attribute.Maximum);
            String actual   = attribute.FormatErrorMessage("Sum");

            Assert.Equal(expected, actual);
        }
Exemple #6
0
            public void LessThanOrEqualMax_ReturnsTrue(int value)
            {
                // Arrange
                var maxValueAttribute = new MaxValueAttribute(5);

                // Act
                var isValid = maxValueAttribute.IsValid(value);

                // Assert
                Assert.True(isValid);
            }
Exemple #7
0
            public void GreaterThanMax_ReturnsFalse()
            {
                // Arrange
                var maxValueAttribute = new MaxValueAttribute(5);
                var value             = 6;

                // Act
                var isValid = maxValueAttribute.IsValid(value);

                // Assert
                Assert.False(isValid);
            }
        public void MaxValueAttribute_IsValid_ValueIsSmallerThanMax_ShouldReturnTrue()
        {
            // arrange
            int maxValue  = 5;
            int value     = 4;
            var attribute = new MaxValueAttribute(maxValue);

            // act
            bool valid = attribute.IsValid(value);

            // assert
            Assert.IsTrue(valid);
        }
        public void MaxValueAttribute_IsValid_ValueIsHigherThanMax_ShouldReturnFalse()
        {
            // arrange
            int maxValue  = 5;
            int value     = 6;
            var attribute = new MaxValueAttribute(maxValue);

            // act
            bool valid = attribute.IsValid(value);

            // assert
            Assert.IsFalse(valid);
        }
Exemple #10
0
        public void GetClientValidationRules_ReturnsMaxRangeValidationRule()
        {
            ModelMetadata   metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(AdaptersModel), "MaxValue");
            MaxValueAdapter adapter  = new MaxValueAdapter(metadata, new ControllerContext(), new MaxValueAttribute(128));

            String expectedMessage           = new MaxValueAttribute(128).FormatErrorMessage(metadata.GetDisplayName());
            ModelClientValidationRule actual = adapter.GetClientValidationRules().Single();

            Assert.Equal(128M, actual.ValidationParameters["max"]);
            Assert.Equal(expectedMessage, actual.ErrorMessage);
            Assert.Equal("range", actual.ValidationType);
            Assert.Single(actual.ValidationParameters);
        }
Exemple #11
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        MaxValueAttribute maxAttrib = attribute as MaxValueAttribute;

        position = EditorGUI.PrefixLabel(position, label);
        if (property.propertyType == SerializedPropertyType.Float)
        {
            property.floatValue = Mathf.Min(maxAttrib.maxVal, EditorGUI.FloatField(position, property.floatValue));
        }
        else if (property.propertyType == SerializedPropertyType.Integer)
        {
            property.intValue = (int)Mathf.Min(maxAttrib.maxVal, EditorGUI.IntField(position, property.intValue));
        }
    }
        public override void ValidateProperty(SerializedProperty property)
        {
            if (_cachedMaxValueAttribute == null)
            {
                _cachedMaxValueAttribute = PropertyUtility.GetAttribute <MaxValueAttribute>(property);
            }

            MaxValueAttribute maxValueAttribute = _cachedMaxValueAttribute;

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                if (property.intValue > maxValueAttribute.MaxValue)
                {
                    property.intValue = (int)maxValueAttribute.MaxValue;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                if (property.floatValue > maxValueAttribute.MaxValue)
                {
                    property.floatValue = maxValueAttribute.MaxValue;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Vector2)
            {
                property.vector2Value = Vector2.Min(property.vector2Value, new Vector2(maxValueAttribute.MaxValue, maxValueAttribute.MaxValue));
            }
            else if (property.propertyType == SerializedPropertyType.Vector3)
            {
                property.vector3Value = Vector3.Min(property.vector3Value, new Vector3(maxValueAttribute.MaxValue, maxValueAttribute.MaxValue, maxValueAttribute.MaxValue));
            }
            else if (property.propertyType == SerializedPropertyType.Vector4)
            {
                property.vector4Value = Vector4.Min(property.vector4Value, new Vector4(maxValueAttribute.MaxValue, maxValueAttribute.MaxValue, maxValueAttribute.MaxValue, maxValueAttribute.MaxValue));
            }
            else if (property.propertyType == SerializedPropertyType.Vector2Int)
            {
                property.vector2IntValue = Vector2Int.Min(property.vector2IntValue, new Vector2Int((int)maxValueAttribute.MaxValue, (int)maxValueAttribute.MaxValue));
            }
            else if (property.propertyType == SerializedPropertyType.Vector3Int)
            {
                property.vector3IntValue = Vector3Int.Min(property.vector3IntValue, new Vector3Int((int)maxValueAttribute.MaxValue, (int)maxValueAttribute.MaxValue, (int)maxValueAttribute.MaxValue));
            }
            else
            {
                string warning = maxValueAttribute.GetType().Name + " can be used only on int, float, Vector or VectorInt fields";
                Debug.LogWarning(warning, property.serializedObject.targetObject);
            }
        }
        public void GetClientValidationRules_ReturnsMaxRangeValidationRule()
        {
            IServiceProvider       services = Substitute.For <IServiceProvider>();
            IModelMetadataProvider provider = new EmptyModelMetadataProvider();
            MaxValueAdapter        adapter  = new MaxValueAdapter(new MaxValueAttribute(128));
            ModelMetadata          metadata = provider.GetMetadataForProperty(typeof(AdaptersModel), "MaxValue");

            ClientModelValidationContext context = new ClientModelValidationContext(metadata, provider, services);
            ModelClientValidationRule    actual  = adapter.GetClientValidationRules(context).Single();
            String expectedMessage = new MaxValueAttribute(128).FormatErrorMessage("MaxValue");

            Assert.Equal(128M, actual.ValidationParameters["max"]);
            Assert.Equal(expectedMessage, actual.ErrorMessage);
            Assert.Equal("range", actual.ValidationType);
            Assert.Single(actual.ValidationParameters);
        }
Exemple #14
0
        public override void ValidateProperty(SerializedProperty property)
        {
            MaxValueAttribute maxValueAttribute = PropertyUtility.GetAttribute <MaxValueAttribute>(property);

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                if (property.intValue > maxValueAttribute.MaxValue)
                {
                    property.intValue = (int)maxValueAttribute.MaxValue;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                if (property.floatValue > maxValueAttribute.MaxValue)
                {
                    property.floatValue = maxValueAttribute.MaxValue;
                }
            }
            else
            {
                string warning = maxValueAttribute.GetType().Name + " can be used only on int or float fields";
                Debug.LogWarning(warning, property.serializedObject.targetObject);
            }
        }
        public override void ValidateProperty(SerializedProperty property, bool drawField)
        {
            MaxValueAttribute maxValueAttribute = PropertyUtility.GetAttribute <MaxValueAttribute>(property);

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                if (property.intValue > maxValueAttribute.MaxValue)
                {
                    property.intValue = (int)maxValueAttribute.MaxValue;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                if (property.floatValue > maxValueAttribute.MaxValue)
                {
                    property.floatValue = maxValueAttribute.MaxValue;
                }
            }
            else
            {
                string warning = maxValueAttribute.GetType().Name + " can be used only on int or float fields";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
            }
        }
        public override void ValidateProperty(SerializedProperty property)
        {
            MaxValueAttribute maxValueAttribute = PropertyUtility.GetAttribute <MaxValueAttribute>(property);

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                if (property.intValue > maxValueAttribute.MaxValue)
                {
                    property.intValue = (int)maxValueAttribute.MaxValue;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                if (property.floatValue > maxValueAttribute.MaxValue)
                {
                    property.floatValue = maxValueAttribute.MaxValue;
                }
            }
            else
            {
                string warning = maxValueAttribute.GetType().Name + " 只作用于整型和单精度浮点型字段!";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property), logToConsole: false);
            }
        }
Exemple #17
0
 public MaxValueAttributeTests()
 {
     attribute = new MaxValueAttribute(12.56);
 }
Exemple #18
0
 public void SetUp()
 {
     attribute = new MaxValueAttribute(12.56);
 }