refactor: internal package
This commit is contained in:
52
internal/logx/logx.go
Normal file
52
internal/logx/logx.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Package logx provides helper functions for logging.
|
||||
package logx
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var logger = log.New(os.Stderr, "", log.LstdFlags)
|
||||
var Verbose = false
|
||||
|
||||
// SetOutput sets the output destination.
|
||||
func SetOutput(w io.Writer) {
|
||||
logger.SetOutput(w)
|
||||
}
|
||||
|
||||
// Printf prints a formatted message.
|
||||
func Printf(format string, v ...any) {
|
||||
logger.Printf(format, v...)
|
||||
}
|
||||
|
||||
// Println prints a message.
|
||||
func Println(v ...any) {
|
||||
logger.Println(v...)
|
||||
}
|
||||
|
||||
// Log prints a message.
|
||||
func Log(message string, args ...any) {
|
||||
if len(args) == 0 {
|
||||
logger.Println(message)
|
||||
} else {
|
||||
logger.Printf(message+"\n", args...)
|
||||
}
|
||||
}
|
||||
|
||||
// Debug prints a message if the verbose mode is on.
|
||||
func Debug(message string, args ...any) {
|
||||
if !Verbose {
|
||||
return
|
||||
}
|
||||
Log(message, args...)
|
||||
}
|
||||
|
||||
// Mock creates a new Memory and installs it as the logger output
|
||||
// instead of the default one. Should be used for testing purposes only.
|
||||
func Mock(path ...string) *Memory {
|
||||
memory := NewMemory("log")
|
||||
SetOutput(memory)
|
||||
Verbose = true
|
||||
return memory
|
||||
}
|
||||
70
internal/logx/logx_test.go
Normal file
70
internal/logx/logx_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package logx
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSetOutput(t *testing.T) {
|
||||
mem := NewMemory("log")
|
||||
SetOutput(mem)
|
||||
Log("hello")
|
||||
if !mem.Has("hello") {
|
||||
t.Error("SetOutput: memory not set as output")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLog(t *testing.T) {
|
||||
mem := NewMemory("log")
|
||||
SetOutput(mem)
|
||||
{
|
||||
Log("value: %d", 42)
|
||||
if len(mem.Lines) != 1 {
|
||||
t.Errorf("Log: expected line count %v", len(mem.Lines))
|
||||
}
|
||||
if !mem.Has("value: 42") {
|
||||
t.Errorf("Log: expected output: %v", mem.Lines)
|
||||
}
|
||||
}
|
||||
{
|
||||
Log("value: %d", 84)
|
||||
if len(mem.Lines) != 2 {
|
||||
t.Errorf("Log: expected line count %v", len(mem.Lines))
|
||||
}
|
||||
if !mem.Has("value: 42") || !mem.Has("value: 84") {
|
||||
t.Errorf("Log: expected output: %v", mem.Lines)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebug(t *testing.T) {
|
||||
t.Run("enabled", func(t *testing.T) {
|
||||
mem := NewMemory("log")
|
||||
SetOutput(mem)
|
||||
Verbose = true
|
||||
{
|
||||
Debug("value: %d", 42)
|
||||
if len(mem.Lines) != 1 {
|
||||
t.Errorf("Log: expected line count %v", len(mem.Lines))
|
||||
}
|
||||
if !mem.Has("value: 42") {
|
||||
t.Errorf("Log: expected output: %v", mem.Lines)
|
||||
}
|
||||
}
|
||||
{
|
||||
Debug("value: %d", 84)
|
||||
if len(mem.Lines) != 2 {
|
||||
t.Errorf("Log: expected line count %v", len(mem.Lines))
|
||||
}
|
||||
if !mem.Has("value: 42") || !mem.Has("value: 84") {
|
||||
t.Errorf("Log: expected output: %v", mem.Lines)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("disabled", func(t *testing.T) {
|
||||
mem := NewMemory("log")
|
||||
SetOutput(mem)
|
||||
Verbose = false
|
||||
Debug("value: %d", 42)
|
||||
if len(mem.Lines) != 0 {
|
||||
t.Errorf("Log: expected line count %v", len(mem.Lines))
|
||||
}
|
||||
})
|
||||
}
|
||||
61
internal/logx/memory.go
Normal file
61
internal/logx/memory.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Memory stores logged messages in a slice.
|
||||
type Memory struct {
|
||||
Name string
|
||||
Lines []string
|
||||
}
|
||||
|
||||
// NewMemory creates a new memory destination.
|
||||
func NewMemory(name string) *Memory {
|
||||
return &Memory{Name: name, Lines: []string{}}
|
||||
}
|
||||
|
||||
// Write implements the io.Writer interface.
|
||||
func (m *Memory) Write(p []byte) (n int, err error) {
|
||||
msg := string(p)
|
||||
m.Lines = append(m.Lines, msg)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// WriteString writes a string to the memory.
|
||||
func (m *Memory) WriteString(s string) {
|
||||
m.Lines = append(m.Lines, s)
|
||||
}
|
||||
|
||||
// Has returns true if the memory has the message.
|
||||
func (m *Memory) Has(msg string) bool {
|
||||
for _, line := range m.Lines {
|
||||
if strings.Contains(line, msg) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// MustHave checks if the memory has the message.
|
||||
func (m *Memory) MustHave(t *testing.T, msg string) {
|
||||
if !m.Has(msg) {
|
||||
t.Errorf("%s must have: %s", m.Name, msg)
|
||||
}
|
||||
}
|
||||
|
||||
// MustNotHave checks if the memory does not have the message.
|
||||
func (m *Memory) MustNotHave(t *testing.T, msg string) {
|
||||
if m.Has(msg) {
|
||||
t.Errorf("%s must NOT have: %s", m.Name, msg)
|
||||
}
|
||||
}
|
||||
|
||||
// Print prints memory lines to stdout.
|
||||
func (m *Memory) Print() {
|
||||
for _, line := range m.Lines {
|
||||
fmt.Print(line)
|
||||
}
|
||||
}
|
||||
43
internal/logx/memory_test.go
Normal file
43
internal/logx/memory_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package logx
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMemory_Name(t *testing.T) {
|
||||
mem := NewMemory("log")
|
||||
if mem.Name != "log" {
|
||||
t.Errorf("Name: unexpected name %q", mem.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemory_Write(t *testing.T) {
|
||||
mem := NewMemory("log")
|
||||
if len(mem.Lines) != 0 {
|
||||
t.Fatalf("Write: unexpected line count %v", len(mem.Lines))
|
||||
}
|
||||
|
||||
n, err := mem.Write([]byte("hello world"))
|
||||
if err != nil {
|
||||
t.Fatalf("Write: unexpected error %v", err)
|
||||
}
|
||||
if n != 11 {
|
||||
t.Errorf("Write: unexpected byte count %v", n)
|
||||
}
|
||||
|
||||
if len(mem.Lines) != 1 {
|
||||
t.Fatalf("Write: unexpected line count %v", len(mem.Lines))
|
||||
}
|
||||
if mem.Lines[0] != "hello world" {
|
||||
t.Errorf("Write: unexpected line #0 %q", mem.Lines[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemory_Has(t *testing.T) {
|
||||
mem := NewMemory("log")
|
||||
if mem.Has("hello world") {
|
||||
t.Error("Has: unexpected true")
|
||||
}
|
||||
_, _ = mem.Write([]byte("hello world"))
|
||||
if !mem.Has("hello world") {
|
||||
t.Error("Has: unexpected false")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user