github.com/Laisky/zap@v1.27.0/zapcore/console_encoder_test.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  package zapcore_test
    21  
    22  import (
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  
    28  	//revive:disable:dot-imports
    29  	. "github.com/Laisky/zap/zapcore"
    30  )
    31  
    32  var testEntry = Entry{
    33  	LoggerName: "main",
    34  	Level:      InfoLevel,
    35  	Message:    `hello`,
    36  	Time:       _epoch,
    37  	Stack:      "fake-stack",
    38  	Caller:     EntryCaller{Defined: true, File: "foo.go", Line: 42, Function: "foo.Foo"},
    39  }
    40  
    41  func TestConsoleEncodeEntry(t *testing.T) {
    42  	tests := []struct {
    43  		desc     string
    44  		expected string
    45  		ent      Entry
    46  		fields   []Field
    47  	}{
    48  		{
    49  			desc:     "info no fields",
    50  			expected: "2018-06-19T16:33:42Z\tinfo\tbob\tlob law\n",
    51  			ent: Entry{
    52  				Level:      InfoLevel,
    53  				Time:       time.Date(2018, 6, 19, 16, 33, 42, 99, time.UTC),
    54  				LoggerName: "bob",
    55  				Message:    "lob law",
    56  			},
    57  		},
    58  		{
    59  			desc:     "zero_time_omitted",
    60  			expected: "info\tname\tmessage\n",
    61  			ent: Entry{
    62  				Level:      InfoLevel,
    63  				Time:       time.Time{},
    64  				LoggerName: "name",
    65  				Message:    "message",
    66  			},
    67  		},
    68  	}
    69  
    70  	cfg := testEncoderConfig()
    71  	cfg.EncodeTime = RFC3339TimeEncoder
    72  	enc := NewConsoleEncoder(cfg)
    73  
    74  	for _, tt := range tests {
    75  		t.Run(tt.desc, func(t *testing.T) {
    76  			buf, err := enc.EncodeEntry(tt.ent, tt.fields)
    77  			if assert.NoError(t, err, "Unexpected console encoding error.") {
    78  				assert.Equal(t, tt.expected, buf.String(), "Incorrect encoded entry.")
    79  			}
    80  			buf.Free()
    81  		})
    82  	}
    83  }
    84  
    85  func TestConsoleSeparator(t *testing.T) {
    86  	tests := []struct {
    87  		desc        string
    88  		separator   string
    89  		wantConsole string
    90  	}{
    91  		{
    92  			desc:        "space console separator",
    93  			separator:   " ",
    94  			wantConsole: "0 info main foo.go:42 foo.Foo hello\nfake-stack\n",
    95  		},
    96  		{
    97  			desc:        "default console separator",
    98  			separator:   "",
    99  			wantConsole: "0\tinfo\tmain\tfoo.go:42\tfoo.Foo\thello\nfake-stack\n",
   100  		},
   101  		{
   102  			desc:        "tag console separator",
   103  			separator:   "\t",
   104  			wantConsole: "0\tinfo\tmain\tfoo.go:42\tfoo.Foo\thello\nfake-stack\n",
   105  		},
   106  		{
   107  			desc:        "dash console separator",
   108  			separator:   "--",
   109  			wantConsole: "0--info--main--foo.go:42--foo.Foo--hello\nfake-stack\n",
   110  		},
   111  	}
   112  
   113  	for _, tt := range tests {
   114  		console := NewConsoleEncoder(encoderTestEncoderConfig(tt.separator))
   115  		t.Run(tt.desc, func(t *testing.T) {
   116  			entry := testEntry
   117  			consoleOut, err := console.EncodeEntry(entry, nil)
   118  			if !assert.NoError(t, err) {
   119  				return
   120  			}
   121  			assert.Equal(
   122  				t,
   123  				tt.wantConsole,
   124  				consoleOut.String(),
   125  				"Unexpected console output",
   126  			)
   127  		})
   128  
   129  	}
   130  }
   131  
   132  func encoderTestEncoderConfig(separator string) EncoderConfig {
   133  	testEncoder := testEncoderConfig()
   134  	testEncoder.ConsoleSeparator = separator
   135  	return testEncoder
   136  }