public override void ValidateProperty(SerializedProperty property) { MinValueAttribute minValueAttribute = PropertyUtility.GetAttribute <MinValueAttribute>(property); if (property.propertyType == SerializedPropertyType.Integer) { if (property.intValue < minValueAttribute.MinValue) { property.intValue = (int)minValueAttribute.MinValue; } } else if (property.propertyType == SerializedPropertyType.Float) { if (property.floatValue < minValueAttribute.MinValue) { property.floatValue = minValueAttribute.MinValue; } } else { string warning = minValueAttribute.GetType().Name + " can be used only on int or float fields"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property)); } }
public void MinValueAttribute_SetsMinimum() { Decimal actual = new MinValueAttribute(12.56).Minimum; Decimal expected = 12.56M; Assert.Equal(expected, actual); }
public void MinValueAttribute_SetsMinimumFromInteger() { Decimal actual = new MinValueAttribute(10).Minimum; Decimal expected = 10M; Assert.Equal(expected, actual); }
public void FormatErrorMessage_FormatsErrorMessageForDouble() { attribute = new MinValueAttribute(12.56); String expected = String.Format(Validations.FieldMustBeGreaterOrEqualTo, "Sum", attribute.Minimum); String actual = attribute.FormatErrorMessage("Sum"); Assert.Equal(expected, actual); }
public void FormatErrorMessage_ForName() { attribute = new MinValueAttribute(12.56); String expected = String.Format(Validations.MinValue, "Sum", attribute.Minimum); String actual = attribute.FormatErrorMessage("Sum"); Assert.Equal(expected, actual); }
public void GreaterThanOrEqualMin_ReturnsTrue(int value) { // Arrange var minValueAttribute = new MinValueAttribute(5); // Act var isValid = minValueAttribute.IsValid(value); // Assert Assert.True(isValid); }
public void LessThanMin_ReturnsFalse() { // Arrange var minValueAttribute = new MinValueAttribute(5); var value = -1; // Act var isValid = minValueAttribute.IsValid(value); // Assert Assert.False(isValid); }
public void MinValueAttribute_IsValid_ValueIsSmallerThanMax_ShouldReturnFalse() { // arrange int minValue = 5; int value = 4; var attribute = new MinValueAttribute(minValue); // act bool valid = attribute.IsValid(value); // assert Assert.IsFalse(valid); }
public void GetClientValidationRules_ReturnsMinRangeValidationRule() { ModelMetadata metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(AdaptersModel), "MinValue"); MinValueAdapter adapter = new MinValueAdapter(metadata, new ControllerContext(), new MinValueAttribute(128)); String expectedMessage = new MinValueAttribute(128).FormatErrorMessage(metadata.GetDisplayName()); ModelClientValidationRule actual = adapter.GetClientValidationRules().Single(); Assert.Equal(128M, actual.ValidationParameters["min"]); Assert.Equal(expectedMessage, actual.ErrorMessage); Assert.Equal("range", actual.ValidationType); Assert.Single(actual.ValidationParameters); }
public void MinValueAttribute_IsValid_ValueIsHigherThanMin_ShouldReturnTrue() { // arrange int minValue = 5; int value = 6; var attribute = new MinValueAttribute(minValue); // act bool valid = attribute.IsValid(value); // assert Assert.IsTrue(valid); }
public override void ValidateProperty(SerializedProperty property) { if (_cachedMinValueAttribute == null) { _cachedMinValueAttribute = PropertyUtility.GetAttribute <MinValueAttribute>(property); } MinValueAttribute minValueAttribute = _cachedMinValueAttribute; if (property.propertyType == SerializedPropertyType.Integer) { if (property.intValue < minValueAttribute.MinValue) { property.intValue = (int)minValueAttribute.MinValue; } } else if (property.propertyType == SerializedPropertyType.Float) { if (property.floatValue < minValueAttribute.MinValue) { property.floatValue = minValueAttribute.MinValue; } } else if (property.propertyType == SerializedPropertyType.Vector2) { property.vector2Value = Vector2.Max(property.vector2Value, new Vector2(minValueAttribute.MinValue, minValueAttribute.MinValue)); } else if (property.propertyType == SerializedPropertyType.Vector3) { property.vector3Value = Vector3.Max(property.vector3Value, new Vector3(minValueAttribute.MinValue, minValueAttribute.MinValue, minValueAttribute.MinValue)); } else if (property.propertyType == SerializedPropertyType.Vector4) { property.vector4Value = Vector4.Max(property.vector4Value, new Vector4(minValueAttribute.MinValue, minValueAttribute.MinValue, minValueAttribute.MinValue, minValueAttribute.MinValue)); } else if (property.propertyType == SerializedPropertyType.Vector2Int) { property.vector2IntValue = Vector2Int.Max(property.vector2IntValue, new Vector2Int((int)minValueAttribute.MinValue, (int)minValueAttribute.MinValue)); } else if (property.propertyType == SerializedPropertyType.Vector3Int) { property.vector3IntValue = Vector3Int.Max(property.vector3IntValue, new Vector3Int((int)minValueAttribute.MinValue, (int)minValueAttribute.MinValue, (int)minValueAttribute.MinValue)); } else { string warning = minValueAttribute.GetType().Name + " can be used only on int, float, Vector or VectorInt fields"; Debug.LogWarning(warning, property.serializedObject.targetObject); } }
public void GetClientValidationRules_ReturnsMinRangeValidationRule() { IServiceProvider services = Substitute.For <IServiceProvider>(); IModelMetadataProvider provider = new EmptyModelMetadataProvider(); MinValueAdapter adapter = new MinValueAdapter(new MinValueAttribute(128)); ModelMetadata metadata = provider.GetMetadataForProperty(typeof(AdaptersModel), "MinValue"); ClientModelValidationContext context = new ClientModelValidationContext(metadata, provider, services); ModelClientValidationRule actual = adapter.GetClientValidationRules(context).Single(); String expectedMessage = new MinValueAttribute(128).FormatErrorMessage("MinValue"); Assert.Equal(128M, actual.ValidationParameters["min"]); Assert.Equal(expectedMessage, actual.ErrorMessage); Assert.Equal("range", actual.ValidationType); Assert.Single(actual.ValidationParameters); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (property.propertyType != SerializedPropertyType.Float && property.propertyType != SerializedPropertyType.Integer) { EditorGUI.LabelField(position, label.text, "Use MinValue with float or int."); return; } EditorGUI.PropertyField(position, property, label); if (GUI.changed) { MinValueAttribute minValue = attribute as MinValueAttribute; property.floatValue = Mathf.Max(property.floatValue, minValue.min); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { MinValueAttribute minAttrib = attribute as MinValueAttribute; switch (property.propertyType) { case SerializedPropertyType.Float: position = EditorGUI.PrefixLabel(position, label); property.floatValue = Mathf.Max(minAttrib.minVal, EditorGUI.FloatField(position, property.floatValue)); break; case SerializedPropertyType.Integer: position = EditorGUI.PrefixLabel(position, label); property.intValue = (int)Mathf.Max(minAttrib.minVal, EditorGUI.IntField(position, property.intValue)); break; case SerializedPropertyType.Vector2: Vector2 v2 = EditorGUI.Vector2Field(position, label, property.vector2Value); property.vector3Value = Vector2.Max(Vector2.one * minAttrib.minVal, v2); expand = position.width < 310; break; case SerializedPropertyType.Vector2Int: Vector2Int v2Int = EditorGUI.Vector2IntField(position, label, property.vector2IntValue); property.vector2IntValue = Vector2Int.Max(Vector2Int.one * (int)minAttrib.minVal, v2Int); expand = position.width < 312; break; case SerializedPropertyType.Vector3: Vector3 v3 = EditorGUI.Vector3Field(position, label, property.vector3Value); property.vector3Value = Vector3.Max(Vector2.one * minAttrib.minVal, v3); expand = position.width < 312; break; case SerializedPropertyType.Vector3Int: Vector3Int v3Int = EditorGUI.Vector3IntField(position, label, property.vector3IntValue); property.vector3IntValue = Vector3Int.Max(Vector3Int.one * (int)minAttrib.minVal, v3Int); expand = position.width < 312; break; } }
void BoxHandles(FiringZone zone, ref Vector3 scale, Vector3 compare, bool stayBigger, string propertyName) { float size = 0.025f; Handles.color = Color.green; MinValueAttribute minAttrib = zone.GetType().GetField(propertyName).GetCustomAttributes(typeof(MinValueAttribute), true)[0] as MinValueAttribute; for (int i = 0; i < FiringZone.cubeVerticies.Length; i++) { EditorGUI.BeginChangeCheck(); Vector3 vec = Handles.FreeMoveHandle( zone.transform.position + 0.5f * Vector3.Scale(zone.transform.localScale, Vector3.Scale(scale, FiringZone.cubeVerticies[i])), zone.transform.rotation, size, Vector3.one, Handles.DotHandleCap); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(zone, "Undo resize: " + FiringZone.cubeVerticies[i]); Vector3 scaled = 2f * (vec - zone.transform.position); for (int j = 0; j < 3; j++) { scaled[j] /= zone.transform.localScale[j]; } scaled = Vector3.Scale(scaled, FiringZone.cubeVerticies[i]); scaled = Vector3.Max(scaled, Vector3.one * minAttrib.minVal); for (int j = 0; j < 3; j++) { if (stayBigger && scaled[j] <= compare[j]) { scaled[j] = compare[j] + minAttrib.minVal; } else if (!stayBigger && scaled[j] >= compare[j]) { scaled[j] = compare[j] - minAttrib.minVal; } } scale = scaled; } } }
public override void ValidateProperty(SerializedProperty property) { MinValueAttribute minValueAttribute = PropertyUtility.GetAttribute <MinValueAttribute>(property); if (property.propertyType == SerializedPropertyType.Integer) { if (property.intValue < minValueAttribute.MinValue) { property.intValue = (int)minValueAttribute.MinValue; } } else if (property.propertyType == SerializedPropertyType.Float) { if (property.floatValue < minValueAttribute.MinValue) { property.floatValue = minValueAttribute.MinValue; } } else { string warning = minValueAttribute.GetType().Name + " 只作用于整型和单精度浮点型字段!"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property), logToConsole: false); } }
public override void ValidateProperty(SerializedProperty property, bool drawField) { MinValueAttribute minValueAttribute = PropertyUtility.GetAttribute <MinValueAttribute>(property); if (property.propertyType == SerializedPropertyType.Integer) { if (property.intValue < minValueAttribute.MinValue) { property.intValue = (int)minValueAttribute.MinValue; } } else if (property.propertyType == SerializedPropertyType.Float) { if (property.floatValue < minValueAttribute.MinValue) { property.floatValue = minValueAttribute.MinValue; } } else { string warning = minValueAttribute.GetType().Name + " can be used only on int or float fields"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property)); } }
public MinValueAttributeTests() { attribute = new MinValueAttribute(12.56); }
public void SetUp() { attribute = new MinValueAttribute(12.56); }