Example #1
0
        public string HighlightDifferences(string expected, string actual)
        {
            if (expected == null)
            {
                expected = "null";
            }
            if (actual == null)
            {
                actual = "null";
            }

            expected = _transform(expected);
            actual   = _transform(actual);
            int maxLengthOfStrings = Math.Max(actual.Length, expected.Length);

            var output = new StringBuilder();

            if (maxLengthOfStrings < maxDiffLength)
            {
                var formattedDetailedDiffString = new FormattedDetailedDifferenceString(actual, expected, _sensitivity, 0);
                output.Append(formattedDetailedDiffString);
                return(output.ToString());
            }
            else
            {
                var indicesOfAllDiffs           = GetIndicesOfAllDifferences(actual, expected);
                var differenceIndexConsolidator = new DifferenceIndexConsolidator(maxDiffLength, maxLengthOfStrings, indicesOfAllDiffs);
                var startIndicesOfAllDiffs      = differenceIndexConsolidator.GetConsolidatedIndices();

                if (startIndicesOfAllDiffs.Count > maxNumberOfDiffs)
                {
                    output.AppendLine(string.Format("Showing some of the {0} differences", indicesOfAllDiffs.Count));
                    startIndicesOfAllDiffs = startIndicesOfAllDiffs.Take(maxNumberOfDiffs).ToList();
                }

                for (int index = 0; index < startIndicesOfAllDiffs.Count; index++)
                {
                    var startIndexOfDiffString = startIndicesOfAllDiffs[index];
                    var trimmedActualValue     = TrimmedValue(actual, startIndexOfDiffString);
                    var trimmedExpectedValue   = TrimmedValue(expected, startIndexOfDiffString);

                    var prefixWithDots = startIndexOfDiffString != 0;
                    var suffixWithDots = startIndexOfDiffString + maxDiffLength < maxLengthOfStrings;
                    var formattedDetailedDiffString = new FormattedDetailedDifferenceString(
                        trimmedActualValue, trimmedExpectedValue, _sensitivity,
                        startIndexOfDiffString, prefixWithDots, suffixWithDots);
                    if (index > 0)
                    {
                        output.AppendLine();
                        output.AppendLine();
                    }
                    output.Append(formattedDetailedDiffString);
                }
                return(output.ToString());
            }
        }
        public string HighlightDifferences(IShouldlyAssertionContext context)
        {
            var actualValue   = context.Actual as string;
            var expectedValue = context.Expected as string;

            int maxLengthOfStrings = Math.Max(actualValue.Length, expectedValue.Length);

            var output          = new StringBuilder();
            var caseSensitivity = context.CaseSensitivity ?? Case.Sensitive;

            output.AppendLine(string.Format("{0}", caseSensitivity == Case.Insensitive ? "Case Insensitive Comparison" : "Case Sensitive Comparison"));

            if (maxLengthOfStrings < maxDiffLength)
            {
                var formattedDetailedDiffString = new FormattedDetailedDifferenceString(actualValue, expectedValue, caseSensitivity, 0);
                output.Append(formattedDetailedDiffString);
                return(output.ToString());
            }
            else
            {
                var indicesOfAllDiffs           = GetIndicesOfAllDifferences(actualValue, expectedValue, caseSensitivity);
                var differenceIndexConsolidator = new DifferenceIndexConsolidator(maxDiffLength, maxLengthOfStrings, indicesOfAllDiffs);
                var startIndicesOfAllDiffs      = differenceIndexConsolidator.GetConsolidatedIndices();

                if (startIndicesOfAllDiffs.Count > maxNumberOfDiffs)
                {
                    output.AppendLine(string.Format("Showing some of the {0} differences", indicesOfAllDiffs.Count));
                    startIndicesOfAllDiffs = startIndicesOfAllDiffs.Take(maxNumberOfDiffs).ToList();
                }

                foreach (var startIndexOfDiffString in startIndicesOfAllDiffs)
                {
                    string trimmedActualValue   = "";
                    string trimmedExpectedValue = "";

                    if (startIndexOfDiffString < actualValue.Length)
                    {
                        if (actualValue.Length >= startIndexOfDiffString + maxDiffLength)
                        {
                            trimmedActualValue = actualValue.Substring(startIndexOfDiffString, maxDiffLength);
                        }
                        else
                        {
                            trimmedActualValue = actualValue.Substring(startIndexOfDiffString);
                        }
                    }

                    if (startIndexOfDiffString < expectedValue.Length)
                    {
                        if (expectedValue.Length >= startIndexOfDiffString + maxDiffLength)
                        {
                            trimmedExpectedValue = expectedValue.Substring(startIndexOfDiffString, maxDiffLength);
                        }
                        else
                        {
                            trimmedExpectedValue = expectedValue.Substring(startIndexOfDiffString);
                        }
                    }

                    var prefixWithDots = startIndexOfDiffString != 0;
                    var suffixWithDots = startIndexOfDiffString + maxDiffLength < maxLengthOfStrings;
                    var formattedDetailedDiffString = new FormattedDetailedDifferenceString(trimmedActualValue, trimmedExpectedValue, caseSensitivity, startIndexOfDiffString, prefixWithDots, suffixWithDots);
                    output.Append(formattedDetailedDiffString);
                }
                return(output.ToString());
            }
        }