1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package tui
import "testing"
func TestRichText_AccessorRoundTrip(t *testing.T) {
spans := []TextSpan{
{Text: "hello "},
{Text: "world", Style: NewStyle().Bold()},
}
e := New(WithRichText(spans...))
got := e.RichText()
if len(got) != 2 || got[0].Text != "hello " || got[1].Text != "world" {
t.Fatalf("RichText() = %+v, want 2 spans hello/world", got)
}
if got[1].Style.Attrs&AttrBold == 0 {
t.Errorf("second span should be bold, got attrs %v", got[1].Style.Attrs)
}
}
func TestRichText_SettingPlainTextClearsRichText(t *testing.T) {
e := New(WithRichText(TextSpan{Text: "rich"}))
e.SetText("plain")
if len(e.RichText()) != 0 {
t.Errorf("SetText should clear richText, got %+v", e.RichText())
}
if e.Text() != "plain" {
t.Errorf("Text() = %q, want \"plain\"", e.Text())
}
}
func TestRichText_SettingRichTextClearsPlainText(t *testing.T) {
e := New(WithText("plain"))
e.SetRichText(TextSpan{Text: "rich"})
if e.Text() != "" {
t.Errorf("SetRichText should clear text, got %q", e.Text())
}
if len(e.RichText()) != 1 {
t.Errorf("RichText() len = %d, want 1", len(e.RichText()))
}
}
func TestMergeSpanStyle(t *testing.T) {
base := NewStyle().Foreground(White).Background(Blue)
got := mergeSpanStyle(base, NewStyle().Bold())
if got.Attrs&AttrBold == 0 {
t.Errorf("bold not merged in: %v", got.Attrs)
}
if got.Fg != White || got.Bg != Blue {
t.Errorf("base colors should survive: fg=%v bg=%v", got.Fg, got.Bg)
}
got = mergeSpanStyle(base, NewStyle().Foreground(Red))
if got.Fg != Red {
t.Errorf("span fg should override: got %v", got.Fg)
}
if got.Bg != Blue {
t.Errorf("base bg should survive: got %v", got.Bg)
}
}
func TestRichTextWidth(t *testing.T) {
spans := []TextSpan{{Text: "ab"}, {Text: "cde", Style: NewStyle().Bold()}}
if got := richTextWidth(spans); got != 5 {
t.Errorf("richTextWidth = %d, want 5", got)
}
}
func TestSpanLineWidth(t *testing.T) {
line := []TextSpan{{Text: "hi "}, {Text: "yo"}}
if got := spanLineWidth(line); got != 5 {
t.Errorf("spanLineWidth = %d, want 5", got)
}
}
|