1package tui
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestRenderDiffRowWrapsWithoutLosingContent(t *testing.T) {
9 const width = 30
10 code := `const message = "alpha beta gamma delta epsilon"`
11 v := View{Theme: Dark}
12
13 rows := v.renderDiffRow("+"+code, width, v.Theme.Tool, 7, '+', "sample.go")
14 if len(rows) < 2 {
15 t.Fatalf("long diff line did not wrap: %q", rows)
16 }
17
18 var rebuilt strings.Builder
19 for i, row := range rows {
20 plain := stripANSI(row)
21 if got := visibleWidth(row); got > width {
22 t.Errorf("row %d width = %d, want <= %d: %q", i, got, width, plain)
23 }
24 prefix := " "
25 if i == 0 {
26 prefix = " + 7 "
27 }
28 if !strings.HasPrefix(plain, prefix) {
29 t.Fatalf("row %d prefix = %q, want %q", i, plain, prefix)
30 }
31 rebuilt.WriteString(strings.TrimPrefix(plain, prefix))
32 }
33 if got := rebuilt.String(); got != code {
34 t.Fatalf("wrapped content changed:\n got: %q\nwant: %q", got, code)
35 }
36 if strings.Contains(stripANSI(strings.Join(rows, "\n")), "...") {
37 t.Fatalf("wrapped diff still contains truncation ellipsis: %q", rows)
38 }
39}
40
41func TestRenderDiffRowWrapsUnicodeByDisplayWidth(t *testing.T) {
42 const width = 18
43 code := "名前 = 🙂🙂🙂 café résumé"
44 v := View{Theme: Dark}
45
46 rows := v.renderDiffRow("-"+code, width, v.Theme.Error, 12, '-', "")
47 if len(rows) < 2 {
48 t.Fatalf("long Unicode diff line did not wrap: %q", rows)
49 }
50
51 var rebuilt strings.Builder
52 for i, row := range rows {
53 plain := stripANSI(row)
54 if got := visibleWidth(row); got > width {
55 t.Errorf("row %d width = %d, want <= %d: %q", i, got, width, plain)
56 }
57 prefix := " "
58 if i == 0 {
59 prefix = " - 12 "
60 }
61 if !strings.HasPrefix(plain, prefix) {
62 t.Fatalf("row %d prefix = %q, want %q", i, plain, prefix)
63 }
64 rebuilt.WriteString(strings.TrimPrefix(plain, prefix))
65 }
66 if got := rebuilt.String(); got != code {
67 t.Fatalf("wrapped Unicode content changed:\n got: %q\nwant: %q", got, code)
68 }
69}