Debian Codegraph @

vt/tests/util.go

raw symbols SCIP
1 // Copyright 2026 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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 // WriteF writes the string, and ensures it is fully flushed
32 // before returning.
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 // ReadF reads content from the term and returns it as a string.
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 // VerifyF validates the condition, printing the message on failure.
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 // AssertF validates the condition, and aborts the test if it fails.
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 // MustClose closes or calls Fatalf.
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 // MustStart starts the terminal or calls Fatalf.
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 // CheckPos is verifies the current cursor position of terminal.
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 // CheckContent verifies the content at a given cell of the terminal.
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 // CheckAttrs verifies the attributes of a given cell.
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 // CheckColors verifies the colors of a given cell.
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 // CheckRead verifies that a read matches.
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 }