github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/amino/deep_copy_test.go (about) 1 package amino_test 2 3 import ( 4 "errors" 5 "testing" 6 7 amino "github.com/gnolang/gno/tm2/pkg/amino" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 type DCFoo1 struct{ a string } 12 13 func newDCFoo1(a string) *DCFoo1 { return &DCFoo1{a: a} } 14 func (dcf *DCFoo1) MarshalAmino() (string, error) { return dcf.a, nil } 15 func (dcf *DCFoo1) UnmarshalAmino(s string) error { dcf.a = s; return nil } 16 17 func TestDeepCopyFoo1(t *testing.T) { 18 t.Parallel() 19 20 dcf1 := newDCFoo1("foobar") 21 dcf2 := amino.DeepCopy(dcf1).(*DCFoo1) 22 assert.Equal(t, "foobar", dcf2.a) 23 } 24 25 type DCFoo2 struct{ a string } 26 27 func newDCFoo2(a string) *DCFoo2 { return &DCFoo2{a: a} } 28 func (dcf DCFoo2) MarshalAmino() (string, error) { return dcf.a, nil } // non-pointer receiver 29 func (dcf *DCFoo2) UnmarshalAmino(s string) error { dcf.a = s; return nil } 30 31 func TestDeepCopyFoo2(t *testing.T) { 32 t.Parallel() 33 34 dcf1 := newDCFoo2("foobar") 35 dcf2 := amino.DeepCopy(dcf1).(*DCFoo2) 36 assert.Equal(t, "foobar", dcf2.a) 37 } 38 39 type DCFoo3 struct{ a string } 40 41 func newDCFoo3(a string) *DCFoo3 { return &DCFoo3{a: a} } 42 func (dcf DCFoo3) MarshalAmino() (string, error) { return dcf.a, nil } 43 func (dcf *DCFoo3) UnmarshalAmino(s []byte) error { dcf.a = string(s); return nil } // mismatch type 44 45 func TestDeepCopyFoo3(t *testing.T) { 46 t.Parallel() 47 48 dcf1 := newDCFoo3("foobar") 49 dcf2 := amino.DeepCopy(dcf1).(*DCFoo3) 50 assert.Equal(t, "", dcf2.a) 51 } 52 53 type DCFoo4 struct{ a string } 54 55 func newDCFoo4(a string) *DCFoo4 { return &DCFoo4{a: a} } 56 func (dcf *DCFoo4) DeepCopy() *DCFoo4 { return &DCFoo4{"good"} } 57 func (dcf DCFoo4) MarshalAmino() (string, error) { return dcf.a, nil } 58 func (dcf *DCFoo4) UnmarshalAmino(s string) error { dcf.a = s; return nil } // mismatch type 59 60 func TestDeepCopyFoo4(t *testing.T) { 61 t.Parallel() 62 63 dcf1 := newDCFoo4("foobar") 64 dcf2 := amino.DeepCopy(dcf1).(*DCFoo4) 65 assert.Equal(t, "good", dcf2.a) 66 } 67 68 type DCFoo5 struct{ a string } 69 70 func newDCFoo5(a string) *DCFoo5 { return &DCFoo5{a: a} } 71 func (dcf DCFoo5) DeepCopy() DCFoo5 { return DCFoo5{"good"} } 72 func (dcf DCFoo5) MarshalAmino() (string, error) { return dcf.a, nil } 73 func (dcf *DCFoo5) UnmarshalAmino(s string) error { dcf.a = s; return nil } // mismatch type 74 75 func TestDeepCopyFoo5(t *testing.T) { 76 t.Parallel() 77 78 dcf1 := newDCFoo5("foobar") 79 dcf2 := amino.DeepCopy(dcf1).(*DCFoo5) 80 assert.Equal(t, "good", dcf2.a) 81 } 82 83 type DCFoo6 struct{ a string } 84 85 func newDCFoo6(a string) *DCFoo6 { return &DCFoo6{a: a} } 86 func (dcf *DCFoo6) DeepCopy() DCFoo6 { return DCFoo6{"good"} } 87 88 func TestDeepCopyFoo6(t *testing.T) { 89 t.Parallel() 90 91 dcf1 := newDCFoo6("foobar") 92 dcf2 := amino.DeepCopy(dcf1).(*DCFoo6) 93 assert.Equal(t, "good", dcf2.a) 94 } 95 96 type DCFoo7 struct{ a string } 97 98 func newDCFoo7(a string) *DCFoo7 { return &DCFoo7{a: a} } 99 func (dcf DCFoo7) DeepCopy() *DCFoo7 { return &DCFoo7{"good"} } 100 101 func TestDeepCopyFoo7(t *testing.T) { 102 t.Parallel() 103 104 dcf1 := newDCFoo7("foobar") 105 dcf2 := amino.DeepCopy(dcf1).(*DCFoo7) 106 assert.Equal(t, "good", dcf2.a) 107 } 108 109 type DCFoo8 struct{ a string } 110 111 func newDCFoo8(a string) *DCFoo8 { return &DCFoo8{a: a} } 112 func (dcf DCFoo8) MarshalAmino() (string, error) { return "", errors.New("uh oh") } // error 113 func (dcf *DCFoo8) UnmarshalAmino(s string) error { dcf.a = s; return nil } 114 115 func TestDeepCopyFoo8(t *testing.T) { 116 t.Parallel() 117 118 dcf1 := newDCFoo8("foobar") 119 assert.Panics(t, func() { amino.DeepCopy(dcf1) }) 120 } 121 122 type DCFoo9 struct{ a string } 123 124 func newDCFoo9(a string) *DCFoo9 { return &DCFoo9{a: a} } 125 func (dcf DCFoo9) MarshalAmino() (string, error) { return dcf.a, nil } 126 func (dcf *DCFoo9) UnmarshalAmino(s string) error { return errors.New("uh oh") } // error 127 128 func TestDeepCopyFoo9(t *testing.T) { 129 t.Parallel() 130 131 dcf1 := newDCFoo9("foobar") 132 assert.Panics(t, func() { amino.DeepCopy(dcf1) }) 133 } 134 135 type DCInterface1 struct { 136 Foo interface{} 137 } 138 139 func TestDeepCopyInterface1(t *testing.T) { 140 t.Parallel() 141 142 dci1 := DCInterface1{Foo: nil} 143 dci2 := amino.DeepCopy(dci1).(DCInterface1) 144 assert.Nil(t, dci2.Foo) 145 } 146 147 func TestDeepCopyInterface2(t *testing.T) { 148 t.Parallel() 149 150 dci1 := DCInterface1{Foo: "foo"} 151 dci2 := amino.DeepCopy(dci1).(DCInterface1) 152 assert.Equal(t, "foo", dci2.Foo) 153 }