github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-control-plane/pkg/log/log_test.go (about)

     1  // Copyright 2020 Envoyproxy Authors
     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  package log
    15  
    16  import (
    17  	"log"
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func ExampleLoggerFuncs() {
    24  	logger := log.Logger{}
    25  
    26  	xdsLogger := LoggerFuncs{
    27  		DebugFunc: logger.Printf,
    28  		InfoFunc:  logger.Printf,
    29  		WarnFunc:  logger.Printf,
    30  		ErrorFunc: logger.Printf,
    31  	}
    32  
    33  	xdsLogger.Debugf("debug")
    34  	xdsLogger.Infof("info")
    35  	xdsLogger.Warnf("warn")
    36  	xdsLogger.Errorf("error")
    37  }
    38  
    39  func TestLoggerFuncs(t *testing.T) {
    40  	debug := 0
    41  	info := 0
    42  	warn := 0
    43  	error := 0
    44  
    45  	xdsLogger := LoggerFuncs{
    46  		DebugFunc: func(string, ...interface{}) { debug++ },
    47  		InfoFunc:  func(string, ...interface{}) { info++ },
    48  		WarnFunc:  func(string, ...interface{}) { warn++ },
    49  		ErrorFunc: func(string, ...interface{}) { error++ },
    50  	}
    51  
    52  	xdsLogger.Debugf("debug")
    53  	xdsLogger.Infof("info")
    54  	xdsLogger.Warnf("warn")
    55  	xdsLogger.Errorf("error")
    56  
    57  	assert.Equal(t, debug, 1)
    58  	assert.Equal(t, info, 1)
    59  	assert.Equal(t, warn, 1)
    60  	assert.Equal(t, error, 1)
    61  }
    62  
    63  func TestNilLoggerFuncs(t *testing.T) {
    64  	xdsLogger := LoggerFuncs{}
    65  
    66  	// Just verifying that nothing panics.
    67  	xdsLogger.Debugf("debug")
    68  	xdsLogger.Infof("info")
    69  	xdsLogger.Warnf("warn")
    70  	xdsLogger.Errorf("error")
    71  }