...

Source file src/github.com/datawire/ambassador/v2/pkg/envoy-control-plane/log/log_test.go

Documentation: github.com/datawire/ambassador/v2/pkg/envoy-control-plane/log

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

View as plain text