11package test
22
33import (
4- types "github.com/cosmos/gogoproto/types/any"
5-
4+ "bytes"
65 "testing"
76
8- "github.com/stretchr/testify/suite"
7+ types "github.com/cosmos/gogoproto/types/any"
8+ "github.com/google/go-cmp/cmp"
99 amino "github.com/tendermint/go-amino"
1010)
1111
@@ -14,118 +14,188 @@ type TypeWithInterface struct {
1414 X int64 `json:"x,omitempty"`
1515}
1616
17- type Suite struct {
18- suite.Suite
17+ type testFixture struct {
1918 cdc * amino.Codec
2019 a TypeWithInterface
2120 b HasAnimal
2221 spot * Dog
2322}
2423
25- func (s * Suite ) SetupTest () {
26- s .cdc = amino .NewCodec ()
27- s .cdc .RegisterInterface ((* Animal )(nil ), nil )
28- s .cdc .RegisterConcrete (& Dog {}, "test/Dog" , nil )
24+ func newTestFixture (t * testing.T ) * testFixture {
25+ t .Helper ()
26+
27+ cdc := amino .NewCodec ()
28+ cdc .RegisterInterface ((* Animal )(nil ), nil )
29+ cdc .RegisterConcrete (& Dog {}, "test/Dog" , nil )
2930
30- s . spot = & Dog {Size_ : "small" , Name : "Spot" }
31- s . a = TypeWithInterface {Animal : s . spot }
31+ spot : = & Dog {Size_ : "small" , Name : "Spot" }
32+ a : = TypeWithInterface {Animal : spot }
3233
33- any , err := types .NewAnyWithCacheWithValue (s .spot )
34- s .Require ().NoError (err )
35- s .b = HasAnimal {Animal : any }
34+ any , err := types .NewAnyWithCacheWithValue (spot )
35+ if err != nil {
36+ t .Fatal (err )
37+ }
38+
39+ b := HasAnimal {Animal : any }
40+
41+ return & testFixture {
42+ cdc : cdc ,
43+ a : a ,
44+ b : b ,
45+ spot : spot ,
46+ }
3647}
3748
38- func (s * Suite ) TestAminoBinary () {
49+ func TestAminoBinary (t * testing.T ) {
50+ s := newTestFixture (t )
51+
3952 bz , err := s .cdc .MarshalBinaryBare (s .a )
40- s .Require ().NoError (err )
53+ if err != nil {
54+ t .Fatal (err )
55+ }
4156
4257 // expect plain amino marshal to fail
4358 _ , err = s .cdc .MarshalBinaryBare (s .b )
44- s .Require ().Error (err )
59+ if err == nil {
60+ t .Fatal ("expected error" )
61+ }
4562
4663 // expect unpack interfaces before amino marshal to succeed
4764 err = types .UnpackInterfaces (s .b , types.AminoPacker {Cdc : s .cdc })
48- s .Require ().NoError (err )
65+ if err != nil {
66+ t .Fatal (err )
67+ }
4968 bz2 , err := s .cdc .MarshalBinaryBare (s .b )
50- s .Require ().NoError (err )
51- s .Require ().Equal (bz , bz2 )
69+ if err != nil {
70+ t .Fatal (err )
71+ }
72+ if ! bytes .Equal (bz , bz2 ) {
73+ t .Fatalf ("expected %X, got %X" , bz , bz2 )
74+ }
5275
5376 var c HasAnimal
5477 err = s .cdc .UnmarshalBinaryBare (bz , & c )
55- s .Require ().NoError (err )
78+ if err != nil {
79+ t .Fatal (err )
80+ }
5681 err = types .UnpackInterfaces (c , types.AminoUnpacker {Cdc : s .cdc })
57- s .Require ().NoError (err )
58- s .Require ().Equal (s .spot , c .Animal .GetCachedValue ())
82+ if err != nil {
83+ t .Fatal (err )
84+ }
85+
86+ if result := cmp .Diff (s .spot , c .Animal .GetCachedValue ()); result != "" {
87+ t .Fatalf ("expected %v, got %v: %v" , s .spot , c .Animal .GetCachedValue (), result )
88+ }
5989}
6090
61- func (s * Suite ) TestAminoJSON () {
91+ func TestAminoJSON (t * testing.T ) {
92+ s := newTestFixture (t )
93+
6294 bz , err := s .cdc .MarshalJSON (s .a )
63- s .Require ().NoError (err )
95+ if err != nil {
96+ t .Fatal (err )
97+ }
6498
6599 // expect plain amino marshal to fail
66100 _ , err = s .cdc .MarshalJSON (s .b )
67- s .Require ().Error (err )
68-
101+ if err == nil {
102+ t .Fatal ("expected error" )
103+ }
69104 // expect unpack interfaces before amino marshal to succeed
70105 err = types .UnpackInterfaces (s .b , types.AminoJSONPacker {Cdc : s .cdc })
71- s .Require ().NoError (err )
106+ if err != nil {
107+ t .Fatal (err )
108+ }
72109 bz2 , err := s .cdc .MarshalJSON (s .b )
73- s .Require ().NoError (err )
74- s .Require ().Equal (string (bz ), string (bz2 ))
110+ if err != nil {
111+ t .Fatal (err )
112+ }
113+ if ! bytes .Equal (bz , bz2 ) {
114+ t .Fatalf ("expected %X, got %X" , bz , bz2 )
115+ }
75116
76117 var c HasAnimal
77118 err = s .cdc .UnmarshalJSON (bz , & c )
78- s .Require ().NoError (err )
119+ if err != nil {
120+ t .Fatal (err )
121+ }
79122 err = types .UnpackInterfaces (c , types.AminoJSONUnpacker {Cdc : s .cdc })
80- s .Require ().NoError (err )
81- s .Require ().Equal (s .spot , c .Animal .GetCachedValue ())
123+ if err != nil {
124+ t .Fatal (err )
125+ }
126+
127+ if result := cmp .Diff (s .spot , c .Animal .GetCachedValue ()); result != "" {
128+ t .Fatalf ("expected %v, got %v: %v" , s .spot , c .Animal .GetCachedValue (), result )
129+ }
82130}
83131
84- func (s * Suite ) TestNested () {
132+ func TestNested (t * testing.T ) {
133+ s := newTestFixture (t )
134+
85135 s .cdc .RegisterInterface ((* HasAnimalI )(nil ), nil )
86136 s .cdc .RegisterInterface ((* HasHasAnimalI )(nil ), nil )
87137 s .cdc .RegisterConcrete (& HasAnimal {}, "test/HasAnimal" , nil )
88138 s .cdc .RegisterConcrete (& HasHasAnimal {}, "test/HasHasAnimal" , nil )
89139 s .cdc .RegisterConcrete (& HasHasHasAnimal {}, "test/HasHasHasAnimal" , nil )
90140
91141 any , err := types .NewAnyWithCacheWithValue (& s .b )
92- s .Require ().NoError (err )
142+ if err != nil {
143+ t .Fatal (err )
144+ }
93145 hha := HasHasAnimal {HasAnimal : any }
94146 any2 , err := types .NewAnyWithCacheWithValue (& hha )
95- s .Require ().NoError (err )
147+ if err != nil {
148+ t .Fatal (err )
149+ }
96150 hhha := HasHasHasAnimal {HasHasAnimal : any2 }
97151
98152 // marshal
99153 err = types .UnpackInterfaces (hhha , types.AminoPacker {Cdc : s .cdc })
100- s .Require ().NoError (err )
154+ if err != nil {
155+ t .Fatal (err )
156+ }
101157 bz , err := s .cdc .MarshalBinaryBare (hhha )
102- s .Require ().NoError (err )
158+ if err != nil {
159+ t .Fatal (err )
160+ }
103161
104162 // unmarshal
105163 var hhha2 HasHasHasAnimal
106164 err = s .cdc .UnmarshalBinaryBare (bz , & hhha2 )
107- s .Require ().NoError (err )
165+ if err != nil {
166+ t .Fatal (err )
167+ }
108168 err = types .UnpackInterfaces (hhha2 , types.AminoUnpacker {Cdc : s .cdc })
109- s .Require ().NoError (err )
169+ if err != nil {
170+ t .Fatal (err )
171+ }
110172
111- s .Require ().Equal (s .spot , hhha2 .TheHasHasAnimal ().TheHasAnimal ().TheAnimal ())
173+ if result := cmp .Diff (hhha2 .TheHasHasAnimal ().TheHasAnimal ().TheAnimal (), s .spot ); result != "" {
174+ t .Fatalf ("expected %v, got %v: %v" , s .spot , hhha2 .TheHasHasAnimal ().TheHasAnimal ().TheAnimal (), result )
175+ }
112176
113177 // json marshal
114178 err = types .UnpackInterfaces (hhha , types.AminoJSONPacker {Cdc : s .cdc })
115- s .Require ().NoError (err )
179+ if err != nil {
180+ t .Fatal (err )
181+ }
116182 jsonBz , err := s .cdc .MarshalJSON (hhha )
117- s .Require ().NoError (err )
183+ if err != nil {
184+ t .Fatal (err )
185+ }
118186
119187 // json unmarshal
120188 var hhha3 HasHasHasAnimal
121189 err = s .cdc .UnmarshalJSON (jsonBz , & hhha3 )
122- s .Require ().NoError (err )
190+ if err != nil {
191+ t .Fatal (err )
192+ }
123193 err = types .UnpackInterfaces (hhha3 , types.AminoJSONUnpacker {Cdc : s .cdc })
124- s .Require ().NoError (err )
125-
126- s .Require ().Equal (s .spot , hhha3 .TheHasHasAnimal ().TheHasAnimal ().TheAnimal ())
127- }
194+ if err != nil {
195+ t .Fatal (err )
196+ }
128197
129- func TestSuite (t * testing.T ) {
130- suite .Run (t , & Suite {})
198+ if result := cmp .Diff (hhha3 .TheHasHasAnimal ().TheHasAnimal ().TheAnimal (), s .spot ); result != "" {
199+ t .Fatalf ("expected %v, got %v: %v" , s .spot , hhha3 .TheHasHasAnimal ().TheHasAnimal ().TheAnimal (), result )
200+ }
131201}
0 commit comments