...

Source file src/github.com/go-logr/logr/testr/testr_test.go

Documentation: github.com/go-logr/logr/testr

     1  /*
     2  Copyright 2021 The logr Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testr
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/go-logr/logr"
    24  )
    25  
    26  func TestLogger(t *testing.T) {
    27  	log := New(t)
    28  	log.Info("info")
    29  	log.V(0).Info("V(0).info")
    30  	log.V(1).Info("v(1).info")
    31  	log.Error(fmt.Errorf("error"), "error")
    32  	log.WithName("testing").WithValues("value", "test").Info("with prefix")
    33  	log.WithName("testing").Error(fmt.Errorf("error"), "with prefix")
    34  	Helper(log, "hello world")
    35  
    36  	log = NewWithOptions(t, Options{
    37  		LogTimestamp: true,
    38  		Verbosity:    1,
    39  	})
    40  	log.V(1).Info("v(1).info with options")
    41  
    42  	underlier, ok := log.GetSink().(Underlier)
    43  	if !ok {
    44  		t.Fatal("couldn't get underlier")
    45  	}
    46  	if t != underlier.GetUnderlying() {
    47  		t.Error("invalid underlier")
    48  	}
    49  }
    50  
    51  func TestLoggerInterface(t *testing.T) {
    52  	log := NewWithInterface(t, Options{})
    53  	log.Info("info")
    54  	log.V(0).Info("V(0).info")
    55  	log.V(1).Info("v(1).info")
    56  	log.Error(fmt.Errorf("error"), "error")
    57  	log.WithName("testing").WithValues("value", "test").Info("with prefix")
    58  	log.WithName("testing").Error(fmt.Errorf("error"), "with prefix")
    59  	Helper(log, "hello world")
    60  
    61  	underlier, ok := log.GetSink().(UnderlierInterface)
    62  	if !ok {
    63  		t.Fatal("couldn't get underlier")
    64  	}
    65  	underlierT, ok := underlier.GetUnderlying().(*testing.T)
    66  	if !ok {
    67  		t.Fatal("couldn't get underlying *testing.T")
    68  	}
    69  	if t != underlierT {
    70  		t.Error("invalid underlier")
    71  	}
    72  }
    73  
    74  func TestLoggerTestingB(_ *testing.T) {
    75  	b := &testing.B{}
    76  	_ = NewWithInterface(b, Options{})
    77  }
    78  
    79  func Helper(log logr.Logger, msg string) {
    80  	helper, log := log.WithCallStackHelper()
    81  	helper()
    82  	helper2(log, msg)
    83  }
    84  
    85  func helper2(log logr.Logger, msg string) {
    86  	helper, log := log.WithCallStackHelper()
    87  	helper()
    88  	log.Info(msg)
    89  }
    90  

View as plain text