Skip to content
This repository was archived by the owner on Feb 4, 2026. It is now read-only.

Commit 6efdeac

Browse files
committed
test: logx
1 parent d25f43a commit 6efdeac

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

logx/logx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
var logger = NewLogger(os.Stdout)
1010

1111
// IsVerbose returns true if the logger is verbose.
12-
func IsVerbose(out io.Writer) bool {
12+
func IsVerbose() bool {
1313
return logger.IsVerbose
1414
}
1515

logx/logx_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package logx
2+
3+
import "testing"
4+
5+
func TestSetOutput(t *testing.T) {
6+
mem := NewMemory("log")
7+
SetOutput(mem)
8+
if logger.out != mem {
9+
t.Errorf("SetOutput: unexpected value %v", logger.out)
10+
}
11+
}
12+
13+
func TestVerbose(t *testing.T) {
14+
mem := NewMemory("log")
15+
SetOutput(mem)
16+
{
17+
if IsVerbose() {
18+
t.Errorf("IsVerbose: expected false, got true")
19+
}
20+
}
21+
{
22+
SetVerbose(true)
23+
if !IsVerbose() {
24+
t.Errorf("IsVerbose: expected true, got false")
25+
}
26+
}
27+
{
28+
SetVerbose(false)
29+
if IsVerbose() {
30+
t.Errorf("IsVerbose: expected false, got true")
31+
}
32+
}
33+
}
34+
35+
func TestLog(t *testing.T) {
36+
mem := NewMemory("log")
37+
SetOutput(mem)
38+
{
39+
Log("value: %d", 42)
40+
if len(mem.Lines) != 1 {
41+
t.Errorf("Log: expected line count %v", len(mem.Lines))
42+
}
43+
if !mem.Has("value: 42") {
44+
t.Errorf("Log: expected output: %v", mem.Lines)
45+
}
46+
}
47+
{
48+
Log("value: %d", 84)
49+
if len(mem.Lines) != 2 {
50+
t.Errorf("Log: expected line count %v", len(mem.Lines))
51+
}
52+
if !mem.Has("value: 42") || !mem.Has("value: 84") {
53+
t.Errorf("Log: expected output: %v", mem.Lines)
54+
}
55+
}
56+
}
57+
58+
func TestDebug(t *testing.T) {
59+
t.Run("enabled", func(t *testing.T) {
60+
mem := NewMemory("log")
61+
SetOutput(mem)
62+
SetVerbose(true)
63+
{
64+
Debug("value: %d", 42)
65+
if len(mem.Lines) != 1 {
66+
t.Errorf("Log: expected line count %v", len(mem.Lines))
67+
}
68+
if !mem.Has("value: 42") {
69+
t.Errorf("Log: expected output: %v", mem.Lines)
70+
}
71+
}
72+
{
73+
Debug("value: %d", 84)
74+
if len(mem.Lines) != 2 {
75+
t.Errorf("Log: expected line count %v", len(mem.Lines))
76+
}
77+
if !mem.Has("value: 42") || !mem.Has("value: 84") {
78+
t.Errorf("Log: expected output: %v", mem.Lines)
79+
}
80+
}
81+
})
82+
t.Run("disabled", func(t *testing.T) {
83+
mem := NewMemory("log")
84+
SetOutput(mem)
85+
SetVerbose(false)
86+
Debug("value: %d", 42)
87+
if len(mem.Lines) != 0 {
88+
t.Errorf("Log: expected line count %v", len(mem.Lines))
89+
}
90+
})
91+
}

logx/memory_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package logx
2+
3+
import "testing"
4+
5+
func TestMemory_Name(t *testing.T) {
6+
mem := NewMemory("log")
7+
if mem.Name != "log" {
8+
t.Errorf("Name: unexpected name %q", mem.Name)
9+
}
10+
}
11+
12+
func TestMemory_Write(t *testing.T) {
13+
mem := NewMemory("log")
14+
if len(mem.Lines) != 0 {
15+
t.Fatalf("Write: unexpected line count %v", len(mem.Lines))
16+
}
17+
18+
n, err := mem.Write([]byte("hello world"))
19+
if err != nil {
20+
t.Fatalf("Write: unexpected error %v", err)
21+
}
22+
if n != 11 {
23+
t.Errorf("Write: unexpected byte count %v", n)
24+
}
25+
26+
if len(mem.Lines) != 1 {
27+
t.Fatalf("Write: unexpected line count %v", len(mem.Lines))
28+
}
29+
if mem.Lines[0] != "hello world" {
30+
t.Errorf("Write: unexpected line #0 %q", mem.Lines[0])
31+
}
32+
}
33+
34+
func TestMemory_Has(t *testing.T) {
35+
mem := NewMemory("log")
36+
if mem.Has("hello world") {
37+
t.Error("Has: unexpected true")
38+
}
39+
_, _ = mem.Write([]byte("hello world"))
40+
if !mem.Has("hello world") {
41+
t.Error("Has: unexpected false")
42+
}
43+
}

0 commit comments

Comments
 (0)