Ejemplo n.º 1
0
        private static string EndsWithImpl(IChecker <string, ICheck <string> > checker, string ends, bool negated)
        {
            var checkedValue = checker.Value;

            // special case if checkedvalue is null
            if (checkedValue == null)
            {
                return(negated ? null : checker.BuildShortMessage("The {0} is null.").For("string").Expected(ends).Comparison("ends with").ToString());
            }

            if (checkedValue.EndsWith(ends) != negated)
            {
                // success
                return(null);
            }

            if (negated)
            {
                return
                    (checker.BuildMessage("The {0} ends with {1}, whereas it must not.")
                     .For("string").Expected(ends)
                     .Comparison("does not end with")
                     .ToString());
            }

            return
                (checker.BuildMessage("The {0}'s end is different from the {1}.")
                 .For("string")
                 .Expected(ends)
                 .Comparison("ends with")
                 .ToString());
        }
Ejemplo n.º 2
0
        private static string MatchesImpl(IChecker <string, ICheck <string> > checker, string regExp, bool negated)
        {
            var checkedValue = checker.Value;

            // special case if checkedvalue is null
            if (checkedValue == null)
            {
                return(negated ? null : checker.BuildShortMessage("The {0} is null.").For("string").Expected(regExp).Comparison("matches").ToString());
            }

            var exp = new Regex(regExp);

            if (exp.IsMatch(checkedValue) != negated)
            {
                // success
                return(null);
            }

            if (negated)
            {
                return
                    (checker.BuildMessage("The {0} matches {1}, whereas it must not.")
                     .For("string")
                     .Expected(regExp)
                     .Comparison("does not match")
                     .ToString());
            }

            return
                (checker.BuildMessage("The {0} does not match the {1}.")
                 .For("string")
                 .Expected(regExp)
                 .Comparison("matches")
                 .ToString());
        }
Ejemplo n.º 3
0
        private static string BuildNotExactlyExceptionMessage(IChecker <IEnumerable, ICheck <IEnumerable> > checker, IEnumerable <object> enumerable, int index)
        {
            var           checkedValue  = checker.Value;
            var           sutCount      = checkedValue.Count();
            var           expectedCount = enumerable.Count();
            FluentMessage message;

            if (checkedValue != null && enumerable != null)
            {
                if (sutCount < expectedCount && index == sutCount)
                {
                    message = checker.BuildMessage(string.Format("The {{0}} does not contain exactly the expected value(s). Items are missing starting at index #{0}.", index));
                }
                else if (sutCount > expectedCount && index == expectedCount)
                {
                    message = checker.BuildMessage(string.Format("The {{0}} does not contain exactly the expected value(s). There are extra items starting at index #{0}.", index));
                }
                else
                {
                    message = checker.BuildMessage(string.Format("The {{0}} does not contain exactly the expected value(s). First difference is at index #{0}.", index));
                }
            }
            else
            {
                message = checker.BuildMessage("The {0} does not contain exactly the expected value(s).");
            }

            message.For(typeof(IEnumerable))
            .On(checkedValue)
            .WithEnumerableCount(sutCount)
            .And.ExpectedValues(enumerable)
            .WithEnumerableCount(expectedCount);

            return(message.ToString());
        }
Ejemplo n.º 4
0
        private static string IsEmptyImpl(IChecker <string, ICheck <string> > checker, bool canBeNull, bool negated)
        {
            var checkedValue = checker.Value;

            // special case if checkedvalue is null
            if (checkedValue == null)
            {
                if (canBeNull != negated)
                {
                    return(null);
                }

                return(negated ? checker.BuildShortMessage("The {0} is null whereas it must have content.").For(typeof(string)).ToString()
                    : checker.BuildShortMessage("The {0} is null instead of being empty.").For(typeof(string)).ToString());
            }

            if (string.IsNullOrEmpty(checkedValue) != negated)
            {
                // success
                return(null);
            }

            if (negated)
            {
                return
                    (checker.BuildShortMessage("The {0} is empty, whereas it must not.")
                     .For(typeof(string))
                     .ToString());
            }

            return
                (checker.BuildMessage("The {0} is not empty or null.")
                 .On(checkedValue)
                 .ToString());
        }
