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());
        }
Exemple #2
0
        /// <summary>
        ///     Builds the error message related to the Equality verification. This should be called only if the test failed (no
        ///     matter it is negated or not).
        /// </summary>
        /// <typeparam name="T">
        ///     Checked type.
        /// </typeparam>
        /// <typeparam name="TU">Checker type.</typeparam>
        /// <param name="checker">
        ///     The check.
        /// </param>
        /// <param name="expected">
        ///     The other operand.
        /// </param>
        /// <param name="isEqual">
        ///     A value indicating whether the two values are equal or not. <c>true</c> if they are equal; <c>false</c> otherwise.
        /// </param>
        /// <param name="usingOperator">true if comparison is done using operator</param>
        /// <returns>
        ///     The error message related to the Equality verification.
        /// </returns>
        public static string BuildErrorMessage <T, TU>(IChecker <T, TU> checker, object expected, bool isEqual,
                                                       bool usingOperator)
            where TU : class, IMustImplementIForkableCheckWithoutDisplayingItsMethodsWithinIntelliSense
        {
            var msg = isEqual
                ? checker.BuildShortMessage("The {0} is equal to the {1} whereas it must not.")
                : checker.BuildShortMessage("The {0} is different from the {1}.");

            FillEqualityErrorMessage(msg, checker.Value, expected, isEqual, usingOperator);

            return(msg.ToString());
        }
Exemple #3
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());
        }
Exemple #4
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());
        }
Exemple #5
0
            public FluentMessage BuildMessage <T>(IChecker <T, ICheck <T> > checker, bool negated)
            {
                FluentMessage result = null;

                if (this.DoValuesMatches == negated)
                {
                    if (negated)
                    {
                        result =
                            checker.BuildShortMessage(
                                string.Format(
                                    "The {{0}}'s {0} has the same value in the comparand, whereas it must not.",
                                    this.Expected.FieldLabel.DoubleCurlyBraces()));
                        EqualityHelper.FillEqualityErrorMessage(result, this.actual.Value, this.expected.Value, true);
                    }
                    else
                    {
                        if (!this.ExpectedFieldFound)
                        {
                            result = checker.BuildShortMessage(
                                string.Format(
                                    "The {{0}}'s {0} is absent from the {{1}}.",
                                    this.Expected.FieldLabel.DoubleCurlyBraces()));
                            result.Expected(this.expected.Value);
                        }
                        else
                        {
                            result =
                                checker.BuildShortMessage(
                                    string.Format(
                                        "The {{0}}'s {0} does not have the expected value.",
                                        this.Expected.FieldLabel.DoubleCurlyBraces()));
                            EqualityHelper.FillEqualityErrorMessage(result, this.actual.Value, this.expected.Value, false);
                        }
                    }
                }

                return(result);
            }
Exemple #6
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());
        }
        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();
        }
        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();
        }
        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();
        }
        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();
        }
        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;
        }
Exemple #12
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);
        }