| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package debug
- import "testing"
- import "strings"
- import "bytes"
- import "time"
- func assertContains(t *testing.T, str, substr string) {
- if !strings.Contains(str, substr) {
- t.Fatalf("expected %q to contain %q", str, substr)
- }
- }
- func assertNotContains(t *testing.T, str, substr string) {
- if strings.Contains(str, substr) {
- t.Fatalf("expected %q to not contain %q", str, substr)
- }
- }
- func TestDefault(t *testing.T) {
- var b []byte
- buf := bytes.NewBuffer(b)
- SetWriter(buf)
- debug := Debug("foo")
- debug("something")
- debug("here")
- debug("whoop")
- if buf.Len() != 0 {
- t.Fatalf("buffer should be empty")
- }
- }
- func TestEnable(t *testing.T) {
- var b []byte
- buf := bytes.NewBuffer(b)
- SetWriter(buf)
- Enable("foo")
- debug := Debug("foo")
- debug("something")
- debug("here")
- debug("whoop")
- if buf.Len() == 0 {
- t.Fatalf("buffer should have output")
- }
- str := string(buf.Bytes())
- assertContains(t, str, "something")
- assertContains(t, str, "here")
- assertContains(t, str, "whoop")
- }
- func TestMultipleOneEnabled(t *testing.T) {
- var b []byte
- buf := bytes.NewBuffer(b)
- SetWriter(buf)
- Enable("foo")
- foo := Debug("foo")
- foo("foo")
- bar := Debug("bar")
- bar("bar")
- if buf.Len() == 0 {
- t.Fatalf("buffer should have output")
- }
- str := string(buf.Bytes())
- assertContains(t, str, "foo")
- assertNotContains(t, str, "bar")
- }
- func TestMultipleEnabled(t *testing.T) {
- var b []byte
- buf := bytes.NewBuffer(b)
- SetWriter(buf)
- Enable("foo,bar")
- foo := Debug("foo")
- foo("foo")
- bar := Debug("bar")
- bar("bar")
- if buf.Len() == 0 {
- t.Fatalf("buffer should have output")
- }
- str := string(buf.Bytes())
- assertContains(t, str, "foo")
- assertContains(t, str, "bar")
- }
- func TestEnableDisable(t *testing.T) {
- var b []byte
- buf := bytes.NewBuffer(b)
- SetWriter(buf)
- Enable("foo,bar")
- Disable()
- foo := Debug("foo")
- foo("foo")
- bar := Debug("bar")
- bar("bar")
- if buf.Len() != 0 {
- t.Fatalf("buffer should not have output")
- }
- }
- func ExampleEnable() {
- Enable("mongo:connection")
- Enable("mongo:*")
- Enable("foo,bar,baz")
- Enable("*")
- }
- func ExampleDebug() {
- var debug = Debug("single")
- for {
- debug("sending mail")
- debug("send email to %s", "tobi@segment.io")
- debug("send email to %s", "loki@segment.io")
- debug("send email to %s", "jane@segment.io")
- time.Sleep(500 * time.Millisecond)
- }
- }
- func BenchmarkDisabled(b *testing.B) {
- debug := Debug("something")
- for i := 0; i < b.N; i++ {
- debug("stuff")
- }
- }
- func BenchmarkNonMatch(b *testing.B) {
- debug := Debug("something")
- Enable("nonmatch")
- for i := 0; i < b.N; i++ {
- debug("stuff")
- }
- }
|