|
| 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 | +} |
0 commit comments