| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package vt |
| 16 |
|
| 17 |
import ( |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/gdamore/tcell/v3/color" |
| 21 |
) |
| 22 |
|
| 23 |
func TestCursorStyleHelpers(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
|
| 26 |
tests := []struct { |
| 27 |
name string |
| 28 |
style CursorStyle |
| 29 |
visible bool |
| 30 |
blinking bool |
| 31 |
}{ |
| 32 |
{name: "steady block", style: SteadyBlock, visible: true, blinking: false}, |
| 33 |
{name: "steady bar", style: SteadyBar, visible: true, blinking: false}, |
| 34 |
{name: "steady underline", style: SteadyUnderline, visible: true, blinking: false}, |
| 35 |
{name: "blinking block", style: BlinkingBlock, visible: true, blinking: true}, |
| 36 |
{name: "hidden blink", style: BlinkingBar.Hide(), visible: false, blinking: true}, |
| 37 |
} |
| 38 |
|
| 39 |
for _, tc := range tests { |
| 40 |
tc := tc |
| 41 |
t.Run(tc.name, func(t *testing.T) { |
| 42 |
t.Parallel() |
| 43 |
|
| 44 |
if got := tc.style.IsVisible(); got != tc.visible { |
| 45 |
t.Fatalf("IsVisible() = %v, want %v", got, tc.visible) |
| 46 |
} |
| 47 |
if got := tc.style.IsBlinking(); got != tc.blinking { |
| 48 |
t.Fatalf("IsBlinking() = %v, want %v", got, tc.blinking) |
| 49 |
} |
| 50 |
|
| 51 |
if got := tc.style.Hide().IsVisible(); got { |
| 52 |
t.Fatalf("Hide() should clear visibility") |
| 53 |
} |
| 54 |
if got := tc.style.Show().IsVisible(); !got { |
| 55 |
t.Fatalf("Show() should set visibility") |
| 56 |
} |
| 57 |
if got := tc.style.Blink().IsBlinking(); !got { |
| 58 |
t.Fatalf("Blink() should set blinking") |
| 59 |
} |
| 60 |
if got := tc.style.Steady().IsBlinking(); got { |
| 61 |
t.Fatalf("Steady() should clear blinking") |
| 62 |
} |
| 63 |
}) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestModeFormatters(t *testing.T) { |
| 68 |
t.Parallel() |
| 69 |
|
| 70 |
tests := []struct { |
| 71 |
name string |
| 72 |
pm PrivateMode |
| 73 |
enable string |
| 74 |
disable string |
| 75 |
query string |
| 76 |
replyOn string |
| 77 |
replyOff string |
| 78 |
ansi AnsiMode |
| 79 |
ansiEnable string |
| 80 |
ansiDisable string |
| 81 |
ansiQuery string |
| 82 |
ansiReply string |
| 83 |
}{ |
| 84 |
{ |
| 85 |
name: "auto margin", |
| 86 |
pm: PmAutoMargin, |
| 87 |
enable: "\x1b[?7h", |
| 88 |
disable: "\x1b[?7l", |
| 89 |
query: "\x1b[?7$p", |
| 90 |
replyOn: "\x1b[?7;1$y", |
| 91 |
replyOff: "\x1b[?7;2$y", |
| 92 |
ansi: AmNewLineMode, |
| 93 |
ansiEnable: "\x1b[20h", |
| 94 |
ansiDisable: "\x1b[20l", |
| 95 |
ansiQuery: "\x1b[20$p", |
| 96 |
ansiReply: "\x1b[20;1$y", |
| 97 |
}, |
| 98 |
} |
| 99 |
|
| 100 |
for _, tc := range tests { |
| 101 |
if got := tc.pm.Enable(); got != tc.enable { |
| 102 |
t.Fatalf("PrivateMode.Enable() = %q, want %q", got, tc.enable) |
| 103 |
} |
| 104 |
if got := tc.pm.Disable(); got != tc.disable { |
| 105 |
t.Fatalf("PrivateMode.Disable() = %q, want %q", got, tc.disable) |
| 106 |
} |
| 107 |
if got := tc.pm.Query(); got != tc.query { |
| 108 |
t.Fatalf("PrivateMode.Query() = %q, want %q", got, tc.query) |
| 109 |
} |
| 110 |
if got := tc.pm.Reply(ModeOn); got != tc.replyOn { |
| 111 |
t.Fatalf("PrivateMode.Reply(ModeOn) = %q, want %q", got, tc.replyOn) |
| 112 |
} |
| 113 |
if got := tc.pm.Reply(ModeOff); got != tc.replyOff { |
| 114 |
t.Fatalf("PrivateMode.Reply(ModeOff) = %q, want %q", got, tc.replyOff) |
| 115 |
} |
| 116 |
|
| 117 |
if got := tc.ansi.Enable(); got != tc.ansiEnable { |
| 118 |
t.Fatalf("AnsiMode.Enable() = %q, want %q", got, tc.ansiEnable) |
| 119 |
} |
| 120 |
if got := tc.ansi.Disable(); got != tc.ansiDisable { |
| 121 |
t.Fatalf("AnsiMode.Disable() = %q, want %q", got, tc.ansiDisable) |
| 122 |
} |
| 123 |
if got := tc.ansi.Query(); got != tc.ansiQuery { |
| 124 |
t.Fatalf("AnsiMode.Query() = %q, want %q", got, tc.ansiQuery) |
| 125 |
} |
| 126 |
if got := tc.ansi.Reply(ModeOn); got != tc.ansiReply { |
| 127 |
t.Fatalf("AnsiMode.Reply(ModeOn) = %q, want %q", got, tc.ansiReply) |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func TestModeStatusChangeable(t *testing.T) { |
| 133 |
t.Parallel() |
| 134 |
|
| 135 |
cases := []struct { |
| 136 |
status ModeStatus |
| 137 |
want bool |
| 138 |
}{ |
| 139 |
{ModeNA, false}, |
| 140 |
{ModeOn, true}, |
| 141 |
{ModeOff, true}, |
| 142 |
{ModeOnLocked, false}, |
| 143 |
{ModeOffLocked, false}, |
| 144 |
} |
| 145 |
|
| 146 |
for _, tc := range cases { |
| 147 |
if got := tc.status.Changeable(); got != tc.want { |
| 148 |
t.Fatalf("Changeable(%v) = %v, want %v", tc.status, got, tc.want) |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
func TestMockBackendResizeAndBells(t *testing.T) { |
| 154 |
t.Parallel() |
| 155 |
|
| 156 |
mb := NewMockBackend(MockOptSize{X: 2, Y: 2}, MockOptColors(0)).(*mockBackend) |
| 157 |
|
| 158 |
if got := mb.Bells(); got != 0 { |
| 159 |
t.Fatalf("initial bells = %d, want 0", got) |
| 160 |
} |
| 161 |
mb.Beep() |
| 162 |
mb.Beep() |
| 163 |
if got := mb.Bells(); got != 2 { |
| 164 |
t.Fatalf("bells after beep = %d, want 2", got) |
| 165 |
} |
| 166 |
|
| 167 |
mb.Put(Coord{X: 0, Y: 0}, Cell{C: "x", S: BaseStyle, W: 1}) |
| 168 |
mb.SetPosition(Coord{X: 1, Y: 1}) |
| 169 |
mb.SetSize(Coord{X: 3, Y: 1}) |
| 170 |
if got := mb.GetSize(); got != (Coord{X: 3, Y: 1}) { |
| 171 |
t.Fatalf("GetSize() = %v, want {3 1}", got) |
| 172 |
} |
| 173 |
|
| 174 |
if got := mb.GetCell(Coord{X: 0, Y: 0}); got.C != "x" || got.W != 1 { |
| 175 |
t.Fatalf("resized cell preserved incorrectly: %#v", got) |
| 176 |
} |
| 177 |
if got := mb.GetCell(Coord{X: 2, Y: 0}); got.S != BaseStyle { |
| 178 |
t.Fatalf("new cell style = %#v, want BaseStyle", got.S) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestStringCacheHelpers(t *testing.T) { |
| 183 |
t.Parallel() |
| 184 |
|
| 185 |
mb := NewMockBackend(MockOptSize{X: 4, Y: 1}, MockOptColors(0)).(*mockBackend) |
| 186 |
em := NewEmulator(mb).(*emulator) |
| 187 |
|
| 188 |
if got := em.runeString('a'); got != "a" { |
| 189 |
t.Fatalf("runeString(ascii) = %q, want %q", got, "a") |
| 190 |
} |
| 191 |
if got := em.runeString('\u03c0'); got != "π" { |
| 192 |
t.Fatalf("runeString(non-ascii) = %q, want %q", got, "π") |
| 193 |
} |
| 194 |
if got := em.runeString('\u03c0'); got != "π" { |
| 195 |
t.Fatalf("runeString cache hit returned %q, want %q", got, "π") |
| 196 |
} |
| 197 |
|
| 198 |
if got := em.clusterString([]byte("e\u0301")); got != "e\u0301" { |
| 199 |
t.Fatalf("clusterString(combining) = %q, want %q", got, "e\u0301") |
| 200 |
} |
| 201 |
if got := em.clusterString([]byte("e\u0301")); got != "e\u0301" { |
| 202 |
t.Fatalf("clusterString cache hit returned %q, want %q", got, "e\u0301") |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func TestEmulatorBellDispatch(t *testing.T) { |
| 207 |
t.Parallel() |
| 208 |
|
| 209 |
mb := NewMockBackend(MockOptSize{X: 4, Y: 1}, MockOptColors(0)).(*mockBackend) |
| 210 |
em := NewEmulator(mb).(*emulator) |
| 211 |
em.style = BaseStyle.WithFg(color.White).WithBg(color.Black) |
| 212 |
em.defaultStyle = em.style |
| 213 |
|
| 214 |
em.beep() |
| 215 |
if got := mb.Bells(); got != 1 { |
| 216 |
t.Fatalf("bell count = %d, want 1", got) |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
func TestShouldCheckGrapheme(t *testing.T) { |
| 221 |
t.Parallel() |
| 222 |
|
| 223 |
cases := []struct { |
| 224 |
name string |
| 225 |
prev byte |
| 226 |
r rune |
| 227 |
want bool |
| 228 |
}{ |
| 229 |
{name: "crlf", prev: '\r', r: '\n', want: true}, |
| 230 |
{name: "ascii false", prev: 'a', r: 'b', want: false}, |
| 231 |
{name: "mark", prev: 'e', r: '\u0301', want: true}, |
| 232 |
{name: "zwj", prev: 'x', r: '\u200d', want: true}, |
| 233 |
{name: "vs16", prev: 'x', r: '\uFE0F', want: true}, |
| 234 |
{name: "supplementary vs", prev: 'x', r: '\U000E0100', want: true}, |
| 235 |
{name: "plain non-ascii", prev: 'x', r: 'π', want: false}, |
| 236 |
} |
| 237 |
|
| 238 |
for _, tc := range cases { |
| 239 |
tc := tc |
| 240 |
t.Run(tc.name, func(t *testing.T) { |
| 241 |
t.Parallel() |
| 242 |
|
| 243 |
if got := shouldCheckGrapheme(tc.prev, tc.r); got != tc.want { |
| 244 |
t.Fatalf("shouldCheckGrapheme(%q, %q) = %v, want %v", tc.prev, tc.r, got, tc.want) |
| 245 |
} |
| 246 |
}) |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
func TestPutRunePaths(t *testing.T) { |
| 251 |
newEm := func(width int) *emulator { |
| 252 |
em := NewEmulator(NewMockBackend(MockOptSize{X: Col(width), Y: 1}, MockOptColors(0))).(*emulator) |
| 253 |
em.style = BaseStyle.WithFg(color.White).WithBg(color.Black) |
| 254 |
em.defaultStyle = em.style |
| 255 |
em.localModes[PmGraphemeClusters] = ModeOn |
| 256 |
em.localModes[PmAutoMargin] = ModeOn |
| 257 |
em.cells[0].S = em.style |
| 258 |
em.cells[0].W = 1 |
| 259 |
return em |
| 260 |
} |
| 261 |
|
| 262 |
t.Run("ascii falls through", func(t *testing.T) { |
| 263 |
em := newEm(4) |
| 264 |
em.cells[0].C = "a" |
| 265 |
em.lastIndex = 1 |
| 266 |
em.setPosition(Coord{X: 1, Y: 0}) |
| 267 |
|
| 268 |
em.putRune('b') |
| 269 |
|
| 270 |
if got := em.cells[1].C; got != "b" { |
| 271 |
t.Fatalf("cell[1].C = %q, want %q", got, "b") |
| 272 |
} |
| 273 |
if got := em.cells[0].C; got != "a" { |
| 274 |
t.Fatalf("cell[0].C = %q, want %q", got, "a") |
| 275 |
} |
| 276 |
}) |
| 277 |
|
| 278 |
t.Run("combining mark extends cluster", func(t *testing.T) { |
| 279 |
em := newEm(4) |
| 280 |
em.cells[0].C = "e" |
| 281 |
em.lastIndex = 1 |
| 282 |
em.setPosition(Coord{X: 1, Y: 0}) |
| 283 |
|
| 284 |
em.putRune('\u0301') |
| 285 |
|
| 286 |
if got := em.cells[0].C; got != "e\u0301" { |
| 287 |
t.Fatalf("cell[0].C = %q, want %q", got, "e\u0301") |
| 288 |
} |
| 289 |
if got := em.cells[0].W; got != 1 { |
| 290 |
t.Fatalf("cell[0].W = %d, want 1", got) |
| 291 |
} |
| 292 |
}) |
| 293 |
|
| 294 |
t.Run("wide grapheme clears next cell", func(t *testing.T) { |
| 295 |
em := newEm(4) |
| 296 |
em.cells[0].C = "\u2764" |
| 297 |
em.cells[1].C = "x" |
| 298 |
em.cells[1].S = em.style |
| 299 |
em.cells[1].W = 1 |
| 300 |
em.lastIndex = 1 |
| 301 |
em.setPosition(Coord{X: 1, Y: 0}) |
| 302 |
|
| 303 |
em.putRune('\uFE0F') |
| 304 |
|
| 305 |
if got := em.cells[0].W; got != 2 { |
| 306 |
t.Fatalf("cell[0].W = %d, want 2", got) |
| 307 |
} |
| 308 |
if got := em.cells[1].C; got != "" || em.cells[1].W != 0 { |
| 309 |
t.Fatalf("cell[1] not cleared: %#v", em.cells[1]) |
| 310 |
} |
| 311 |
}) |
| 312 |
|
| 313 |
t.Run("auto wrap when width reaches margin", func(t *testing.T) { |
| 314 |
em := newEm(2) |
| 315 |
em.cells[0].C = "a" |
| 316 |
em.lastIndex = 1 |
| 317 |
em.setPosition(Coord{X: 1, Y: 0}) |
| 318 |
|
| 319 |
em.putRune('宽') |
| 320 |
|
| 321 |
if !em.autoWrap { |
| 322 |
t.Fatalf("autoWrap = false, want true") |
| 323 |
} |
| 324 |
}) |
| 325 |
|
| 326 |
t.Run("grapheme extension at margin preserves wrap", func(t *testing.T) { |
| 327 |
em := newEm(2) |
| 328 |
em.cells[1].C = "\u2764" |
| 329 |
em.cells[1].S = em.style |
| 330 |
em.cells[1].W = 1 |
| 331 |
em.lastIndex = 2 |
| 332 |
em.setPosition(Coord{X: 1, Y: 0}) |
| 333 |
|
| 334 |
em.putRune('\uFE0F') |
| 335 |
|
| 336 |
if !em.autoWrap { |
| 337 |
t.Fatalf("autoWrap = false, want true") |
| 338 |
} |
| 339 |
if got := em.getPosition(); got.X != 1 { |
| 340 |
t.Fatalf("cursor X = %d, want 1", got.X) |
| 341 |
} |
| 342 |
}) |
| 343 |
} |