public static void AssertEqual(string Expected, string Generated, string Message) { if (Generated != Expected) { StringBuilder sb = new StringBuilder(); sb.AppendLine(Message); sb.AppendLine(); sb.AppendLine("Changes required:"); sb.AppendLine(); EditScript <string> Script = Difference.AnalyzeRows(Generated, Expected); foreach (Step <string> Op in Script.Steps) { foreach (string Row in Op.Symbols) { switch (Op.Operation) { case EditOperation.Delete: sb.Append("- "); sb.AppendLine(Row); break; case EditOperation.Insert: sb.Append("+ "); sb.AppendLine(Row); break; } } } throw new Exception(sb.ToString()); } }
private void TestRows(string s1, string s2, string Expected) { EditScript <string> Script = Difference.AnalyzeRows(s1, s2); StringBuilder sb = new StringBuilder(); foreach (Step <string> Step in Script.Steps) { switch (Step.Operation) { case EditOperation.Keep: this.Append <string>(sb, Step.Symbols, " ", "\r\n"); break; case EditOperation.Insert: this.Append <string>(sb, Step.Symbols, "+ ", "\r\n"); break; case EditOperation.Delete: this.Append <string>(sb, Step.Symbols, "- ", "\r\n"); break; } } string Result = sb.ToString(); Assert.AreEqual <string>(Expected, Result); }
/// <summary> /// Sends the contents of the database to support. /// </summary> /// <param name="FileName">Name of file used to keep track of state changes.</param> /// <param name="IncludeUnchanged">If unchanged material should be included.</param> /// <param name="SendAsAlert">If an alert is to be sent.</param> public static async Task EvaluateDatabaseDiff(string FileName, bool IncludeUnchanged, bool SendAsAlert) { Dictionary <string, bool> CollectionNames = new Dictionary <string, bool>(); foreach (string CollectionName in await Database.GetCollections()) { CollectionNames[CollectionName] = true; } CollectionNames.Remove("DnsCache"); string[] Selection = new string[CollectionNames.Count]; CollectionNames.Keys.CopyTo(Selection, 0); StringBuilder Xml = new StringBuilder(); using (XmlDatabaseExport Output = new XmlDatabaseExport(Xml, true, 256)) { await Database.Export(Output, Selection); } string AppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); FileName = Path.Combine(AppDataFolder, FileName); string CurrentState = Xml.ToString(); string PrevState = File.Exists(FileName) ? File.ReadAllText(FileName) : string.Empty; EditScript <string> Script = Difference.AnalyzeRows(PrevState, CurrentState); StringBuilder Markdown = new StringBuilder(); string Prefix; Markdown.Append("Database content changes (`"); Markdown.Append(FileName); Markdown.AppendLine("`):"); foreach (Step <string> Step in Script.Steps) { Markdown.AppendLine(); switch (Step.Operation) { case EditOperation.Keep: default: if (!IncludeUnchanged) { continue; } Prefix = ">\t"; break; case EditOperation.Insert: Prefix = "+>\t"; break; case EditOperation.Delete: Prefix = "->\t"; break; } Markdown.Append(Prefix); Markdown.AppendLine("```xml"); foreach (string Row in Step.Symbols) { Markdown.Append(Prefix); Markdown.Append(Row); Markdown.AppendLine(" "); } Markdown.Append(Prefix); Markdown.AppendLine("```"); } string DiffMsg = Markdown.ToString(); if (SendAsAlert) { await SendAlert(DiffMsg, "text/markdown"); } File.WriteAllText(FileName, CurrentState); File.WriteAllText(FileName + ".diff.md", DiffMsg); }