public void ThrowsStringIsWhiteSpaceExceptionForStringConsistingOfTabsSpacesOrLineBreaks()
        {
            var space              = " ";
            var tab                = "\t";
            var lineBreak          = "\n";
            var newLine            = "\r\n";
            var environmentNewLine = Environment.NewLine;

            Assert.Throws <StringIsWhitespaceContractViolationException>(() => Contract.Argument(() => space).NotNullEmptyOrWhiteSpace());
            Assert.Throws <StringIsWhitespaceContractViolationException>(() => Contract.Argument(() => tab).NotNullEmptyOrWhiteSpace());
            Assert.Throws <StringIsWhitespaceContractViolationException>(() => Contract.Argument(() => lineBreak).NotNullEmptyOrWhiteSpace());
            Assert.Throws <StringIsWhitespaceContractViolationException>(() => Contract.Argument(() => newLine).NotNullEmptyOrWhiteSpace());
            Assert.Throws <StringIsWhitespaceContractViolationException>(() => Contract.Argument(() => environmentNewLine).NotNullEmptyOrWhiteSpace());

            Assert.Throws <StringIsWhitespaceContractViolationException>(() => Contract.Argument(() => environmentNewLine, () => space).NotNullEmptyOrWhiteSpace());


            var badValues = new List <string> {
                space, tab, lineBreak, newLine, environmentNewLine
            };
            var goodValues = new List <string> {
                "aoeu", "lorem"
            };

            InspectionTestHelper.BatchTestInspection <StringIsWhitespaceContractViolationException, string>(
                assert: inspected => inspected.NotNullEmptyOrWhiteSpace(),
                badValues: badValues,
                goodValues: goodValues);
        }
Пример #2
0
        public void ThrowsObjectIsDefaultExceptionIfAnyValueIsDefault()
        {
            var myStructure = new MyStructure();
            // ReSharper disable ConvertToConstant.Local
            var zero = 0;

            // ReSharper restore ConvertToConstant.Local

            Assert.Throws <ObjectIsDefaultContractViolationException>(() => Contract.Argument(() => zero).NotDefault());
            Assert.Throws <ObjectIsDefaultContractViolationException>(() => Contract.Argument(() => zero).NotDefault());
            Assert.Throws <ObjectIsDefaultContractViolationException>(() => Contract.Argument(() => myStructure).NotDefault());
            Assert.Throws <ObjectIsDefaultContractViolationException>(() => Contract.Argument(() => myStructure).NotDefault());

            var badValues = new List <object> {
                zero, myStructure
            };
            var goodValues = new List <object> {
                new Object(), "", Guid.NewGuid()
            };

            InspectionTestHelper.InspectBadValue <ObjectIsDefaultContractViolationException, MyStructure>(
                inspected => inspected.NotDefault(),
                new MyStructure());

            InspectionTestHelper.BatchTestInspection <ObjectIsDefaultContractViolationException, int>(
                inspected => inspected.NotDefault(),
                badValues: new List <int> {
                0
            },
                goodValues: new List <int> {
                1, 2, 3
            });
        }
Пример #3
0
        public void ThrowsObjectNullExceptionForNullValues()
        {
            InspectionTestHelper.BatchTestInspection <ObjectIsNullContractViolationException, object>(
                inspected => inspected.NotNull(),
                badValues: new List <object> {
                null, null
            },
                goodValues: new List <object> {
                new object(), "", Guid.NewGuid()
            });


            var nullString = (string)null;
            var anObject   = new object();

            Assert.Throws <ObjectIsNullContractViolationException>(() => Contract.Argument(() => nullString).NotNull());
            Assert.Throws <ObjectIsNullContractViolationException>(() => Contract.Argument(() => anObject, () => nullString).NotNull());
            Assert.Throws <ObjectIsNullContractViolationException>(() => Contract.Argument(() => nullString).NotNull())
            .Message.Should().Contain("nullString");

            Assert.Throws <ObjectIsNullContractViolationException>(() => Contract.Invariant(() => nullString).NotNull());
            Assert.Throws <ObjectIsNullContractViolationException>(() => Contract.Invariant(() => anObject, () => nullString).NotNull());
            Assert.Throws <ObjectIsNullContractViolationException>(() => Contract.Invariant(() => nullString).NotNull())
            .Message.Should().Contain("nullString");
        }
