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

« back to all changes in this revision

Viewing changes to tests/UnitTests/Mono.TextEditor.Tests.DefaultEditActions/TextEditorTestBase.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
 
// 
2
 
// TextEditorTestBase.cs
3
 
//  
4
 
// Author:
5
 
//       Mike Krüger <mkrueger@xamarin.com>
6
 
// 
7
 
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
8
 
// 
9
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
 
// of this software and associated documentation files (the "Software"), to deal
11
 
// in the Software without restriction, including without limitation the rights
12
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 
// copies of the Software, and to permit persons to whom the Software is
14
 
// furnished to do so, subject to the following conditions:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
19
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 
// THE SOFTWARE.
26
 
 
27
 
using System;
28
 
using NUnit.Framework;
29
 
using System.Collections.Generic;
30
 
using System.Text;
31
 
 
32
 
namespace Mono.TextEditor.Tests
33
 
{
34
 
        public class TextEditorTestBase
35
 
        {
36
 
                static bool firstRun = true;
37
 
                
38
 
                [TestFixtureSetUp]
39
 
                public virtual void Setup ()
40
 
                {
41
 
                        if (firstRun) {
42
 
                                Gtk.Application.Init ();
43
 
                                firstRun = false;
44
 
                        }
45
 
                }
46
 
 
47
 
                [TestFixtureTearDown]
48
 
                public virtual void TearDown ()
49
 
                {
50
 
                }
51
 
 
52
 
 
53
 
 
54
 
                public static TextEditorData Create (string content, ITextEditorOptions options = null)
55
 
                {
56
 
                        var data = new TextEditorData ();
57
 
                        if (options != null)
58
 
                                data.Options = options;
59
 
                        var sb = new StringBuilder ();
60
 
                        int caretIndex = -1, selectionStart = -1, selectionEnd = -1;
61
 
                        var foldSegments = new List<FoldSegment> ();
62
 
                        var foldStack = new Stack<FoldSegment> ();
63
 
 
64
 
                        for (int i = 0; i < content.Length; i++) {
65
 
                                var ch = content [i];
66
 
                                switch (ch) {
67
 
                                case '$':
68
 
                                        caretIndex = sb.Length;
69
 
                                        break;
70
 
                                case '<':
71
 
                                        if (i + 1 < content.Length) {
72
 
                                                if (content [i + 1] == '-') {
73
 
                                                        selectionStart = sb.Length;
74
 
                                                        i++;
75
 
                                                        break;
76
 
                                                }
77
 
                                        }
78
 
                                        goto default;
79
 
                                case '-':
80
 
                                        if (i + 1 < content.Length) {
81
 
                                                var next = content [i + 1];
82
 
                                                if (next == '>') {
83
 
                                                        selectionEnd = sb.Length;
84
 
                                                        i++;
85
 
                                                        break;
86
 
                                                }
87
 
                                                if (next == '[') {
88
 
                                                        var segment = new FoldSegment (data.Document, "...", sb.Length, 0, FoldingType.None);
89
 
                                                        segment.IsFolded = false;
90
 
                                                        foldStack.Push (segment);
91
 
                                                        i++;
92
 
                                                        break;
93
 
                                                }
94
 
                                        }
95
 
                                        goto default;
96
 
                                case '+':
97
 
                                        if (i + 1 < content.Length) {
98
 
                                                var next = content [i + 1];
99
 
                                                if (next == '[') {
100
 
                                                        var segment = new FoldSegment (data.Document, "...", sb.Length, 0, FoldingType.None);
101
 
                                                        segment.IsFolded = true;
102
 
                                                        foldStack.Push (segment);
103
 
                                                        i++;
104
 
                                                        break;
105
 
                                                }
106
 
                                        }
107
 
                                        goto default;
108
 
                                case ']':
109
 
                                        if (foldStack.Count > 0) {
110
 
                                                FoldSegment segment = foldStack.Pop ();
111
 
                                                segment.Length = sb.Length - segment.Offset;
112
 
                                                foldSegments.Add (segment);
113
 
                                                break;
114
 
                                        }
115
 
                                        goto default;
116
 
                                default:
117
 
                                        sb.Append (ch);
118
 
                                        break;
119
 
                                }
120
 
                        }
121
 
 
122
 
                        data.Text = sb.ToString ();
123
 
 
124
 
                        if (caretIndex >= 0)
125
 
                                data.Caret.Offset = caretIndex;
126
 
                        if (selectionStart >= 0) {
127
 
                                if (caretIndex == selectionStart) {
128
 
                                        data.SetSelection (selectionEnd, selectionStart);
129
 
                                } else {
130
 
                                        data.SetSelection (selectionStart, selectionEnd);
131
 
                                }
132
 
                        }
133
 
                        if (foldSegments.Count > 0)
134
 
                                data.Document.UpdateFoldSegments (foldSegments);
135
 
                        return data;
136
 
                }
137
 
 
138
 
                public static void Check (TextEditorData data, string content)
139
 
                {
140
 
                        var checkDocument = Create (content);
141
 
                        Assert.AreEqual (checkDocument.Caret.Offset, data.Caret.Offset, "Caret offset mismatch.");
142
 
                        if (data.IsSomethingSelected || checkDocument.IsSomethingSelected)
143
 
                                Assert.AreEqual (checkDocument.SelectionRange, data.SelectionRange, "Selection mismatch.");
144
 
                        if (checkDocument.Document.HasFoldSegments || data.Document.HasFoldSegments) {
145
 
                                var list1 = new List<FoldSegment> (checkDocument.Document.FoldSegments);
146
 
                                var list2 = new List<FoldSegment> (data.Document.FoldSegments);
147
 
                                Assert.AreEqual (list1.Count, list2.Count, "Fold segment count mismatch.");
148
 
                                for (int i = 0; i < list1.Count; i++) {
149
 
                                        Assert.AreEqual (list1 [i].Segment, list2 [i].Segment, "Fold " + i + " segment mismatch.");
150
 
                                        Assert.AreEqual (list1 [i].IsFolded, list2 [i].IsFolded, "Fold " + i + " isFolded mismatch.");
151
 
                                }
152
 
                        }
153
 
                        Assert.AreEqual (checkDocument.Text, data.Text);
154
 
                }
155
 
        }
156
 
}