github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/zlog/redirect_std_test.go (about)

     1  package zlog
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestDetectLevel(t *testing.T) {
    10  	testData := []struct {
    11  		Message   string
    12  		WantLevel Level
    13  		WantOk    bool
    14  	}{
    15  		{"[TRACE] ", TraceLevel, true},
    16  		{"[Trace]", TraceLevel, true},
    17  		{"[trace]some message", TraceLevel, true},
    18  		{"[TRACE] some message", TraceLevel, true},
    19  		{"trace some message", 0, false},
    20  		{"trace: some message", TraceLevel, true},
    21  		{"tracesomemessage", 0, false},
    22  
    23  		{"[DEBUG] ", DebugLevel, true},
    24  		{"[Debug] some message", DebugLevel, true},
    25  		{"[debug]some message", DebugLevel, true},
    26  		{"[DEBUG]some message", DebugLevel, true},
    27  		{"DEBUG some message", 0, false},
    28  		{"DEBUG: some message", DebugLevel, true},
    29  		{"DEBUGsome message", 0, false},
    30  
    31  		{"[INFO] ", InfoLevel, true},
    32  		{"[Info]", InfoLevel, true},
    33  		{"[info]some message", InfoLevel, true},
    34  		{"info: some message", InfoLevel, true},
    35  		{"INFO some message", 0, false},
    36  		{"some info message", 0, false},
    37  
    38  		{"[WARN] ", WarnLevel, true},
    39  		{"[WARNING] ", WarnLevel, true},
    40  		{"warn: some message", WarnLevel, true},
    41  		{"[Warn] some message", WarnLevel, true},
    42  		{"WARN some message", 0, false},
    43  		{"warning message", 0, false},
    44  		{"[Warning] some message", WarnLevel, true},
    45  		{"[WARNING] message", WarnLevel, true},
    46  		{"warning: message", WarnLevel, true},
    47  
    48  		{"[ERROR] ", ErrorLevel, true},
    49  		{"[Error]", ErrorLevel, true},
    50  		{"[error]some message", ErrorLevel, true},
    51  		{"[ERROR] some message", ErrorLevel, true},
    52  		{"error some message", 0, false},
    53  		{"error: some message", ErrorLevel, true},
    54  		{"errormessage", 0, false},
    55  
    56  		{"[PANIC] ", ErrorLevel, true},
    57  		{"[Panic]", ErrorLevel, true},
    58  		{"panic: some message", ErrorLevel, true},
    59  		{"panic some message", 0, false},
    60  
    61  		{"[FATAL] ", ErrorLevel, true},
    62  		{"[Fatal]", ErrorLevel, true},
    63  		{"Fatal: some message", ErrorLevel, true},
    64  		{"Fatal error occurred", 0, false},
    65  	}
    66  
    67  	for _, tc := range testData {
    68  		gotLevel, gotOk := detectLevel(tc.Message)
    69  		assert.Equal(t, tc.WantLevel, gotLevel)
    70  		assert.Equal(t, tc.WantOk, gotOk)
    71  	}
    72  }