protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { expectedOutput = NormalizeNewlines(expectedOutput); IDocument doc = GetResult(policy, input, mode, options); if (expectedOutput != doc.Text) { Console.WriteLine ("expected:"); Console.WriteLine (expectedOutput); Console.WriteLine ("got:"); Console.WriteLine (doc.Text); for (int i = 0; i < expectedOutput.Length && i < doc.TextLength; i++) { if (expectedOutput [i] != doc.GetCharAt(i)) { Console.WriteLine ( "i:"+i+" differ:"+ expectedOutput[i].ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t") + " !=" + doc.GetCharAt(i).ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t") ); Console.WriteLine(">"+expectedOutput.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t")); Console.WriteLine(">"+doc.Text.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t")); break; } } } Assert.AreEqual (expectedOutput, doc.Text); return doc; }
private void Serialize(StringBuilder sb, object value, FormattingMode mode) { if (value == null) { sb.Append("null"); return; } switch (mode) { case FormattingMode.Default: this.SerializeDefault(sb, value); break; case FormattingMode.Members: this.SerializeMembers(sb, value); break; case FormattingMode.Items: this.SerializeItems(sb, value, false); break; case FormattingMode.ItemsMembers: this.SerializeItems(sb, value, true); break; case FormattingMode.DoNotLog: sb.Append("-"); break; default: throw new ArgumentOutOfRangeException("mode", string.Format(CultureInfo.InvariantCulture, "Unexpected formatting mode '{0}'.", mode)); } }
protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { StringBuilderDocument document; var changes = GetChanges(policy, input, out document, mode, options); changes.ApplyChanges(); return document; }
protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive) { expectedOutput = NormalizeNewlines(expectedOutput); IDocument doc = GetResult(policy, input, mode); if (expectedOutput != doc.Text) { Console.WriteLine (doc.Text); } Assert.AreEqual (expectedOutput, doc.Text); return doc; }
protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, out StringBuilderDocument document, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { options = GetActualOptions(options); input = NormalizeNewlines(input); document = new StringBuilderDocument(input); var visitor = new CSharpFormatter(policy, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser().Parse(document, "test.cs"); return visitor.AnalyzeFormatting(document, syntaxTree); }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) { changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); StringBuilder b = new StringBuilder(text); foreach (var change in changes) { //Console.WriteLine ("---- apply:" + change); // Console.WriteLine (adapter.Text); if (change.Offset > b.Length) continue; b.Remove(change.Offset, change.RemovedChars); b.Insert(change.Offset, change.InsertedText); } // Console.WriteLine ("---result:"); // Console.WriteLine (adapter.Text); return b.ToString(); }*/ protected static IDocument GetResult (CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly) { input = NormalizeNewlines (input); var document = new StringBuilderDocument (input); var options = new TextEditorOptions (); options.EolMarker = "\n"; var visitor = new AstFormattingVisitor (policy, document, options); visitor.FormattingMode = mode; var compilationUnit = new CSharpParser ().Parse (document, "test.cs"); compilationUnit.AcceptVisitor (visitor); visitor.ApplyChanges(); return document; }
protected static void Continue(CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly) { expectedOutput = NormalizeNewlines (expectedOutput); var options = new TextEditorOptions (); options.EolMarker = "\n"; var formatter = new CSharpFormatter (policy, options); formatter.FormattingMode = formattingMode; string newText = formatter.Format (document); if (expectedOutput != newText) { Console.WriteLine (newText); } Assert.AreEqual (expectedOutput, newText); }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) { changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); StringBuilder b = new StringBuilder(text); foreach (var change in changes) { //Console.WriteLine ("---- apply:" + change); // Console.WriteLine (adapter.Text); if (change.Offset > b.Length) continue; b.Remove(change.Offset, change.RemovedChars); b.Insert(change.Offset, change.InsertedText); } // Console.WriteLine ("---result:"); // Console.WriteLine (adapter.Text); return b.ToString(); }*/ protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive) { input = NormalizeNewlines(input); var document = new StringBuilderDocument(input); var options = new TextEditorOptions(); options.EolMarker = "\n"; options.WrapLineLength = 80; var visitor = new CSharpFormatter (policy, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser ().Parse (document, "test.cs"); var changes = visitor.AnalyzeFormatting(document, syntaxTree); changes.ApplyChanges(); return document; }
public void ReadModePropertyInfo_ReturnsTheResultOfTheBaseReader() { // Arrange IFormattingModeReader baseReader = Substitute.For <IFormattingModeReader>(); baseReader.ReadMode(TestProperty).Returns(FormattingMode.ItemsMembers); IFormattingModeReader reader = new CachedFormattingModeReader(baseReader); // Act FormattingMode mode = reader.ReadMode(TestProperty); // Assert Assert.AreEqual(FormattingMode.ItemsMembers, mode); }
public void Serialize(StringBuilder sb, object value, ParameterInfo parameter) { if (sb == null) { throw new ArgumentNullException("sb"); } if (parameter == null) { throw new ArgumentNullException("parameter"); } FormattingMode mode = this.reader.ReadMode(parameter); this.Serialize(sb, value, mode); }
protected static void Continue (CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly) { expectedOutput = NormalizeNewlines (expectedOutput); var options = new TextEditorOptions (); options.EolMarker = "\n"; var visitior = new AstFormattingVisitor (policy, document, options); visitior.FormattingMode = formattingMode; var syntaxTree = new CSharpParser ().Parse (document, "test.cs"); syntaxTree.AcceptVisitor (visitior); visitior.ApplyChanges(); string newText = document.Text; if (expectedOutput != newText) { Console.WriteLine (newText); } Assert.AreEqual (expectedOutput, newText); }
private static void TestFormatter(object value, FormattingMode mode, string expectedOutput, IFormatProvider formatProvider = null, ICustomValueFormatter customValueFormatter = null) { // Arrange IFormattingModeReader reader = Substitute.For <IFormattingModeReader>(); reader.ReadMode(TestParameter).Returns(mode); IParameterFormatter formatter = new ParameterFormatter(reader, formatProvider ?? CultureInfo.InvariantCulture, customValueFormatter); StringBuilder sb = new StringBuilder(); // Act formatter.Serialize(sb, value, TestParameter); // Assert Assert.AreEqual(expectedOutput, sb.ToString()); }
private void SerializeMembers(StringBuilder sb, object value) { List <PropertyInfo> properties = value.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(x => x.GetIndexParameters().Length == 0).ToList(); sb.Append("{ "); for (int i = 0; i < properties.Count; i++) { if (i != 0) { sb.Append(", "); } sb.Append(properties[i].Name); sb.Append(": "); object memberValue = properties[i].GetValue(value, null); FormattingMode mode = this.reader.ReadMode(properties[i]); this.Serialize(sb, memberValue, mode); } sb.Append(" }"); }
protected static IDocument Test(CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { expectedOutput = NormalizeNewlines(expectedOutput); IDocument doc = GetResult(policy, input, mode, options); if (expectedOutput != doc.Text) { Console.WriteLine("expected:"); Console.WriteLine(expectedOutput); Console.WriteLine("got:"); Console.WriteLine(doc.Text); for (int i = 0; i < expectedOutput.Length && i < doc.TextLength; i++) { if (expectedOutput [i] != doc.GetCharAt(i)) { Console.WriteLine( "i:" + i + " differ:" + expectedOutput[i].ToString().Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t") + " !=" + doc.GetCharAt(i).ToString().Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t") ); Console.WriteLine(">" + expectedOutput.Substring(i).Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t")); Console.WriteLine(">" + doc.Text.Substring(i).Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t")); break; } } } Assert.AreEqual(expectedOutput, doc.Text); return(doc); }
protected static void TestNoUnnecessaryChanges(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { var formatted = GetResult(policy, input, mode, options).Text; var changes = GetChanges(policy, formatted, mode, options); Assert.AreEqual(0, changes.Count, "Wrong amount of changes"); }
protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { StringBuilderDocument document; return GetChanges(policy, input, out document, mode, options); }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) * { * changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); * StringBuilder b = new StringBuilder(text); * foreach (var change in changes) { * //Console.WriteLine ("---- apply:" + change); * // Console.WriteLine (adapter.Text); * if (change.Offset > b.Length) * continue; * b.Remove(change.Offset, change.RemovedChars); * b.Insert(change.Offset, change.InsertedText); * } * // Console.WriteLine ("---result:"); * // Console.WriteLine (adapter.Text); * return b.ToString(); * }*/ protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly) { input = NormalizeNewlines(input); var document = new StringBuilderDocument(input); var options = new TextEditorOptions(); options.EolMarker = "\n"; var visitor = new AstFormattingVisitor(policy, document, options); visitor.FormattingMode = mode; var compilationUnit = new CSharpParser().Parse(document, "test.cs"); compilationUnit.AcceptVisitor(visitor); visitor.ApplyChanges(); return(document); }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) * { * changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); * StringBuilder b = new StringBuilder(text); * foreach (var change in changes) { * //Console.WriteLine ("---- apply:" + change); * // Console.WriteLine (adapter.Text); * if (change.Offset > b.Length) * continue; * b.Remove(change.Offset, change.RemovedChars); * b.Insert(change.Offset, change.InsertedText); * } * // Console.WriteLine ("---result:"); * // Console.WriteLine (adapter.Text); * return b.ToString(); * }*/ protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { if (options == null) { options = new TextEditorOptions(); options.EolMarker = "\n"; options.WrapLineLength = 80; } input = NormalizeNewlines(input); var document = new StringBuilderDocument(input); var visitor = new CSharpFormatter(policy, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser().Parse(document, "test.cs"); var changes = visitor.AnalyzeFormatting(document, syntaxTree); changes.ApplyChanges(); return(document); }
protected static void Continue(CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly) { expectedOutput = NormalizeNewlines(expectedOutput); var options = new TextEditorOptions(); options.EolMarker = "\n"; var formatter = new CSharpFormatter(policy, options); formatter.FormattingMode = formattingMode; string newText = formatter.Format(document); if (expectedOutput != newText) { Console.WriteLine("expected:"); Console.WriteLine(expectedOutput); Console.WriteLine("got:"); Console.WriteLine(newText); } Assert.AreEqual(expectedOutput, newText); }
protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, out StringBuilderDocument document, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { options = GetActualOptions(options); input = NormalizeNewlines(input); document = new StringBuilderDocument(input); var visitor = new CSharpFormatter(policy, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser().Parse(document, "test.cs"); return(visitor.AnalyzeFormatting(document, syntaxTree)); }
protected static void Continue(CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly) { expectedOutput = NormalizeNewlines(expectedOutput); var options = new TextEditorOptions(); options.EolMarker = "\n"; var visitior = new AstFormattingVisitor(policy, document, options); visitior.FormattingMode = formattingMode; var syntaxTree = new CSharpParser().Parse(document, "test.cs"); syntaxTree.AcceptVisitor(visitior); visitior.ApplyChanges(); string newText = document.Text; if (expectedOutput != newText) { Console.WriteLine(newText); } Assert.AreEqual(expectedOutput, newText); }
protected static IDocument Test(CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive) { expectedOutput = NormalizeNewlines(expectedOutput); IDocument doc = GetResult(policy, input, mode); if (expectedOutput != doc.Text) { Console.WriteLine("expected:"); Console.WriteLine(expectedOutput); Console.WriteLine("got:"); Console.WriteLine(doc.Text); } Assert.AreEqual(expectedOutput, doc.Text); return(doc); }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) * { * changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); * StringBuilder b = new StringBuilder(text); * foreach (var change in changes) { * //Console.WriteLine ("---- apply:" + change); * // Console.WriteLine (adapter.Text); * if (change.Offset > b.Length) * continue; * b.Remove(change.Offset, change.RemovedChars); * b.Insert(change.Offset, change.InsertedText); * } * // Console.WriteLine ("---result:"); * // Console.WriteLine (adapter.Text); * return b.ToString(); * }*/ protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive) { input = NormalizeNewlines(input); var document = new StringBuilderDocument(input); var options = new TextEditorOptions(); options.EolMarker = "\n"; options.WrapLineLength = 80; var visitor = new AstFormattingVisitor(policy, document, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser().Parse(document, "test.cs"); syntaxTree.AcceptVisitor(visitor); visitor.ApplyChanges(); return(document); }
protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { StringBuilderDocument document; return(GetChanges(policy, input, out document, mode, options)); }
protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { StringBuilderDocument document; var changes = GetChanges(policy, input, out document, mode, options); changes.ApplyChanges(); return(document); }