예제 #1
0
        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);
        }
예제 #2
0
        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"));
        }
예제 #3
0
        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.");
        }
예제 #4
0
        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"));
        }
예제 #5
0
        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."
                );
        }
예제 #6
0
        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.");
        }
예제 #7
0
        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."
                );
        }
예제 #8
0
        public static void Validate_SetOnlyProperty_ThrowsArgumentException()
        {
            CompareAttribute attribute = new CompareAttribute(nameof(CompareObject.SetOnlyProperty));

            Assert.Throws <ArgumentException>(null, () => attribute.Validate("b", s_context));
        }
예제 #9
0
        public static void Validate_Indexer_ThrowsArgumentException_Netcoreapp()
        {
            CompareAttribute attribute = new CompareAttribute("Item");

            Assert.Throws <ArgumentException>(null, () => attribute.Validate("b", s_context));
        }
예제 #10
0
        public static void Validate_Indexer_ThrowsTargetParameterCountException_Netfx()
        {
            CompareAttribute attribute = new CompareAttribute("Item");

            Assert.Throws <TargetParameterCountException>(() => attribute.Validate("b", s_context));
        }
예제 #11
0
 public static void Validate_SetOnlyProperty_ThrowsArgumentException()
 {
     CompareAttribute attribute = new CompareAttribute(nameof(CompareObject.SetOnlyProperty));
     Assert.Throws<ArgumentException>(null, () => attribute.Validate("b", s_context));
 }
예제 #12
0
 public static void Validate_Indexer_ThrowsTargetParameterCountException()
 {
     CompareAttribute attribute = new CompareAttribute("Item");
     Assert.Throws<TargetParameterCountException>(() => attribute.Validate("b", s_context));
 }
예제 #13
0
        public static void Validate_PrivateProperty_ThrowsArgumentException()
        {
            CompareAttribute attribute = new CompareAttribute("PrivateProperty");

            Assert.Throws <ValidationException>(() => attribute.Validate("b", s_context));
        }