github.com/jspc/eggos@v0.5.1-0.20221028160421-556c75c878a5/log/log_test.go (about) 1 package log_test 2 3 import ( 4 "testing" 5 6 "github.com/jspc/eggos/log" 7 ) 8 9 func ExampleSetLogLevel() { 10 // Set the log level to Debug 11 log.Level = log.LoglvlDebug 12 } 13 14 func TestLog_SetLevel(t *testing.T) { 15 for _, test := range []struct { 16 name string 17 l log.LogLevel 18 expectError bool 19 }{ 20 // These two cases require users to do something pretty explcitly 21 // wrong to hit, but they're worth catching 22 {"log level is too low", log.LogLevel(-1), true}, 23 {"log level is too high", log.LogLevel(6), true}, 24 25 // Included log levels 26 {"log.LoglvlDebug is valid", log.LoglvlDebug, false}, 27 {"log.LoglvlInfo is valid", log.LoglvlInfo, false}, 28 {"log.LoglvlWarn is valid", log.LoglvlWarn, false}, 29 {"log.LoglvlError is valid", log.LoglvlError, false}, 30 {"log.LoglvlNone is valid", log.LoglvlNone, false}, 31 } { 32 t.Run(test.name, func(t *testing.T) { 33 err := log.SetLevel(test.l) 34 35 if err == nil && test.expectError { 36 t.Error("expected error, received none") 37 } else if err != nil && !test.expectError { 38 t.Errorf("unexpected error: %#v", err) 39 } 40 41 if !test.expectError { 42 if log.Level != test.l { 43 t.Errorf("log.Level should be %#v, received %#v", log.Level, test.l) 44 } 45 } 46 }) 47 } 48 }