okami.mom/ultraviolet

git clone https://git.nkp.pet/okami.mom/ultraviolet

Christian Rochafix(renderer): make wide-cell placeholder handling consistentc7cac3e

main
1.9 KiB54 linesraw
1package uv
2
3import (
4	"testing"
5
6	"github.com/charmbracelet/x/ansi"
7)
8
9// TestStyledWideCellPlaceholderDetection reproduces the placeholder-detection
10// inconsistency introduced by PR #124.
11//
12// Line.Set marks the continuation columns of a wide cell with Width == 0
13// placeholders. The PR makes those placeholders inherit the wide cell's Style
14// and Link. The renderer detects placeholders two different ways:
15//
16//   - putAttrCell (terminal_renderer.go:490) uses cell.Width == 0
17//   - putRange / transformLine (terminal_renderer.go:694,942,950) use IsZero()
18//
19// For an unstyled wide cell both agree, because the placeholder equals Cell{}.
20// For a styled wide cell the placeholder is no longer zero, so IsZero() stops
21// recognizing it while Width == 0 still does. The two checks disagree, which is
22// how transformLine can pick a move/insert point inside a wide glyph.
23func TestStyledWideCellPlaceholderDetection(t *testing.T) {
24	t.Run("unstyled wide cell: both checks agree", func(t *testing.T) {
25		l := make(Line, 5)
26		l.Set(0, &Cell{Content: "你", Width: 2})
27
28		ph := l.At(1)
29		if ph.Width != 0 {
30			t.Fatalf("expected placeholder Width == 0, got %d", ph.Width)
31		}
32		if !ph.IsZero() {
33			t.Errorf("expected unstyled placeholder to be IsZero()")
34		}
35	})
36
37	t.Run("styled wide cell: checks disagree (regression)", func(t *testing.T) {
38		l := make(Line, 5)
39		l.Set(0, &Cell{Content: "你", Width: 2, Style: Style{Bg: ansi.Red}})
40
41		ph := l.At(1)
42		if ph.Width != 0 {
43			t.Fatalf("expected placeholder Width == 0, got %d", ph.Width)
44		}
45		// This is the bug: the Width==0 placeholder is no longer IsZero(), so
46		// every renderer path still keyed on IsZero() fails to skip it.
47		if ph.IsZero() {
48			t.Skip("placeholder is zero; PR #124 style inheritance not present")
49		}
50		t.Errorf("styled wide-cell placeholder has Width==0 but IsZero()==false: "+
51			"renderer paths using IsZero() (terminal_renderer.go:694,942,950) "+
52			"will not recognize it; placeholder=%+v", *ph)
53	})
54}