106 lines
2.3 KiB
Go
106 lines
2.3 KiB
Go
package color_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
color "git.sharkk.net/Go/Color"
|
|
)
|
|
|
|
func TestColorFunctions(t *testing.T) {
|
|
// Enable colors for testing
|
|
color.SetColors(true)
|
|
|
|
tests := []struct {
|
|
name string
|
|
colorFunc func(string) string
|
|
expected string
|
|
}{
|
|
{"Red", color.Red, "\033[31mtest\033[0m"},
|
|
{"Green", color.Green, "\033[32mtest\033[0m"},
|
|
{"Yellow", color.Yellow, "\033[33mtest\033[0m"},
|
|
{"Blue", color.Blue, "\033[34mtest\033[0m"},
|
|
{"Purple", color.Purple, "\033[35mtest\033[0m"},
|
|
{"Cyan", color.Cyan, "\033[36mtest\033[0m"},
|
|
{"White", color.White, "\033[37mtest\033[0m"},
|
|
{"Gray", color.Gray, "\033[90mtest\033[0m"},
|
|
{"Reset", color.Reset, "\033[0mtest\033[0m"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := tt.colorFunc("test")
|
|
if result != tt.expected {
|
|
t.Errorf("got %q, want %q", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestColorsDisabled(t *testing.T) {
|
|
// Disable colors
|
|
color.SetColors(false)
|
|
|
|
input := "test"
|
|
expected := "test"
|
|
|
|
colorFuncs := []func(string) string{
|
|
color.Red, color.Green, color.Yellow, color.Blue, color.Purple, color.Cyan, color.White, color.Gray, color.Reset,
|
|
}
|
|
|
|
for _, colorFunc := range colorFuncs {
|
|
result := colorFunc(input)
|
|
if result != expected {
|
|
t.Errorf("expected plain text when colors disabled, got %q", result)
|
|
}
|
|
}
|
|
|
|
// Re-enable for other tests
|
|
color.SetColors(true)
|
|
}
|
|
|
|
func TestSetColorsAndColorsEnabled(t *testing.T) {
|
|
// Test enabling colors
|
|
color.SetColors(true)
|
|
if !color.ColorsEnabled() {
|
|
t.Error("expected colors to be enabled")
|
|
}
|
|
|
|
// Test disabling colors
|
|
color.SetColors(false)
|
|
if color.ColorsEnabled() {
|
|
t.Error("expected colors to be disabled")
|
|
}
|
|
|
|
// Restore default state
|
|
color.SetColors(color.DetectShellColors())
|
|
}
|
|
|
|
func TestEmptyString(t *testing.T) {
|
|
color.SetColors(true)
|
|
result := color.Red("")
|
|
expected := "\033[31m\033[0m"
|
|
if result != expected {
|
|
t.Errorf("got %q, want %q", result, expected)
|
|
}
|
|
}
|
|
|
|
func BenchmarkColorFunction(b *testing.B) {
|
|
color.SetColors(true)
|
|
input := "benchmark test"
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = color.Red(input)
|
|
}
|
|
}
|
|
|
|
func BenchmarkColorFunctionDisabled(b *testing.B) {
|
|
color.SetColors(false)
|
|
input := "benchmark test"
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = color.Red(input)
|
|
}
|
|
}
|