Ejemplo n.º 5
0
        private static string BuildHasSizeExceptionMessage(IChecker <IEnumerable, ICheck <IEnumerable> > checker)
        {
            var  checkedEnumerable = checker.Value;
            long itemsCount        = checkedEnumerable.Cast <object>().LongCount();
            var  foundElementsNumberDescription = BuildElementNumberLiteral(itemsCount);

            return(checker.BuildMessage(string.Format("The {{0}} has {0} which is unexpected.", foundElementsNumberDescription.DoubleCurlyBraces())).On(checkedEnumerable).ToString());
        }
Ejemplo n.º 6
0
        private static string BuildExceptionMessageForContainsExactly(IChecker <IEnumerable, ICheck <IEnumerable> > checker, IEnumerable enumerable)
        {
            var checkedValue = checker.Value;

            return(checker.BuildMessage("The {0} contains exactly the given values whereas it must not.")
                   .On(checkedValue)
                   .WithEnumerableCount(checkedValue.Count())
                   .ToString());
        }
Ejemplo n.º 7
0
 private static MessageBlock GenerateMessageWhenSameLenghtButDiffContent(Stream expected, IChecker<Stream, ICheck<Stream>> checker, Stream value)
 {
     var message =
         checker.BuildMessage(
                 "The {0} doesn't have the same content as the expected one (despite the fact that they have the same Length: " +
                 value.Length + ").")
             .On(value)
             .And.Expected(expected);
     return message;
 }
Ejemplo n.º 8
0
 private static MessageBlock GenerateMessageWhenFullyDistinct(Stream expected, IChecker<Stream, ICheck<Stream>> checker, Stream value)
 {
     var message =
         checker.BuildMessage(
                 "The {0} doesn't have the same content as the expected one. They don't even have the same Length!")
             .On(value)
             .Comparison(string.Format("(Length: {0})", value.Length))
             .And.Expected(expected)
             .Comparison(string.Format("(Length: {0})", expected.Length));
     return message;
 }
Ejemplo n.º 9
0
        private static MessageBlock GenerateMessageWhenSameLenghtButDiffContent(Stream expected, IChecker <Stream, ICheck <Stream> > checker, Stream value)
        {
            var message =
                checker.BuildMessage(
                    "The {0} doesn't have the same content as the expected one (despite the fact that they have the same Length: " +
                    value.Length + ").")
                .On(value)
                .And.Expected(expected);

            return(message);
        }
Ejemplo n.º 10
0
        private static string ContainsImpl(IChecker <string, ICheck <string> > checker, IEnumerable <string> values, bool negated, bool notContains)
        {
            var checkedValue = checker.Value;

            // special case if checkedvalue is null
            if (checkedValue == null)
            {
                return((negated || notContains) ? null : checker.BuildShortMessage("The {0} is null.").For("string").Expected(values).Label("The {0} substring(s):").ToString());
            }

            var items = values.Where(item => checkedValue.Contains(item) == notContains).ToList();

            if (negated == (items.Count > 0))
            {
                return(null);
            }

            if (!notContains && negated)
            {
                items = values.ToList();
            }

            if (negated != notContains)
            {
                return
                    (checker.BuildMessage(
                         "The {0} contains unauthorized value(s): " + items.ToEnumeratedString())
                     .For("string")
                     .Expected(values)
                     .Label("The unauthorized substring(s):")
                     .ToString());
            }

            return
                (checker.BuildMessage(
                     "The {0} does not contains the expected value(s): " + items.ToEnumeratedString())
                 .For("string")
                 .Expected(values)
                 .Label("The {0} substring(s):")
                 .ToString());
        }
Ejemplo n.º 11
0
        private static string BuildNotExactlyExceptionMessage(IChecker <IEnumerable, ICheck <IEnumerable> > checker, IList <object> enumerable)
        {
            var checkedValue = checker.Value;
            var message      = checker.BuildMessage("The {0} does not contain exactly the expected value(s).")
                               .For(LabelForEnumerable)
                               .On(checkedValue)
                               .WithEnumerableCount(checkedValue.Count())
                               .And.ExpectedValues(enumerable)
                               .WithEnumerableCount(enumerable.Count());

            return(message.ToString());
        }
Ejemplo n.º 12
0
        private static MessageBlock GenerateMessageWhenFullyDistinct(Stream expected, IChecker <Stream, ICheck <Stream> > checker, Stream value)
        {
            var message =
                checker.BuildMessage(
                    "The {0} doesn't have the same content as the expected one. They don't even have the same Length!")
                .On(value)
                .Comparison(string.Format("(Length: {0})", value.Length))
                .And.Expected(expected)
                .Comparison(string.Format("(Length: {0})", expected.Length));

            return(message);
        }
