generated from atomicgo/template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cursor_test_linux.go
131 lines (93 loc) · 2.17 KB
/
cursor_test_linux.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
package cursor
import (
"os"
"testing"
)
// TestCustomIOWriter tests the cursor functions with a custom Writer.
func TestCustomIOWriter(t *testing.T) {
t.Parallel()
tmpFile, err := os.CreateTemp("", "testingTmpFile-")
defer os.Remove(tmpFile.Name())
if err != nil {
t.Error(err)
}
target := tmpFile
SetTarget(target)
Up(2)
expected := "\x1b[2A"
actual := getFileContent(t, target.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, target)
Down(2)
expected = "\x1b[2B"
actual = getFileContent(t, target.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, target)
Right(2)
expected = "\x1b[2C"
actual = getFileContent(t, target.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, target)
Left(2)
expected = "\x1b[2D"
actual = getFileContent(t, target.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, target)
Hide()
expected = "\x1b[?25l"
actual = getFileContent(t, target.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, target)
Show()
expected = "\x1b[?25h"
actual = getFileContent(t, target.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, target)
ClearLine()
expected = "\x1b[2K"
actual = getFileContent(t, target.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, target)
HorizontalAbsolute(3)
expected = "\x1b[4G"
actual = getFileContent(t, target.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
}
func getFileContent(t *testing.T, fileName string) string {
t.Helper()
content, err := os.ReadFile(fileName)
if err != nil {
t.Errorf("failed to read file contents: %s", err)
return ""
}
return string(content)
}
func clearFile(t *testing.T, file *os.File) {
t.Helper()
err := file.Truncate(0)
if err != nil {
t.Errorf("failed to clear file")
return
}
_, err = file.Seek(0, 0)
if err != nil {
t.Errorf("failed to clear file")
return
}
}