Debian Codegraph @

demos/hello/hello_test.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 main
16
17 import (
18 "sync"
19 "testing"
20 "time"
21
22 "github.com/gdamore/tcell/v3"
23 "github.com/gdamore/tcell/v3/color"
24 "github.com/gdamore/tcell/v3/vt"
25 )
26
27 func TestHello(t *testing.T) {
28 // ensure we only use 8 color ANSI for now
29
30 mt := vt.NewMockTerm(vt.MockOptColors(8))
31 scr, err := tcell.NewTerminfoScreenFromTty(mt, tcell.OptColors(8), tcell.OptTerm("ansi"))
32 if err != nil {
33 t.Fatalf("failed to create screen: %v", err)
34 }
35 tcell.ShimScreen(scr)
36 var wg sync.WaitGroup
37 wg.Add(1)
38
39 go func() {
40 defer wg.Done()
41 main()
42 }()
43
44 // give enough time for the screen to draw
45 // this is needed because main is running asynchronously.
46 time.Sleep(time.Millisecond * 50)
47
48 mt.Drain()
49
50 expect := []struct {
51 X vt.Col
52 Y vt.Row
53 C string
54 Fg color.Color
55 Bg color.Color
56 Attr vt.Attr
57 }{
58 {X: 33, Y: 11, C: "H", Fg: color.Teal, Bg: color.Silver},
59 {X: 34, Y: 11, C: "e", Fg: color.Teal, Bg: color.Silver},
60 {X: 35, Y: 11, C: "l", Fg: color.Teal, Bg: color.Silver},
61 {X: 36, Y: 11, C: "l", Fg: color.Teal, Bg: color.Silver},
62 {X: 37, Y: 11, C: "o", Fg: color.Teal, Bg: color.Silver},
63 {X: 38, Y: 11, C: ",", Fg: color.Teal, Bg: color.Silver},
64 {X: 39, Y: 11, C: " ", Fg: color.Teal, Bg: color.Silver},
65 {X: 40, Y: 11, C: "W", Fg: color.Teal, Bg: color.Silver},
66 {X: 41, Y: 11, C: "o", Fg: color.Teal, Bg: color.Silver},
67 {X: 42, Y: 11, C: "r", Fg: color.Teal, Bg: color.Silver},
68 {X: 43, Y: 11, C: "l", Fg: color.Teal, Bg: color.Silver},
69 {X: 44, Y: 11, C: "d", Fg: color.Teal, Bg: color.Silver},
70 {X: 45, Y: 11, C: "!", Fg: color.Teal, Bg: color.Silver},
71 {X: 31, Y: 13, C: "P", Fg: color.Silver, Bg: color.Black},
72 {X: 32, Y: 13, C: "r", Fg: color.Silver, Bg: color.Black},
73 {X: 33, Y: 13, C: "e", Fg: color.Silver, Bg: color.Black},
74 {X: 34, Y: 13, C: "s", Fg: color.Silver, Bg: color.Black},
75 {X: 35, Y: 13, C: "s", Fg: color.Silver, Bg: color.Black},
76 {X: 36, Y: 13, C: " ", Fg: color.Silver, Bg: color.Black},
77 {X: 37, Y: 13, C: "E", Fg: color.Silver, Bg: color.Black, Attr: vt.Bold},
78 {X: 38, Y: 13, C: "S", Fg: color.Silver, Bg: color.Black, Attr: vt.Bold},
79 {X: 39, Y: 13, C: "C", Fg: color.Silver, Bg: color.Black, Attr: vt.Bold},
80 {X: 40, Y: 13, C: " ", Fg: color.Silver, Bg: color.Black},
81 {X: 41, Y: 13, C: "t", Fg: color.Silver, Bg: color.Black},
82 {X: 42, Y: 13, C: "o", Fg: color.Silver, Bg: color.Black},
83 {X: 43, Y: 13, C: " ", Fg: color.Silver, Bg: color.Black},
84 {X: 44, Y: 13, C: "e", Fg: color.Silver, Bg: color.Black},
85 {X: 45, Y: 13, C: "x", Fg: color.Silver, Bg: color.Black},
86 {X: 46, Y: 13, C: "i", Fg: color.Silver, Bg: color.Black},
87 {X: 47, Y: 13, C: "t", Fg: color.Silver, Bg: color.Black},
88 {X: 48, Y: 13, C: ".", Fg: color.Silver, Bg: color.Black},
89 }
90
91 for _, v := range expect {
92 cell := mt.GetCell(vt.Coord{X: v.X, Y: v.Y})
93 if v.C != string(cell.C) {
94 t.Errorf("Mismatch string at %d,%d: %q != %q", v.X, v.Y, string(cell.C), v.C)
95 }
96 if v.Fg != cell.S.Fg() {
97 t.Errorf("Mismatch foreground at %d,%d: %s != %s", v.X, v.Y, cell.S.Fg().String(), v.Fg.String())
98 }
99 if v.Bg != cell.S.Bg() {
100 t.Errorf("Mismatch background at %d,%d: %s != %s", v.X, v.Y, cell.S.Bg().String(), v.Bg.String())
101 }
102 if v.Attr != cell.S.Attr() {
103 t.Errorf("Mismatch attr at %d,%d: %x != %x", v.X, v.Y, cell.S.Attr(), v.Attr)
104 }
105 }
106
107 mt.KeyTap(vt.KeyEsc)
108 wg.Wait()
109 }