2020-06-08 03:29:51 +03:00
|
|
|
package ansi_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/internal/ansi"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-10-21 01:46:21 +03:00
|
|
|
func TestF(t *testing.T) {
|
2020-06-08 03:29:51 +03:00
|
|
|
c := ansi.FromString("blue")
|
|
|
|
b := &bytes.Buffer{}
|
2021-10-21 01:46:21 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-21 01:46:21 +03:00
|
|
|
func TestW(t *testing.T) {
|
2020-06-08 03:29:51 +03:00
|
|
|
c := ansi.FromString("blue+underline")
|
|
|
|
b := &bytes.Buffer{}
|
2021-10-21 01:46:21 +03:00
|
|
|
_, _ = 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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|