@@ -2,7 +2,11 @@ package ledger
22
33import (
44 "encoding/hex"
5+ "encoding/json"
56 "testing"
7+
8+ "github.com/blinklabs-io/gouroboros/cbor"
9+ "github.com/blinklabs-io/gouroboros/internal/test"
610)
711
812func TestAssetFingerprint (t * testing.T ) {
@@ -43,3 +47,59 @@ func TestAssetFingerprint(t *testing.T) {
4347 }
4448 }
4549}
50+
51+ func TestMultiAssetJson (t * testing.T ) {
52+ testDefs := []struct {
53+ multiAssetObj interface {}
54+ expectedJson string
55+ }{
56+ {
57+ multiAssetObj : MultiAsset [MultiAssetTypeOutput ]{
58+ data : map [Blake2b224 ]map [cbor.ByteString ]MultiAssetTypeOutput {
59+ NewBlake2b224 (test .DecodeHexString ("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61" )): {
60+ cbor .ByteString (test .DecodeHexString ("6675726e697368613239686e" )): 123456 ,
61+ },
62+ },
63+ },
64+ expectedJson : `[{"name":"furnisha29hn","nameHex":"6675726e697368613239686e","policyId":"29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61","fingerprint":"asset1jdu2xcrwlqsjqqjger6kj2szddz8dcpvcg4ksz","amount":123456}]` ,
65+ },
66+ {
67+ multiAssetObj : MultiAsset [MultiAssetTypeOutput ]{
68+ data : map [Blake2b224 ]map [cbor.ByteString ]MultiAssetTypeOutput {
69+ NewBlake2b224 (test .DecodeHexString ("eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5" )): {
70+ cbor .ByteString (test .DecodeHexString ("426f7764757261436f6e63657074733638" )): 234567 ,
71+ },
72+ },
73+ },
74+ expectedJson : `[{"name":"BowduraConcepts68","nameHex":"426f7764757261436f6e63657074733638","policyId":"eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5","fingerprint":"asset1kp7hdhqc7chmyqvtqrsljfdrdt6jz8mg5culpe","amount":234567}]` ,
75+ },
76+ {
77+ multiAssetObj : MultiAsset [MultiAssetTypeOutput ]{
78+ data : map [Blake2b224 ]map [cbor.ByteString ]MultiAssetTypeOutput {
79+ NewBlake2b224 (test .DecodeHexString ("cf78aeb9736e8aa94ce8fab44da86b522fa9b1c56336b92a28420525" )): {
80+ cbor .ByteString (test .DecodeHexString ("363438346330393264363164373033656236333233346461" )): 12345678 ,
81+ },
82+ },
83+ },
84+ expectedJson : `[{"name":"6484c092d61d703eb63234da","nameHex":"363438346330393264363164373033656236333233346461","policyId":"cf78aeb9736e8aa94ce8fab44da86b522fa9b1c56336b92a28420525","fingerprint":"asset1rx3cnlsvh3udka56wyqyed3u695zd5q2jck2yd","amount":12345678}]` ,
85+ },
86+ }
87+ for _ , test := range testDefs {
88+ var err error
89+ var jsonData []byte
90+ switch v := test .multiAssetObj .(type ) {
91+ case MultiAsset [MultiAssetTypeOutput ]:
92+ jsonData , err = json .Marshal (& v )
93+ case MultiAsset [MultiAssetTypeMint ]:
94+ jsonData , err = json .Marshal (& v )
95+ default :
96+ t .Fatalf ("unexpected test object type: %T" , test .multiAssetObj )
97+ }
98+ if err != nil {
99+ t .Fatalf ("failed to marshal MultiAsset object into JSON: %s" , err )
100+ }
101+ if string (jsonData ) != test .expectedJson {
102+ t .Fatalf ("MultiAsset object did not marshal into expected JSON\n got: %s\n wanted: %s" , jsonData , test .expectedJson )
103+ }
104+ }
105+ }
0 commit comments