152 lines
4.8 KiB
Go
152 lines
4.8 KiB
Go
package tui
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"image/color"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"charm.land/lipgloss/v2"
|
||
|
|
"github.com/charmbracelet/colorprofile"
|
||
|
|
)
|
||
|
|
|
||
|
|
// TestSlateDarkColorsStayGreyWithoutTruecolor guards the slate theme's
|
||
|
|
// dark surfaces against a terminal without truecolor (no COLORTERM, e.g.
|
||
|
|
// Apple Terminal, ssh, tmux without Tc): bubbletea then downsamples every
|
||
|
|
// color to the 256-color palette, and a dark grey with a slight blue tint
|
||
|
|
// (#2A2D33 for Panel did) lands on index 17, #00005F -- painting the list
|
||
|
|
// panel and search bar dark blue. Each of these must map to a grey.
|
||
|
|
func TestSlateDarkColorsStayGreyWithoutTruecolor(t *testing.T) {
|
||
|
|
th := ThemeByName("slate")
|
||
|
|
for name, c := range map[string]color.Color{
|
||
|
|
"Background": th.Background,
|
||
|
|
"Panel": th.Panel,
|
||
|
|
"PanelAlt": th.PanelAlt,
|
||
|
|
"AccentText": th.AccentText,
|
||
|
|
"SelectedText": th.SelectedText,
|
||
|
|
} {
|
||
|
|
got := colorprofile.ANSI256.Convert(c)
|
||
|
|
r, g, b, _ := got.RGBA()
|
||
|
|
if r != g || g != b {
|
||
|
|
t.Errorf("slate %s converts to non-grey 256-color %v (#%02x%02x%02x)", name, got, r>>8, g>>8, b>>8)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestThemeNames(t *testing.T) {
|
||
|
|
for in, want := range map[string]string{"": "slate", "default": "slate", "bogus": "slate", "dialog": "dialog", "midnight": "midnight"} {
|
||
|
|
if got := NormalizeThemeName(in); got != want {
|
||
|
|
t.Errorf("NormalizeThemeName(%q) = %q, want %q", in, got, want)
|
||
|
|
}
|
||
|
|
if got := ThemeByName(in).Name; got != want {
|
||
|
|
t.Errorf("ThemeByName(%q).Name = %q, want %q", in, got, want)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
name := "slate"
|
||
|
|
for i, want := range []string{"dialog", "midnight", "slate"} {
|
||
|
|
name = NextThemeName(name)
|
||
|
|
if name != want {
|
||
|
|
t.Fatalf("cycle step %d = %q, want %q", i, name, want)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, n := range append([]string{""}, ThemeNames...) {
|
||
|
|
if !ValidTheme(n) {
|
||
|
|
t.Errorf("ValidTheme(%q) = false", n)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if ValidTheme("default") || ValidTheme("x") {
|
||
|
|
t.Error("ValidTheme accepts a name outside ThemeNames")
|
||
|
|
}
|
||
|
|
_ = ThemeByName("dialog").Styles() // must not panic
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestListState(t *testing.T) {
|
||
|
|
var l ListState
|
||
|
|
if l.Index() != -1 {
|
||
|
|
t.Fatal("empty list must have Index -1")
|
||
|
|
}
|
||
|
|
l.Rebuild(10, func(i int) bool { return i%2 == 0 }) // 0 2 4 6 8
|
||
|
|
l.Move(3)
|
||
|
|
if l.Index() != 6 {
|
||
|
|
t.Fatalf("Index = %d, want 6", l.Index())
|
||
|
|
}
|
||
|
|
l.Move(100)
|
||
|
|
if l.Cursor != 4 || l.Index() != 8 {
|
||
|
|
t.Fatalf("Move past end: cursor %d index %d", l.Cursor, l.Index())
|
||
|
|
}
|
||
|
|
l.ClampScroll(2)
|
||
|
|
if l.Scroll != 3 {
|
||
|
|
t.Fatalf("Scroll = %d, want 3", l.Scroll)
|
||
|
|
}
|
||
|
|
l.Rebuild(10, func(i int) bool { return i < 4 }) // 0 1 2 3: cursor clamps
|
||
|
|
if l.Cursor != 3 {
|
||
|
|
t.Fatalf("Rebuild must clamp cursor, got %d", l.Cursor)
|
||
|
|
}
|
||
|
|
l.Move(-100)
|
||
|
|
l.ClampScroll(2)
|
||
|
|
if l.Cursor != 0 || l.Scroll != 0 {
|
||
|
|
t.Fatalf("cursor %d scroll %d", l.Cursor, l.Scroll)
|
||
|
|
}
|
||
|
|
l.Rebuild(3, func(int) bool { return false })
|
||
|
|
if l.Cursor != 0 || l.Index() != -1 {
|
||
|
|
t.Fatal("empty filter")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestTextHelpers(t *testing.T) {
|
||
|
|
cases := []struct {
|
||
|
|
in string
|
||
|
|
w int
|
||
|
|
want string
|
||
|
|
}{{"hello", 10, "hello"}, {"hello world", 8, "hello..."}, {"hello", 2, "he"}, {"hello", 0, ""}, {"äöüäöü", 5, "äö..."}}
|
||
|
|
for _, c := range cases {
|
||
|
|
if got := Truncate(c.in, c.w); got != c.want {
|
||
|
|
t.Errorf("Truncate(%q,%d) = %q, want %q", c.in, c.w, got, c.want)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if got := TruncateBlock("abcdef\nxy", 5); got != "ab...\nxy" {
|
||
|
|
t.Errorf("TruncateBlock = %q", got)
|
||
|
|
}
|
||
|
|
if got := FirstNonEmpty("", "", "b", "c"); got != "b" {
|
||
|
|
t.Errorf("FirstNonEmpty = %q", got)
|
||
|
|
}
|
||
|
|
if got := WrapStyled("one two three", 7, lipgloss.NewStyle()); len(got) != 2 {
|
||
|
|
t.Errorf("WrapStyled = %q", got)
|
||
|
|
}
|
||
|
|
if got := WrapStyled(" ", 7, lipgloss.NewStyle()); len(got) != 1 {
|
||
|
|
t.Errorf("WrapStyled blank = %q", got)
|
||
|
|
}
|
||
|
|
kv := KeyValue(12, ThemeByName("slate").Styles())("name", "a-very-long-value")
|
||
|
|
if w := lipgloss.Width(kv); w > 12 {
|
||
|
|
t.Errorf("KeyValue width %d > 12: %q", w, kv)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPaintReappliesColorsAndPads(t *testing.T) {
|
||
|
|
th := ThemeByName("slate")
|
||
|
|
set, _, _ := strings.Cut(lipgloss.NewStyle().Foreground(th.Text).Background(th.Background).Render("x"), "x")
|
||
|
|
if set == "" {
|
||
|
|
t.Skip("lipgloss rendered no color sequence")
|
||
|
|
}
|
||
|
|
got := Paint("a\x1b[0mb\nc", th.Text, th.Background, 4)
|
||
|
|
lines := strings.Split(got, "\n")
|
||
|
|
if len(lines) != 2 {
|
||
|
|
t.Fatalf("lines = %q", lines)
|
||
|
|
}
|
||
|
|
if want := set + "a\x1b[m" + set + "b \x1b[m"; lines[0] != want {
|
||
|
|
t.Errorf("line 0 = %q, want %q", lines[0], want)
|
||
|
|
}
|
||
|
|
if lipgloss.Width(lines[1]) != 4 {
|
||
|
|
t.Errorf("line 1 not padded to 4: %q", lines[1])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestOuter(t *testing.T) {
|
||
|
|
s := Outer(lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).Padding(0, 1), 20, 5)
|
||
|
|
out := s.Render("x")
|
||
|
|
if w, h := lipgloss.Width(out), lipgloss.Height(out); w != 20 || h != 5 {
|
||
|
|
t.Errorf("Outer block %dx%d, want 20x5", w, h)
|
||
|
|
}
|
||
|
|
if h := lipgloss.Height(Outer(lipgloss.NewStyle(), 10, 0).Render("a\nb\nc")); h != 3 {
|
||
|
|
t.Errorf("height 0 must keep natural height, got %d", h)
|
||
|
|
}
|
||
|
|
}
|