github.com/mailgun/holster/v4@v4.20.0/tracing/level_test.go (about) 1 package tracing_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/mailgun/holster/v4/tracing" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestLevel(t *testing.T) { 13 t.Run("String()", func(t *testing.T) { 14 assert.Equal(t, tracing.PanicLevel.String(), "PANIC") 15 assert.Equal(t, tracing.FatalLevel.String(), "FATAL") 16 assert.Equal(t, tracing.ErrorLevel.String(), "ERROR") 17 assert.Equal(t, tracing.WarnLevel.String(), "WARNING") 18 assert.Equal(t, tracing.InfoLevel.String(), "INFO") 19 assert.Equal(t, tracing.DebugLevel.String(), "DEBUG") 20 assert.Equal(t, tracing.TraceLevel.String(), "TRACE") 21 }) 22 23 t.Run("ParseLogLevel()", func(t *testing.T) { 24 testCases := []struct { 25 Input string 26 Expected tracing.Level 27 }{ 28 {Input: "PANIC", Expected: tracing.PanicLevel}, 29 {Input: "FATAL", Expected: tracing.FatalLevel}, 30 {Input: "ERROR", Expected: tracing.ErrorLevel}, 31 {Input: "WARNING", Expected: tracing.WarnLevel}, 32 {Input: "INFO", Expected: tracing.InfoLevel}, 33 {Input: "DEBUG", Expected: tracing.DebugLevel}, 34 {Input: "TRACE", Expected: tracing.TraceLevel}, 35 } 36 37 for _, testCase := range testCases { 38 t.Run(testCase.Input, func(t *testing.T) { 39 level, err := tracing.ParseLogLevel(testCase.Input) 40 require.NoError(t, err) 41 assert.Equal(t, testCase.Expected, level) 42 level, err = tracing.ParseLogLevel(strings.ToLower(testCase.Input)) 43 require.NoError(t, err) 44 assert.Equal(t, testCase.Expected, level) 45 }) 46 } 47 48 t.Run("Handle parse error", func(t *testing.T) { 49 _, err := tracing.ParseLogLevel("bogus") 50 assert.ErrorContains(t, err, "unknown log level") 51 }) 52 }) 53 }