public static void Validate_Invalid_Throws(string otherProperty, ValidationContext context, string otherPropertyDisplayName, Type exceptionType) { var attribute = new CompareAttribute(otherProperty); Assert.Throws(exceptionType, () => attribute.Validate("b", context)); Assert.Equal(otherPropertyDisplayName, attribute.OtherPropertyDisplayName); // Make sure that we can run Validate twice Assert.Throws(exceptionType, () => attribute.Validate("b", context)); Assert.Equal(otherPropertyDisplayName, attribute.OtherPropertyDisplayName); }
public static void Validate_Invalid_Throws(string otherProperty, ValidationContext context, string otherPropertyDisplayName, Type exceptionType) { var attribute = new CompareAttribute(otherProperty); Assert.Throws(exceptionType, () => attribute.Validate("b", context)); Assert.Equal(otherPropertyDisplayName, attribute.OtherPropertyDisplayName); // Make sure that we can run Validate twice Assert.Throws(exceptionType, () => attribute.Validate("b", context)); Assert.Equal(otherPropertyDisplayName, attribute.OtherPropertyDisplayName); }
public void ValidateDoesNotThrowWhenComparedObjectsAreEqual() { object otherObject = new CompareObject("test"); CompareObject currentObject = new CompareObject("test"); ValidationContext testContext = new ValidationContext(otherObject, null, null); CompareAttribute attr = new CompareAttribute("CompareProperty"); attr.Validate(currentObject.CompareProperty, testContext); }
public static void Validate_EqualObjects_DoesNotThrow() { var otherObject = new CompareObject("test"); var currentObject = new CompareObject("test"); var testContext = new ValidationContext(otherObject, null, null); var attribute = new CompareAttribute("CompareProperty"); attribute.Validate(currentObject.CompareProperty, testContext); }
public static void Validate_does_not_throw_when_compared_objects_are_equal() { var otherObject = new CompareObject("test"); var currentObject = new CompareObject("test"); var testContext = new ValidationContext(otherObject, null, null); var attribute = new CompareAttribute("CompareProperty"); AssertEx.DoesNotThrow(() => attribute.Validate(currentObject.CompareProperty, testContext)); }
public static void Validate_does_not_throw_when_compared_objects_are_equal() { var otherObject = new CompareObject("test"); var currentObject = new CompareObject("test"); var testContext = new ValidationContext(otherObject, null, null); var attribute = new CompareAttribute("CompareProperty"); AssertEx.DoesNotThrow(() => attribute.Validate(currentObject.CompareProperty, testContext)); }
public static void Validate_EqualObjects_DoesNotThrow() { var otherObject = new CompareObject("test"); var currentObject = new CompareObject("test"); var testContext = new ValidationContext(otherObject, null, null); var attribute = new CompareAttribute("CompareProperty"); attribute.Validate(currentObject.CompareProperty, testContext); }
public static void Validate_throws_with_OtherProperty_DisplayName() { var currentObject = new CompareObject("a"); var otherObject = new CompareObject("b"); var testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; var attribute = new CompareAttribute("ComparePropertyWithDisplayName"); Assert.Throws<ValidationException>( () => attribute.Validate(currentObject.CompareProperty, testContext)); }
public void ValidateThrowsWithOtherPropertyDisplayName() { CompareObject currentObject = new CompareObject("a"); object otherObject = new CompareObject("b"); ValidationContext testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; CompareAttribute attr = new CompareAttribute("ComparePropertyWithDisplayName"); Assert.Throws<ValidationException>( delegate { attr.Validate(currentObject.CompareProperty, testContext); }, "'CurrentProperty' and 'DisplayName' do not match."); }
public static void Validate_PropertyHasDisplayName_UpdatesFormatErrorMessageToContainDisplayName() { CompareAttribute attribute = new CompareAttribute(nameof(CompareObject.ComparePropertyWithDisplayName)); string oldErrorMessage = attribute.FormatErrorMessage("name"); Assert.False(oldErrorMessage.Contains("CustomDisplayName")); Assert.Throws<ValidationException>(() => attribute.Validate("test1", new ValidationContext(new CompareObject("test")))); string newErrorMessage = attribute.FormatErrorMessage("name"); Assert.NotEqual(oldErrorMessage, newErrorMessage); Assert.True(newErrorMessage.Contains("CustomDisplayName")); }
public void ValidateThrowsWhenPropertyNameIsUnknown() { CompareObject currentObject = new CompareObject("a"); object otherObject = new CompareObject("b"); ValidationContext testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; CompareAttribute attr = new CompareAttribute("UnknownPropertyName"); ExceptionHelper.ExpectException<System.ComponentModel.DataAnnotations.ValidationException>( () => attr.Validate(currentObject.CompareProperty, testContext), "Could not find a property named UnknownPropertyName." ); }
public void ValidateThrowsWhenComparedObjectsAreNotEqual() { CompareObject currentObject = new CompareObject("a"); object otherObject = new CompareObject("b"); ValidationContext testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; CompareAttribute attr = new CompareAttribute("CompareProperty"); ExceptionHelper.ExpectException<System.ComponentModel.DataAnnotations.ValidationException>( delegate { attr.Validate(currentObject.CompareProperty, testContext); }, "'CurrentProperty' and 'CompareProperty' do not match."); }
public static void Validate_throws_when_PropertyName_is_unknown() { var currentObject = new CompareObject("a"); var otherObject = new CompareObject("b"); var testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; var attribute = new CompareAttribute("UnknownPropertyName"); Assert.Throws<ValidationException>( () => attribute.Validate(currentObject.CompareProperty, testContext)); // cannot check error message - not defined on ret builds }
public static void Validate_throws_with_OtherProperty_DisplayName() { var currentObject = new CompareObject("a"); var otherObject = new CompareObject("b"); var testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; var attribute = new CompareAttribute("ComparePropertyWithDisplayName"); Assert.Throws <ValidationException>( () => attribute.Validate(currentObject.CompareProperty, testContext)); }
public static void Validate_throws_when_PropertyName_is_unknown() { var currentObject = new CompareObject("a"); var otherObject = new CompareObject("b"); var testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; var attribute = new CompareAttribute("UnknownPropertyName"); Assert.Throws <ValidationException>( () => attribute.Validate(currentObject.CompareProperty, testContext)); // cannot check error message - not defined on ret builds }
public static void Validate_PropertyHasDisplayName_UpdatesFormatErrorMessageToContainDisplayName() { CompareAttribute attribute = new CompareAttribute(nameof(CompareObject.ComparePropertyWithDisplayName)); string oldErrorMessage = attribute.FormatErrorMessage("name"); Assert.False(oldErrorMessage.Contains("CustomDisplayName")); Assert.Throws <ValidationException>(() => attribute.Validate("test1", new ValidationContext(new CompareObject("test")))); string newErrorMessage = attribute.FormatErrorMessage("name"); Assert.NotEqual(oldErrorMessage, newErrorMessage); Assert.True(newErrorMessage.Contains("CustomDisplayName")); }
public void ValidateThrowsWhenPropertyNameIsUnknown() { CompareObject currentObject = new CompareObject("a"); object otherObject = new CompareObject("b"); ValidationContext testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; CompareAttribute attr = new CompareAttribute("UnknownPropertyName"); Assert.Throws<ValidationException>( () => attr.Validate(currentObject.CompareProperty, testContext), "Could not find a property named UnknownPropertyName." ); }