github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/marshal_go118_test.go (about) 1 //go:build go1.18 2 3 package phpserialize_test 4 5 import ( 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 "github.com/trim21/go-phpserialize" 10 "github.com/trim21/go-phpserialize/internal/test" 11 ) 12 13 type Generic[T any] struct { 14 Value T 15 } 16 17 type Generic2[T any] struct { 18 B bool // prevent direct 19 Value T 20 } 21 22 var go118TestCase = []test.Case{ 23 { 24 Name: "generic[int]", 25 Data: Generic[int]{1}, 26 Expected: `a:1:{s:5:"Value";i:1;}`, 27 }, 28 { 29 Name: "generic[struct]", 30 Data: Generic[test.User]{test.User{}}, 31 Expected: `a:1:{s:5:"Value";a:2:{s:2:"id";i:0;s:4:"name";s:0:"";}}`, 32 }, 33 34 { 35 Name: "generic[map]", 36 Data: Generic[map[string]int]{map[string]int{"one": 1}}, 37 Expected: `a:1:{s:5:"Value";a:1:{s:3:"one";i:1;}}`, 38 }, 39 40 { 41 Name: "generic[slice]", 42 Data: Generic[[]string]{[]string{"hello", "world"}}, 43 Expected: `a:1:{s:5:"Value";a:2:{i:0;s:5:"hello";i:1;s:5:"world";}}`, 44 }, 45 46 { 47 Name: "generic2[slice]", 48 Data: Generic2[[]string]{Value: []string{"hello", "world"}}, 49 Expected: `a:2:{s:1:"B";b:0;s:5:"Value";a:2:{i:0;s:5:"hello";i:1;s:5:"world";}}`, 50 }, 51 } 52 53 func TestMarshal_go118_concrete_types(t *testing.T) { 54 t.Parallel() 55 for _, data := range go118TestCase { 56 data := data 57 t.Run(data.Name, func(t *testing.T) { 58 actual, err := phpserialize.Marshal(data.Data) 59 require.NoError(t, err) 60 61 test.StringEqual(t, data.Expected, string(actual)) 62 }) 63 } 64 } 65 66 func TestMarshal_go118_interface(t *testing.T) { 67 t.Parallel() 68 for _, data := range go118TestCase { 69 data := data 70 t.Run(data.Name, func(t *testing.T) { 71 t.Parallel() 72 actual, err := phpserialize.Marshal(data) 73 require.NoError(t, err) 74 75 test.StringEqual(t, data.WrappedExpected(), string(actual)) 76 }) 77 } 78 }