github.com/rudderlabs/rudder-go-kit@v0.30.0/stringify/stringify_test.go (about) 1 package stringify_test 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/rudderlabs/rudder-go-kit/stringify" 10 ) 11 12 type failOnJSONMarshal struct{} 13 14 func (f failOnJSONMarshal) MarshalJSON() ([]byte, error) { 15 return nil, errors.New("failed to marshal") 16 } 17 18 func TestStringyData(t *testing.T) { 19 testCases := []struct { 20 name string 21 input any 22 expected string 23 }{ 24 { 25 name: "Nil input", 26 input: nil, 27 expected: "", 28 }, 29 { 30 name: "String input", 31 input: "test string", 32 expected: "test string", 33 }, 34 { 35 name: "Struct input", 36 input: struct { 37 Name string `json:"name"` 38 Age int `json:"age"` 39 }{Name: "John", Age: 30}, 40 expected: `{"name":"John","age":30}`, 41 }, 42 { 43 name: "Slice input", 44 input: []string{ 45 "apple", "banana", "cherry", 46 }, 47 expected: `["apple","banana","cherry"]`, 48 }, 49 { 50 name: "Fail on JSON marshal", 51 input: failOnJSONMarshal{}, 52 expected: "{}", 53 }, 54 } 55 56 for _, tc := range testCases { 57 t.Run(tc.name, func(t *testing.T) { 58 result := stringify.Any(tc.input) 59 require.Equal(t, tc.expected, result) 60 }) 61 } 62 }