...

Source file src/github.com/spf13/jwalterweatherman/notepad_test.go

Documentation: github.com/spf13/jwalterweatherman

     1  // Copyright © 2016 Steve Francia <spf@spf13.com>.
     2  //
     3  // Use of this source code is governed by an MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package jwalterweatherman
     7  
     8  import (
     9  	"bytes"
    10  	"io"
    11  	"io/ioutil"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestNotepad(t *testing.T) {
    18  	var logHandle, outHandle bytes.Buffer
    19  
    20  	errorCounter := &Counter{}
    21  
    22  	n := NewNotepad(LevelCritical, LevelError, &outHandle, &logHandle, "TestNotePad", 0, LogCounter(errorCounter, LevelError))
    23  
    24  	require.Equal(t, LevelCritical, n.GetStdoutThreshold())
    25  	require.Equal(t, LevelError, n.GetLogThreshold())
    26  
    27  	n.DEBUG.Println("Some debug")
    28  	n.ERROR.Println("Some error")
    29  	n.CRITICAL.Println("Some critical error")
    30  
    31  	require.Contains(t, logHandle.String(), "[TestNotePad] ERROR Some error")
    32  	require.NotContains(t, logHandle.String(), "Some debug")
    33  	require.NotContains(t, outHandle.String(), "Some error")
    34  	require.Contains(t, outHandle.String(), "Some critical error")
    35  
    36  	// 1 error + 1 critical
    37  	require.Equal(t, errorCounter.Count(), uint64(2))
    38  }
    39  
    40  func TestNotepadLogListener(t *testing.T) {
    41  	assert := require.New(t)
    42  
    43  	var errorBuff, infoBuff bytes.Buffer
    44  
    45  	errorCapture := func(t Threshold) io.Writer {
    46  		if t != LevelError {
    47  			// Only interested in ERROR
    48  			return nil
    49  		}
    50  
    51  		return &errorBuff
    52  	}
    53  
    54  	infoCapture := func(t Threshold) io.Writer {
    55  		if t != LevelInfo {
    56  			return nil
    57  		}
    58  
    59  		return &infoBuff
    60  	}
    61  
    62  	n := NewNotepad(LevelCritical, LevelError, ioutil.Discard, ioutil.Discard, "TestNotePad", 0, infoCapture, errorCapture)
    63  
    64  	n.DEBUG.Println("Some debug")
    65  	n.INFO.Println("Some info")
    66  	n.INFO.Println("Some more info")
    67  	n.ERROR.Println("Some error")
    68  	n.CRITICAL.Println("Some critical error")
    69  	n.ERROR.Println("Some more error")
    70  
    71  	assert.Equal(`[TestNotePad] ERROR Some error
    72  [TestNotePad] ERROR Some more error
    73  `, errorBuff.String())
    74  	assert.Equal(`[TestNotePad] INFO Some info
    75  [TestNotePad] INFO Some more info
    76  `, infoBuff.String())
    77  
    78  }
    79  
    80  func TestThresholdString(t *testing.T) {
    81  	require.Equal(t, LevelError.String(), "ERROR")
    82  	require.Equal(t, LevelTrace.String(), "TRACE")
    83  }
    84  
    85  func BenchmarkLogPrintOnlyToCounter(b *testing.B) {
    86  	var logHandle, outHandle bytes.Buffer
    87  	n := NewNotepad(LevelCritical, LevelCritical, &outHandle, &logHandle, "TestNotePad", 0)
    88  
    89  	b.ResetTimer()
    90  	for i := 0; i < b.N; i++ {
    91  		n.INFO.Print("Test")
    92  	}
    93  }
    94  

View as plain text