Esempio n. 1
0
        private static void Diff(string expected, string actual, string message = null)
        {
            var d             = new Differ();
            var inlineBuilder = new InlineDiffBuilder(d);
            var result        = inlineBuilder.BuildDiffModel(expected, actual);
            var hasChanges    = result.Lines.Any(l => l.Type != ChangeType.Unchanged);

            if (!hasChanges)
            {
                return;
            }

            var diff = result.Lines.Aggregate(new StringBuilder().AppendLine(message), (sb, line) =>
            {
                if (line.Type == ChangeType.Inserted)
                {
                    sb.Append("+ ");
                }
                else if (line.Type == ChangeType.Deleted)
                {
                    sb.Append("- ");
                }
                else
                {
                    sb.Append("  ");
                }
                sb.AppendLine(line.Text);
                return(sb);
            }, sb => sb.ToString());

            throw new Exception(diff);
        }
Esempio n. 2
0
        public string GetDiffedFile()
        {
            var stringBuilder     = new StringBuilder();
            var differ            = new Differ();
            var inlineDiffBuilder = new InlineDiffBuilder(differ);
            var result            = inlineDiffBuilder.BuildDiffModel(oldText, newText);

            foreach (var line in result.Lines)
            {
                if (line.Type == ChangeType.Inserted)
                {
                    stringBuilder.Append("+ ");
                }
                else if (line.Type == ChangeType.Deleted)
                {
                    stringBuilder.Append("- ");
                }
                else if (line.Type == ChangeType.Modified)
                {
                    stringBuilder.Append("* ");
                }
                else if (line.Type == ChangeType.Imaginary)
                {
                    stringBuilder.Append("? ");
                }
                else if (line.Type == ChangeType.Unchanged)
                {
                    stringBuilder.Append("  ");
                }

                stringBuilder.Append(line.Text + "\r\n");
            }

            return(stringBuilder.ToString());
        }
Esempio n. 3
0
        private static void Main(string[] args)
        {
            OldText = CorrectWordnewLine(CorrectInLine(CorrectSpace(OldText)));
            NewText = CorrectWordnewLine(CorrectInLine(CorrectSpace(NewText)));

            var diffBuilder = new InlineDiffBuilder(new Differ());
            var diff        = diffBuilder.BuildDiffModel(OldText, NewText);

            foreach (var line in diff.Lines)
            {
                switch (line.Type)
                {
                case ChangeType.Inserted:
                    FinalText += " {" + line.Text + "}";
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("+ ");
                    break;

                case ChangeType.Deleted:
                    FinalText += " <" + line.Text + ">";
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("- ");
                    break;

                default:
                    FinalText += " " + line.Text;
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("  ");
                    break;
                }
                Console.WriteLine(line.Text);
            }
            Console.WriteLine(CorrectSpace(FinalText).Replace("} {", " ").Replace("> <", " ").Replace("<>", "").Replace("{}", ""));
            Console.ReadKey();
        }
Esempio n. 4
0
        private static string CreateDiff(string expected, string actual, string message, Differ d)
        {
            var inlineBuilder = new InlineDiffBuilder(d);
            var result        = inlineBuilder.BuildDiffModel(expected, actual);
            var hasChanges    = result.Lines.Any(l => l.Type != ChangeType.Unchanged);

            if (!hasChanges)
            {
                return(string.Empty);
            }

            return(result.Lines.Aggregate(new StringBuilder().AppendLine(message), (sb, line) =>
            {
                if (line.Type == ChangeType.Inserted)
                {
                    sb.Append("+ ");
                }
                else if (line.Type == ChangeType.Deleted)
                {
                    sb.Append("- ");
                }
                else
                {
                    sb.Append("  ");
                }
                sb.AppendLine(line.Text);
                return sb;
            }, sb => sb.ToString()));
        }
        private static DiffPaneModel GetDiff(string oldText, string newText)
        {
            var differ        = new Differ();
            var inlineBuilder = new InlineDiffBuilder(differ);

            return(inlineBuilder.BuildDiffModel(oldText, newText));
        }
