Skip to content

Commit

Permalink
chore: update tests and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 8, 2024
1 parent b89f1a3 commit 295631e
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 272 deletions.
12 changes: 6 additions & 6 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ func Color[T string | int](c T) color.Color {
return h
} else if i, err := strconv.Atoi(c); err == nil {
if i < 16 {

Check failure on line 67 in color.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 16, in <condition> detected (gomnd)
return ansi.BasicColor(i)
return ansi.BasicColor(i) //nolint:gosec
} else if i < 256 {

Check failure on line 69 in color.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 256, in <condition> detected (gomnd)
return ansi.ExtendedColor(i)
return ansi.ExtendedColor(i) //nolint:gosec
}
return ansi.TrueColor(i)
return ansi.TrueColor(i) //nolint:gosec
}
case int:
if c < 16 {

Check failure on line 75 in color.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 16, in <condition> detected (gomnd)
return ansi.BasicColor(c)
return ansi.BasicColor(c) //nolint:gosec
} else if c < 256 {

Check failure on line 77 in color.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 256, in <condition> detected (gomnd)
return ansi.ExtendedColor(c)
return ansi.ExtendedColor(c) //nolint:gosec
}
return ansi.TrueColor(c)
return ansi.TrueColor(c) //nolint:gosec
}
return col
}
Expand Down
179 changes: 7 additions & 172 deletions color_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lipgloss

import (
"image/color"
"testing"
)

Expand Down Expand Up @@ -31,8 +30,8 @@ func TestHexToColor(t *testing.T) {
}

for i, tc := range tt {
h := hexToColor(tc.input)
o := uint(h.R)<<16 + uint(h.G)<<8 + uint(h.B)
r, g, b, _ := Color(tc.input).RGBA()
o := uint(r>>8)<<16 + uint(g>>8)<<8 + uint(b>>8)
if o != tc.expected {
t.Errorf("expected %X, got %X (test #%d)", tc.expected, o, i+1)
}
Expand All @@ -41,194 +40,30 @@ func TestHexToColor(t *testing.T) {

func TestRGBA(t *testing.T) {
tt := []struct {
profile Profile
darkBg bool
input TerminalColor
input string
expected uint
}{
// lipgloss.Color
{
TrueColor,
true,
Color("#FF0000"),
0xFF0000,
},
{
TrueColor,
true,
Color("9"),
0xFF0000,
},
{
TrueColor,
true,
Color("21"),
0x0000FF,
},
// lipgloss.AdaptiveColor
{
TrueColor,
true,
AdaptiveColor{Light: "#0000FF", Dark: "#FF0000"},
0xFF0000,
},
{
TrueColor,
false,
AdaptiveColor{Light: "#0000FF", Dark: "#FF0000"},
0x0000FF,
},
{
TrueColor,
true,
AdaptiveColor{Light: "21", Dark: "9"},
"#FF0000",
0xFF0000,
},
{
TrueColor,
false,
AdaptiveColor{Light: "21", Dark: "9"},
0x0000FF,
},
// lipgloss.CompleteColor
{
TrueColor,
true,
CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "12"},
"9",
0xFF0000,
},
{
ANSI256,
true,
CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "12"},
0xFFFFFF,
},
{
ANSI,
true,
CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "12"},
"21",
0x0000FF,
},
{
TrueColor,
true,
CompleteColor{TrueColor: "", ANSI256: "231", ANSI: "12"},
0x000000,
},
// lipgloss.CompleteAdaptiveColor
// dark
{
TrueColor,
true,
CompleteAdaptiveColor{
Light: CompleteColor{TrueColor: "#0000FF", ANSI256: "231", ANSI: "12"},
Dark: CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "12"},
},
0xFF0000,
},
{
ANSI256,
true,
CompleteAdaptiveColor{
Light: CompleteColor{TrueColor: "#FF0000", ANSI256: "21", ANSI: "12"},
Dark: CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "12"},
},
0xFFFFFF,
},
{
ANSI,
true,
CompleteAdaptiveColor{
Light: CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "9"},
Dark: CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "12"},
},
0x0000FF,
},
// light
{
TrueColor,
false,
CompleteAdaptiveColor{
Light: CompleteColor{TrueColor: "#0000FF", ANSI256: "231", ANSI: "12"},
Dark: CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "12"},
},
0x0000FF,
},
{
ANSI256,
false,
CompleteAdaptiveColor{
Light: CompleteColor{TrueColor: "#FF0000", ANSI256: "21", ANSI: "12"},
Dark: CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "12"},
},
0x0000FF,
},
{
ANSI,
false,
CompleteAdaptiveColor{
Light: CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "9"},
Dark: CompleteColor{TrueColor: "#FF0000", ANSI256: "231", ANSI: "12"},
},
0xFF0000,
},
}

