private void bg_DoWork(object sender, DoWorkEventArgs e) { Word.Application wordApp = e.Argument as Word.Application; BackgroundWorker bg = sender as BackgroundWorker; string lastPage = string.Empty; bool isChanged = false; DateTime dtChaged = DateTime.Now; while (true) { try { if (Application.Documents.Count > 0) { if (Application.ActiveDocument.Words.Count > 0) { var currentPage = Application.ActiveDocument.Bookmarks["\\Page"].Range.Text; if (currentPage != null && currentPage != lastPage) { var differ = new DiffPlex.Differ(); var builder = new DiffPlex.DiffBuilder.InlineDiffBuilder(differ); var difference = builder.BuildDiffModel(lastPage, currentPage); var change = from d in difference.Lines where d.Type != DiffPlex.DiffBuilder.Model.ChangeType.Unchanged select d; if (change.Any()) { string changeLastText = change.Last().Text; if (!string.IsNullOrEmpty(changeLastText)) { dtChaged = DateTime.Now; isChanged = true; } } lastPage = currentPage; } TimeSpan span = (TimeSpan)(DateTime.Now - dtChaged); if (isChanged && span.Milliseconds >= 300) { isChanged = false; bg.ReportProgress(50, ""); } } } } catch (Exception) { } if (bg.CancellationPending) { break; } System.Threading.Thread.Sleep(100); } }
/// <summary> /// Checks that the generated output actually matches the stored output. /// </summary> /// <param name="doctests">Extracted doctests</param> /// <param name="relativeInputPath">Relative path to the input file</param> /// <param name="outputPath">Absolute path to the output doctest file</param> /// <returns>Outcome of the check</returns> public static Report.IReport Check( List <Extraction.Doctest> doctests, string relativeInputPath, string outputPath) { // Pre-condition(s) if (!Path.IsPathRooted(outputPath)) { throw new ArgumentException($"Expected a rooted outputPath, but got: {outputPath}"); } // Implementation if (doctests.Count == 0) { if (File.Exists(outputPath)) { return(new Report.ShouldNotExist()); } return(new Report.Ok()); } if (doctests.Count > 0 && !File.Exists(outputPath)) { return(new Report.DoesntExist()); } string identifier = Identifier(relativeInputPath); using var stringWriter = new System.IO.StringWriter(); Generation.Generate(doctests, identifier, stringWriter); string expected = stringWriter.ToString(); string got = File.ReadAllText(outputPath); // Split and re-join by new lines to be system-agnostic expected = String.Join( System.Environment.NewLine, expected.Split( new[] { "\r\n", "\r", "\n" }, System.StringSplitOptions.None )); got = String.Join( System.Environment.NewLine, got.Split( new[] { "\r\n", "\r", "\n" }, System.StringSplitOptions.None )); if (expected == got) { return(new Report.Ok()); } // Compute the difference if expected and got are not equal var diffBuilder = new DiffPlex.DiffBuilder.InlineDiffBuilder(new DiffPlex.Differ()); var diff = diffBuilder.BuildDiffModel(expected, got); return(new Report.Different(diff)); }
public static void DirectoriesAreEqual( IFileSystemDirectory expectedDataDirectory, IFileSystemDirectory actualDataDirectory, string assertion = "Directories are equal") { var expectedFileSystemEntries = GetFilesWithAttributes(expectedDataDirectory); var actualFileSystemEntries = GetFilesWithAttributes(actualDataDirectory); var assert = ComponentResolver.Instance.Resolve <IClassicAssert>(); var inExpectedOnly = expectedFileSystemEntries.Except(actualFileSystemEntries).ToList(); var inActualOnly = actualFileSystemEntries.Except(expectedFileSystemEntries).ToList(); var messageBuilder = new StringBuilder(); var directoriesInExpectedOnly = GetDirectoryListString(inExpectedOnly); if (directoriesInExpectedOnly != "[]") { messageBuilder.AppendLine($" Directories in expected only: {directoriesInExpectedOnly}"); } var directoriesInActualOnly = GetDirectoryListString(inActualOnly); if (directoriesInActualOnly != "[]") { messageBuilder.AppendLine($" Directories in actual only: {directoriesInActualOnly}"); } var filesInExpectedOnly = GetFileListString(inExpectedOnly); if (filesInExpectedOnly != "[]") { messageBuilder.AppendLine($" Files in expected only: {filesInExpectedOnly}"); } var filesInActualOnly = GetFileListString(inActualOnly); if (filesInActualOnly != "[]") { messageBuilder.AppendLine($" Files in actual only: {filesInActualOnly}"); } // ReSharper disable once InvokeAsExtensionMethod var filesInBothDirs = Enumerable.Intersect(expectedFileSystemEntries, actualFileSystemEntries) .Where(_ => !_.Attributes.HasFlag(FileAttributes.Directory)) .Select(_ => _.Path) .ToList(); var differ = new DiffPlex.DiffBuilder.InlineDiffBuilder(new Differ()); var differingFiles = new List <string>(); foreach (var filePath in filesInBothDirs) { var diffModel = differ.BuildDiffModel( expectedDataDirectory.File.ReadAllText(filePath), actualDataDirectory.File.ReadAllText(filePath)); if (diffModel.Lines.Any(_ => _.Type != ChangeType.Unchanged)) { differingFiles.Add(filePath); } } if (differingFiles.Count > 0) { messageBuilder.Append($" Differing files: [{string.Join(", ", differingFiles)}]\r\n"); } var messagesString = messageBuilder.ToString(); assert.IsTrue(messagesString == "", $"{assertion}" + Environment.NewLine + $" Expected test data directory: {expectedDataDirectory.Url}" + Environment.NewLine + $" Actual test data directory: {actualDataDirectory.Url}" + Environment.NewLine + messagesString); }
private void RequestSourceDiff() { var newSource = ""; var mime = ""; if (!Removed) { var file = DownloadFile(_user, _slug, _branch, _path, out mime); if (mime.StartsWith("text/plain")) newSource = System.Security.SecurityElement.Escape(System.IO.File.ReadAllText(file, System.Text.Encoding.UTF8)); else { LoadFile(file); return; } } var oldSource = ""; if (_parent != null && !Added) { var file = DownloadFile(_user, _slug, _parent, _path, out mime); if (mime.StartsWith("text/plain")) oldSource = System.Security.SecurityElement.Escape(System.IO.File.ReadAllText(file, System.Text.Encoding.UTF8)); else { LoadFile(file); return; } } var differ = new DiffPlex.DiffBuilder.InlineDiffBuilder(new DiffPlex.Differ()); var a = differ.BuildDiffModel(oldSource, newSource); var builder = new StringBuilder(); foreach (var k in a.Lines) { if (k.Type == DiffPlex.DiffBuilder.Model.ChangeType.Deleted) builder.Append("<span style='background-color: #ffe0e0;'>" + k.Text + "</span>"); else if (k.Type == DiffPlex.DiffBuilder.Model.ChangeType.Inserted) builder.Append("<span style='background-color: #e0ffe0;'>" + k.Text + "</span>"); else if (k.Type == DiffPlex.DiffBuilder.Model.ChangeType.Modified) builder.Append("<span style='background-color: #ffffe0;'>" + k.Text + "</span>"); else builder.Append(k.Text); builder.AppendLine(); } LoadRawData(builder.ToString()); }