Esempio n. 6
0
        public static string Compare(string oldText, string newText)
        {
            StringBuilder sb = new StringBuilder();

            var d       = new Differ();
            var builder = new InlineDiffBuilder(d);
            var result  = builder.BuildDiffModel(oldText, newText);

            foreach (var line in result.Lines)
            {
                if (line.Type == ChangeType.Inserted)
                {
                    sb.Append("+ ");
                }
                else if (line.Type == ChangeType.Deleted)
                {
                    sb.Append("- ");
                }
                else if (line.Type == ChangeType.Modified)
                {
                    sb.Append("* ");
                }
                else if (line.Type == ChangeType.Imaginary)
                {
                    sb.Append("? ");
                }
                //else if (line.Type == ChangeType.Unchanged)
                //{
                //    sb.Append("  ");
                //}

                sb.Append(line.Text + "<br/>");
            }
            return(sb.ToString());
        }
Esempio n. 7
0
        public DiffRenderer(string oldText, string newText, int maxContextLines = 3, int tabWidth = 8)
        {
            this.maxContextLines = maxContextLines;

            var inlineBuilder = new InlineDiffBuilder(new Differ());
            var diff          = inlineBuilder.BuildDiffModel(oldText, newText);

            int            maxLineNumber = 0;
            DiffPieceGroup currentGroup  = null;

            foreach (var line in diff.Lines)
            {
                if (currentGroup == null || currentGroup.Type != line.Type)
                {
                    currentGroup = new DiffPieceGroup(currentGroup, line.Type);
                    if (FirstGroup == null)
                    {
                        FirstGroup = currentGroup;
                    }
                }

                if (line.Position != null)
                {
                    maxLineNumber = Math.Max(maxLineNumber, line.Position.Value);
                }

                maxLineWidth = Math.Max(maxLineWidth, GetLineWidth(line.Text, tabWidth));

                currentGroup.Lines.Add(line);
            }

            maxLineNumberDigits = (int)Math.Floor(Math.Log10(maxLineNumber) + 1);
        }
Esempio n. 8
0
        public static DiffPiece[] DiffAndShow(string before, string after)
        {
            var diffBuilder = new InlineDiffBuilder(new Differ());
            var diff        = diffBuilder.BuildDiffModel(before, after);
            var oldColor    = Console.ForegroundColor;

            foreach (var line in diff.Lines)
            {
                switch (line.Type)
                {
                case ChangeType.Inserted:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("+ ");
                    break;

                case ChangeType.Deleted:
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("- ");
                    break;

                default:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("  ");
                    break;
                }
                Console.WriteLine(line.Text);
            }
            Console.ForegroundColor = oldColor;
            return(diff.Lines.Where(line => line.Type != ChangeType.Unchanged).ToArray());
        }
Esempio n. 9
0
            public void Will_build_diffModel_for_duplicate_strings()
            {
                string text = "a\nb\nc\nd\n\n";

                string[] textLines = { "a", "b", "c", "d", "" };
                var      differ    = new Mock <IDiffer>();

                differ.Setup(x => x.CreateLineDiffs(text, text, true))
                .Returns(new DiffResult(textLines, textLines, new List <DiffBlock>()
                {
                    new DiffBlock(0, 0, 5, 0)
                }));
                var builder = new InlineDiffBuilder(differ.Object);

                var bidiff = builder.BuildDiffModel(text, text);

                Assert.NotNull(bidiff);
                Assert.Equal(textLines.Length, bidiff.Lines.Count);
                for (int i = 0; i < textLines.Length; i++)
                {
                    Assert.Equal(textLines[i], bidiff.Lines[i].Text);
                    Assert.Equal(ChangeType.Unchanged, bidiff.Lines[i].Type);
                    Assert.Equal(i + 1, bidiff.Lines[i].Position);
                }
            }
Esempio n. 10
0
            public void Will_build_diffModel_when_oldText_is_empty()
            {
                string textOld = "";
                string textNew = "z\ny\nx\nw\n";

                string[] textLinesOld = { };
                string[] textLinesNew = { "z", "y" };
                var      differ       = new Mock <IDiffer>();

                differ.Setup(x => x.CreateLineDiffs(textOld, textNew, true))
                .Returns(new DiffResult(textLinesOld, textLinesNew, new List <DiffBlock> {
                    new DiffBlock(0, 0, 0, 2)
                }));
                var builder = new InlineDiffBuilder(differ.Object);

                var bidiff = builder.BuildDiffModel(textOld, textNew);

                Assert.NotNull(bidiff);
                Assert.Equal(2, bidiff.Lines.Count);

                for (int j = 0; j < textLinesNew.Length; j++)
                {
                    Assert.Equal(textLinesNew[j], bidiff.Lines[j].Text);
                    Assert.Equal(ChangeType.Inserted, bidiff.Lines[j].Type);
                    Assert.Equal(j + 1, bidiff.Lines[j].Position);
                }
            }
