msg: fixes and tests added.

This commit is contained in:
2025-11-20 19:32:27 -08:00
parent 1fceb0e0da
commit 17e9649d1e
2 changed files with 159 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ package msg
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"git.wntrmute.dev/kyle/goutils/lib" "git.wntrmute.dev/kyle/goutils/lib"
@@ -22,10 +23,19 @@ import (
var ( var (
enableQuiet bool enableQuiet bool
enableVerbose bool enableVerbose bool
debug = dbg.New()
w io.Writer debug = dbg.New()
w io.Writer = os.Stdout
) )
func Reset() {
enableQuiet = false
enableVerbose = false
debug = dbg.New()
w = os.Stdout
}
func SetQuiet(q bool) { func SetQuiet(q bool) {
enableQuiet = q enableQuiet = q
} }

147
msg/msg_test.go Normal file
View File

@@ -0,0 +1,147 @@
package msg_test
import (
"bytes"
"testing"
"git.wntrmute.dev/kyle/goutils/msg"
)
func checkExpected(buf *bytes.Buffer, expected string) bool {
return buf.String() == expected
}
func resetBuf() *bytes.Buffer {
buf := &bytes.Buffer{}
msg.SetWriter(buf)
return buf
}
func TestVerbosePrint(t *testing.T) {
buf := resetBuf()
msg.SetVerbose(false) // ensure verbose is explicitly not set
msg.Vprint("hello, world")
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.Vprintf("hello, %s", "world")
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.Vprintln("hello, world")
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.SetVerbose(true)
msg.Vprint("hello, world")
if !checkExpected(buf, "hello, world") {
t.Fatalf("expected output %q, have %q", "hello, world", buf.String())
}
buf.Reset()
msg.Vprintf("hello, %s", "world")
if !checkExpected(buf, "hello, world") {
t.Fatalf("expected output %q, have %q", "hello, world", buf.String())
}
buf.Reset()
msg.Vprintln("hello, world")
if !checkExpected(buf, "hello, world\n") {
t.Fatalf("expected output %q, have %q", "hello, world\n", buf.String())
}
}
func TestQuietPrint(t *testing.T) {
buf := resetBuf()
msg.SetQuiet(true)
msg.Qprint("hello, world")
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.Qprintf("hello, %s", "world")
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.Qprintln("hello, world")
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.SetQuiet(false)
msg.Qprint("hello, world")
if !checkExpected(buf, "hello, world") {
t.Fatalf("expected output %q, have %q", "hello, world", buf.String())
}
buf.Reset()
msg.Qprintf("hello, %s", "world")
if !checkExpected(buf, "hello, world") {
t.Fatalf("expected output %q, have %q", "hello, world", buf.String())
}
buf.Reset()
msg.Qprintln("hello, world")
if !checkExpected(buf, "hello, world\n") {
t.Fatalf("expected output %q, have %q", "hello, world\n", buf.String())
}
}
func TestDebugPrint(t *testing.T) {
buf := resetBuf()
msg.SetDebug(false) // ensure debug is explicitly not set
msg.Dprint("hello, world")
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.Dprintf("hello, %s", "world")
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.Dprintln("hello, world")
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.StackTrace()
if buf.Len() != 0 {
t.Fatalf("expected no output, have %s", buf.String())
}
msg.SetDebug(true)
msg.Dprint("hello, world")
if !checkExpected(buf, "hello, world") {
t.Fatalf("expected output %q, have %q", "hello, world", buf.String())
}
buf.Reset()
msg.Dprintf("hello, %s", "world")
if !checkExpected(buf, "hello, world") {
t.Fatalf("expected output %q, have %q", "hello, world", buf.String())
}
buf.Reset()
msg.Dprintln("hello, world")
if !checkExpected(buf, "hello, world\n") {
t.Fatalf("expected output %q, have %q", "hello, world\n", buf.String())
}
buf.Reset()
msg.StackTrace()
if buf.Len() == 0 {
t.Fatal("expected stack trace output, received no output")
}
}