@@ -2,11 +2,13 @@ package models
22
33import (
44 "encoding/json"
5- "log "
5+ "reflect "
66 "testing"
77
88 "github.com/ethereum/go-ethereum/common/hexutil"
9+ "github.com/fxamacker/cbor/v2"
910 "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/require"
1012)
1113
1214func Test_ParseCBOR (t * testing.T ) {
@@ -21,28 +23,34 @@ func Test_ParseCBOR(t *testing.T) {
2123 {
2224 "hello world" ,
2325 `0xbf6375726c781a68747470733a2f2f657468657270726963652e636f6d2f61706964706174689f66726563656e7463757364ffff` ,
24- jsonMustUnmarshal (`{"path":["recent","usd"],"url":"https://etherprice.com/api"}` ),
26+ jsonMustUnmarshal (t , `{"path":["recent","usd"],"url":"https://etherprice.com/api"}` ),
2527 false ,
2628 },
2729 {
2830 "trailing empty bytes" ,
2931 `0xbf6375726c781a68747470733a2f2f657468657270726963652e636f6d2f61706964706174689f66726563656e7463757364ffff000000` ,
30- jsonMustUnmarshal (`{"path":["recent","usd"],"url":"https://etherprice.com/api"}` ),
32+ jsonMustUnmarshal (t , `{"path":["recent","usd"],"url":"https://etherprice.com/api"}` ),
3133 false ,
3234 },
3335 {
3436 "nested maps" ,
3537 `0xbf657461736b739f6868747470706f7374ff66706172616d73bf636d73676f68656c6c6f5f636861696e6c696e6b6375726c75687474703a2f2f6c6f63616c686f73743a36363930ffff` ,
36- jsonMustUnmarshal (`{"params":{"msg":"hello_chainlink","url":"http://localhost:6690"},"tasks":["httppost"]}` ),
38+ jsonMustUnmarshal (t , `{"params":{"msg":"hello_chainlink","url":"http://localhost:6690"},"tasks":["httppost"]}` ),
3739 false ,
3840 },
3941 {
4042 "missing initial start map marker" ,
4143 `0x636B65796576616C7565ff` ,
42- jsonMustUnmarshal (`{"key":"value"}` ),
44+ jsonMustUnmarshal (t , `{"key":"value"}` ),
4345 false ,
4446 },
45- {"empty object" , `0xa0` , jsonMustUnmarshal (`{}` ), false },
47+ {
48+ "bignums" ,
49+ `0xbf676269676e756d739fc249010000000000000000c258204000000000000000000000000000000000000000000000000000000000000000c348ffffffffffffffffc358203fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff` ,
50+ jsonMustUnmarshal (t , `{"bignums":[18446744073709551616,28948022309329048855892746252171976963317496166410141009864396001978282409984,-18446744073709551616,-28948022309329048855892746252171976963317496166410141009864396001978282409984]}` ),
51+ false ,
52+ },
53+ {"empty object" , `0xa0` , jsonMustUnmarshal (t , `{}` ), false },
4654 {"empty string" , `0x` , JSON {}, false },
4755 {"invalid CBOR" , `0xff` , JSON {}, true },
4856 }
@@ -120,11 +128,108 @@ func Test_autoAddMapDelimiters(t *testing.T) {
120128 }
121129}
122130
123- func jsonMustUnmarshal (in string ) JSON {
131+ func jsonMustUnmarshal (t * testing. T , in string ) JSON {
124132 var j JSON
125133 err := json .Unmarshal ([]byte (in ), & j )
126- if err != nil {
127- log .Panicf ("Failed to unmarshal '%s'" , in )
128- }
134+ require .NoError (t , err )
129135 return j
130136}
137+
138+ func TestCoerceInterfaceMapToStringMap (t * testing.T ) {
139+ t .Parallel ()
140+
141+ tests := []struct {
142+ name string
143+ input interface {}
144+ want interface {}
145+ }{
146+ {"empty map" , map [interface {}]interface {}{}, map [string ]interface {}{}},
147+ {"simple map" , map [interface {}]interface {}{"key" : "value" }, map [string ]interface {}{"key" : "value" }},
148+ {"int map" , map [int ]interface {}{1 : "value" }, map [int ]interface {}{1 : "value" }},
149+ {
150+ "nested string map map" ,
151+ map [string ]interface {}{"key" : map [interface {}]interface {}{"nk" : "nv" }},
152+ map [string ]interface {}{"key" : map [string ]interface {}{"nk" : "nv" }},
153+ },
154+ {
155+ "nested map map" ,
156+ map [interface {}]interface {}{"key" : map [interface {}]interface {}{"nk" : "nv" }},
157+ map [string ]interface {}{"key" : map [string ]interface {}{"nk" : "nv" }},
158+ },
159+ {
160+ "nested map array" ,
161+ map [interface {}]interface {}{"key" : []interface {}{1 , "value" }},
162+ map [string ]interface {}{"key" : []interface {}{1 , "value" }},
163+ },
164+ {"empty array" , []interface {}{}, []interface {}{}},
165+ {"simple array" , []interface {}{1 , "value" }, []interface {}{1 , "value" }},
166+ {
167+ "nested array map" ,
168+ []interface {}{map [interface {}]interface {}{"key" : map [interface {}]interface {}{"nk" : "nv" }}},
169+ []interface {}{map [string ]interface {}{"key" : map [string ]interface {}{"nk" : "nv" }}},
170+ },
171+ }
172+
173+ for _ , test := range tests {
174+ t .Run (test .name , func (t * testing.T ) {
175+ decoded , err := CoerceInterfaceMapToStringMap (test .input )
176+ require .NoError (t , err )
177+ assert .True (t , reflect .DeepEqual (test .want , decoded ))
178+ })
179+ }
180+ }
181+
182+ func TestCoerceInterfaceMapToStringMap_BadInputs (t * testing.T ) {
183+ t .Parallel ()
184+
185+ tests := []struct {
186+ name string
187+ input interface {}
188+ }{
189+ {"error map" , map [interface {}]interface {}{1 : "value" }},
190+ {"error array" , []interface {}{map [interface {}]interface {}{1 : "value" }}},
191+ }
192+
193+ for _ , test := range tests {
194+ t .Run (test .name , func (t * testing.T ) {
195+ _ , err := CoerceInterfaceMapToStringMap (test .input )
196+ assert .Error (t , err )
197+ })
198+ }
199+ }
200+
201+ func TestJSON_CBOR (t * testing.T ) {
202+ t .Parallel ()
203+
204+ tests := []struct {
205+ name string
206+ in JSON
207+ }{
208+ {"empty object" , JSON {}},
209+ {"array" , jsonMustUnmarshal (t , `[1,2,3,4]` )},
210+ {
211+ "basic object" ,
212+ jsonMustUnmarshal (t , `{"path":["recent","usd"],"url":"https://etherprice.com/api"}` ),
213+ },
214+ {
215+ "complex object" ,
216+ jsonMustUnmarshal (t , `{"a":{"1":[{"b":"free"},{"c":"more"},{"d":["less", {"nesting":{"4":"life"}}]}]}}` ),
217+ },
218+ }
219+
220+ for _ , test := range tests {
221+ t .Run (test .name , func (t * testing.T ) {
222+ encoded , err := test .in .CBOR ()
223+ assert .NoError (t , err )
224+
225+ var decoded interface {}
226+ err = cbor .Unmarshal (encoded , & decoded )
227+
228+ assert .NoError (t , err )
229+
230+ decoded , err = CoerceInterfaceMapToStringMap (decoded )
231+ assert .NoError (t , err )
232+ assert .True (t , reflect .DeepEqual (test .in .Result .Value (), decoded ))
233+ })
234+ }
235+ }
0 commit comments