Debian Codegraph @

vt/emulate_internal_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 vt
16
17 import (
18 "slices"
19 "testing"
20
21 "github.com/gdamore/tcell/v3/color"
22 )
23
24 type noMouseBackend struct {
25 mb *mockBackend
26 }
27
28 func (n noMouseBackend) GetPrivateMode(pm PrivateMode) ModeStatus { return n.mb.GetPrivateMode(pm) }
29 func (n noMouseBackend) SetPrivateMode(pm PrivateMode, status ModeStatus) error {
30 return n.mb.SetPrivateMode(pm, status)
31 }
32 func (n noMouseBackend) GetSize() Coord { return n.mb.GetSize() }
33 func (n noMouseBackend) Colors() int { return n.mb.Colors() }
34 func (n noMouseBackend) Put(pos Coord, cell Cell) { n.mb.Put(pos, cell) }
35 func (n noMouseBackend) GetPosition() Coord { return n.mb.GetPosition() }
36 func (n noMouseBackend) SetPosition(pos Coord) { n.mb.SetPosition(pos) }
37 func (n noMouseBackend) Reset() { n.mb.Reset() }
38 func (n noMouseBackend) RaiseResize() { n.mb.RaiseResize() }
39 func (n noMouseBackend) Buffering(enabled bool) { n.mb.Buffering(enabled) }
40 func (n noMouseBackend) SetCursor(cs CursorStyle) { n.mb.SetCursor(cs) }
41
42 func TestEmulatorModeHelpers(t *testing.T) {
43 mb := NewMockBackend().(*mockBackend)
44 em := NewEmulator(mb).(*emulator)
45
46 ansiKeys := em.ansiModeKeys()
47 if !slices.Contains(ansiKeys, AmNewLineMode) {
48 t.Fatalf("expected AmNewLineMode in ansi mode keys, got %v", ansiKeys)
49 }
50
51 privateKeys := em.privateModeKeys()
52 if !slices.Contains(privateKeys, PmAutoMargin) {
53 t.Fatalf("expected PmAutoMargin in private mode keys, got %v", privateKeys)
54 }
55
56 em.setAnsiMode(AmNewLineMode, ModeOn)
57 if got := em.getAnsiMode(AmNewLineMode); got != ModeOn {
58 t.Fatalf("setAnsiMode did not update changeable mode: got %v", got)
59 }
60
61 em.setAnsiMode(AmNewLineMode, ModeNA)
62 if got := em.getAnsiMode(AmNewLineMode); got != ModeOn {
63 t.Fatalf("setAnsiMode changed mode for non-changeable status: got %v", got)
64 }
65
66 em.ansiModes[AmInsertReplace] = ModeOnLocked
67 em.setAnsiMode(AmInsertReplace, ModeOff)
68 if got := em.getAnsiMode(AmInsertReplace); got != ModeOnLocked {
69 t.Fatalf("setAnsiMode changed locked mode: got %v", got)
70 }
71
72 em.setAnsiMode(AnsiMode(9999), ModeOn)
73 if _, ok := em.ansiModes[AnsiMode(9999)]; ok {
74 t.Fatalf("setAnsiMode created an unknown mode entry")
75 }
76 }
77
78 func TestEmulatorUpdateMouseReporting(t *testing.T) {
79 mb := NewMockBackend().(*mockBackend)
80 em := NewEmulator(mb).(*emulator)
81
82 em.localModes[PmMouseButton] = ModeOn
83 em.updateMouseReporting()
84 if em.mouseReports != MouseButtons {
85 t.Fatalf("expected mouse buttons reporting, got %v", em.mouseReports)
86 }
87
88 em.localModes[PmMouseDrag] = ModeOn
89 em.updateMouseReporting()
90 if em.mouseReports != MouseDrag {
91 t.Fatalf("expected mouse drag reporting, got %v", em.mouseReports)
92 }
93
94 em.localModes[PmMouseMotion] = ModeOn
95 em.updateMouseReporting()
96 if em.mouseReports != MouseMotion {
97 t.Fatalf("expected mouse motion reporting, got %v", em.mouseReports)
98 }
99
100 em.localModes[PmMouseButton] = ModeOff
101 em.localModes[PmMouseDrag] = ModeOff
102 em.localModes[PmMouseMotion] = ModeOff
103 em.localModes[PmMouseX10] = ModeOn
104 em.updateMouseReporting()
105 if em.mouseReports != MouseButtons {
106 t.Fatalf("expected mouse X10 reporting to map to buttons, got %v", em.mouseReports)
107 }
108
109 em.localModes[PmMouseX10] = ModeOff
110 em.updateMouseReporting()
111 if em.mouseReports != MouseDisabled {
112 t.Fatalf("expected mouse reporting disabled, got %v", em.mouseReports)
113 }
114
115 noMouse := NewEmulator(noMouseBackend{mb: NewMockBackend().(*mockBackend)}).(*emulator)
116 noMouse.updateMouseReporting()
117 }
118
119 func TestMockBackendHelpers(t *testing.T) {
120 mb := NewMockBackend().(*mockBackend)
121
122 style := BaseStyle.WithFg(color.Red).WithBg(color.Blue)
123 mb.SetStyle(style)
124 if mb.style != style {
125 t.Fatalf("SetStyle did not update backend style")
126 }
127
128 mb.SetMouse(MouseMotion)
129 mb.Buffering(true)
130 mb.Buffering(false)
131
132 MockOptNoBlit{}.SetMockOpt(mb)
133 }
134
135 func TestApplyResizePrunesTabStops(t *testing.T) {
136 em := NewEmulator(NewMockBackend(MockOptSize{X: 80, Y: 24})).(*emulator)
137 em.tabStops = []Col{8, 16, 24, 32, 40, 48}
138
139 em.applyResize(Coord{X: 32, Y: 24})
140
141 if got, want := em.tabStops, []Col{8, 16, 24}; !slices.Equal(got, want) {
142 t.Fatalf("unexpected tab stops after resize: got %v, want %v", got, want)
143 }
144
145 em.setPosition(Coord{X: 0, Y: 0})
146 for _, want := range []Col{8, 16, 24, 31} {
147 em.nextTab()
148 if got := em.getPosition().X; got != want {
149 t.Fatalf("unexpected tab position after resize: got %d, want %d", got, want)
150 }
151 }
152 }
153
154 func TestPutRuneGraphemeExtensions(t *testing.T) {
155 t.Parallel()
156
157 newEm := func() *emulator {
158 em := NewEmulator(NewMockBackend(MockOptSize{X: 8, Y: 1}, MockOptColors(0))).(*emulator)
159 em.style = BaseStyle.WithFg(color.White).WithBg(color.Black)
160 em.defaultStyle = em.style
161 em.localModes[PmGraphemeClusters] = ModeOn
162 em.localModes[PmAutoMargin] = ModeOn
163 em.cells[0].S = em.style
164 em.cells[0].W = 1
165 return em
166 }
167
168 t.Run("combining", func(t *testing.T) {
169 em := newEm()
170 em.cells[0].C = "e"
171 em.lastIndex = 1
172 em.pos = Coord{X: 1, Y: 0}
173
174 em.putRune('\u0301')
175
176 if got := em.cells[0].C; got != "e\u0301" {
177 t.Fatalf("unexpected cluster: %q", got)
178 }
179 if got := em.cells[0].W; got != 1 {
180 t.Fatalf("unexpected width: got %d, want 1", got)
181 }
182 })
183
184 t.Run("variation-selector", func(t *testing.T) {
185 em := newEm()
186 em.cells[0].C = "\u2764"
187 em.lastIndex = 1
188 em.pos = Coord{X: 1, Y: 0}
189
190 em.putRune('\uFE0F')
191
192 if got := em.cells[0].W; got != 2 {
193 t.Fatalf("unexpected width: got %d, want 2", got)
194 }
195 })
196
197 t.Run("regional-indicator", func(t *testing.T) {
198 em := newEm()
199 em.cells[0].C = "\U0001F1E6"
200 em.lastIndex = 1
201 em.pos = Coord{X: 1, Y: 0}
202
203 em.putRune('\U0001F1E7')
204
205 if got := em.cells[0].W; got != 2 {
206 t.Fatalf("unexpected width: got %d, want 2", got)
207 }
208 })
209 }