From 17e9649d1e575a1902a0266508fc4aacbe311650 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 20 Nov 2025 19:32:27 -0800 Subject: [PATCH] msg: fixes and tests added. --- msg/msg.go | 14 ++++- msg/msg_test.go | 147 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 msg/msg_test.go diff --git a/msg/msg.go b/msg/msg.go index ba80988..69625ed 100644 --- a/msg/msg.go +++ b/msg/msg.go @@ -13,6 +13,7 @@ package msg import ( "fmt" "io" + "os" "git.wntrmute.dev/kyle/goutils/lib" @@ -22,10 +23,19 @@ import ( var ( enableQuiet 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) { enableQuiet = q } diff --git a/msg/msg_test.go b/msg/msg_test.go new file mode 100644 index 0000000..3aee9da --- /dev/null +++ b/msg/msg_test.go @@ -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") + } +}