Пример #4
0
        public void ThrowsEnumerableIsEmptyException()
        {
            var emptyStringList = new List <string>();

            Assert.Throws <EnumerableIsEmptyContractViolationException>(() => Contract.Argument(() => emptyStringList).NotNullOrEmptyEnumerable());

            var exception = Assert.Throws <EnumerableIsEmptyContractViolationException>(() => Contract.Argument(() => emptyStringList).NotNullOrEmptyEnumerable());

            exception.BadValue.Type.Should().Be(InspectionType.Argument);

            exception = Assert.Throws <EnumerableIsEmptyContractViolationException>(() => Contract.Invariant(() => emptyStringList).NotNullOrEmptyEnumerable());
            exception.BadValue.Type.Should().Be(InspectionType.Invariant);

            exception = Assert.Throws <EnumerableIsEmptyContractViolationException>(() => ReturnValueContractHelper.Return(emptyStringList, inspected => inspected.NotNullOrEmptyEnumerable()));
            exception.BadValue.Type.Should().Be(InspectionType.ReturnValue);


            InspectionTestHelper.BatchTestInspection <EnumerableIsEmptyContractViolationException, IEnumerable <string> >(
                assert: inspected => inspected.NotNullOrEmptyEnumerable(),
                badValues: new List <IEnumerable <string> > {
                emptyStringList, new List <string>()
            },
                goodValues: new List <IEnumerable <string> > {
                new List <string> {
                    ""
                }, new List <string> {
                    ""
                }
            });
        }
Пример #5
0
 public void NotEmptyThrowsArgumentExceptionForEmptyGuidNew()
 {
     InspectionTestHelper.BatchTestInspection <GuidIsEmptyContractViolationException, Guid>(
         assert: inspected => inspected.NotEmpty(),
         badValues: new[] { Guid.Empty, new Guid() },
         goodValues: new[] { Guid.NewGuid(), Guid.NewGuid() });
 }
Пример #6
0
 public void ThrowsObjectIsNullForNullStrings()
 {
     InspectionTestHelper.BatchTestInspection <ObjectIsNullContractViolationException, string>(
         inspected => inspected.NotNullOrEmpty(),
         badValues: new List <string> {
         null, null
     },
         goodValues: new List <string> {
         "a", "aa", "aaa"
     });
 }
Пример #7
0
 public void ThrowsStringIsEmptyForEmptyStrings()
 {
     InspectionTestHelper.BatchTestInspection <StringIsEmptyContractViolationException, string>(
         inspected => inspected.NotNullOrEmpty(),
         badValues: new List <string> {
         "", ""
     },
         goodValues: new List <string> {
         "a", "aa", "aaa"
     });
 }
        public void ThrowsObjectIsDefaultExceptionIfAnyValueIsDefault()
        {
            var    anObject           = new object();
            string emptyString        = "";
            var    zero               = 0;
            var    defaultMyStructure = new MyStructure();
            var    aMyStructure       = new MyStructure(1);

            Assert.Throws <ObjectIsDefaultContractViolationException>(() => Contract.Argument(() => zero).NotNullOrDefault());
            Assert.Throws <ObjectIsDefaultContractViolationException>(() => Contract.Argument(() => anObject, () => zero).NotNullOrDefault());
            Assert.Throws <ObjectIsDefaultContractViolationException>(() => Contract.Argument(() => emptyString, () => anObject, () => defaultMyStructure).NotNullOrDefault());
            Contract.Argument(() => emptyString, () => anObject, () => aMyStructure).NotNullOrDefault();


            InspectionTestHelper.BatchTestInspection <ObjectIsDefaultContractViolationException, object>(
                inspected => inspected.NotNullOrDefault(),
                badValues: new List <object> {
                zero, defaultMyStructure
            },
                goodValues: new List <object> {
                new object(), "", Guid.NewGuid()
            });
        }