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
|
package cursor
import (
"log"
"os"
"testing"
)
// TestCustomIOWriter tests the cursor functions with a custom Writer.
func TestCustomIOWriter(t *testing.T) {
tmpFile, err := os.CreateTemp("", "testingTmpFile-")
defer os.Remove(tmpFile.Name())
if err != nil {
log.Fatal(err)
}
w := tmpFile
SetTarget(w)
Up(2)
expected := "\x1b[2A"
actual := getFileContent(t, w.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, w)
Down(2)
expected = "\x1b[2B"
actual = getFileContent(t, w.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, w)
Right(2)
expected = "\x1b[2C"
actual = getFileContent(t, w.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, w)
Left(2)
expected = "\x1b[2D"
actual = getFileContent(t, w.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, w)
Hide()
expected = "\x1b[?25l"
actual = getFileContent(t, w.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, w)
Show()
expected = "\x1b[?25h"
actual = getFileContent(t, w.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, w)
ClearLine()
expected = "\x1b[2K"
actual = getFileContent(t, w.Name())
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
clearFile(t, w)
HorizontalAbsolute(3)
expected = "\x1b[4G"
actual = getFileContent(t, w.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
}
}
|