This commit is contained in:
u80864958
2024-11-15 12:17:38 +01:00
parent 058ab2bad2
commit 1de9439225
9 changed files with 272 additions and 0 deletions

44
lvl/lvl.go Normal file
View File

@ -0,0 +1,44 @@
package lvl
type Level int
const (
Debug Level = iota
Info
Warn
Error
)
// String makes the Level to a string
func (l Level) String() string {
switch l {
case Debug:
return "Debug"
case Info:
return "Info"
case Warn:
return "Warn"
case Error:
return "Error"
}
return "Undefined"
}
// Colorize colors a string for the shell matching to its level
func (l Level) Colorize(str string) string {
var color string
switch l {
case Debug:
color = "\033[32m" // green
case Info:
color = "\033[97m" // white
case Warn:
color = "\033[33m" // yellow
case Error:
color = "\033[31m" // red
}
return color + str + "\033[0m"
}

60
lvl/lvl_test.go Normal file
View File

@ -0,0 +1,60 @@
package lvl
import (
"testing"
)
func TestString(t *testing.T) {
cases := []struct {
l Level
e string
}{
{
l: Debug,
e: "Debug",
},
{
l: Info,
e: "Info",
},
{
l: Warn,
e: "Warn",
},
{
l: Error,
e: "Error",
},
}
for _, c := range cases {
t.Run(c.e, func(t *testing.T) {
s := c.l.String()
if s != c.e {
t.Errorf("Expected %s but got %s", c.e, s)
}
})
}
}
func TestColor(t *testing.T) {
testCases := []struct {
level Level
message string
expected string
}{
{Debug, "Debug message", "\033[32mDebug message\033[0m"},
{Info, "Info message", "\033[97mInfo message\033[0m"},
{Warn, "Warning message", "\033[33mWarning message\033[0m"},
{Error, "Error message", "\033[31mError message\033[0m"},
}
for _, tc := range testCases {
coloredMessage := tc.level.Colorize(tc.message)
if coloredMessage != tc.expected {
t.Errorf("For level %v, expected '%v', but got '%v'", tc.level, tc.expected, coloredMessage)
}
}
}