| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package tests |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/gdamore/tcell/v3/color" |
| 22 |
"github.com/gdamore/tcell/v3/vt" |
| 23 |
) |
| 24 |
|
| 25 |
type MockTerm = vt.MockTerm |
| 26 |
type Row = vt.Row |
| 27 |
type Col = vt.Col |
| 28 |
type Coord = vt.Coord |
| 29 |
type Attr = vt.Attr |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
func WriteF(t *testing.T, term MockTerm, str string, args ...any) { |
| 34 |
t.Helper() |
| 35 |
b := fmt.Appendf(nil, str, args...) |
| 36 |
for len(b) != 0 { |
| 37 |
if n, err := term.Write(b); err != nil { |
| 38 |
t.Fatalf("Failed to write: %v", err) |
| 39 |
} else { |
| 40 |
b = b[n:] |
| 41 |
} |
| 42 |
} |
| 43 |
if err := term.Drain(); err != nil { |
| 44 |
t.Fatalf("Failed to flush: %v", err) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func ReadF(t *testing.T, term MockTerm) string { |
| 50 |
t.Helper() |
| 51 |
buf := make([]byte, 128) |
| 52 |
n, err := term.Read(buf) |
| 53 |
if err != nil { |
| 54 |
t.Errorf("failed read: %v", err) |
| 55 |
return "" |
| 56 |
} |
| 57 |
return string(buf[:n]) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
func VerifyF(t *testing.T, b bool, fmt string, args ...any) { |
| 62 |
t.Helper() |
| 63 |
if !b { |
| 64 |
t.Errorf("validation failure: "+fmt, args...) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func AssertF(t *testing.T, b bool, fmt string, args ...any) { |
| 70 |
t.Helper() |
| 71 |
if !b { |
| 72 |
t.Fatalf("validation failure: "+fmt, args...) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
func MustClose(t *testing.T, term MockTerm) { |
| 78 |
t.Helper() |
| 79 |
err := term.Close() |
| 80 |
AssertF(t, err == nil, "close failed: %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func MustStart(t *testing.T, term MockTerm) { |
| 85 |
t.Helper() |
| 86 |
err := term.Start() |
| 87 |
AssertF(t, err == nil, "start failed: %v", err) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func CheckPos(t *testing.T, term MockTerm, x Col, y Row) { |
| 92 |
t.Helper() |
| 93 |
VerifyF(t, term.Pos().X == x && term.Pos().Y == y, |
| 94 |
"bad position %d,%d (expected %d,%d)", term.Pos().X, term.Pos().Y, x, y) |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func CheckContent(t *testing.T, term MockTerm, x Col, y Row, s string) { |
| 99 |
t.Helper() |
| 100 |
if actual := string(term.GetCell(Coord{X: x, Y: y}).C); actual != s { |
| 101 |
t.Errorf("bad content %d,%d (expected %q got %q)", x, y, s, actual) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func CheckAttrs(t *testing.T, term MockTerm, x Col, y Row, a Attr) { |
| 107 |
t.Helper() |
| 108 |
if actual := term.GetCell(Coord{X: x, Y: y}).S.Attr(); actual != a { |
| 109 |
t.Errorf("bad attr %d,%d (expected %x got %x)", x, y, a, actual) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
func CheckColors(t *testing.T, term MockTerm, x Col, y Row, fg color.Color, bg color.Color) { |
| 115 |
t.Helper() |
| 116 |
if actual := term.GetCell(Coord{X: x, Y: y}).S.Fg(); actual != fg { |
| 117 |
t.Errorf("bad foreground %d,%d (expected %s got %s)", x, y, fg.String(), actual.String()) |
| 118 |
} |
| 119 |
if actual := term.GetCell(Coord{X: x, Y: y}).S.Bg(); actual != bg { |
| 120 |
t.Errorf("bad background %d,%d (expected %s got %s)", x, y, bg.String(), actual.String()) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func CheckRead(t *testing.T, term MockTerm, want string) { |
| 126 |
t.Helper() |
| 127 |
result := ReadF(t, term) |
| 128 |
VerifyF(t, want == result, "wrong read %q != %q", result, want) |
| 129 |
} |