-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsave_test.go
170 lines (144 loc) · 3.84 KB
/
save_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
// 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 commands
import (
"io/ioutil"
"os"
"testing"
"github.com/limetext/backend"
)
var testfile = "testdata/save_test.txt"
func TestSave(t *testing.T) {
hold, err := ioutil.ReadFile(testfile)
if err != nil {
t.Fatalf("Couldn't read test file %s", testfile)
}
if err := ioutil.WriteFile(testfile, []byte("Before text"), 0644); err != nil {
t.Fatalf("Couldn't write test file %s", testfile)
}
tests := []struct {
text string
expect string
}{
{
" ab\ncd",
"Before text ab\ncd",
},
{
"\n",
"Before text\n",
},
}
ed := backend.GetEditor()
w := ed.NewWindow()
defer w.Close()
for i, test := range tests {
err := ioutil.WriteFile(testfile, []byte("Before text"), 0644)
if err != nil {
t.Fatal("Could not write to test file")
}
v := w.OpenFile(testfile, 0)
e := v.BeginEdit()
v.Insert(e, v.Size(), test.text)
v.EndEdit(e)
ed.CommandHandler().RunTextCommand(v, "save", nil)
if data, _ := ioutil.ReadFile(testfile); test.expect != string(data) {
t.Errorf("Test %d: Expected %s, but got %s", i, test.expect, string(data))
}
v.Close()
if err := ioutil.WriteFile(testfile, hold, 0644); err != nil {
t.Fatalf("Couldn't write back test file %s", testfile)
}
}
}
func TestSaveAs(t *testing.T) {
hold, err := ioutil.ReadFile(testfile)
if err != nil {
t.Fatalf("Couldn't read test file %s", testfile)
}
if err := ioutil.WriteFile(testfile, []byte(""), 0644); err != nil {
t.Fatalf("Couldn't write test file %s", testfile)
}
var fe front
const name = "testdata/save_as_test.txt"
fe.files = []string{name}
ed := backend.GetEditor()
ed.SetFrontend(&fe)
w := ed.NewWindow()
defer w.Close()
v := w.OpenFile(testfile, 0)
e := v.BeginEdit()
v.Insert(e, 0, "Testing save_as ")
v.BeginEdit()
ed.CommandHandler().RunTextCommand(v, "prompt_save_as", nil)
if _, err := os.Stat(name); os.IsNotExist(err) {
t.Errorf("The new test file %s wasn't created", name)
}
if data, _ := ioutil.ReadFile(name); "Testing save_as " != string(data) {
t.Errorf("Expected %s, but got %s", "Testing save_as ", string(data))
}
if err := os.Remove(name); err != nil {
t.Errorf("Couldn't remove test file %s", name)
}
if err := ioutil.WriteFile(testfile, hold, 0644); err != nil {
t.Fatalf("Couldn't write back test file %s", testfile)
}
}
func TestSaveAll(t *testing.T) {
var err error
holds := make(map[int][]byte)
views := make(map[int]*backend.View)
files := []struct {
file string
expect string
}{
{
"testdata/save_all_test.txt",
"Testing save all 1",
},
{
"testdata/save_another_all_test.txt",
"Testing save all 2",
},
}
ed := backend.GetEditor()
ed.SetFrontend(&front{})
fe := ed.Frontend()
if dfe, ok := fe.(*front); ok {
// Make it *not* reload the file
dfe.SetDefaultAction(false)
}
w := ed.NewWindow()
// defer w.Close()
for i, f := range files {
holds[i], err = ioutil.ReadFile(f.file)
if err != nil {
t.Fatalf("Test %d: Couldn't read file %s", i, f.file)
}
if err := ioutil.WriteFile(f.file, []byte(""), 0644); err != nil {
t.Fatalf("Test %d: Couldn't write test file %s", i, f.file)
}
v := w.OpenFile(f.file, 0)
views[i] = v
e := v.BeginEdit()
v.Insert(e, 0, f.expect)
v.EndEdit(e)
}
if err := ed.CommandHandler().RunWindowCommand(w, "save_all", nil); err != nil {
t.Errorf("failed to run save_all: %s", err)
}
for i, f := range files {
if data, err := ioutil.ReadFile(f.file); err != nil {
t.Errorf("failed to read in file: %s", err)
} else if s := string(data); s != f.expect {
t.Errorf("Test %d: Expected to get `%s`, but got `%s`", i, f.expect, s)
}
}
for i, f := range files {
// v := views[i]
// v.SetScratch(true)
// v.Close()
ioutil.WriteFile(f.file, holds[i], 0644)
}
}