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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
package color
import (
"strings"
"testing"
)
func TestWrapANSIDisabled(t *testing.T) {
s := "this is a very long line that would normally be wrapped if enabled"
if got := WrapANSI(s, 0); got != s {
t.Errorf("WrapANSI(_, 0) = %q, want unchanged", got)
}
}
func TestWrapANSIShortLineUntouched(t *testing.T) {
s := " indented menu item"
if got := WrapANSI(s, 80); got != s {
t.Errorf("short line was modified: got %q, want %q", got, s)
}
}
func TestWrapANSIWrapsLongLine(t *testing.T) {
s := "alpha bravo charlie delta echo foxtrot golf hotel india juliet"
got := WrapANSI(s, 20)
lines := strings.Split(got, "\r\n")
if len(lines) < 2 {
t.Fatalf("expected multiple lines, got %d: %q", len(lines), got)
}
for _, l := range lines {
if VisibleLen(l) > 20 {
t.Errorf("line exceeds width: %q (%d)", l, VisibleLen(l))
}
}
if strings.Join(strings.Fields(got), " ") != s {
t.Errorf("words not preserved: %q", got)
}
}
func TestWrapANSIIgnoresColorCodes(t *testing.T) {
colored := FgCode("xterm256", 182) + "hello world" + Reset
if VisibleLen(colored) > 80 {
t.Fatalf("test setup wrong, visible len = %d", VisibleLen(colored))
}
if got := WrapANSI(colored, 80); got != colored {
t.Errorf("colored line within width was wrapped: %q", got)
}
}
func TestWrapANSISplitsOnNewlines(t *testing.T) {
got := WrapANSI("first line\nsecond line", 80)
want := "first line\r\nsecond line"
if got != want {
t.Errorf("WrapANSI newline handling = %q, want %q", got, want)
}
}
func TestParseHexIndex(t *testing.T) {
cases := []struct {
in string
want int
}{
{"00", 0},
{"0F", 15},
{"0f", 15},
{"D0", 208},
{"FF", 255},
{"F", 15},
}
for _, c := range cases {
if got := Parse(c.in).Fg; got != c.want {
t.Errorf("Parse(%q).Fg = %d, want %d", c.in, got, c.want)
}
}
}
func TestParseHexRejectsInvalid(t *testing.T) {
for _, in := range []string{"ZZ", "100", "G1"} {
if got := Parse(in).Fg; got != -1 {
t.Errorf("Parse(%q).Fg = %d, want -1 (rejected)", in, got)
}
}
}
func TestParseHexBgAndGradient(t *testing.T) {
s := Parse("D0 bg:00 g:C4,52 bold")
if s.Fg != 208 {
t.Errorf("Fg = %d, want 208", s.Fg)
}
if s.Bg != 0 {
t.Errorf("Bg = %d, want 0", s.Bg)
}
if !s.Bold {
t.Error("Bold not set")
}
if len(s.Gradient) != 2 || s.Gradient[0] != 196 || s.Gradient[1] != 82 {
t.Errorf("Gradient = %v, want [196 82]", s.Gradient)
}
}
func TestAverage(t *testing.T) {
// black (0) and white (15) average to a mid grey.
mid := Average(Parse("00"), Parse("0F"))
if mid.Fg < 0 {
t.Fatalf("expected a foreground, got %d", mid.Fg)
}
r, g, b := Xterm256ToRGB(mid.Fg)
if r > 200 || r < 55 || g > 200 || g < 55 || b > 200 || b < 55 {
t.Errorf("midpoint of black/white not grey: rgb=%d,%d,%d (idx %d)", r, g, b, mid.Fg)
}
// one side colorless -> use the other.
if got := Average(NoColor(), Parse("D0")); got.Fg != 208 {
t.Errorf("Average(none, D0).Fg = %d, want 208", got.Fg)
}
// both colorless -> NoColor.
if got := Average(NoColor(), NoColor()); got.Fg != -1 {
t.Errorf("Average(none, none).Fg = %d, want -1", got.Fg)
}
}
|