Example #1
0
        public static string GetAssertMessage <T>(
            IEnumerable <T> expected,
            IEnumerable <T> actual,
            IEqualityComparer <T> comparer = null,
            Func <T, string> toString      = null,
            string separator = ",\r\n",
            string expectedValueSourcePath = null,
            int expectedValueSourceLine    = 0)
        {
            if (toString == null)
            {
                toString = new Func <T, string>(obj => (obj != null) ? obj.ToString() : "<null>");
            }

            var actualString = string.Join(separator, actual.Select(toString));

            var message = new StringBuilder();

            message.AppendLine();
            message.AppendLine("Actual:");
            message.AppendLine(actualString);
            message.AppendLine("Differences:");
            message.AppendLine(DiffUtil.DiffReport(expected, actual, comparer, toString, separator));

            string link;

            if (TryGenerateExpectedSourceFielAndGetDiffLink(actualString, expected.Count(), expectedValueSourcePath, expectedValueSourceLine, out link))
            {
                message.AppendLine(link);
            }

            return(message.ToString());
        }
Example #2
0
        public static string GetAssertMessage <T>(
            IEnumerable <T> expected,
            IEnumerable <T> actual,
            IEqualityComparer <T> comparer = null,
            Func <T, string> itemInspector = null,
            string itemSeparator           = null,
            string expectedValueSourcePath = null,
            int expectedValueSourceLine    = 0)
        {
            if (itemInspector == null)
            {
                if (expected is IEnumerable <byte> )
                {
                    itemInspector = b => $"0x{b:X2}";
                }
                else if (expected is IEnumerable <string> )
                {
                    itemInspector = new Func <T, string>(obj => (obj != null) ? string.Format("\"{0}\"", obj.ToString()) : "<null>");
                }
                else
                {
                    itemInspector = new Func <T, string>(obj => (obj != null) ? obj.ToString() : "<null>");
                }
            }

            if (itemSeparator == null)
            {
                if (expected is IEnumerable <byte> )
                {
                    itemSeparator = ", ";
                }
                else
                {
                    itemSeparator = ",\r\n";
                }
            }

            var expectedString = string.Join(itemSeparator, expected.Select(itemInspector));
            var actualString   = string.Join(itemSeparator, actual.Select(itemInspector));

            var message = new StringBuilder();

            message.AppendLine();
            message.AppendLine("Expected:");
            message.AppendLine(expectedString);
            message.AppendLine("Actual:");
            message.AppendLine(actualString);
            message.AppendLine("Differences:");
            message.AppendLine(DiffUtil.DiffReport(expected, actual, comparer, itemInspector, itemSeparator));

            string link;

            if (TryGenerateExpectedSourceFileAndGetDiffLink(actualString, expected.Count(), expectedValueSourcePath, expectedValueSourceLine, out link))
            {
                message.AppendLine(link);
            }

            return(message.ToString());
        }
Example #3
0
        public static string GetAssertMessage <T>(
            IEnumerable <T> expected,
            IEnumerable <T> actual,
            IEqualityComparer <T> comparer = null,
            Func <T, string> itemInspector = null,
            string itemSeparator           = null,
            string expectedValueSourcePath = null,
            int expectedValueSourceLine    = 0)
        {
            if (itemInspector == null)
            {
                if (typeof(T) == typeof(byte))
                {
                    itemInspector = b => $"0x{b:X2}";
                }
                else
                {
                    itemInspector = new Func <T, string>(obj => (obj != null) ? obj.ToString() : "<null>");
                }
            }

            if (itemSeparator == null)
            {
                if (typeof(T) == typeof(byte))
                {
                    itemSeparator = ", ";
                }
                else
                {
                    itemSeparator = "," + Environment.NewLine;
                }
            }

            var expectedString = string.Join(itemSeparator, expected.Take(10).Select(itemInspector));
            var actualString   = string.Join(itemSeparator, actual.Select(itemInspector));

            var message = new StringBuilder();

            message.AppendLine();
            message.AppendLine("Expected:");
            message.AppendLine(expectedString);
            if (expected.Count() > 10)
            {
                message.AppendLine("... truncated ...");
            }
            message.AppendLine("Actual:");
            message.AppendLine(actualString);
            message.AppendLine("Differences:");
            message.AppendLine(DiffUtil.DiffReport(expected, actual, itemSeparator, comparer, itemInspector));

            if (TryGenerateExpectedSourceFileAndGetDiffLink(actualString, expected.Count(), expectedValueSourcePath, expectedValueSourceLine, out var link))
            {
                message.AppendLine(link);
            }

            return(message.ToString());
        }
