-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
190 lines (163 loc) · 5.1 KB
/
main_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
188
189
190
//go:build memory
package main
import (
"bytes"
"testing"
"github.com/google/go-cmp/cmp"
)
func Test_print_command(t *testing.T) {
tests := []struct {
cmd *command
expected string
}{
{&command{
addrStart: "1",
addrEnd: "2",
action: 'p',
}, "1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sed ante eu ...\n2 Duis ut porta mi, eu ornare orci. Etiam sed vehicula orci. ...\n"},
}
cache := &cache{}
for i, test := range tests {
controlBuffer := getTestActionBuffer()
out := bytes.NewBufferString(``)
in := bytes.NewBufferString(``)
term, _ := newTerm(in, out, "", false) // _ is an unused destructor
doCmd(*test.cmd, controlBuffer, term, &localFS{}, cache)
// t.Error(controlBuffer.String(), test.expected.String())
if diff := cmp.Diff(out.String(), test.expected); diff != "" {
t.Errorf("idx: %d; -got/+want\n%s", i, diff)
}
}
}
func Test_quit_command(t *testing.T) {
tests := []struct {
cmd *command
expected error
}{
{
&command{addrStart: "1", addrEnd: "2", action: 'q'},
errQuit,
},
}
cache := &cache{}
for i, test := range tests {
controlBuffer := getTestActionBuffer()
out := bytes.NewBufferString(``)
in := bytes.NewBufferString(``)
term, _ := newTerm(in, out, "", false) // _ is an unused destructor
_, err := doCmd(*test.cmd, controlBuffer, term, &localFS{}, cache)
if diff := cmp.Diff(err.Error(), test.expected.Error()); diff != "" {
t.Errorf("idx: %d; -got/+want\n%s", i, diff)
}
}
}
func Test_interactive_glob(t *testing.T) {
tests := []struct {
cmd *command
expected buffer
}{
{
// &command{addrPattern: "ui", action: 's', pattern: "ui", replace: "++", replaceNum: "g"},
&command{addrPattern: "ui", globalPrefix: 'G'},
&memoryBuf{
curline: 2,
lastline: 5,
lines: []bufferLine{
{txt: ``},
{txt: `1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sed ante eu ...`},
{txt: `2 D++s ut porta mi, eu ornare orci. Etiam sed vehicula orci. ...`},
{txt: `3 Nunc scelerisque urna a erat gravida porttitor. Donec pulvinar leo urna, id ...`},
{txt: `4 Nullam lacus magna, congue aliquam luctus ac, faucibus vel purus. Integer ...`},
{txt: `5 Mauris nunc purus, congue non vehicula eu, blandit sit amet est. ...`},
},
filename: "filename",
dirty: true,
rev: 1,
},
},
}
cache := &cache{}
for i, test := range tests {
controlBuffer := getTestActionBuffer()
out := bytes.NewBufferString(``)
in := bytes.NewBufferString("s/ui/++/g\nQ\n")
term, _ := newTerm(in, out, "", false) // _ is an unused destructor
_, err := doCmd(*test.cmd, controlBuffer, term, &localFS{}, cache)
if err != nil && err != errStop {
t.Error(err)
}
if diff := cmp.Diff(controlBuffer.String(), test.expected.String()); diff != "" {
t.Errorf("idx: %d; -got/+want\n%s", i, diff)
}
}
}
func Test_interactive_v_glob(t *testing.T) {
tests := []struct {
cmd *command
expected buffer
}{
{
// &command{addrPattern: "ui", action: 's', pattern: "ui", replace: "++", replaceNum: "g"},
&command{addrPattern: "ui", globalPrefix: 'V'},
&memoryBuf{
curline: 2,
lastline: 5,
lines: []bufferLine{
{txt: ``},
{txt: `1 Lor!m ipsum dolor sit am!t, cons!ct!tur adipiscing !lit. Morbi s!d ant! !u ...`},
{txt: `2 Duis ut porta mi, eu ornare orci. Etiam sed vehicula orci. ...`},
{txt: `3 Nunc sc!l!risqu! urna a !rat gravida porttitor. Don!c pulvinar l!o urna, id ...`},
{txt: `4 Nullam lacus magna, congu! aliquam luctus ac, faucibus v!l purus. Int!g!r ...`},
{txt: `5 Mauris nunc purus, congu! non v!hicula !u, blandit sit am!t !st. ...`},
},
filename: "filename",
dirty: true,
rev: 4,
},
},
}
cache := &cache{}
for i, test := range tests {
controlBuffer := getTestActionBuffer()
out := bytes.NewBufferString(``)
in := bytes.NewBufferString("s/e/!/g\nq\ns/e/!/g\nq\ns/e/!/g\nq\ns/e/!/g\nq\nQ\n")
term, _ := newTerm(in, out, "", false) // _ is an unused destructor
_, err := doCmd(*test.cmd, controlBuffer, term, &localFS{}, cache)
if err != nil && err != errStop {
t.Error(err)
}
if diff := cmp.Diff(controlBuffer.String(), test.expected.String()); diff != "" {
t.Errorf("idx: %d; -got/+want\n%s", i, diff)
}
}
}
func Test_doExternalShell(t *testing.T) {
tests := []struct {
cmd *command
expectedResult string
expectedReturn string
}{
{
&command{action: '!', argument: "echo 867-5309"},
"867-5309\n",
"!",
},
}
cache := &cache{}
for i, test := range tests {
controlBuffer := getTestActionBuffer()
out := bytes.NewBufferString(``)
in := bytes.NewBufferString("s/e/!/g\nq\ns/e/!/g\nq\ns/e/!/g\nq\ns/e/!/g\nq\nQ\n")
term, _ := newTerm(in, out, "", false) // _ is an unused destructor
str, err := doCmd(*test.cmd, controlBuffer, term, &localFS{}, cache)
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(out.String(), test.expectedResult); diff != "" {
t.Errorf("idx: %d; -got/+want\n%s", i, diff)
}
if diff := cmp.Diff(str, test.expectedReturn); diff != "" {
t.Errorf("idx: %d; -got/+want\n%s", i, diff)
}
}
}