...

Source file src/go.uber.org/zap/zaptest/observer/logged_entry_test.go

Documentation: go.uber.org/zap/zaptest/observer

     1  // Copyright (c) 2017 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  
    21  package observer
    22  
    23  import (
    24  	"testing"
    25  
    26  	"go.uber.org/zap"
    27  	"go.uber.org/zap/zapcore"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestLoggedEntryContextMap(t *testing.T) {
    33  	tests := []struct {
    34  		msg    string
    35  		fields []zapcore.Field
    36  		want   map[string]interface{}
    37  	}{
    38  		{
    39  			msg:    "no fields",
    40  			fields: nil,
    41  			want:   map[string]interface{}{},
    42  		},
    43  		{
    44  			msg: "simple",
    45  			fields: []zapcore.Field{
    46  				zap.String("k1", "v"),
    47  				zap.Int64("k2", 10),
    48  			},
    49  			want: map[string]interface{}{
    50  				"k1": "v",
    51  				"k2": int64(10),
    52  			},
    53  		},
    54  		{
    55  			msg: "overwrite",
    56  			fields: []zapcore.Field{
    57  				zap.String("k1", "v1"),
    58  				zap.String("k1", "v2"),
    59  			},
    60  			want: map[string]interface{}{
    61  				"k1": "v2",
    62  			},
    63  		},
    64  		{
    65  			msg: "nested",
    66  			fields: []zapcore.Field{
    67  				zap.String("k1", "v1"),
    68  				zap.Namespace("nested"),
    69  				zap.String("k2", "v2"),
    70  			},
    71  			want: map[string]interface{}{
    72  				"k1": "v1",
    73  				"nested": map[string]interface{}{
    74  					"k2": "v2",
    75  				},
    76  			},
    77  		},
    78  	}
    79  
    80  	for _, tt := range tests {
    81  		t.Run(tt.msg, func(t *testing.T) {
    82  			entry := LoggedEntry{
    83  				Context: tt.fields,
    84  			}
    85  			assert.Equal(t, tt.want, entry.ContextMap())
    86  		})
    87  	}
    88  }
    89  

View as plain text