github.com/pavlo67/common@v0.5.3/common/serialization/jlist_test.go (about) 1 package serialization 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 type common struct { 10 name string 11 prefix string 12 indent string 13 want string 14 wantErr bool 15 } 16 17 type testCase[T any] struct { 18 common 19 list []T 20 } 21 22 func TestJSONList(t *testing.T) { 23 testInt := testCase[int]{ 24 list: []int{1, 2, 3, 4}, 25 common: common{ 26 prefix: "\n", 27 indent: " ", 28 want: "[\n 1,\n 2,\n 3,\n 4\n]", 29 wantErr: false, 30 }, 31 } 32 CheckOneJSONList(t, testInt.common, testInt.list) 33 34 testStr := testCase[string]{ 35 list: []string{"1", "2", "3", "4"}, 36 common: common{ 37 prefix: "\n", 38 indent: " ", 39 want: "[\n \"1\",\n \"2\",\n \"3\",\n \"4\"\n]", 40 wantErr: false, 41 }, 42 } 43 CheckOneJSONList(t, testStr.common, testStr.list) 44 45 testAny := testCase[interface{}]{ 46 list: []interface{}{"1", "2", 3, []int{4, 5, 6}}, 47 common: common{ 48 prefix: "\n", 49 indent: " ", 50 want: "[\n \"1\",\n \"2\",\n 3,\n [4,5,6]\n]", 51 wantErr: false, 52 }, 53 } 54 CheckOneJSONList(t, testAny.common, testAny.list) 55 56 } 57 58 func CheckOneJSONList[T any](t *testing.T, tt common, list []T) { 59 t.Run(tt.name, func(t *testing.T) { 60 got, err := JSONList(list, tt.prefix, tt.indent) 61 require.Equalf(t, tt.wantErr, err != nil, "tt.wantErr = %t, err = %s", tt.wantErr, err) 62 require.Equal(t, tt.want, string(got)) 63 }) 64 65 }