2
using System.Collections.Generic;
4
using ICSharpCode.NRefactory.CSharp;
6
using ICSharpCode.NRefactory.Editor;
8
using ICSharpCode.NRefactory.CSharp.Refactoring;
10
namespace ICSharpCode.NRefactory.CSharp.FormattingTests
12
public abstract class TestBase
14
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes)
16
changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset));
17
StringBuilder b = new StringBuilder(text);
18
foreach (var change in changes) {
19
//Console.WriteLine ("---- apply:" + change);
20
// Console.WriteLine (adapter.Text);
21
if (change.Offset > b.Length)
23
b.Remove(change.Offset, change.RemovedChars);
24
b.Insert(change.Offset, change.InsertedText);
26
// Console.WriteLine ("---result:");
27
// Console.WriteLine (adapter.Text);
31
protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive)
33
input = NormalizeNewlines(input);
34
var document = new StringBuilderDocument(input);
35
var options = new TextEditorOptions();
36
options.EolMarker = "\n";
37
options.WrapLineLength = 80;
38
var visitor = new CSharpFormatter (policy, options);
39
visitor.FormattingMode = mode;
40
var syntaxTree = new CSharpParser ().Parse (document, "test.cs");
41
var changes = visitor.AnalyzeFormatting(document, syntaxTree);
42
changes.ApplyChanges();
46
protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive)
48
expectedOutput = NormalizeNewlines(expectedOutput);
49
IDocument doc = GetResult(policy, input, mode);
50
if (expectedOutput != doc.Text) {
51
Console.WriteLine ("expected:");
52
Console.WriteLine (expectedOutput);
53
Console.WriteLine ("got:");
54
Console.WriteLine (doc.Text);
55
for (int i = 0; i < expectedOutput.Length && i < doc.TextLength; i++) {
56
if (expectedOutput [i] != doc.GetCharAt(i)) {
59
expectedOutput[i].ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t") +
61
doc.GetCharAt(i).ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t")
63
Console.WriteLine(">"+expectedOutput.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t"));
64
Console.WriteLine(">"+doc.Text.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t"));
69
Assert.AreEqual (expectedOutput, doc.Text);
73
protected static string NormalizeNewlines(string input)
75
return input.Replace("\r\n", "\n");
78
protected static void Continue (CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly)
80
expectedOutput = NormalizeNewlines (expectedOutput);
81
var options = new TextEditorOptions ();
82
options.EolMarker = "\n";
83
var formatter = new CSharpFormatter (policy, options);
84
formatter.FormattingMode = formattingMode;
85
string newText = formatter.Format (document);
86
if (expectedOutput != newText) {
87
Console.WriteLine (newText);
89
Assert.AreEqual (expectedOutput, newText);