xonecas.io
62159
Sy

symb / files

symb / internal / tui / editor / editor_test.go

3.5 KB
1package editor
2
3import (
4	"strings"
5	"testing"
6
7	"charm.land/lipgloss/v2"
8)
9
10func TestLineWidthWithTabs(t *testing.T) {
11	ed := New()
12	ed.ShowLineNumbers = true
13	ed.ReadOnly = true
14	ed.Language = "go"
15	ed.SyntaxTheme = "github-dark"
16	ed.BgColor = lipgloss.Color("#000000")
17	ed.LineNumStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#1c1c1c"))
18	ed.CursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#00AA00"))
19
20	content := "\t\tcreds, err := config.LoadCredentials()\n\t\tfmt.Printf(\"Error loading credentials: %v\\n\", err)\n\t\t\tfactory = provider.NewOllamaFactory(name, providerCfg.Endpoint)"
21
22	ed.SetWidth(50)
23	ed.SetHeight(6)
24	ed.SetValue(content)
25	ed.Focus()
26
27	view := ed.View()
28	lines := strings.Split(view, "\n")
29	for i, line := range lines {
30		w := lipgloss.Width(line)
31		if w != 50 {
32			t.Errorf("line %d: width=%d (want 50)", i, w)
33		}
34	}
35}
36
37func TestSoftLineWrap(t *testing.T) {
38	ed := New()
39	ed.ShowLineNumbers = true
40	ed.ReadOnly = true
41	ed.BgColor = lipgloss.Color("#000000")
42	ed.LineNumStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#666666"))
43
44	// Width 30, gutter ~3 chars ("_1 "), so text width = 27.
45	// Line "abcdefghij..." (40 chars) should wrap into 2 visual rows.
46	ed.SetWidth(30)
47	ed.SetHeight(10)
48	ed.SetValue("short\n" + strings.Repeat("a", 40) + "\nend")
49
50	view := ed.View()
51	lines := strings.Split(view, "\n")
52
53	// Should have: line 1 (short), line 2 wrap-1, line 2 wrap-2, line 3 (end), + 6 empty = 10
54	if len(lines) != 10 {
55		t.Errorf("expected 10 visual rows, got %d", len(lines))
56	}
57	// Every visual row must be exactly 30 wide.
58	for i, line := range lines {
59		w := lipgloss.Width(line)
60		if w != 30 {
61			t.Errorf("visual row %d: width=%d (want 30)", i, w)
62		}
63	}
64
65	// The long line should produce a continuation row with blank gutter.
66	// First visual row of long line should have "2" in gutter, second should not.
67	if len(lines) >= 3 {
68		// Row index 1 = first segment of buffer line 2 → should have "2" in gutter
69		if !strings.Contains(lines[1], "2") {
70			t.Errorf("visual row 1 should show line number 2, got: %q", lines[1])
71		}
72	}
73}
74
75func TestWrapPlain(t *testing.T) {
76	cases := []struct {
77		in    string
78		width int
79		want  int // number of segments
80	}{
81		{"hello", 10, 1},
82		{"hello world!!!", 5, 3}, // "hello", " worl", "d!!!"
83		{"", 5, 1},
84		{strings.Repeat("x", 20), 7, 3}, // 7+7+6
85	}
86	for _, tc := range cases {
87		got := wrapPlain(tc.in, tc.width)
88		if len(got) != tc.want {
89			t.Errorf("wrapPlain(%q, %d) = %d segments, want %d: %v",
90				tc.in, tc.width, len(got), tc.want, got)
91		}
92	}
93}
94
95func TestVisualRowCount(t *testing.T) {
96	ed := New()
97	ed.SetWidth(10) // no line numbers, so textWidth = 10
98	ed.SetHeight(20)
99	ed.SetValue("short\n" + strings.Repeat("a", 25) + "\nend")
100
101	// Line 0: "short" (5 chars) → 1 visual row
102	// Line 1: 25 chars → ceil(25/10) = 3 visual rows
103	// Line 2: "end" (3 chars) → 1 visual row
104	// Total: 5
105	got := ed.visualRowCount()
106	if got != 5 {
107		t.Errorf("visualRowCount() = %d, want 5", got)
108	}
109}
110
111func TestExpandTabs(t *testing.T) {
112	cases := []struct {
113		in   string
114		want int // visual width (all chars are ASCII, so rune count = display width)
115	}{
116		{"\thello", 4 + 5},       // 1 tab (4 spaces) + "hello"
117		{"\t\thello", 4 + 4 + 5}, // 2 tabs + "hello"
118		{"ab\tc", 2 + 2 + 1},     // "ab" then tab to col 4, then "c"
119		{"no tabs", 7},
120	}
121	for _, tc := range cases {
122		got := expandTabs(tc.in)
123		w := len([]rune(got))
124		if w != tc.want {
125			t.Errorf("expandTabs(%q) width=%d, want %d (got %q)", tc.in, w, tc.want, got)
126		}
127	}
128}