Esempio n. 11
0
        public static Task Report(string receivedFile, string verifiedFile, string?message)
        {
#if (!DEBUG)
            var receivedText = File.ReadAllText(receivedFile);
            var verifiedText = File.ReadAllText(verifiedFile);
            var diffBuilder  = new InlineDiffBuilder(new Differ());
            var diff         = diffBuilder.BuildDiffModel(verifiedText, receivedText);

            foreach (var line in diff.Lines)
            {
                if (line.Type == ChangeType.Unchanged)
                {
                    continue;
                }

                var prefix = "  ";
                switch (line.Type)
                {
                case ChangeType.Inserted:
                    prefix = "+ ";
                    break;

                case ChangeType.Deleted:
                    prefix = "- ";
                    break;
                }

                Console.WriteLine("{0}{1}", prefix, line.Text);
            }
#endif

            return(Task.CompletedTask);
        }
Esempio n. 12
0
        private void DiffButton_Click(object sender, RoutedEventArgs e)
        {
            if (SideBySideDiff.Visibility == Visibility.Visible)
            {
                SideBySideDiff.Visibility = Visibility.Collapsed;
                InlineDiff.Visibility     = Visibility.Visible;
                if (inline == null)
                {
                    var builder = new InlineDiffBuilder(new Differ());
                    inline = builder.BuildDiffModel(TestData.DuplicateText(TestData.OldText, 50), TestData.DuplicateText(TestData.NewText, 50));
                }

                InlineDiff.DiffModel = inline;
                return;
            }

            InlineDiff.Visibility     = Visibility.Collapsed;
            SideBySideDiff.Visibility = Visibility.Visible;
            if (sideBySide == null)
            {
                var builder = new SideBySideDiffBuilder(new Differ());
                sideBySide = builder.BuildDiffModel(TestData.DuplicateText(TestData.OldText, 50), TestData.DuplicateText(TestData.NewText, 50));
            }

            SideBySideDiff.DiffModel = sideBySide;
        }
Esempio n. 13
0
            public void Will_build_diffModel_when_newText_is_empty()
            {
                string textNew = "";
                string textOld = "z\ny\nx\nw\n";

                string[] textLinesNew = { };
                string[] textLinesOld = { "z", "y" };
                var      differ       = new Mock <IDiffer>();

                differ.Setup(x => x.CreateDiffs(textOld, textNew, true, false, It.IsNotNull <IChunker>()))
                .Returns(new DiffResult(textLinesOld, textLinesNew, new List <DiffBlock> {
                    new DiffBlock(0, 2, 0, 0)
                }));
                var builder = new InlineDiffBuilder(differ.Object);

                var bidiff = builder.BuildDiffModel(textOld, textNew);

                Assert.NotNull(bidiff);
                Assert.Equal(2, bidiff.Lines.Count);

                for (int j = 0; j < textLinesOld.Length; j++)
                {
                    Assert.Equal(textLinesOld[j], bidiff.Lines[j].Text);
                    Assert.Equal(ChangeType.Deleted, bidiff.Lines[j].Type);
                    Assert.Null(bidiff.Lines[j].Position);
                }

                Assert.True(bidiff.HasDifferences);
            }
Esempio n. 14
0
        public bool Commit2(string newContent)
        {
            //var mc = new FlowMicroCommit();

            var diffBuilder = new InlineDiffBuilder(new Differ());
            var diff        = diffBuilder.BuildDiffModel(FullContent, newContent);

            Form1.D3.Clear();

            string log3 = "";

            string log2 = "";

            foreach (var dl in diff.Lines)
            {
                switch (dl.Type)
                {
                case ChangeType.Unchanged:
                {
                    log2 += "[ ] " + dl.Text + "\r\n";
                    break;
                }

                case ChangeType.Deleted: break;

                case ChangeType.Modified:
                {
                    log2 += "[m] " + dl.Text + "\r\n";
                    break;
                }

                case ChangeType.Inserted:
                {
                    log2 += "[i] " + dl.Text + "\r\n";
                    break;
                }

                case ChangeType.Imaginary:
                {
                    log2 += "[g] " + dl.Text + "\r\n";
                    break;
                }
                }
                //var mc = new FlowMicroCommit();
                //mc.N = LastN++;
                //mc.P = dl.Position.Value;
                //mc.C = dl.Text;

                log3 += string.Format("{0}| {1}, \"{2}\"\r\n", dl.Type, dl.Position, dl.Text);

                //dl.Type
            }

            Form1.D2.Text = log2;
            Form1.D3.Text = log3;

            LastN++;
            FullContent = newContent;
            return(false);
        }
