go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

vanilla_test.go (1069B)


      1 package logging
      2 
      3 import (
      4 	"bytes"
      5 	"context"
      6 	"strings"
      7 	"testing"
      8 )
      9 
     10 func TestVanilla(t *testing.T) {
     11 	logg := NewVanilla().WithDomain("test").WithLevel(LVL_WARN)
     12 	w := bytes.NewBuffer(nil)
     13 	logg.Writef(w, LVL_DEBUG, "message", "xyzzy", 666, "inky", "pinky")
     14 	if len(w.Bytes()) > 0 {
     15 		t.Errorf("expected nothing, got %s", w.Bytes())
     16 	}
     17 	logg = logg.WithLevel(LVL_DEBUG)
     18 	logg.Writef(w, LVL_DEBUG, "message", "xyzzy", 666, "inky", "pinky")
     19 	if len(w.Bytes()) == 0 {
     20 		t.Errorf("expected output")
     21 	}
     22 }
     23 
     24 func TestVanillaCtx(t *testing.T) {
     25 	logg := NewVanilla().WithDomain("test").WithLevel(LVL_DEBUG).WithContextKey("foo")
     26 	ctx := context.WithValue(context.Background(), "foo", "bar")
     27 	w := bytes.NewBuffer(nil)
     28 	LogWriter = w
     29 
     30 	logg.DebugCtxf(ctx, "message", "xyzzy", 666, "inky", "pinky")
     31 	s := string(w.Bytes())
     32 	if len(s) == 0 {
     33 		t.Errorf("expected output")
     34 	}
     35 	if !strings.Contains(s, "foo=bar") {
     36 		t.Errorf("expected 'foo=bar' in output, output was: %s", s)
     37 	}
     38 	if !strings.Contains(s, "test") {
     39 		t.Errorf("expected 'test' in output, output was: %s", s)
     40 	}
     41 }