1
1
mirror of https://github.com/wader/fq.git synced 2024-12-25 22:34:14 +03:00
fq/internal/ansi/ansi_test.go

54 lines
1.1 KiB
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package ansi_test
import (
"bytes"
"fmt"
"testing"
"github.com/wader/fq/internal/ansi"
2020-06-08 03:29:51 +03:00
)
func TestF(t *testing.T) {
2020-06-08 03:29:51 +03:00
c := ansi.FromString("blue")
b := &bytes.Buffer{}
fmt.Fprintf(b, "%s", c.F("test"))
actual := b.String()
expected := "\x1b[34mtest\x1b[39m"
if expected != actual {
t.Errorf("expected %v, got %v", expected, actual)
}
2020-06-08 03:29:51 +03:00
}
func TestW(t *testing.T) {
2020-06-08 03:29:51 +03:00
c := ansi.FromString("blue+underline")
b := &bytes.Buffer{}
_, _ = c.W(b).Write([]byte("test"))
actual := b.String()
expected := "\x1b[34;4mtest\x1b[39;24m"
if expected != actual {
t.Errorf("expected %v, got %v", expected, actual)
}
2020-06-08 03:29:51 +03:00
}
func TestLen(t *testing.T) {
testCases := []struct {
s string
l int
}{
{"", 0},
{"abc", 3},
{ansi.Red.SetString + "a" + "bc" + ansi.Red.ResetString + "d", 4},
{"a" + ansi.Red.SetString + "bc" + ansi.Red.ResetString + "d", 4},
{"a" + ansi.Red.SetString + "bcd" + ansi.Red.ResetString, 4},
{"a│b", 3},
}
for _, tC := range testCases {
t.Run(tC.s, func(t *testing.T) {
actualL := ansi.Len(tC.s)
if tC.l != actualL {
t.Errorf("expected %d, got %d", tC.l, actualL)
}
})
}
}