r := DefaultRenderer()
for i, tc := range tt {
r.SetColorProfile(tc.profile)
r.SetHasDarkBackground(tc.darkBg)

r, g, b, _ := tc.input.color(r).RGBA()
r, g, b, _ := Color(tc.input).RGBA()
o := uint(r/256)<<16 + uint(g/256)<<8 + uint(b/256)

if o != tc.expected {
t.Errorf("expected %X, got %X (test #%d)", tc.expected, o, i+1)
}
}
}

// hexToColor translates a hex color string (#RRGGBB or #RGB) into a color.RGB,
// which satisfies the color.Color interface. If an invalid string is passed
// black with 100% opacity will be returned: or, in hex format, 0x000000FF.
func hexToColor(hex string) (c color.RGBA) {
c.A = 0xFF

if hex == "" || hex[0] != '#' {
return c
}

const (
fullFormat = 7 // #RRGGBB
shortFormat = 4 // #RGB
)

switch len(hex) {
case fullFormat:
const offset = 4
c.R = hexToByte(hex[1])<<offset + hexToByte(hex[2])
c.G = hexToByte(hex[3])<<offset + hexToByte(hex[4])
c.B = hexToByte(hex[5])<<offset + hexToByte(hex[6])
case shortFormat:
const offset = 0x11
c.R = hexToByte(hex[1]) * offset
c.G = hexToByte(hex[2]) * offset
c.B = hexToByte(hex[3]) * offset
}

return c
}

func hexToByte(b byte) byte {
const offset = 10
switch {
case b >= '0' && b <= '9':
return b - '0'
case b >= 'a' && b <= 'f':
return b - 'a' + offset
case b >= 'A' && b <= 'F':
return b - 'A' + offset
}
// Invalid, but just return 0.
return 0
}
3 changes: 2 additions & 1 deletion list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/list"
"github.com/charmbracelet/lipgloss/tree"
"github.com/charmbracelet/x/ansi"
)

// XXX: can't write multi-line examples if the underlying string uses
Expand Down Expand Up @@ -194,7 +195,7 @@ func TestComplexSublist(t *testing.T) {
C. bar
• Baz
`
assertEqual(t, expected, l.String())
assertEqual(t, expected, ansi.Strip(l.String()))
}

func TestMultiline(t *testing.T) {
Expand Down
11 changes: 3 additions & 8 deletions runes_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lipgloss

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -55,14 +54,10 @@ func TestStyleRunes(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
res := fn(tc.input, tc.indices)
if res != tc.expected {
t.Errorf("Expected:\n\n`%s`\n`%s`\n\nActual Output:\n\n`%s`\n`%s`\n\n",
tc.expected, formatEscapes(tc.expected),
res, formatEscapes(res))
t.Errorf("Expected:\n\n`%q`\n`%q`\n\nActual Output:\n\n`%q`\n`%q`\n\n",
tc.expected, tc.expected,
res, res)
}
})
}
}

func formatEscapes(str string) string {
return strings.ReplaceAll(str, "\x1b", "\\x1b")
}
Loading

0 comments on commit 295631e

Please sign in to comment.