Esempio n. 15
0
        public void Report(string approved, string received)
        {
            var approvedText = File.Exists(approved) ? File.ReadAllText(approved) : string.Empty;
            var receivedText = File.ReadAllText(received);

            var diffBuilder = new InlineDiffBuilder(new Differ());
            var diff        = diffBuilder.BuildDiffModel(approvedText, receivedText);

            foreach (var line in diff.Lines)
            {
                if (line.Type == ChangeType.Unchanged)
                {
                    continue;
                }

                var prefix = "  ";
                switch (line.Type)
                {
                case ChangeType.Inserted:
                    prefix = "+ ";
                    break;

                case ChangeType.Deleted:
                    prefix = "- ";
                    break;
                }

                Output.WriteLine("{0}{1}", prefix, line.Text);
            }
        }
Esempio n. 16
0
        private static void Main()
        {
            var diffBuilder = new InlineDiffBuilder(new Differ());
            var diff        = diffBuilder.BuildDiffModel(OldText, NewText);

            foreach (var line in diff.Lines)
            {
                switch (line.Type)
                {
                case ChangeType.Inserted:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("+ ");
                    break;

                case ChangeType.Deleted:
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("- ");
                    break;

                default:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("  ");
                    break;
                }

                Console.WriteLine(line.Text);
            }
            Console.ReadKey();
        }
Esempio n. 17
0
        private async Task <Boolean> getUrlChanges(Link link, Boolean enableOverwrite)
        {
            using (HttpClient client = new HttpClient()) {
                using (HttpResponseMessage response = await client.GetAsync(link.Url, HttpCompletionOption.ResponseHeadersRead)) {
                    Boolean shouldAdd = false;
                    if (System.IO.File.Exists(link.PathToFile))
                    {
                        shouldAdd = true;
                        string newText = await response.Content.ReadAsStringAsync();

                        string oldText = await System.IO.File.ReadAllTextAsync(link.PathToFile);

                        var d       = new Differ();
                        var builder = new InlineDiffBuilder(d);
                        var result  = builder.BuildDiffModel(oldText, newText);

                        foreach (var line in result.Lines)
                        {
                            if (line.Type == ChangeType.Inserted)
                            {
                                additions.Add(line.Text);
                            }
                        }
                    }
                    else
                    {
                        using (Stream streamToWriteTo = System.IO.File.Open(link.PathToFile, FileMode.Create))
                        {
                            Stream webStream = await response.Content.ReadAsStreamAsync();

                            await webStream.CopyToAsync(streamToWriteTo);
                        }
                    }

                    if (enableOverwrite)
                    {
                        using (Stream streamToWriteTo = System.IO.File.Open(link.PathToFile, FileMode.Create))
                        {
                            Stream webStream = await response.Content.ReadAsStreamAsync();

                            await webStream.CopyToAsync(streamToWriteTo);
                        }
                    }

                    List <string> res = new List <string>();
                    if (shouldAdd)
                    {
                        foreach (var addition in additions)
                        {
                            res.Add(addition);
                        }
                    }
                    changesPerUrl.Add(link.Url, res);
                    additions.Clear();
                }
            }

            return(true);
        }
        public static void Diff(this string expected, string actual, string message = null)
        {
            var d             = new Differ();
            var inlineBuilder = new InlineDiffBuilder(d);
            var result        = inlineBuilder.BuildDiffModel(expected, actual);
            var hasChanges    = result.Lines.Any(l => l.Type != ChangeType.Unchanged);

            if (!hasChanges)
            {
                return;
            }

            var diff = result.Lines.Aggregate(new StringBuilder().AppendLine(message), (sb, line) =>
            {
                if (line.Type == ChangeType.Inserted)
                {
                    sb.Append("+ ");
                }
                else if (line.Type == ChangeType.Deleted)
                {
                    sb.Append("- ");
                }
                else
                {
                    sb.Append("  ");
                }
                sb.AppendLine(line.Text);
                return(sb);
            }, sb => sb.ToString());

            diff += "\r\n C# approximation of actual ------ ";
            diff += "\r\n new ";
            var approx = Regex.Replace(actual, @"^(?=.*:.*)[^:]+:", (s) => s
                                       .Value.Replace("\"", "")
                                       .Replace(":", " =")
                                       , RegexOptions.Multiline)
                         .Replace(" = {", " = new {")
                         .Replace(" = [", " = new [] {")
            ;

            approx = Regex.Replace(approx, @"^\s*\],?.*$", s => s.Value.Replace("]", "}"), RegexOptions.Multiline);
            diff  += approx + ";";

            diff  += "\r\n C# approximation of expected ------ ";
            diff  += "\r\n new ";
            approx = Regex.Replace(expected, @"^(?=.*:.*)[^:]+:", (s) => s
                                   .Value.Replace("\"", "")
                                   .Replace(":", " =")
                                   , RegexOptions.Multiline)
                     .Replace(" = {", " = new {")
                     .Replace(" = [", " = new [] {")
            ;
            approx = Regex.Replace(approx, @"^\s*\],?.*$", s => s.Value.Replace("]", "}"), RegexOptions.Multiline);
            diff  += approx + ";";


            throw new Exception(diff.Substring(0, diff.Length > 4896 ? 4896 : diff.Length));
        }
