1 package termenv
2
3 import (
4 "bytes"
5 "io/ioutil"
6 "os"
7 "strings"
8 "testing"
9 )
10
11 type testEnv struct{}
12
13 func (te testEnv) Environ() []string {
14 return []string{"TERM=xterm-256color"}
15 }
16
17 func (te testEnv) Getenv(key string) string {
18 if key == "TERM" {
19 return "xterm-256color"
20 }
21 return ""
22 }
23
24 func tempOutput(t *testing.T) *Output {
25 t.Helper()
26
27 f, err := ioutil.TempFile("", "termenv")
28 if err != nil {
29 t.Fatal(err)
30 }
31
32 return NewOutput(f, WithEnvironment(testEnv{}), WithProfile(TrueColor))
33 }
34
35 func verify(t *testing.T, o *Output, exp string) {
36 t.Helper()
37 tty := o.tty.(*os.File)
38
39 if _, err := tty.Seek(0, 0); err != nil {
40 t.Fatal(err)
41 }
42
43 b, err := ioutil.ReadAll(tty)
44 if err != nil {
45 t.Fatal(err)
46 }
47 if string(b) != exp {
48 b = bytes.Replace(b, []byte("\x1b"), []byte("\\x1b"), -1)
49 exp = strings.Replace(exp, "\x1b", "\\x1b", -1)
50 t.Errorf("output does not match, expected %s, got %s", exp, string(b))
51 }
52
53
54 os.Remove(tty.Name())
55 }
56
57 func TestReset(t *testing.T) {
58 o := tempOutput(t)
59 o.Reset()
60 verify(t, o, "\x1b[0m")
61 }
62
63 func TestSetForegroundColor(t *testing.T) {
64 o := tempOutput(t)
65 o.SetForegroundColor(ANSI.Color("0"))
66 verify(t, o, "\x1b]10;#000000\a")
67 }
68
69 func TestSetBackgroundColor(t *testing.T) {
70 o := tempOutput(t)
71 o.SetBackgroundColor(ANSI.Color("0"))
72 verify(t, o, "\x1b]11;#000000\a")
73 }
74
75 func TestSetCursorColor(t *testing.T) {
76 o := tempOutput(t)
77 o.SetCursorColor(ANSI.Color("0"))
78 verify(t, o, "\x1b]12;#000000\a")
79 }
80
81 func TestRestoreScreen(t *testing.T) {
82 o := tempOutput(t)
83 o.RestoreScreen()
84 verify(t, o, "\x1b[?47l")
85 }
86
87 func TestSaveScreen(t *testing.T) {
88 o := tempOutput(t)
89 o.SaveScreen()
90 verify(t, o, "\x1b[?47h")
91 }
92
93 func TestAltScreen(t *testing.T) {
94 o := tempOutput(t)
95 o.AltScreen()
96 verify(t, o, "\x1b[?1049h")
97 }
98
99 func TestExitAltScreen(t *testing.T) {
100 o := tempOutput(t)
101 o.ExitAltScreen()
102 verify(t, o, "\x1b[?1049l")
103 }
104
105 func TestClearScreen(t *testing.T) {
106 o := tempOutput(t)
107 o.ClearScreen()
108 verify(t, o, "\x1b[2J\x1b[1;1H")
109 }
110
111 func TestMoveCursor(t *testing.T) {
112 o := tempOutput(t)
113 o.MoveCursor(16, 8)
114 verify(t, o, "\x1b[16;8H")
115 }
116
117 func TestHideCursor(t *testing.T) {
118 o := tempOutput(t)
119 o.HideCursor()
120 verify(t, o, "\x1b[?25l")
121 }
122
123 func TestShowCursor(t *testing.T) {
124 o := tempOutput(t)
125 o.ShowCursor()
126 verify(t, o, "\x1b[?25h")
127 }
128
129 func TestSaveCursorPosition(t *testing.T) {
130 o := tempOutput(t)
131 o.SaveCursorPosition()
132 verify(t, o, "\x1b[s")
133 }
134
135 func TestRestoreCursorPosition(t *testing.T) {
136 o := tempOutput(t)
137 o.RestoreCursorPosition()
138 verify(t, o, "\x1b[u")
139 }
140
141 func TestCursorUp(t *testing.T) {
142 o := tempOutput(t)
143 o.CursorUp(8)
144 verify(t, o, "\x1b[8A")
145 }
146
147 func TestCursorDown(t *testing.T) {
148 o := tempOutput(t)
149 o.CursorDown(8)
150 verify(t, o, "\x1b[8B")
151 }
152
153 func TestCursorForward(t *testing.T) {
154 o := tempOutput(t)
155 o.CursorForward(8)
156 verify(t, o, "\x1b[8C")
157 }
158
159 func TestCursorBack(t *testing.T) {
160 o := tempOutput(t)
161 o.CursorBack(8)
162 verify(t, o, "\x1b[8D")
163 }
164
165 func TestCursorNextLine(t *testing.T) {
166 o := tempOutput(t)
167 o.CursorNextLine(8)
168 verify(t, o, "\x1b[8E")
169 }
170
171 func TestCursorPrevLine(t *testing.T) {
172 o := tempOutput(t)
173 o.CursorPrevLine(8)
174 verify(t, o, "\x1b[8F")
175 }
176
177 func TestClearLine(t *testing.T) {
178 o := tempOutput(t)
179 o.ClearLine()
180 verify(t, o, "\x1b[2K")
181 }
182
183 func TestClearLineLeft(t *testing.T) {
184 o := tempOutput(t)
185 o.ClearLineLeft()
186 verify(t, o, "\x1b[1K")
187 }
188
189 func TestClearLineRight(t *testing.T) {
190 o := tempOutput(t)
191 o.ClearLineRight()
192 verify(t, o, "\x1b[0K")
193 }
194
195 func TestClearLines(t *testing.T) {
196 o := tempOutput(t)
197 o.ClearLines(8)
198 verify(t, o, "\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K")
199 }
200
201 func TestChangeScrollingRegion(t *testing.T) {
202 o := tempOutput(t)
203 o.ChangeScrollingRegion(16, 8)
204 verify(t, o, "\x1b[16;8r")
205 }
206
207 func TestInsertLines(t *testing.T) {
208 o := tempOutput(t)
209 o.InsertLines(8)
210 verify(t, o, "\x1b[8L")
211 }
212
213 func TestDeleteLines(t *testing.T) {
214 o := tempOutput(t)
215 o.DeleteLines(8)
216 verify(t, o, "\x1b[8M")
217 }
218
219 func TestEnableMousePress(t *testing.T) {
220 o := tempOutput(t)
221 o.EnableMousePress()
222 verify(t, o, "\x1b[?9h")
223 }
224
225 func TestDisableMousePress(t *testing.T) {
226 o := tempOutput(t)
227 o.DisableMousePress()
228 verify(t, o, "\x1b[?9l")
229 }
230
231 func TestEnableMouse(t *testing.T) {
232 o := tempOutput(t)
233 o.EnableMouse()
234 verify(t, o, "\x1b[?1000h")
235 }
236
237 func TestDisableMouse(t *testing.T) {
238 o := tempOutput(t)
239 o.DisableMouse()
240 verify(t, o, "\x1b[?1000l")
241 }
242
243 func TestEnableMouseHilite(t *testing.T) {
244 o := tempOutput(t)
245 o.EnableMouseHilite()
246 verify(t, o, "\x1b[?1001h")
247 }
248
249 func TestDisableMouseHilite(t *testing.T) {
250 o := tempOutput(t)
251 o.DisableMouseHilite()
252 verify(t, o, "\x1b[?1001l")
253 }
254
255 func TestEnableMouseCellMotion(t *testing.T) {
256 o := tempOutput(t)
257 o.EnableMouseCellMotion()
258 verify(t, o, "\x1b[?1002h")
259 }
260
261 func TestDisableMouseCellMotion(t *testing.T) {
262 o := tempOutput(t)
263 o.DisableMouseCellMotion()
264 verify(t, o, "\x1b[?1002l")
265 }
266
267 func TestEnableMouseAllMotion(t *testing.T) {
268 o := tempOutput(t)
269 o.EnableMouseAllMotion()
270 verify(t, o, "\x1b[?1003h")
271 }
272
273 func TestDisableMouseAllMotion(t *testing.T) {
274 o := tempOutput(t)
275 o.DisableMouseAllMotion()
276 verify(t, o, "\x1b[?1003l")
277 }
278
279 func TestEnableMouseExtendedMode(t *testing.T) {
280 o := tempOutput(t)
281 o.EnableMouseExtendedMode()
282 verify(t, o, "\x1b[?1006h")
283 }
284
285 func TestDisableMouseExtendedMode(t *testing.T) {
286 o := tempOutput(t)
287 o.DisableMouseExtendedMode()
288 verify(t, o, "\x1b[?1006l")
289 }
290
291 func TestEnableMousePixelsMode(t *testing.T) {
292 o := tempOutput(t)
293 o.EnableMousePixelsMode()
294 verify(t, o, "\x1b[?1016h")
295 }
296
297 func TestDisableMousePixelsMode(t *testing.T) {
298 o := tempOutput(t)
299 o.DisableMousePixelsMode()
300 verify(t, o, "\x1b[?1016l")
301 }
302
303 func TestSetWindowTitle(t *testing.T) {
304 o := tempOutput(t)
305 o.SetWindowTitle("test")
306 verify(t, o, "\x1b]2;test\a")
307 }
308
309 func TestCopyClipboard(t *testing.T) {
310 o := tempOutput(t)
311 o.Copy("hello")
312 verify(t, o, "\x1b]52;c;aGVsbG8=\a")
313 }
314
315 func TestCopyPrimary(t *testing.T) {
316 o := tempOutput(t)
317 o.CopyPrimary("hello")
318 verify(t, o, "\x1b]52;p;aGVsbG8=\a")
319 }
320
321 func TestHyperlink(t *testing.T) {
322 o := tempOutput(t)
323 o.WriteString(o.Hyperlink("http://example.com", "example"))
324 verify(t, o, "\x1b]8;;http://example.com\x1b\\example\x1b]8;;\x1b\\")
325 }
326
View as plain text