2
// TextEditorTestBase.cs
5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using NUnit.Framework;
29
using System.Collections.Generic;
32
namespace Mono.TextEditor.Tests
34
public class TextEditorTestBase
36
static bool firstRun = true;
39
public virtual void Setup ()
42
Gtk.Application.Init ();
48
public virtual void TearDown ()
54
public static TextEditorData Create (string content, ITextEditorOptions options = null)
56
var data = new TextEditorData ();
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> ();
64
for (int i = 0; i < content.Length; i++) {
68
caretIndex = sb.Length;
71
if (i + 1 < content.Length) {
72
if (content [i + 1] == '-') {
73
selectionStart = sb.Length;
80
if (i + 1 < content.Length) {
81
var next = content [i + 1];
83
selectionEnd = sb.Length;
88
var segment = new FoldSegment (data.Document, "...", sb.Length, 0, FoldingType.None);
89
segment.IsFolded = false;
90
foldStack.Push (segment);
97
if (i + 1 < content.Length) {
98
var next = content [i + 1];
100
var segment = new FoldSegment (data.Document, "...", sb.Length, 0, FoldingType.None);
101
segment.IsFolded = true;
102
foldStack.Push (segment);
109
if (foldStack.Count > 0) {
110
FoldSegment segment = foldStack.Pop ();
111
segment.Length = sb.Length - segment.Offset;
112
foldSegments.Add (segment);
122
data.Text = sb.ToString ();
125
data.Caret.Offset = caretIndex;
126
if (selectionStart >= 0) {
127
if (caretIndex == selectionStart) {
128
data.SetSelection (selectionEnd, selectionStart);
130
data.SetSelection (selectionStart, selectionEnd);
133
if (foldSegments.Count > 0)
134
data.Document.UpdateFoldSegments (foldSegments);
138
public static void Check (TextEditorData data, string content)
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.");
153
Assert.AreEqual (checkDocument.Text, data.Text);