Ejemplo n.º 13
0
        private static void HasSizeImpl(IChecker <IEnumerable, ICheck <IEnumerable> > checker, long expectedSize)
        {
            var  checkedEnumerable = checker.Value;
            long itemsCount        = checkedEnumerable.Cast <object>().LongCount();

            if (expectedSize != itemsCount)
            {
                var foundElementsNumberDescription = BuildElementNumberLiteral(itemsCount);

                var errorMessage = checker.BuildMessage(string.Format("The {{0}} has {0} instead of {1}.", foundElementsNumberDescription.DoubleCurlyBraces(), expectedSize)).On(checkedEnumerable).ToString();
                throw new FluentCheckException(errorMessage);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Checks that an instance is in the inheritance hierarchy of a specified type.
        /// </summary>
        /// <param name="checker">The instance to be checked.</param>
        /// <param name="expectedBaseType">The Type which is expected to be a base Type of the instance.</param>
        /// <exception cref="FluentCheckException">The instance is not in the inheritance hierarchy of the specified type.</exception>
        public static void InheritsFrom(IChecker <object, ICheck <object> > checker, Type expectedBaseType)
        {
            var instanceType = checker.Value.GetTypeWithoutThrowingException();

            if (expectedBaseType.IsAssignableFrom(instanceType))
            {
                return;
            }

            var message =
                checker.BuildMessage("The {0} does not have the expected inheritance.")
                .For("expression type")
                .On(instanceType)
                .And.Expected(expectedBaseType)
                .Comparison("inherits from");

            throw new FluentCheckException(message.ToString());
        }
Ejemplo n.º 15
0
        private static string MatchesImpl(IChecker<string, ICheck<string>> checker, string regExp, bool negated)
        {
            var checkedValue = checker.Value;

            // special case if checkedvalue is null
            if (checkedValue == null)
            {
                return negated ? null : checker.BuildShortMessage("The {0} is null.").For("string").Expected(regExp).Comparison("matches").ToString();
            }

            var exp = new Regex(regExp);
            if (exp.IsMatch(checkedValue) != negated)
            {
                // success
                return null;
            }

            if (negated)
            {
                return
                    checker.BuildMessage("The {0} matches {1}, whereas it must not.")
                    .For("string")
                                 .Expected(regExp)
                                 .Comparison("does not match")
                                 .ToString();
            }

            return
                checker.BuildMessage("The {0} does not match the {1}.")
                .For("string")
                             .Expected(regExp)
                             .Comparison("matches")
                             .ToString();
        }
Ejemplo n.º 16
0
        private static string AssessEquals(IChecker <string, ICheck <string> > checker, object expected, bool negated, bool ignoreCase = false)
        {
            if (string.Equals(checker.Value, (string)expected, ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture) != negated)
            {
                return(null);
            }

            string messageText;

            if (negated)
            {
                messageText = checker.BuildShortMessage("The {0} is equal to the {1} whereas it must not.")
                              .For("string")
                              .Expected(expected)
                              .Comparison("different from")
                              .ToString();
            }
            else
            {
                // we try to refine the difference
                var expectedString               = expected as string;
                var message                      = "The {0} is different from {1}.";
                var isCrlfAndLfDifference        = false;
                var isTabAndWhiteSpaceDifference = false;
                var firstDiffPos                 = 0;

                // TODO: refactor to reduce method lines
                var value = checker.Value;
                if (expectedString != null && value != null)
                {
                    var firstDiff  = 0;
                    var blockStart = 0;
                    var blockLen   = 0;

                    var minLength = Math.Min(value.Length, expectedString.Length);

                    for (; firstDiff < minLength; firstDiff++)
                    {
                        if (value[firstDiff] != expectedString[firstDiff])
                        {
                            firstDiffPos                 = firstDiff;
                            isCrlfAndLfDifference        = IsACRLFDifference(firstDiff, expectedString, value);
                            isTabAndWhiteSpaceDifference = IsATabAndWhiteSpaceDifference(firstDiff, expectedString, value);

                            blockStart = Math.Max(0, firstDiff - 10);
                            blockLen   = Math.Min(minLength - blockStart, 20);
                            break;
                        }
                    }

                    if (expectedString.Length == value.Length)
                    {
                        // same length
                        if (string.Compare(value, expectedString, StringComparison.CurrentCultureIgnoreCase) == 0)
                        {
                            message = "The {0} is different from the {1} but only in case.";
                        }
                        else
                        {
                            message = "The {0} is different from the {1} but has same length.";
                        }

                        var prefix = blockStart == 0 ? string.Empty : "...";
                        var suffix = (blockStart + blockLen) == minLength ? string.Empty : "...";
                        message += string.Format(
                            " At {0}, expected '{3}{1}{4}' was '{3}{2}{4}'",
                            firstDiff,
                            expectedString.Substring(blockStart, blockLen),
                            value.Substring(blockStart, blockLen),
                            prefix,
                            suffix);
                    }
                    else
                    {
                        if (expectedString.Length > value.Length)
                        {
                            if (expectedString.StartsWith(value, ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture))
                            {
                                message = "The {0} is different from {1}, it is missing the end.";
                            }
                        }
                        else
                        {
                            if (value.StartsWith(expectedString, ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture))
                            {
                                message = "The {0} is different from {1}, it contains extra text at the end.";
                            }
                        }
                    }
                }

                if (isCrlfAndLfDifference)
                {
                    value          = HighlightFirstCrlfOrLfIfAny(value, firstDiffPos);
                    expectedString = HighlightFirstCrlfOrLfIfAny(expectedString, firstDiffPos);
                }

                if (isTabAndWhiteSpaceDifference)
                {
                    value          = HighlightTabsIfAny(value);
                    expectedString = HighlightTabsIfAny(expectedString);
                }

                messageText = checker.BuildMessage(message).For("string").On(value).And.Expected(expectedString).ToString();
            }

            return(messageText);
        }
Ejemplo n.º 17
0
        private static string AssessEquals(IChecker<string, ICheck<string>> checker, object expected, bool negated, bool ignoreCase = false)
        {
            if (string.Equals(checker.Value, (string)expected, ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture) != negated)
            {
                return null;
            }

            string messageText;
            if (negated)
            {
                messageText = checker.BuildShortMessage("The {0} is equal to the {1} whereas it must not.")
                                    .For("string")
                                    .Expected(expected)
                                    .Comparison("different from")
                                    .ToString();
            }
            else
            {
                // we try to refine the difference
                var expectedString = expected as string;
                var message = "The {0} is different from {1}.";
                var isCrlfAndLfDifference = false;
                var isTabAndWhiteSpaceDifference = false;
                var firstDiffPos = 0;

                // TODO: refactor to reduce method lines
                var value = checker.Value;
                if (expectedString != null && value != null)
                {
                    var firstDiff = 0;
                    var blockStart = 0;
                    var blockLen = 0;

                    var minLength = Math.Min(value.Length, expectedString.Length);

                    for (; firstDiff < minLength; firstDiff++)
                    {
                        if (value[firstDiff] != expectedString[firstDiff])
                        {
                            firstDiffPos = firstDiff;
                            isCrlfAndLfDifference = IsACRLFDifference(firstDiff, expectedString, value);
                            isTabAndWhiteSpaceDifference = IsATabAndWhiteSpaceDifference(firstDiff, expectedString, value);
                        
                            blockStart = Math.Max(0, firstDiff - 10);
                            blockLen = Math.Min(minLength - blockStart, 20);
                            break;
                        }
                    }

                    if (expectedString.Length == value.Length)
                    {
                        // same length
                        if (string.Compare(value, expectedString, StringComparison.CurrentCultureIgnoreCase) == 0)
                        {
                            message = "The {0} is different from the {1} but only in case.";
                        }
                        else
                        {
                            message = "The {0} is different from the {1} but has same length.";
                        }   

                        var prefix = blockStart == 0 ? string.Empty : "...";
                        var suffix = (blockStart + blockLen) == minLength ? string.Empty : "...";
                        message += string.Format(
                                                    " At {0}, expected '{3}{1}{4}' was '{3}{2}{4}'",
                                                    firstDiff,
                                                    expectedString.Substring(blockStart, blockLen),
                                                    value.Substring(blockStart, blockLen),
                                                    prefix,
                                                    suffix);
                    }
                    else
                    {
                        if (expectedString.Length > value.Length)
                        {
                            if (expectedString.StartsWith(value, ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture))
                            {
                                message = "The {0} is different from {1}, it is missing the end.";
                            }
                        }
                        else
                        {
                            if (value.StartsWith(expectedString, ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture))
                            {
                                message = "The {0} is different from {1}, it contains extra text at the end.";
                            }
                        }
                    }
                }

                if (isCrlfAndLfDifference)
                {
                    value = HighlightFirstCrlfOrLfIfAny(value, firstDiffPos);
                    expectedString = HighlightFirstCrlfOrLfIfAny(expectedString, firstDiffPos);
                }

                if (isTabAndWhiteSpaceDifference)
                {
                    value = HighlightTabsIfAny(value);
                    expectedString = HighlightTabsIfAny(expectedString);    
                }

                messageText = checker.BuildMessage(message).For("string").On(value).And.Expected(expectedString).ToString();
            }

            return messageText;
        }
Ejemplo n.º 18
0
        private static string ContainsImpl(IChecker<string, ICheck<string>> checker, IEnumerable<string> values, bool negated, bool notContains)
        {
            var checkedValue = checker.Value;

            // special case if checkedvalue is null
            if (checkedValue == null)
            {
                return (negated || notContains) ? null : checker.BuildShortMessage("The {0} is null.").For("string").Expected(values).Label("The {0} substring(s):").ToString();
            }

            var items = values.Where(item => checkedValue.Contains(item) == notContains).ToList();

            if (negated == (items.Count > 0))
            {
                return null;
            }

            if (!notContains && negated)
            {
                items = values.ToList();
            }

            if (negated != notContains)
            {
                return
                    checker.BuildMessage(
                        "The {0} contains unauthorized value(s): " + items.ToEnumeratedString())
                                 .For("string")
                                 .Expected(values)
                                 .Label("The unauthorized substring(s):")
                                 .ToString();
            }

            return
                checker.BuildMessage(
                    "The {0} does not contains the expected value(s): " + items.ToEnumeratedString())
                             .For("string")
                             .Expected(values)
                             .Label("The {0} substring(s):")
                             .ToString();
        }
Ejemplo n.º 19
0
        private static string EndsWithImpl(IChecker<string, ICheck<string>> checker, string ends, bool negated)
        {
            var checkedValue = checker.Value;

            // special case if checkedvalue is null
            if (checkedValue == null)
            {
                return negated ? null : checker.BuildShortMessage("The {0} is null.").For("string").Expected(ends).Comparison("ends with").ToString();
            }

            if (checkedValue.EndsWith(ends) != negated)
            {
                // success
                return null;
            }

            if (negated)
            {
                return
                    checker.BuildMessage("The {0} ends with {1}, whereas it must not.")
                    .For("string").Expected(ends)
                                 .Comparison("does not end with")
                                 .ToString();
            }

            return
                checker.BuildMessage("The {0}'s end is different from the {1}.")
                .For("string")
                             .Expected(ends)
                             .Comparison("ends with")
                             .ToString();
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Checks that an instance is in the inheritance hierarchy of a specified type.
        /// </summary>
        /// <param name="checker">The instance to be checked.</param>
        /// <param name="expectedBaseType">The Type which is expected to be a base Type of the instance.</param>
        /// <exception cref="FluentCheckException">The instance is not in the inheritance hierarchy of the specified type.</exception>
        public static void InheritsFrom(IChecker<object, ICheck<object>> checker, Type expectedBaseType)
        {
            var instanceType = checker.Value.GetTypeWithoutThrowingException();
            if (expectedBaseType.IsAssignableFrom(instanceType))
            {
                return;
            }

            var message = checker.BuildMessage("The {0} does not have the expected inheritance.")
                             .For("expression type")
                             .On(instanceType)
                             .Label("Indeed, the {0} {1}")
                             .And.Expected(expectedBaseType)
                             .Label("is not a derived type of");
            
            throw new FluentCheckException(message.ToString());
        }
Ejemplo n.º 21
0
        private static string IsEmptyImpl(IChecker<string, ICheck<string>> checker, bool canBeNull, bool negated)
        {
            var checkedValue = checker.Value;
            
            // special case if checkedvalue is null
            if (checkedValue == null)
            {
                if (canBeNull != negated)
                {
                    return null;
                }

                return negated ? checker.BuildShortMessage("The {0} is null whereas it must have content.").For("string").ToString()
                    : checker.BuildShortMessage("The {0} is null instead of being empty.").For("string").ToString();
            }

            if (string.IsNullOrEmpty(checkedValue) != negated)
            {
                // success
                return null;
            }

            if (negated)
            {
                return
                    checker.BuildShortMessage("The {0} is empty, whereas it must not.")
                    .For("string")
                                 .ToString();
            }

            return
                checker.BuildMessage("The {0} is not empty or null.")
                .For("string")
                             .On(checkedValue)
                             .ToString();
        }