Add tests. They are quite basic ...
This commit is contained in:
72
logger/logger_test.go
Normal file
72
logger/logger_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
// Copyright 2025, IPng Networks GmbH, Pim van Pelt <pim@ipng.ch>
|
||||
|
||||
package logger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"govpp-snmp-agentx/config"
|
||||
)
|
||||
|
||||
func TestPrintf(t *testing.T) {
|
||||
// Capture log output
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
|
||||
Printf("test message: %s", "hello")
|
||||
|
||||
output := buf.String()
|
||||
if !strings.Contains(output, "logger_test.go:logger.TestPrintf") {
|
||||
t.Errorf("Expected log output to contain caller info, got: %s", output)
|
||||
}
|
||||
if !strings.Contains(output, "test message: hello") {
|
||||
t.Errorf("Expected log output to contain message, got: %s", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebugfWithDebugEnabled(t *testing.T) {
|
||||
// Save original debug state
|
||||
originalDebug := config.Debug
|
||||
defer func() { config.Debug = originalDebug }()
|
||||
|
||||
// Enable debug
|
||||
config.Debug = true
|
||||
|
||||
// Capture log output
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
|
||||
Debugf("debug message: %s", "test")
|
||||
|
||||
output := buf.String()
|
||||
if !strings.Contains(output, "debug message: test") {
|
||||
t.Errorf("Expected debug message to be logged when debug is enabled, got: %s", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebugfWithDebugDisabled(t *testing.T) {
|
||||
// Save original debug state
|
||||
originalDebug := config.Debug
|
||||
defer func() { config.Debug = originalDebug }()
|
||||
|
||||
// Disable debug
|
||||
config.Debug = false
|
||||
|
||||
// Capture log output
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
|
||||
Debugf("debug message: %s", "test")
|
||||
|
||||
output := buf.String()
|
||||
if strings.Contains(output, "debug message: test") {
|
||||
t.Errorf("Expected debug message to NOT be logged when debug is disabled, got: %s", output)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user