github.com/tcncloud/wollemi@v0.8.1/adapters/logrus/logger_test.go (about)

     1  package logrus_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/tcncloud/wollemi/ports/logging"
    10  )
    11  
    12  func TestLogger_Infof(t *testing.T) {
    13  	NewLoggerSuite(t).TestInfof()
    14  }
    15  
    16  func TestLogger_Warnf(t *testing.T) {
    17  	NewLoggerSuite(t).TestWarnf()
    18  }
    19  
    20  func TestLogger_Warn(t *testing.T) {
    21  	NewLoggerSuite(t).TestWarn()
    22  }
    23  
    24  func TestLogger_Debug(t *testing.T) {
    25  	NewLoggerSuite(t).TestDebug()
    26  }
    27  
    28  func TestLogger_WithError(t *testing.T) {
    29  	NewLoggerSuite(t).TestWithError()
    30  }
    31  
    32  func TestLogger_WithField(t *testing.T) {
    33  	NewLoggerSuite(t).TestWithField()
    34  }
    35  
    36  func TestLogger_WithFields(t *testing.T) {
    37  	NewLoggerSuite(t).TestWithFields()
    38  }
    39  
    40  func TestLogger_SetLevel(t *testing.T) {
    41  	NewLoggerSuite(t).TestSetLevel()
    42  }
    43  
    44  // -----------------------------------------------------------------------------
    45  
    46  func (t *LoggerSuite) TestInfof() {
    47  	t.BehavesLikeLogsMessage(
    48  		func(log logging.Logger) { log.Infof("hello: %q", "joe") },
    49  		logging.InfoLevel,
    50  		`hello: "joe"`,
    51  	)
    52  }
    53  
    54  func (t *LoggerSuite) TestWarnf() {
    55  	t.BehavesLikeLogsMessage(
    56  		func(log logging.Logger) { log.Warnf("one: %d, two: %d", 1, 2) },
    57  		logging.WarnLevel,
    58  		`one: 1, two: 2`,
    59  	)
    60  }
    61  
    62  func (t *LoggerSuite) TestWarn() {
    63  	t.BehavesLikeLogsMessage(
    64  		func(log logging.Logger) { log.Warn("winter is coming") },
    65  		logging.WarnLevel,
    66  		"winter is coming",
    67  	)
    68  }
    69  
    70  func (t *LoggerSuite) TestDebug() {
    71  	t.BehavesLikeLogsMessage(
    72  		func(log logging.Logger) { log.Debug("windows") },
    73  		logging.DebugLevel,
    74  		"windows",
    75  	)
    76  }
    77  
    78  func (t *LoggerSuite) TestWithError() {
    79  	type T = LoggerSuite
    80  
    81  	t.It("adds error to log entry", func(t *T) {
    82  		t.log.WithError(fmt.Errorf("boom")).
    83  			Infof("goes the dynamite")
    84  
    85  		var entry struct {
    86  			Error string `json:"error"`
    87  		}
    88  
    89  		t.Decode(t.stdout, &entry)
    90  		assert.Equal(t, "boom", entry.Error)
    91  	})
    92  }
    93  
    94  func (t *LoggerSuite) TestWithField() {
    95  	type T = LoggerSuite
    96  
    97  	t.It("adds field to log entry", func(t *T) {
    98  		t.log.WithField("foo", "ab").
    99  			WithField("bar", 1234).
   100  			WithField("baz", true).
   101  			Infof("")
   102  
   103  		var entry struct {
   104  			Foo string `json:"foo"`
   105  			Bar int    `json:"bar"`
   106  			Baz bool   `json:"baz"`
   107  		}
   108  
   109  		t.Decode(t.stdout, &entry)
   110  		assert.Equal(t, "ab", entry.Foo)
   111  		assert.Equal(t, 1234, entry.Bar)
   112  		assert.Equal(t, true, entry.Baz)
   113  	})
   114  }
   115  
   116  func (t *LoggerSuite) TestWithFields() {
   117  	type T = LoggerSuite
   118  
   119  	t.It("adds field to log entry", func(t *T) {
   120  		t.log.WithFields(logging.Fields{
   121  			"foo": "ab",
   122  			"bar": 1234,
   123  			"baz": true,
   124  		}).Infof("")
   125  
   126  		var entry struct {
   127  			Foo string `json:"foo"`
   128  			Bar int    `json:"bar"`
   129  			Baz bool   `json:"baz"`
   130  		}
   131  
   132  		t.Decode(t.stdout, &entry)
   133  		assert.Equal(t, "ab", entry.Foo)
   134  		assert.Equal(t, 1234, entry.Bar)
   135  		assert.Equal(t, true, entry.Baz)
   136  	})
   137  }
   138  
   139  func (t *LoggerSuite) TestSetLevel() {
   140  	type T = LoggerSuite
   141  
   142  	for _, lvl := range []logging.Level{
   143  		logging.PanicLevel,
   144  		logging.FatalLevel,
   145  		logging.ErrorLevel,
   146  		logging.WarnLevel,
   147  		logging.InfoLevel,
   148  		logging.DebugLevel,
   149  		logging.TraceLevel,
   150  	} {
   151  		t.It(fmt.Sprintf("sets log level to %s", lvl), func(t *T) {
   152  			t.log.SetLevel(lvl)
   153  			assert.Equal(t, lvl, t.log.GetLevel())
   154  		})
   155  	}
   156  }