Debian Codegraph @

vt_test.go

raw
1 package terminal
2
3 import (
4 "io"
5 "strings"
6 "testing"
7 )
8
9 func extractStr(t *State, x0, x1, row int) string {
10 var s []rune
11 for i := x0; i <= x1; i++ {
12 c, _, _ := t.Cell(i, row)
13 s = append(s, c)
14 }
15 return string(s)
16 }
17
18 func TestPlainChars(t *testing.T) {
19 var st State
20 term, err := Create(&st, nil)
21 if err != nil {
22 t.Fatal(err)
23 }
24 expected := "Hello world!"
25 _, err = term.Write([]byte(expected))
26 if err != nil && err != io.EOF {
27 t.Fatal(err)
28 }
29 actual := extractStr(&st, 0, len(expected)-1, 0)
30 if expected != actual {
31 t.Fatal(actual)
32 }
33 }
34
35 func TestNewline(t *testing.T) {
36 var st State
37 term, err := Create(&st, nil)
38 if err != nil {
39 t.Fatal(err)
40 }
41 expected := "Hello world!\n...and more."
42 _, err = term.Write([]byte("\033[20h")) // set CRLF mode
43 if err != nil && err != io.EOF {
44 t.Fatal(err)
45 }
46 _, err = term.Write([]byte(expected))
47 if err != nil && err != io.EOF {
48 t.Fatal(err)
49 }
50
51 split := strings.Split(expected, "\n")
52 actual := extractStr(&st, 0, len(split[0])-1, 0)
53 actual += "\n"
54 actual += extractStr(&st, 0, len(split[1])-1, 1)
55 if expected != actual {
56 t.Fatal(actual)
57 }
58
59 // A newline with a color set should not make the next line that color,
60 // which used to happen if it caused a scroll event.
61 st.moveTo(0, st.rows-1)
62 _, err = term.Write([]byte("\033[1;37m\n$ \033[m"))
63 if err != nil && err != io.EOF {
64 t.Fatal(err)
65 }
66 _, fg, bg := st.Cell(st.Cursor())
67 if fg != DefaultFG {
68 t.Fatal(st.cur.x, st.cur.y, fg, bg)
69 }
70 }