github.com/matrixorigin/matrixone@v1.2.0/pkg/logutil/api_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logutil
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  func TestLog(t *testing.T) {
    27  	type args struct {
    28  		msg    string
    29  		fields []zap.Field
    30  	}
    31  	tests := []struct {
    32  		name string
    33  		args args
    34  	}{
    35  		{
    36  			name: "normal",
    37  			args: args{
    38  				msg:    "test",
    39  				fields: []zap.Field{zap.Int("int", 0), zap.String("string", "")},
    40  			},
    41  		},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			Debug(tt.args.msg, tt.args.fields...)
    46  			Info(tt.args.msg, tt.args.fields...)
    47  			Warn(tt.args.msg, tt.args.fields...)
    48  			Error(tt.args.msg, tt.args.fields...)
    49  		})
    50  	}
    51  }
    52  
    53  func TestLog_panic(t *testing.T) {
    54  	type args struct {
    55  		msg    string
    56  		fields []zap.Field
    57  	}
    58  	tests := []struct {
    59  		name   string
    60  		args   args
    61  		printf bool
    62  	}{
    63  		{
    64  			name: "panic",
    65  			args: args{
    66  				msg:    "test",
    67  				fields: []zap.Field{zap.Int("int", 0), zap.String("string", "")},
    68  			},
    69  			printf: false,
    70  		},
    71  		{
    72  			name: "panicF",
    73  			args: args{
    74  				msg:    "test",
    75  				fields: []zap.Field{zap.Int("int", 0), zap.String("string", "")},
    76  			},
    77  			printf: true,
    78  		},
    79  	}
    80  	for _, tt := range tests {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			defer func() {
    83  				err := recover()
    84  				require.Equal(t, tt.args.msg, fmt.Sprintf("%s", err))
    85  			}()
    86  			if tt.printf {
    87  				Panicf(tt.args.msg)
    88  			} else {
    89  				Panic(tt.args.msg, tt.args.fields...)
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestLogf(t *testing.T) {
    96  	type args struct {
    97  		msg    string
    98  		fields []any
    99  	}
   100  	tests := []struct {
   101  		name string
   102  		args args
   103  	}{
   104  		{
   105  			name: "normal",
   106  			args: args{
   107  				msg:    "test",
   108  				fields: []any{},
   109  			},
   110  		},
   111  	}
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			Debugf(tt.args.msg, tt.args.fields...)
   115  			Infof(tt.args.msg, tt.args.fields...)
   116  			Warnf(tt.args.msg, tt.args.fields...)
   117  			Errorf(tt.args.msg, tt.args.fields...)
   118  			gl := GoettyLogger{}
   119  			gl.Debugf(tt.args.msg, tt.args.fields...)
   120  			gl.Infof(tt.args.msg, tt.args.fields...)
   121  			gl.Errorf(tt.args.msg, tt.args.fields...)
   122  			//gl.Fatalf(tt.args.msg, tt.args.fields...)
   123  		})
   124  	}
   125  }
   126  
   127  func TestAdjust(t *testing.T) {
   128  	type args struct {
   129  		logger  *zap.Logger
   130  		options []zap.Option
   131  	}
   132  	tests := []struct {
   133  		name string
   134  		args args
   135  		want *zap.Logger
   136  	}{
   137  		{
   138  			name: "normal",
   139  			args: args{
   140  				logger:  GetGlobalLogger(),
   141  				options: nil,
   142  			},
   143  			want: GetGlobalLogger(),
   144  		},
   145  	}
   146  	for _, tt := range tests {
   147  		t.Run(tt.name, func(t *testing.T) {
   148  			if got := Adjust(tt.args.logger, tt.args.options...); !reflect.DeepEqual(got, tt.want) {
   149  				t.Errorf("Adjust() = %v, want %v", got, tt.want)
   150  			}
   151  		})
   152  	}
   153  }