forked from limetext/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_test.go
187 lines (149 loc) · 3.23 KB
/
window_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package backend
import (
"path/filepath"
"testing"
)
func TestWindowNewFile(t *testing.T) {
w := GetEditor().NewWindow()
defer w.Close()
v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()
if len(w.Views()) != 1 {
t.Errorf("Expected 1 view, but got %d", len(w.Views()))
}
}
func TestWindowRemove(t *testing.T) {
w := GetEditor().NewWindow()
defer w.Close()
v0 := w.NewFile()
defer v0.Close()
v1 := w.NewFile()
v2 := w.NewFile()
defer v2.Close()
l := len(w.Views())
w.remove(v1)
if len(w.Views()) != l-1 {
t.Errorf("Expected %d open views, but got %d", l-1, len(w.Views()))
}
}
func TestWindowActiveView(t *testing.T) {
w := GetEditor().NewWindow()
defer w.Close()
v0 := w.NewFile()
defer v0.Close()
v1 := w.NewFile()
defer v1.Close()
if w.ActiveView() != v1 {
t.Error("Expected v1 to be the active view, but it wasn't")
}
}
func TestWindowClose(t *testing.T) {
ed := GetEditor()
l := len(ed.Windows())
w := ed.NewWindow()
for _, v := range w.Views() {
v.SetScratch(true)
v.Close()
}
w.Close()
if len(ed.Windows()) != l {
t.Errorf("Expected window to close, but we have %d still open", len(ed.Windows()))
}
}
func TestWindowCloseFail(t *testing.T) {
ed := GetEditor()
ed.SetFrontend(&DummyFrontend{})
fe := ed.Frontend()
if dfe, ok := fe.(*DummyFrontend); ok {
dfe.SetDefaultAction(false)
}
w := ed.NewWindow()
l := len(ed.Windows())
v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()
edit := v.BeginEdit()
v.Insert(edit, 0, "test")
v.EndEdit(edit)
if w.Close() {
t.Errorf("Expected window to fail to close, but it didn't")
}
if len(ed.Windows()) != l {
t.Error("Expected window not to close, but it did")
}
}
func TestWindowCloseAllViews(t *testing.T) {
w := GetEditor().NewWindow()
defer w.Close()
w.NewFile()
w.NewFile()
w.CloseAllViews()
if len(w.Views()) != 0 {
t.Errorf("Expected 0 open views, but got %d", len(w.Views()))
}
}
func TestWindowCloseAllViewsFail(t *testing.T) {
ed := GetEditor()
ed.SetFrontend(&DummyFrontend{})
fe := ed.Frontend()
if dfe, ok := fe.(*DummyFrontend); ok {
dfe.SetDefaultAction(false)
}
w := ed.NewWindow()
defer w.Close()
w.NewFile()
v := w.NewFile()
l := len(w.Views())
w.NewFile()
defer func() {
for _, vw := range w.Views() {
vw.SetScratch(true)
vw.Close()
}
}()
edit := v.BeginEdit()
v.Insert(edit, 0, "test")
v.EndEdit(edit)
if w.CloseAllViews() {
t.Errorf("Expected views to fail to close, but they didn't")
}
if len(w.Views()) != l {
t.Error("Expected only one view to close, but more did")
}
}
func TestOpenFile(t *testing.T) {
abs := func(path string) string {
abs, _ := filepath.Abs(path)
return abs
}
tests := []struct {
in string
exp string
}{
{
"testdata/file",
abs("testdata/file"),
},
{
"/nonexistdir/o",
"/nonexistdir/o",
},
}
w := GetEditor().NewWindow()
defer w.Close()
for i, test := range tests {
v := w.OpenFile(test.in, 0644)
if out := v.FileName(); out != test.exp {
t.Errorf("Test %d: Expected view file name %s, but got %s", i, test.exp, out)
}
v.Close()
}
}