~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/FormattingTests/TextEditorTestAdapter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Text;
 
4
using ICSharpCode.NRefactory.CSharp;
 
5
using System.IO;
 
6
using ICSharpCode.NRefactory.Editor;
 
7
using NUnit.Framework;
 
8
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
9
 
 
10
namespace ICSharpCode.NRefactory.CSharp.FormattingTests
 
11
{
 
12
        public abstract class TestBase
 
13
        {
 
14
                /*public static string ApplyChanges (string text, List<TextReplaceAction> changes)
 
15
                {
 
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)
 
22
                                        continue;
 
23
                                b.Remove(change.Offset, change.RemovedChars);
 
24
                                b.Insert(change.Offset, change.InsertedText);
 
25
                        }
 
26
//                      Console.WriteLine ("---result:");
 
27
//                      Console.WriteLine (adapter.Text);
 
28
                        return b.ToString();
 
29
                }*/
 
30
                
 
31
                protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive)
 
32
                {
 
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();
 
43
                        return document;
 
44
                }
 
45
                
 
46
                protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive)
 
47
                {
 
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)) {
 
57
                                                Console.WriteLine (
 
58
                                                        "i:"+i+" differ:"+ 
 
59
                                                        expectedOutput[i].ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t") +
 
60
                                                        " !=" + 
 
61
                                                        doc.GetCharAt(i).ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t")
 
62
                                                );
 
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"));
 
65
                                                break;
 
66
                                        }
 
67
                                }
 
68
                        }
 
69
                        Assert.AreEqual (expectedOutput, doc.Text);
 
70
                        return doc;
 
71
                }
 
72
                
 
73
                protected static string NormalizeNewlines(string input)
 
74
                {
 
75
                        return input.Replace("\r\n", "\n");
 
76
                }
 
77
 
 
78
                protected static void Continue (CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly)
 
79
                {
 
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);
 
88
                        }
 
89
                        Assert.AreEqual (expectedOutput, newText);
 
90
                }
 
91
 
 
92
                
 
93
        }
 
94
}
 
95