4.8 KiB
4.8 KiB
package termext
import (
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
"strings"
"testing"
)
const (
resetSeq = ""
redSeq = ""
greenSeq = ""
yellowSeq = ""
blueSeq = ""
purpleSeq = ""
cyanSeq = ""
graySeq = ""
whiteSeq = ""
)
func TestRedEmpty(t *testing.T) {
tst.AssertEqual(t, Red(""), redSeq+resetSeq)
}
func TestGreenEmpty(t *testing.T) {
tst.AssertEqual(t, Green(""), greenSeq+resetSeq)
}
func TestYellowEmpty(t *testing.T) {
tst.AssertEqual(t, Yellow(""), yellowSeq+resetSeq)
}
func TestBlueEmpty(t *testing.T) {
tst.AssertEqual(t, Blue(""), blueSeq+resetSeq)
}
func TestPurpleEmpty(t *testing.T) {
tst.AssertEqual(t, Purple(""), purpleSeq+resetSeq)
}
func TestCyanEmpty(t *testing.T) {
tst.AssertEqual(t, Cyan(""), cyanSeq+resetSeq)
}
func TestGrayEmpty(t *testing.T) {
tst.AssertEqual(t, Gray(""), graySeq+resetSeq)
}
func TestWhiteEmpty(t *testing.T) {
tst.AssertEqual(t, White(""), whiteSeq+resetSeq)
}
func TestColorsContainOriginalString(t *testing.T) {
input := "hello world"
tst.AssertTrue(t, strings.Contains(Red(input), input))
tst.AssertTrue(t, strings.Contains(Green(input), input))
tst.AssertTrue(t, strings.Contains(Yellow(input), input))
tst.AssertTrue(t, strings.Contains(Blue(input), input))
tst.AssertTrue(t, strings.Contains(Purple(input), input))
tst.AssertTrue(t, strings.Contains(Cyan(input), input))
tst.AssertTrue(t, strings.Contains(Gray(input), input))
tst.AssertTrue(t, strings.Contains(White(input), input))
}
func TestColorsEndWithReset(t *testing.T) {
tst.AssertTrue(t, strings.HasSuffix(Red("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Green("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Yellow("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Blue("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Purple("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Cyan("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Gray("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(White("x"), resetSeq))
}
func TestColorsStartWithCorrectSequence(t *testing.T) {
tst.AssertTrue(t, strings.HasPrefix(Red("x"), redSeq))
tst.AssertTrue(t, strings.HasPrefix(Green("x"), greenSeq))
tst.AssertTrue(t, strings.HasPrefix(Yellow("x"), yellowSeq))
tst.AssertTrue(t, strings.HasPrefix(Blue("x"), blueSeq))
tst.AssertTrue(t, strings.HasPrefix(Purple("x"), purpleSeq))
tst.AssertTrue(t, strings.HasPrefix(Cyan("x"), cyanSeq))
tst.AssertTrue(t, strings.HasPrefix(Gray("x"), graySeq))
tst.AssertTrue(t, strings.HasPrefix(White("x"), whiteSeq))
}
func TestColorsAreDistinct(t *testing.T) {
input := "value"
results := []string{
Red(input),
Green(input),
Yellow(input),
Blue(input),
Purple(input),
Cyan(input),
Gray(input),
White(input),
}
for i := 0; i < len(results); i++ {
for j := i + 1; j < len(results); j++ {
tst.AssertNotEqual(t, results[i], results[j])
}
}
}
func TestCleanStringEmpty(t *testing.T) {
tst.AssertEqual(t, CleanString(""), "")
}
func TestCleanStringWithoutColors(t *testing.T) {
input := "plain text without any colors"
tst.AssertEqual(t, CleanString(input), input)
}
func TestCleanStringMultipleColors(t *testing.T) {
input := Red("foo") + " " + Green("bar") + " " + Blue("baz")
tst.AssertEqual(t, CleanString(input), "foo bar baz")
}
func TestCleanStringNested(t *testing.T) {
input := Red(Green("inner"))
tst.AssertEqual(t, CleanString(input), "inner")
}
func TestCleanStringIdempotent(t *testing.T) {
input := Yellow("hello") + Purple("world")
cleaned := CleanString(input)
tst.AssertEqual(t, CleanString(cleaned), cleaned)
}
func TestCleanStringEmptyColorWraps(t *testing.T) {
tst.AssertEqual(t, CleanString(Red("")), "")
tst.AssertEqual(t, CleanString(Green("")), "")
tst.AssertEqual(t, CleanString(Yellow("")), "")
tst.AssertEqual(t, CleanString(Blue("")), "")
tst.AssertEqual(t, CleanString(Purple("")), "")
tst.AssertEqual(t, CleanString(Cyan("")), "")
tst.AssertEqual(t, CleanString(Gray("")), "")
tst.AssertEqual(t, CleanString(White("")), "")
}
func TestCleanStringPreservesNonAnsiContent(t *testing.T) {
input := "before " + Red("middle") + " after\nnewline\ttab"
expected := "before middle after\nnewline\ttab"
tst.AssertEqual(t, CleanString(input), expected)
}
func TestCleanStringRemovesBareResetSequence(t *testing.T) {
input := "abc" + resetSeq + "def"
tst.AssertEqual(t, CleanString(input), "abcdef")
}
func TestCleanStringUnicode(t *testing.T) {
input := Red("héllo wörld 你好 🌍")
tst.AssertEqual(t, CleanString(input), "héllo wörld 你好 🌍")
}
func TestColorRoundTrip(t *testing.T) {
cases := []string{"", "x", "hello", "multi\nline", "with spaces", "héllo", "🌈"}
wrappers := []func(string) string{Red, Green, Yellow, Blue, Purple, Cyan, Gray, White}
for _, c := range cases {
for _, w := range wrappers {
tst.AssertEqual(t, CleanString(w(c)), c)
}
}
}
import (
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
"strings"
"testing"
)
const (
resetSeq = ""
redSeq = ""
greenSeq = ""
yellowSeq = ""
blueSeq = ""
purpleSeq = ""
cyanSeq = ""
graySeq = ""
whiteSeq = ""
)
func TestRedEmpty(t *testing.T) {
tst.AssertEqual(t, Red(""), redSeq+resetSeq)
}
func TestGreenEmpty(t *testing.T) {
tst.AssertEqual(t, Green(""), greenSeq+resetSeq)
}
func TestYellowEmpty(t *testing.T) {
tst.AssertEqual(t, Yellow(""), yellowSeq+resetSeq)
}
func TestBlueEmpty(t *testing.T) {
tst.AssertEqual(t, Blue(""), blueSeq+resetSeq)
}
func TestPurpleEmpty(t *testing.T) {
tst.AssertEqual(t, Purple(""), purpleSeq+resetSeq)
}
func TestCyanEmpty(t *testing.T) {
tst.AssertEqual(t, Cyan(""), cyanSeq+resetSeq)
}
func TestGrayEmpty(t *testing.T) {
tst.AssertEqual(t, Gray(""), graySeq+resetSeq)
}
func TestWhiteEmpty(t *testing.T) {
tst.AssertEqual(t, White(""), whiteSeq+resetSeq)
}
func TestColorsContainOriginalString(t *testing.T) {
input := "hello world"
tst.AssertTrue(t, strings.Contains(Red(input), input))
tst.AssertTrue(t, strings.Contains(Green(input), input))
tst.AssertTrue(t, strings.Contains(Yellow(input), input))
tst.AssertTrue(t, strings.Contains(Blue(input), input))
tst.AssertTrue(t, strings.Contains(Purple(input), input))
tst.AssertTrue(t, strings.Contains(Cyan(input), input))
tst.AssertTrue(t, strings.Contains(Gray(input), input))
tst.AssertTrue(t, strings.Contains(White(input), input))
}
func TestColorsEndWithReset(t *testing.T) {
tst.AssertTrue(t, strings.HasSuffix(Red("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Green("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Yellow("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Blue("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Purple("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Cyan("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(Gray("x"), resetSeq))
tst.AssertTrue(t, strings.HasSuffix(White("x"), resetSeq))
}
func TestColorsStartWithCorrectSequence(t *testing.T) {
tst.AssertTrue(t, strings.HasPrefix(Red("x"), redSeq))
tst.AssertTrue(t, strings.HasPrefix(Green("x"), greenSeq))
tst.AssertTrue(t, strings.HasPrefix(Yellow("x"), yellowSeq))
tst.AssertTrue(t, strings.HasPrefix(Blue("x"), blueSeq))
tst.AssertTrue(t, strings.HasPrefix(Purple("x"), purpleSeq))
tst.AssertTrue(t, strings.HasPrefix(Cyan("x"), cyanSeq))
tst.AssertTrue(t, strings.HasPrefix(Gray("x"), graySeq))
tst.AssertTrue(t, strings.HasPrefix(White("x"), whiteSeq))
}
func TestColorsAreDistinct(t *testing.T) {
input := "value"
results := []string{
Red(input),
Green(input),
Yellow(input),
Blue(input),
Purple(input),
Cyan(input),
Gray(input),
White(input),
}
for i := 0; i < len(results); i++ {
for j := i + 1; j < len(results); j++ {
tst.AssertNotEqual(t, results[i], results[j])
}
}
}
func TestCleanStringEmpty(t *testing.T) {
tst.AssertEqual(t, CleanString(""), "")
}
func TestCleanStringWithoutColors(t *testing.T) {
input := "plain text without any colors"
tst.AssertEqual(t, CleanString(input), input)
}
func TestCleanStringMultipleColors(t *testing.T) {
input := Red("foo") + " " + Green("bar") + " " + Blue("baz")
tst.AssertEqual(t, CleanString(input), "foo bar baz")
}
func TestCleanStringNested(t *testing.T) {
input := Red(Green("inner"))
tst.AssertEqual(t, CleanString(input), "inner")
}
func TestCleanStringIdempotent(t *testing.T) {
input := Yellow("hello") + Purple("world")
cleaned := CleanString(input)
tst.AssertEqual(t, CleanString(cleaned), cleaned)
}
func TestCleanStringEmptyColorWraps(t *testing.T) {
tst.AssertEqual(t, CleanString(Red("")), "")
tst.AssertEqual(t, CleanString(Green("")), "")
tst.AssertEqual(t, CleanString(Yellow("")), "")
tst.AssertEqual(t, CleanString(Blue("")), "")
tst.AssertEqual(t, CleanString(Purple("")), "")
tst.AssertEqual(t, CleanString(Cyan("")), "")
tst.AssertEqual(t, CleanString(Gray("")), "")
tst.AssertEqual(t, CleanString(White("")), "")
}
func TestCleanStringPreservesNonAnsiContent(t *testing.T) {
input := "before " + Red("middle") + " after\nnewline\ttab"
expected := "before middle after\nnewline\ttab"
tst.AssertEqual(t, CleanString(input), expected)
}
func TestCleanStringRemovesBareResetSequence(t *testing.T) {
input := "abc" + resetSeq + "def"
tst.AssertEqual(t, CleanString(input), "abcdef")
}
func TestCleanStringUnicode(t *testing.T) {
input := Red("héllo wörld 你好 🌍")
tst.AssertEqual(t, CleanString(input), "héllo wörld 你好 🌍")
}
func TestColorRoundTrip(t *testing.T) {
cases := []string{"", "x", "hello", "multi\nline", "with spaces", "héllo", "🌈"}
wrappers := []func(string) string{Red, Green, Yellow, Blue, Purple, Cyan, Gray, White}
for _, c := range cases {
for _, w := range wrappers {
tst.AssertEqual(t, CleanString(w(c)), c)
}
}
}