stonelog/log_test.go
Rene Nochebuena 00e6488185
Some checks failed
Go CI/CD / go-ci (push) Has been cancelled
Validate log level and improve test coverage
Added validation to ensure log levels are within valid bounds in `SetLogLevel`. Simplified prefix mapping logic by removing the fallback to INFO level. Expanded test coverage with new test cases for plain text and JSON log outputs.
2025-04-10 09:50:54 -06:00

70 lines
1.4 KiB
Go

package stonelog
import (
"testing"
)
func Test_LogLevelString(t *testing.T) {
levelTests := []struct {
level StoneLevel
expected string
}{
{TRACE, "TRACE"},
{DEBUG, "DEBUG"},
{INFO, "INFO"},
{WARN, "WARN"},
{ERROR, "ERROR"},
{FATAL, "FATAL"},
{PANIC, "PANIC"},
}
for _, tt := range levelTests {
if tt.level.String() != tt.expected {
t.Errorf("Expected %s, got %s", tt.expected, tt.level.String())
}
}
}
func Test_LogLevelColor(t *testing.T) {
levelTests := []struct {
level StoneLevel
expected string
}{
{TRACE, colorWhite},
{DEBUG, colorCyan},
{INFO, colorGreen},
{WARN, colorYellow},
{ERROR, colorRed},
{FATAL, colorMagenta},
{PANIC, colorRed + "\033[1m"},
}
for _, tt := range levelTests {
if tt.level.Color() != tt.expected {
t.Errorf("Expected %s, got %s", tt.expected, tt.level.Color())
}
}
}
func Test_PlainTextLogs(t *testing.T) {
InitStoneLog(TRACE, false, false)
Trace("This is a trace log")
Debug("This is a debug log")
Observation("This is an observation log")
Hypothesis("This is a hypothesis log")
Failure("This is a failure log")
SetLogLevel(INFO)
}
func Test_JSONLogs(t *testing.T) {
InitStoneLog(TRACE, true, false)
Trace("This is a trace log")
Debug("This is a debug log")
Observation("This is an observation log")
Hypothesis("This is a hypothesis log")
Failure("This is a failure log")
SetLogLevel(INFO)
}