Esempio n. 19
0
        /// <summary>
        /// Insert line, mark with "++++"
        /// delete line, mark with "----"
        /// </summary>
        /// <param name="oldFileFullPath"></param>
        /// <param name="newFileFullPath"></param>
        /// <param name="diffMessage"></param>
        /// <returns></returns>
        private static bool CompareFiles(string oldFileFullPath, string newFileFullPath, out string diffMessage)
        {
            StringBuilder diffSb = new StringBuilder();

            var    diffBuilder = new InlineDiffBuilder(new Differ());
            string oldText     = File.ReadAllText(oldFileFullPath);
            string newText     = File.ReadAllText(newFileFullPath);

            var diff = diffBuilder.BuildDiffModel(oldText, newText);

            int lineCount = 1;

            foreach (var line in diff.Lines)
            {
                switch (line.Type)
                {
                case ChangeType.Inserted:
                    if (line.Position.HasValue)
                    {
                        diffSb.AppendLine(string.Format("Line:{0} ++++", line.Position));
                    }
                    else
                    {
                        diffSb.AppendLine("++++");
                    }

                    diffSb.AppendLine(line.Text);
                    break;

                case ChangeType.Deleted:
                    if (line.Position.HasValue)
                    {
                        diffSb.AppendLine(string.Format("Line:{0} ----", line.Position));
                    }
                    else
                    {
                        diffSb.AppendLine("----");
                    }
                    diffSb.AppendLine(line.Text);
                    break;

                default:
                    break;
                }
                lineCount++;
            }
            diffMessage = diffSb.ToString();

            if (string.IsNullOrEmpty(diffMessage))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 20
0
        public string GetDiff(SiteResponse currentResponse, SiteResponse lastResponse)
        {
            string currentResponseText = SiteHtmlUtil.StripHTMLAdvanced(currentResponse.Contents);
            string lastResponseText    = "";

            if (lastResponse != null)
            {
                lastResponseText = SiteHtmlUtil.StripHTMLAdvanced(lastResponse.Contents);
            }

            StringBuilder     stringBuilder     = new StringBuilder();
            Differ            differ            = new Differ();
            InlineDiffBuilder inlineDiffBuilder = new InlineDiffBuilder(differ);
            var  result = inlineDiffBuilder.BuildDiffModel(lastResponseText, currentResponseText);
            bool added_anchor_to_first_change = false;

            foreach (var line in result.Lines)
            {
                if (line.Type == ChangeType.Inserted)
                {
                    if (!added_anchor_to_first_change)
                    {
                        stringBuilder.Append("<p class='added'><a name='anchor'>+</a> ");
                        added_anchor_to_first_change = true;
                    }
                    else
                    {
                        stringBuilder.Append("<p class='added'>+ ");
                    }
                    stringBuilder.AppendLine(line.Text);
                    stringBuilder.Append("</p>");
                }
                else if (line.Type == ChangeType.Deleted)
                {
                    if (!added_anchor_to_first_change)
                    {
                        stringBuilder.Append("<p class='removed'><a name='anchor'>-</a> ");
                        added_anchor_to_first_change = true;
                    }
                    else
                    {
                        stringBuilder.Append("<p class='removed'>- ");
                    }
                    stringBuilder.AppendLine(line.Text);
                    stringBuilder.Append("</p>");
                }
                else
                {
                    stringBuilder.Append("<p class='notchanged'>");
                    stringBuilder.AppendLine(line.Text);
                    stringBuilder.Append("</p>");
                }

                //stringBuilder.Append("<br/>");
            }
            return(stringBuilder.ToString());
        }
Esempio n. 21
0
        public static void EqualOrDiff(string expected, string actual, string message = null)
        {
            if (expected == actual)
            {
                return;
            }

            var diff           = s_diffBuilder.BuildDiffModel(expected, actual, ignoreWhitespace: false, ignoreCase: false, s_lineChunker);
            var messageBuilder = new StringBuilder();

            messageBuilder.AppendLine(
                string.IsNullOrEmpty(message)
                    ? "Actual and expected values differ. Expected shown in baseline of diff:"
                    : message);

            if (!diff.Lines.Any(line => line.Type == ChangeType.Inserted || line.Type == ChangeType.Deleted))
            {
                // We have a failure only caused by line ending differences; recalculate with line endings visible
                diff = s_diffBuilder.BuildDiffModel(expected, actual, ignoreWhitespace: false, ignoreCase: false, s_lineEndingsPreservingChunker);
            }

            foreach (var line in diff.Lines)
            {
                switch (line.Type)
                {
                case ChangeType.Inserted:
                    messageBuilder.Append('+');
                    break;

                case ChangeType.Deleted:
                    messageBuilder.Append('-');
                    break;

                default:
                    messageBuilder.Append(' ');
                    break;
                }

                messageBuilder.AppendLine(line.Text.Replace("\r", "<CR>").Replace("\n", "<LF>"));
            }

            Assert.True(false, messageBuilder.ToString());
        }
Esempio n. 22
0
            public void Will_build_diffModel_for_multiple_diff_blocks()
            {
                string textOld = "1\n2\na\nb\nc\nd\ne\f\n";
                string textNew = "1\n2\nz\ny\nc\nw\ne\f\n";

                string[] textLinesOld = { "1", "2", "a", "b", "c", "d", "e", "f" };
                string[] textLinesNew = { "1", "2", "z", "y", "c", "w", "e", "f" };
                var      differ       = new Mock <IDiffer>();

                differ.Setup(x => x.CreateDiffs(textOld, textNew, true, false, It.IsNotNull <IChunker>()))
                .Returns(new DiffResult(textLinesOld, textLinesNew, new List <DiffBlock> {
                    new DiffBlock(2, 2, 2, 2), new DiffBlock(5, 1, 5, 1)
                }));
                var builder = new InlineDiffBuilder(differ.Object);

                var bidiff = builder.BuildDiffModel(textOld, textNew);

                Assert.NotNull(bidiff);
                Assert.Equal(11, bidiff.Lines.Count);

                Assert.Equal("1", bidiff.Lines[0].Text);
                Assert.Equal(ChangeType.Unchanged, bidiff.Lines[0].Type);
                Assert.Equal(1, bidiff.Lines[0].Position);
                Assert.Equal("2", bidiff.Lines[1].Text);
                Assert.Equal(ChangeType.Unchanged, bidiff.Lines[1].Type);
                Assert.Equal(2, bidiff.Lines[1].Position);
                Assert.Equal("a", bidiff.Lines[2].Text);
                Assert.Equal(ChangeType.Deleted, bidiff.Lines[2].Type);
                Assert.Null(bidiff.Lines[2].Position);
                Assert.Equal("b", bidiff.Lines[3].Text);
                Assert.Equal(ChangeType.Deleted, bidiff.Lines[3].Type);
                Assert.Null(bidiff.Lines[3].Position);
                Assert.Equal("z", bidiff.Lines[4].Text);
                Assert.Equal(ChangeType.Inserted, bidiff.Lines[4].Type);
                Assert.Equal(3, bidiff.Lines[4].Position);
                Assert.Equal("y", bidiff.Lines[5].Text);
                Assert.Equal(ChangeType.Inserted, bidiff.Lines[5].Type);
                Assert.Equal(4, bidiff.Lines[5].Position);
                Assert.Equal("c", bidiff.Lines[6].Text);
                Assert.Equal(ChangeType.Unchanged, bidiff.Lines[6].Type);
                Assert.Equal(5, bidiff.Lines[6].Position);
                Assert.Equal("d", bidiff.Lines[7].Text);
                Assert.Equal(ChangeType.Deleted, bidiff.Lines[7].Type);
                Assert.Null(bidiff.Lines[7].Position);
                Assert.Equal("w", bidiff.Lines[8].Text);
                Assert.Equal(ChangeType.Inserted, bidiff.Lines[8].Type);
                Assert.Equal(6, bidiff.Lines[8].Position);
                Assert.Equal("e", bidiff.Lines[9].Text);
                Assert.Equal(ChangeType.Unchanged, bidiff.Lines[9].Type);
                Assert.Equal(7, bidiff.Lines[9].Position);
                Assert.Equal("f", bidiff.Lines[10].Text);
                Assert.Equal(ChangeType.Unchanged, bidiff.Lines[10].Type);
                Assert.Equal(8, bidiff.Lines[10].Position);
                Assert.True(bidiff.HasDifferences);
            }
Esempio n. 23
0
        public static void DownloadPage(Uri uri, ExtractResult result, ExtractBlock block, PageDownloadHandler handler, int maxRetry = 10)
        {
            handler(uri, result);

            var pages = new Dictionary <string, ExtractResult>();

            pages.Add(uri.ToString(), result);

            var lines  = String.Join("\n", result.Paging.Distinct());
            var reader = new StringReader(lines);

            var crawler = new RuiJiCrawler();

            var url = reader.ReadLine();

            var diffBuilder = new InlineDiffBuilder(new Differ());

            while (!string.IsNullOrEmpty(url))
            {
                var u = new Uri(uri, url);
                if (pages.ContainsKey(u.ToString()))
                {
                    url = reader.ReadLine();
                    continue;
                }

                var request = new Request(u);

                var response = crawler.Request(request);
                var content  = response.Data.ToString();

                var r = RuiJiExtractor.Extract(content, block);
                if (r.Paging == null || r.Paging.Count == 0)
                {
                    Thread.Sleep(5000);
                    if (--maxRetry == 0)
                    {
                        break;
                    }

                    continue;
                }

                pages.Add(u.ToString(), r);
                handler(u, r);

                var nlines = String.Join("\n", r.Paging.Distinct());
                var diff   = diffBuilder.BuildDiffModel(lines, nlines);

                nlines = string.Join("\n", diff.Lines.Select(m => m.Text));
                reader = new StringReader(nlines);
                url    = reader.ReadLine();
            }
        }
        public static DiffResult Diff([CanBeNull] this string actual, [CanBeNull] string expected)
        {
            var diffBuilder = new InlineDiffBuilder(new Differ());
            var diff        = diffBuilder.BuildDiffModel(expected, actual);

            if (diff.Lines.All(p => p.Type == ChangeType.Unchanged))
            {
                return(DiffResult.Equivalent);
            }
            return(new DiffResult(diff.Lines));
        }
Esempio n. 25
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var diffBuilder = new InlineDiffBuilder(new Differ());
            var before      = "The text without changes\nThe text without changes\nThe text without changes\n";
            var after       = "The text without changes\nThe text with change. A little one\nThe text without changes\n";
            var diff        = diffBuilder.BuildDiffModel(before, after);

            _ = diff;
        }
