github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/log/field_test.go (about)

     1  package log
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  type stringerTest string
    12  
    13  func (s stringerTest) String() string {
    14  	return string(s)
    15  }
    16  
    17  func TestField_String(t *testing.T) {
    18  	for _, tt := range []struct {
    19  		f     Field
    20  		want  string
    21  		panic bool
    22  		fail  bool
    23  	}{
    24  		{f: Int("int", 1), want: "1"},
    25  		{f: Int64("int64", 9223372036854775807), want: "9223372036854775807"},
    26  		{f: String("string", "test"), want: "test"},
    27  		{f: Bool("bool", true), want: "true"},
    28  		{f: Duration("duration", time.Hour), want: time.Hour.String()},
    29  		{f: Strings("strings", []string{"Abc", "Def", "Ghi"}), want: "[Abc Def Ghi]"},
    30  		{f: NamedError("named_error", errors.New("named error")), want: "named error"},
    31  		{f: Error(errors.New("error")), want: "error"},
    32  		{f: Error(nil), want: "<nil>"},
    33  		{f: Any("any_int", 1), want: "1"},
    34  		{f: Any("any_int64", 9223372036854775807), want: "9223372036854775807"},
    35  		{f: Any("any_string", "any string"), want: "any string"},
    36  		{f: Any("any_bool", true), want: "true"},
    37  		{f: Any("any_strings", []string{"Abc", "Def", "Ghi"}), want: "[Abc Def Ghi]"},
    38  		{f: Any("any_error", errors.New("error")), want: "*errors.errorString({error})"},
    39  		{f: Any("any_nil", nil), want: "<nil>"},
    40  		{f: Any("any_in64_ptr", func(v int64) *int64 { return &v }(9223372036854775807)), want: "*int64(9223372036854775807)"}, //nolint:lll
    41  		{f: Any("any_in64_nil", (*int64)(nil)), want: "<nil>"},
    42  		{f: Any("any_string_ptr", func(v string) *string { return &v }("string pointer")), want: "*string(string pointer)"}, //nolint:lll
    43  		{f: Any("any_string_nil", (*string)(nil)), want: "<nil>"},
    44  		{f: Stringer("stringer", stringerTest("stringerTest")), want: "stringerTest"},
    45  		{f: Field{ftype: InvalidType, key: "invalid"}, want: "", panic: true},
    46  	} {
    47  		t.Run(tt.f.key, func(t *testing.T) {
    48  			// Known fieldType, but String() panics with it.
    49  			if tt.panic {
    50  				require.Panics(t, func() { _ = tt.f.String() })
    51  
    52  				return
    53  			}
    54  			// Unknown fieldType, maybe a new one has been added
    55  			if tt.fail {
    56  				t.Fail()
    57  
    58  				return
    59  			}
    60  			require.Equal(t, tt.want, tt.f.String())
    61  		})
    62  	}
    63  }
    64  
    65  func TestField_AnyValue(t *testing.T) {
    66  	for _, tt := range []struct {
    67  		name string
    68  		f    Field
    69  		want interface{}
    70  	}{
    71  		{name: "int", f: Int("any", 1), want: 1},
    72  		{name: "int64", f: Int64("any", 9223372036854775807), want: int64(9223372036854775807)},
    73  		{name: "string", f: String("any", "any string"), want: "any string"},
    74  		{name: "bool", f: Bool("any", true), want: true},
    75  		{name: "[]string", f: Strings("any", []string{"Abc", "Def", "Ghi"}), want: []string{"Abc", "Def", "Ghi"}},
    76  		{name: "error", f: Error(errors.New("error")), want: errors.New("error")},
    77  		{name: "namedError", f: NamedError("any", nil), want: nil},
    78  		{name: "stringer", f: Stringer("any", stringerTest("stringerTest")), want: stringerTest("stringerTest")},
    79  
    80  		{name: "any_int", f: Any("any", 1), want: 1},
    81  		{name: "any_int64", f: Any("any", 9223372036854775807), want: 9223372036854775807},
    82  		{name: "any_int64_ptr", f: Any("any", func(v int64) *int64 { return &v }(9223372036854775807)), want: func(v int64) *int64 { return &v }(9223372036854775807)}, //nolint:lll
    83  		{name: "any_int64_nil", f: Any("any", (*int64)(nil)), want: (*int64)(nil)},
    84  		{name: "any_string", f: Any("any", "any string"), want: "any string"},
    85  		{name: "any_string_ptr", f: Any("any", func(v string) *string { return &v }("any string pointer")), want: func(v string) *string { return &v }("any string pointer")}, //nolint:lll
    86  		{name: "any_string_nil", f: Any("any", (*string)(nil)), want: (*string)(nil)},
    87  		{name: "any_bool", f: Any("any", true), want: true},
    88  		{name: "any_[]string", f: Any("any", []string{"Abc", "Def", "Ghi"}), want: []string{"Abc", "Def", "Ghi"}},
    89  		{name: "any_error", f: Any("any", errors.New("error")), want: errors.New("error")},
    90  		{name: "struct", f: Any("any", struct{ str string }{str: "test"}), want: struct{ str string }{str: "test"}},
    91  		{name: "any_nil", f: Any("any", nil), want: nil},
    92  	} {
    93  		t.Run(tt.name, func(t *testing.T) {
    94  			require.Equal(t, tt.want, tt.f.AnyValue())
    95  		})
    96  	}
    97  }