1package copy
2
3import (
4 "testing"
5
6 renderer "github.com/bnema/vev-vt/ansi"
7 "github.com/stretchr/testify/require"
8)
9
10func TestDrawCopyStatus(t *testing.T) {
11 tests := []struct {
12 name string
13 width int
14 lines []string
15 query string
16 want string
17 }{
18 {name: "empty document", width: 12, want: " [SCROLL] 0/"},
19 {name: "position", width: 16, lines: []string{"a", "b"}, want: " [SCROLL] 2/2 "},
20 {name: "truncated", width: 5, lines: []string{"a"}, want: " [SCR"},
21 {name: "zero width", width: 0, lines: []string{"a"}, want: ""},
22 {name: "multibyte query is contiguous", width: 22, lines: []string{"éa"}, query: "é", want: " [SCROLL] 1/1 1/1 /é "},
23 }
24 for _, tt := range tests {
25 t.Run(tt.name, func(t *testing.T) {
26 m := modeFor(tt.lines, 1)
27 if tt.query != "" {
28 m.Search(tt.query)
29 }
30 row := make([]renderer.Cell, tt.width)
31 drawCopyStatus(row, m, m.document.Len(), renderer.Style{})
32 var got []rune
33 for _, cell := range row {
34 got = append(got, cell.Rune)
35 }
36 require.Equal(t, tt.want, string(got)[:len(tt.want)])
37 require.Len(t, row, tt.width)
38 })
39 }
40}
41
42// TestRenderRowsRangeReusesRowsAcrossWidths guards the pooled row buffer:
43// a narrow render between two wide ones must not leak cells.
44func TestRenderRowsRangeReusesRowsAcrossWidths(t *testing.T) {
45 wide := NewMode(NewDocument(NewSnapshotFromRows([][]renderer.Cell{row("abcdefgh")}, 8, 1), ""))
46 narrow := NewMode(NewDocument(NewSnapshotFromRows([][]renderer.Cell{row("xy")}, 2, 1), ""))
47 for _, m := range []*Mode{wide, narrow, wide} {
48 full := m.Render()
49 m.RenderRowsRange(0, 1, func(y int, got []renderer.Cell) {
50 require.Equal(t, full.Row(y), got)
51 })
52 }
53}