Esempio n. 26
0
            public void Will_throw_is_NewText_is_null()
            {
                var differ  = new Mock <IDiffer>();
                var builder = new InlineDiffBuilder(differ.Object);

                var ex = Record.Exception(() => builder.BuildDiffModel("asa", null));

                Assert.IsType <ArgumentNullException>(ex);
                var an = (ArgumentNullException)ex;

                Assert.Equal("newText", an.ParamName);
            }
Esempio n. 27
0
        /// <summary>
        /// Compare changes between snapshots and add to cache
        /// </summary>
        private void Compare(SnapshotVM currrent, SnapshotVM parent)
        {
            if (parent == null)
            {
                return;
            }
            DiffsDeleted[currrent.Index] = new Dictionary <int, Dictionary <int, LineState> >();
            DiffsDeleted[currrent.Index][parent.Index] = new Dictionary <int, LineState>();
            DiffsChanged[currrent.Index] = new Dictionary <int, Dictionary <int, LineState> >();
            DiffsChanged[currrent.Index][parent.Index]       = new Dictionary <int, LineState>();
            DiffsParentLineNum[currrent.Index]               = new Dictionary <int, Dictionary <int, int> >();
            DiffsParentLineNum[currrent.Index][parent.Index] = new Dictionary <int, int>();

            var fileComparer = new InlineDiffBuilder(new Differ());
            var diff         = fileComparer.BuildDiffModel(parent.File, currrent.File);
            int updatedLines = -1;
            int deletedLines = -1;
            int parentLineIn = -1;

            foreach (var line in diff.Lines)
            {
                updatedLines++;
                deletedLines++;
                parentLineIn++;

                switch (line.Type)
                {
                case ChangeType.Modified:
                    DiffsChanged[currrent.Index][parent.Index][updatedLines] = LineState.Modified;
                    break;

                case ChangeType.Inserted:
                    DiffsChanged[currrent.Index][parent.Index][updatedLines] = LineState.Inserted;
                    deletedLines--;
                    parentLineIn--;
                    break;

                case ChangeType.Deleted:
                    DiffsDeleted[currrent.Index][parent.Index][deletedLines] = LineState.Deleted;
                    updatedLines--;
                    break;

                default:
                    break;
                }

                if (line.Type != ChangeType.Deleted)
                {
                    DiffsParentLineNum[currrent.Index][parent.Index][updatedLines] = parentLineIn;
                }
            }
        }
