github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/pkg/copier/copier_test.go (about) 1 package copier 2 3 import ( 4 "testing" 5 6 "github.com/lovung/GoCleanArchitecture/pkg/testhelper" 7 ) 8 9 func TestMustCopy(t *testing.T) { 10 type a struct { 11 A int 12 } 13 type b struct { 14 A int64 15 } 16 type c struct { 17 A string 18 } 19 type args struct { 20 toValue interface{} 21 fromValue interface{} 22 } 23 testCases := []struct { 24 name string 25 args args 26 ok bool 27 }{ 28 { 29 args: args{ 30 toValue: &a{}, 31 fromValue: &b{A: 1}, 32 }, 33 ok: true, 34 }, 35 { 36 args: args{ 37 toValue: &a{}, 38 fromValue: &c{A: "1"}, 39 }, 40 ok: true, 41 }, 42 { 43 args: args{ 44 toValue: a{}, 45 fromValue: &c{A: "1"}, 46 }, 47 ok: false, 48 }, 49 { 50 args: args{ 51 toValue: &a{}, 52 fromValue: nil, 53 }, 54 ok: true, 55 }, 56 } 57 for _, tt := range testCases { 58 if tt.ok { 59 t.Run(tt.name, func(t *testing.T) { 60 MustCopy(tt.args.toValue, tt.args.fromValue) 61 }) 62 } else { 63 testhelper.ShouldPanic(t, func() { 64 MustCopy(tt.args.toValue, tt.args.fromValue) 65 }) 66 } 67 } 68 }