public void CustomObjectListContainsInvalidEmail(string invalidEmail)
        {
            // email address is in the String named property
            EmailAddressListAttribute attr = new EmailAddressListAttribute("String");

            List <SomeDemoObject> inList = new List <SomeDemoObject>(4);

            inList.Add(new SomeDemoObject()
            {
                String = "*****@*****.**"
            });
            inList.Add(new SomeDemoObject()
            {
                String = "*****@*****.**"
            });
            inList.Add(new SomeDemoObject()
            {
                String = "*****@*****.**"
            });
            inList.Add(new SomeDemoObject()
            {
                String = invalidEmail
            });

            attr.IsValid(inList).Should().BeFalse();
        }
        public void EmptyList()
        {
            // empty list, nothing to validate

            EmailAddressListAttribute attr = new EmailAddressListAttribute();

            attr.IsValid(new ArrayList()).Should().BeTrue();
        }
        public void UsageWithDefaultSettingsNullValue()
        {
            // validator should try to validate null list, nothing to validate so should return true

            EmailAddressListAttribute attr = new EmailAddressListAttribute();

            attr.IsValid(null).Should().BeTrue();
        }
        public void UsageWithDefaultSettingsInvalidPropertyType()
        {
            // don't set propertyname and pass empty string (this means the attribute is used on wrong kind of property)
            // this should throw exception about wrong kind of property type, only valid type is ilist

            EmailAddressListAttribute attr = new EmailAddressListAttribute();
            // isvalid is actually passed an object property
            Action act = () => attr.IsValid(string.Empty);

            act.ShouldThrow <Exception>(because: "EmailAddressListAttribute is used on wrong type of property. Should be only used on properties of type IList.");
        }
        public void PropertyNameNotDefinedEmptyString()
        {
            // string.empty is currently the default but if the implementation is changed then we still need to test with empty.string
            EmailAddressListAttribute attr = new EmailAddressListAttribute(string.Empty);

            Action act = () => attr.IsValid(new ArrayList()
            {
                new AttributeHelperTestingObject()
            });

            act.ShouldThrow <Exception>(because: "EmailAddressListAttribute propertyName is not defined (string.empty).");
        }
        public void PropertyNameNotDefinedNull()
        {
            // explicitly set property name to null
            EmailAddressListAttribute attr = new EmailAddressListAttribute(null);

            Action act = () => attr.IsValid(new ArrayList()
            {
                new AttributeHelperTestingObject()
            });

            act.ShouldThrow <Exception>(because: "EmailAddressListAttribute propertyName is not defined (null).");
        }
        public void PropertyNameNotDefinedDefault()
        {
            EmailAddressListAttribute attr = new EmailAddressListAttribute();

            // because property name is not defined the email validation should be done on the list item not to its property
            // this should fail because the passed object cannot be validated for email address string
            // if passing custom object, then the propertyname needs to be set

            Action act = () => attr.IsValid(new ArrayList()
            {
                new AttributeHelperTestingObject()
            });

            act.ShouldThrow <Exception>(because: "EmailAddressListAttribute propertyName is not defined (default).");
        }
        public void GenericListContainsValidEmails(string propertyName)
        {
            // if propertyName is null or empty
            // theimplementation should use the item in the list (its value)

            EmailAddressListAttribute attr = new EmailAddressListAttribute(propertyName);

            List <string> inList = new List <string>(5);

            inList.Add("*****@*****.**");
            inList.Add("*****@*****.**");
            inList.Add("*****@*****.**");
            inList.Add("*****@*****.**");
            inList.Add("*****@*****.**");

            attr.IsValid(inList).Should().BeTrue();
        }
        public void InvalidPropertyName()
        {
            // explicitly set property name to null
            EmailAddressListAttribute attr = new EmailAddressListAttribute("BogusPropName");

            // it is valid to assume that the list have items that are of the same type or implement the same interface
            // so all items should have the same property and if the property is not found on the object it should throw
            // or change this test to expect validation error
            // exception would be better to indicate that one or more items in the list doesn't have the expected property

            Action act = () => attr.IsValid(new ArrayList()
            {
                new AttributeHelperTestingObject()
            });

            act.ShouldThrow <Exception>(because: "EmailAddressListAttribute invalid propertyName defined (BogusPropName).");
        }
        [InlineData("[email protected] ")] // this validates ok by the framework attribute
        public void CustomObjectListContainsValidEmails(string actuallyValidEmailAddressByFrameworkAttribute)
        {
            // Comment: see RFC http://www.faqs.org/rfcs/rfc2822.html and at the bottom the silly examples A.6.1
            // so the above inlinedata email addresses are valid and reason why those are used here
            // yes on purpose running a test per bogus valid email so if we decide to write own validator
            // we can easily move the above test data there to validate "invalid" addresses

            // email address is in the String named property
            EmailAddressListAttribute attr = new EmailAddressListAttribute("String");

            List <SomeDemoObject> inList = new List <SomeDemoObject>(5);

            inList.Add(new SomeDemoObject()
            {
                String = "*****@*****.**"
            });
            inList.Add(new SomeDemoObject()
            {
                String = "*****@*****.**"
            });
            inList.Add(new SomeDemoObject()
            {
                String = "*****@*****.**"
            });
            inList.Add(new SomeDemoObject()
            {
                String = "*****@*****.**"
            });
            inList.Add(new SomeDemoObject()
            {
                String = "*****@*****.**"
            });
            inList.Add(new SomeDemoObject()
            {
                String = actuallyValidEmailAddressByFrameworkAttribute
            });

            attr.IsValid(inList).Should().BeTrue();
        }