github.com/icexin/eggos@v0.4.2-0.20220216025428-78b167e4f349/log/log_test.go (about)

     1  package log_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/icexin/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  }