private void AssertDefaultValue(MemberInfo memberInfo)
        {
            ExtractMemberInfo(memberInfo, out Type memberType, out Func <object, object> memberGetter);

            // Resolve the default value for the current member type and check it matches
            var factory      = DefaultValueFactory.MakeGenericMethod(memberType);
            var defaultValue = factory.Invoke(this, new object[0]);
            var value        = memberGetter.Invoke(Subject);
            var equal        = value == null && defaultValue == null;

            if (!equal)
            {
                // Ensure Equals() is called on a non-null instance
                if (value != null)
                {
                    equal = value.Equals(defaultValue);
                }
                else
                {
                    equal = defaultValue.Equals(value);
                }
            }

            Scope = Scope
                    .ForCondition(equal)
                    .FailWith($"Expected a default '{memberType.FullName}' value for '{memberInfo.Name}'.")
                    .Then;
        }
        public XmlReaderValidator(XmlReader subjectReader, XmlReader otherReader, string because, object[] reasonArgs)
        {
            assertion = Execute.Assertion.BecauseOf(because, reasonArgs);

            this.subjectReader = subjectReader;
            this.otherReader   = otherReader;
        }
Beispiel #3
0
        protected StringValidator(string subject, string expected, string because, object[] becauseArgs)
        {
            assertion = Execute.Assertion.BecauseOf(because, becauseArgs);

            this.subject  = subject;
            this.expected = expected;
        }
Beispiel #4
0
 public static Continuation AssertCollectionHasNotTooManyItems <T>(this IAssertionScope scope, ICollection <object> subject, ICollection <T> expectation)
 {
     return(scope
            .ForCondition(subject.Count <= expectation.Count)
            .FailWith(", but {0}{3}contains {1} item(s) more than{3}{2}.",
                      subject,
                      subject.Count - expectation.Count,
                      expectation,
                      Environment.NewLine));
 }
        private AndConstraint <object> AssertSubject()
        {
            var type      = Subject.GetType();
            var assertion = GetAssertion(type);

            Scope = Execute.Assertion;

            assertion.Invoke(type.Name, type, Subject);

            return(new AndConstraint <object>(Subject));
        }
Beispiel #6
0
 public static Continuation AssertEitherCollectionIsNotEmpty <T>(this IAssertionScope scope, ICollection <object> subject, ICollection <T> expectation)
 {
     return(scope
            .ForCondition((subject.Count > 0) || (expectation.Count == 0))
            .FailWith(", but found an empty collection.")
            .Then
            .ForCondition((subject.Count == 0) || (expectation.Count > 0))
            .FailWith(", but {0}{2}contains {1} item(s).",
                      subject,
                      subject.Count,
                      Environment.NewLine));
 }
Beispiel #7
0
        /// <summary>
        /// Asserts that the thrown exception has a message that matches <paramref name = "expectedWildcardPattern" />.
        /// </summary>
        /// <param name = "expectedWildcardPattern">
        /// The wildcard pattern with which the exception message is matched, where * and ? have special meanings.
        /// </param>
        /// <param name = "because">
        /// A formatted phrase as is supported by <see cref = "string.Format(string,object[])" /> explaining why the assertion
        /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name = "becauseArgs">
        /// Zero or more objects to format using the placeholders in <see cref = "because" />.
        /// </param>
        public virtual ExceptionAssertions <TException> WithMessage(string expectedWildcardPattern, string because = "",
                                                                    params object[] becauseArgs)
        {
            IAssertionScope assertion = Execute.Assertion.BecauseOf(because, becauseArgs).UsingLineBreaks;

            assertion
            .ForCondition(Subject.Any())
            .FailWith("Expected exception with message {0}{reason}, but no exception was thrown.", expectedWildcardPattern);

            outerMessageAssertion.Execute(Subject.Select(exc => exc.Message).ToArray(), expectedWildcardPattern, because, becauseArgs);

            return(this);
        }
        public AndConstraint <object> NotBeGenerated()
        {
            var type        = Subject.GetType();
            var memberInfos = GetMemberInfos(type);

            Scope = Execute.Assertion;

            foreach (var memberInfo in memberInfos)
            {
                AssertDefaultValue(memberInfo);
            }

            return(new AndConstraint <object>(Subject));
        }
        public AndConstraint <object> BeGenerated()
        {
            var type      = Subject.GetType();
            var assertion = GetAssertion(type);

            Scope = Execute.Assertion;

            // Assert the value and output any fail messages
            var message = assertion.Invoke(null, type, Subject);

            Scope = Scope
                    .ForCondition(message == null)
                    .FailWith(message)
                    .Then;

            return(new AndConstraint <object>(Subject));
        }
        private void AssertMember(string path, MemberInfo memberInfo, object instance)
        {
            ExtractMemberInfo(memberInfo, out Type memberType, out Func <object, object> memberGetter);

            // Create a trace path for the current member
            path = string.Concat(path, ".", memberInfo.Name);

            // Resolve the assertion and value for the member type
            var value     = memberGetter.Invoke(instance);
            var assertion = GetAssertion(memberType);
            var message   = assertion.Invoke(path, memberType, value);

            // Register an assertion for each member
            Scope = Scope
                    .ForCondition(message == null)
                    .FailWith(message)
                    .Then;
        }
Beispiel #11
0
        public void Validate()
        {
            if ((expected != null) || (subject != null))
            {
                if (ValidateAgainstNulls())
                {
                    if (IsLongOrMultiline(expected) || IsLongOrMultiline(subject))
                    {
                        assertion = assertion.UsingLineBreaks;
                    }

                    if (ValidateAgainstSuperfluousWhitespace())
                    {
                        if (ValidateAgainstLengthDifferences())
                        {
                            ValidateAgainstMismatch();
                        }
                    }
                }
            }
        }
Beispiel #12
0
        public void Validate()
        {
            if ((Expected is not null) || (Subject is not null))
            {
                if (ValidateAgainstNulls())
                {
                    if (IsLongOrMultiline(Expected) || IsLongOrMultiline(Subject))
                    {
                        Assertion = Assertion.UsingLineBreaks;
                    }

                    if (ValidateAgainstSuperfluousWhitespace())
                    {
                        if (ValidateAgainstLengthDifferences())
                        {
                            ValidateAgainstMismatch();
                        }
                    }
                }
            }
        }
 public static Continuation AssertIsNotNull <T>(this IAssertionScope assertionScope, T instance, string subjectName, string itemName, string accessorName)
     where T : class
 => assertionScope.ForCondition(!(instance is null))