Esempio n. 28
0
        public static string DiffText(string left, string right)
        {
            var differ  = new Differ();
            var builder = new InlineDiffBuilder(differ);
            var diff    = builder.BuildDiffModel(left, right);
            var result  = new List <string>();

            foreach (var line in diff.Lines.Where(line => line.Type != ChangeType.Unchanged))
            {
                result.Add(DiffStatuses[(int)line.Type] + " " + line.Text + " ");
            }
            return(string.Join(Environment.NewLine, result));
        }
Esempio n. 29
0
        /// <summary>
        /// Determines if the given actual and expected text is equivalent.
        /// </summary>
        /// <param name="expected">The expected text.</param>
        /// <param name="actual">The actual text.</param>
        /// <returns>
        /// The text differences.
        /// </returns>
        public virtual bool IsEqual(string expected, string actual)
        {
            var differ        = new Differ();
            var inlineBuilder = new InlineDiffBuilder(differ);

            this.differences = inlineBuilder.BuildDiffModel(expected, actual);

            if (!AreAllLinesUnchanged(this.differences))
            {
                this.OnTextDifferenceDetected(this.differences);
            }

            return(AreAllLinesUnchanged(this.differences));
        }
Esempio n. 30
0
            public void Will_build_diffModel_for_middle_is_different_documents()
            {
                string textOld = "1\n2\na\nb\nc\nd\ne\f\n";
                string textNew = "1\n2\nz\ny\nx\nw\ne\f\n";

                string[] textLinesOld = { "1", "2", "a", "b", "c", "d", "e", "f" };
                string[] textLinesNew = { "1", "2", "z", "y", "x", "w", "e", "f" };
                var      differ       = new Mock <IDiffer>();

                differ.Setup(x => x.CreateDiffs(textOld, textNew, true, false, It.IsNotNull <IChunker>()))
                .Returns(new DiffResult(textLinesOld, textLinesNew, new List <DiffBlock> {
                    new DiffBlock(2, 4, 2, 4)
                }));
                var builder = new InlineDiffBuilder(differ.Object);

                var bidiff = builder.BuildDiffModel(textOld, textNew);

                Assert.NotNull(bidiff);
                Assert.Equal(12, bidiff.Lines.Count);

                Assert.Equal("1", bidiff.Lines[0].Text);
                Assert.Equal(ChangeType.Unchanged, bidiff.Lines[0].Type);
                Assert.Equal(1, bidiff.Lines[0].Position);
                Assert.Equal("2", bidiff.Lines[1].Text);
                Assert.Equal(ChangeType.Unchanged, bidiff.Lines[1].Type);
                Assert.Equal(2, bidiff.Lines[1].Position);

                for (int i = 2; i <= 5; i++)
                {
                    Assert.Equal(textLinesOld[i], bidiff.Lines[i].Text);
                    Assert.Equal(ChangeType.Deleted, bidiff.Lines[i].Type);
                    Assert.Null(bidiff.Lines[i].Position);
                }

                for (int i = 6; i <= 9; i++)
                {
                    Assert.Equal(textLinesNew[i - 4], bidiff.Lines[i].Text);
                    Assert.Equal(ChangeType.Inserted, bidiff.Lines[i].Type);
                    Assert.Equal(i - 3, bidiff.Lines[i].Position);
                }

                Assert.Equal("e", bidiff.Lines[10].Text);
                Assert.Equal(ChangeType.Unchanged, bidiff.Lines[10].Type);
                Assert.Equal(7, bidiff.Lines[10].Position);
                Assert.Equal("f", bidiff.Lines[11].Text);
                Assert.Equal(ChangeType.Unchanged, bidiff.Lines[11].Type);
                Assert.Equal(8, bidiff.Lines[11].Position);
                Assert.True(bidiff.HasDifferences);
            }