lol_test.mx raw

   1  package lol
   2  
   3  import (
   4  	"errors"
   5  	"testing"
   6  )
   7  
   8  func TestLevelNames(t *testing.T) {
   9  	if len(LevelNames) != 7 {
  10  		t.Fatalf("expected 7 levels, got %d", len(LevelNames))
  11  	}
  12  }
  13  
  14  func TestSetLevel(t *testing.T) {
  15  	old := GetLevel()
  16  	defer SetLevel(old)
  17  	SetLevel(Trace)
  18  	if GetLevel() != Trace {
  19  		t.Fatal("expected Trace")
  20  	}
  21  	SetLevel(Off)
  22  	if GetLevel() != Off {
  23  		t.Fatal("expected Off")
  24  	}
  25  }
  26  
  27  func TestSetLevelByName(t *testing.T) {
  28  	old := GetLevel()
  29  	defer SetLevel(old)
  30  	SetLevelByName([]byte("debug"))
  31  	if GetLevel() != Debug {
  32  		t.Fatalf("expected Debug(%d), got %d", Debug, GetLevel())
  33  	}
  34  	SetLevelByName([]byte("bogus"))
  35  	if GetLevel() != Info {
  36  		t.Fatalf("expected Info(%d) for unknown name, got %d", Info, GetLevel())
  37  	}
  38  }
  39  
  40  func TestGetLevelByName(t *testing.T) {
  41  	if GetLevelByName([]byte("trace")) != Trace {
  42  		t.Fatal("expected Trace")
  43  	}
  44  	if GetLevelByName([]byte("nope")) != Info {
  45  		t.Fatal("expected Info for unknown")
  46  	}
  47  }
  48  
  49  func TestPrintersWithChannel(t *testing.T) {
  50  	ch := chan []byte{64}
  51  	SetLevel(Trace)
  52  	defer SetLevel(Info)
  53  	Init(ch)
  54  	defer Init(nil)
  55  
  56  	Main.Log.I.Ln("hello", "world")
  57  
  58  	select {
  59  	case entry := <-ch:
  60  		if len(entry) == 0 {
  61  			t.Fatal("empty entry")
  62  		}
  63  		// Should contain INF tag and the message
  64  		s := string(entry)
  65  		if !contains(s, "INF") {
  66  			t.Fatalf("missing INF tag in: %s", s)
  67  		}
  68  		if !contains(s, "helloworld") {
  69  			t.Fatalf("missing message in: %s", s)
  70  		}
  71  	default:
  72  		t.Fatal("no entry received on channel")
  73  	}
  74  }
  75  
  76  func TestPrintersF(t *testing.T) {
  77  	ch := chan []byte{64}
  78  	SetLevel(Trace)
  79  	defer SetLevel(Info)
  80  	Init(ch)
  81  	defer Init(nil)
  82  
  83  	Main.Log.I.F([]byte("count=%d"), 42)
  84  
  85  	select {
  86  	case entry := <-ch:
  87  		s := string(entry)
  88  		if !contains(s, "count=42") {
  89  			t.Fatalf("missing formatted message in: %s", s)
  90  		}
  91  	default:
  92  		t.Fatal("no entry received")
  93  	}
  94  }
  95  
  96  func TestChk(t *testing.T) {
  97  	ch := chan []byte{64}
  98  	SetLevel(Trace)
  99  	defer SetLevel(Info)
 100  	Init(ch)
 101  	defer Init(nil)
 102  
 103  	if Main.Check.E(nil) {
 104  		t.Fatal("nil error should return false")
 105  	}
 106  
 107  	err := errors.New("test error")
 108  	if !Main.Check.E(err) {
 109  		t.Fatal("non-nil error should return true")
 110  	}
 111  
 112  	select {
 113  	case entry := <-ch:
 114  		if !contains(string(entry), "test error") {
 115  			t.Fatalf("missing error text in: %s", string(entry))
 116  		}
 117  	default:
 118  		t.Fatal("no entry for error check")
 119  	}
 120  }
 121  
 122  func TestLevelSuppression(t *testing.T) {
 123  	ch := chan []byte{64}
 124  	SetLevel(Error)
 125  	defer SetLevel(Info)
 126  	Init(ch)
 127  	defer Init(nil)
 128  
 129  	Main.Log.D.Ln("suppressed")
 130  
 131  	select {
 132  	case entry := <-ch:
 133  		t.Fatalf("debug should be suppressed at Error level, got: %s", string(entry))
 134  	default:
 135  		// good — nothing sent
 136  	}
 137  }
 138  
 139  func TestErrorf(t *testing.T) {
 140  	ch := chan []byte{64}
 141  	SetLevel(Trace)
 142  	defer SetLevel(Info)
 143  	Init(ch)
 144  	defer Init(nil)
 145  
 146  	err := Main.Errorf.E([]byte("fail: %d"), 99)
 147  	if err == nil {
 148  		t.Fatal("expected error")
 149  	}
 150  	if err.Error() != "fail: 99" {
 151  		t.Fatalf("wrong error: %s", err.Error())
 152  	}
 153  }
 154  
 155  func contains(s, sub string) bool {
 156  	for i := 0; i <= len(s)-len(sub); i++ {
 157  		if s[i:i+len(sub)] == sub {
 158  			return true
 159  		}
 160  	}
 161  	return false
 162  }
 163