78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
package color
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
var color = New()
|
|
|
|
func TestMain(m *testing.M) {
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func TestColor(t *testing.T) {
|
|
tt := []struct {
|
|
Name string
|
|
SetAttrs []Attribute
|
|
AddAttrs []Attribute
|
|
Size int
|
|
}{
|
|
{"Init", nil, nil, 0},
|
|
{"Set 2", []Attribute{Bold, FgBlack}, nil, 2},
|
|
{"Add 2", nil, []Attribute{Italic, BgWhite}, 4},
|
|
{"Set 3", []Attribute{Bold, FgBlack, BgWhite}, nil, 3},
|
|
{"Add 3", nil, []Attribute{Italic, FgWhite, BgBlack}, 6},
|
|
{"Add Same", nil, []Attribute{Italic, FgWhite, BgBlack}, 6},
|
|
{"Add 2 Same", nil, []Attribute{Italic, FgWhite, BgGreen}, 7},
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
if tc.SetAttrs != nil {
|
|
color.Attrs = tc.SetAttrs
|
|
}
|
|
if tc.AddAttrs != nil {
|
|
color.Add(tc.AddAttrs...)
|
|
}
|
|
if len(color.Attrs) != tc.Size {
|
|
t.Logf("color has `%d` attributes, but should have `%d`", len(color.Attrs), tc.Size)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseLevel(t *testing.T) {
|
|
tt := []struct {
|
|
Parse string
|
|
Expected *Color
|
|
}{
|
|
{"T", Trace},
|
|
{"Trace", Trace},
|
|
{"D", Debug},
|
|
{"Debug", Debug},
|
|
{"I", Info},
|
|
{"Info", Info},
|
|
{"W", Warn},
|
|
{"Warn", Warn},
|
|
{"E", Error},
|
|
{"Error", Error},
|
|
{"F", Fatal},
|
|
{"Fatal", Fatal},
|
|
{"Unknown", Info},
|
|
{"N/A", Info},
|
|
{"1234", Info},
|
|
{"A Space", Info},
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
t.Run(tc.Parse, func(t *testing.T) {
|
|
level := ParseLevel(tc.Parse)
|
|
if level != tc.Expected {
|
|
t.Logf("Expected `%s`, got `%s`", tc.Expected, level)
|
|
t.Fail()
|
|
}
|
|
})
|
|
}
|
|
}
|