Example #4
0
        public static string GetAssertMessage <T>(
            IEnumerable <T> expected,
            IEnumerable <T> actual,
            Func <T, T, bool> comparer     = null,
            Func <T, string> itemInspector = null,
            string itemSeparator           = null,
            string expectedValueSourcePath = null,
            int expectedValueSourceLine    = 0)
        {
            if (itemInspector == null)
            {
                if (expected is IEnumerable <byte> )
                {
                    itemInspector = b => $"0x{b:X2}";
                }
                else
                {
                    itemInspector = new Func <T, string>(obj => (obj != null) ? obj.ToString() : "<null>");
                }
            }

            if (itemSeparator == null)
            {
                if (expected is IEnumerable <byte> )
                {
                    itemSeparator = ", ";
                }
                else
                {
                    itemSeparator = ",\r\n";
                }
            }

            var message = new StringBuilder();

            message.AppendLine();
            message.AppendLine("Actual:");
            message.AppendLine(string.Join(itemSeparator, actual.Select(itemInspector)));

            message.AppendLine();
            message.AppendLine("Expected:");
            message.AppendLine(string.Join(itemSeparator, expected.Select(itemInspector)));

            message.AppendLine();
            message.AppendLine("Diff:");
            message.Append(DiffUtil.DiffReport(expected, actual, comparer, itemInspector, itemSeparator));

            return(message.ToString());
        }
Example #5
0
        public static string GetAssertMessage <T>(IEnumerable <T> expected, IEnumerable <T> actual, IEqualityComparer <T> comparer = null, Func <T, string> toString = null, string separator = ",\r\n")
        {
            if (toString == null)
            {
                toString = new Func <T, string>(obj => obj.ToString());
            }

            var message = new StringBuilder();

            message.AppendLine();
            message.AppendLine("Actual:");
            message.AppendLine(string.Join(separator, actual.Select(toString)));
            message.AppendLine("Differences:");
            message.AppendLine(DiffUtil.DiffReport(expected, actual, comparer, toString, separator));
            return(message.ToString());
        }
Example #6
0
        public static string GetAssertMessage <T>(
            IEnumerable <T> expected,
            IEnumerable <T> actual,
            IEqualityComparer <T> comparer = null,
            Func <T, string> toString      = null,
            string separator = ",\r\n",
            string expectedValueSourcePath = null,
            int expectedValueSourceLine    = 0)
        {
            if (toString == null)
            {
                toString = new Func <T, string>(obj => (obj != null) ? obj.ToString() : "<null>");
            }

            var actualString = string.Join(separator, actual.Select(toString));

            var message = new StringBuilder();

            message.AppendLine();
            message.AppendLine("Actual:");
            message.AppendLine(actualString);
            message.AppendLine("Differences:");
            message.AppendLine(DiffUtil.DiffReport(expected, actual, comparer, toString, separator));

            // add a link to a .cmd file that opens a diff tool:
            if (!string.IsNullOrEmpty(DiffToolPath) && expectedValueSourcePath != null && expectedValueSourceLine != 0)
            {
                var actualFile    = Path.GetTempFileName();
                var testFileLines = File.ReadAllLines(expectedValueSourcePath);

                File.WriteAllLines(actualFile, testFileLines.Take(expectedValueSourceLine));
                File.AppendAllText(actualFile, actualString);
                File.AppendAllLines(actualFile, testFileLines.Skip(expectedValueSourceLine + expected.Count()));

                var compareCmd = Path.GetTempFileName() + ".cmd";
                File.WriteAllText(compareCmd, string.Format("\"{0}\" \"{1}\" \"{2}\"", DiffToolPath, actualFile, expectedValueSourcePath));

                message.AppendLine("file://" + compareCmd);
            }

            return(message.ToString());
        }