//go:build test package logutil import ( "testing" "time" "github.com/stretchr/testify/assert" ) // NO_COLOR is unset in tests so all ANSI vars are non-empty. The // noColor=true path is not testable here because init() runs once at // package load time; it would require a separate test binary. func TestColorVars_AreNotEmpty(t *testing.T) { t.Parallel() tests := []struct { name string v string }{ {"Reset", Reset}, {"Bold", Bold}, {"Dim", Dim}, {"Cyan", Cyan}, {"Green", Green}, {"Red", Red}, {"Yellow", Yellow}, {"Magenta", Magenta}, {"BoldGreen", BoldGreen}, {"BoldYellow", BoldYellow}, {"BoldRed", BoldRed}, {"BoldBlue", BoldBlue}, {"BoldMagenta", BoldMagenta}, {"DebugLvl", DebugLvl}, {"WarnLvl", WarnLvl}, {"ErrorLvl", ErrorLvl}, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() assert.NotEmpty(t, tt.v, "package var %s should contain ANSI code when NO_COLOR is unset", tt.name) }) } } func TestColorVars_StartWithEscape(t *testing.T) { t.Parallel() tests := []struct { name string v string }{ {"Reset", Reset}, {"Bold", Bold}, {"Dim", Dim}, {"Cyan", Cyan}, {"Green", Green}, {"Red", Red}, {"Yellow", Yellow}, {"Magenta", Magenta}, {"BoldGreen", BoldGreen}, {"BoldYellow", BoldYellow}, {"BoldRed", BoldRed}, {"BoldBlue", BoldBlue}, {"BoldMagenta", BoldMagenta}, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() assert.True(t, tt.v[0] == '\033', "package var %s should start with ESC byte", tt.name) }) } } func TestColoredDuration(t *testing.T) { t.Parallel() tests := []struct { name string d time.Duration wantPre string wantText string wantSuf string }{ { name: "under_500ms_returns_green", d: 200 * time.Millisecond, wantPre: Green, wantText: "200ms", wantSuf: Reset, }, { name: "exactly_499ms_returns_green", d: 499 * time.Millisecond, wantPre: Green, wantText: "499ms", wantSuf: Reset, }, { name: "exactly_500ms_returns_yellow", d: 500 * time.Millisecond, wantPre: Yellow, wantText: "500ms", wantSuf: Reset, }, { name: "between_500ms_and_5s_returns_yellow", d: 3 * time.Second, wantPre: Yellow, wantText: "3s", wantSuf: Reset, }, { name: "exactly_4999ms_returns_yellow", d: 4999 * time.Millisecond, wantPre: Yellow, wantText: "4.999s", wantSuf: Reset, }, { name: "exactly_5s_returns_red", d: 5 * time.Second, wantPre: Red, wantText: "5s", wantSuf: Reset, }, { name: "over_5s_returns_red", d: 10 * time.Second, wantPre: Red, wantText: "10s", wantSuf: Reset, }, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() got := ColoredDuration(tt.d) want := tt.wantPre + tt.wantText + tt.wantSuf assert.Equal(t, want, got) }) } } func TestColoredRows(t *testing.T) { t.Parallel() tests := []struct { name string n int wantPre string wantText string wantSuf string }{ { name: "zero_rows_plural", n: 0, wantPre: BoldBlue, wantText: "0 rows", wantSuf: Reset, }, { name: "one_row_singular", n: 1, wantPre: BoldBlue, wantText: "1 row", wantSuf: Reset, }, { name: "two_rows_plural", n: 2, wantPre: BoldBlue, wantText: "2 rows", wantSuf: Reset, }, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() got := ColoredRows(tt.n) want := tt.wantPre + tt.wantText + tt.wantSuf assert.Equal(t, want, got) }) } }