@@ -2,6 +2,7 @@ package types
22
33import (
44 "encoding/json"
5+ "reflect"
56 "testing"
67
78 "github.com/ethereum/go-ethereum/common"
@@ -44,6 +45,31 @@ func TestUnmarshalHeader(t *testing.T) {
4445 }
4546}
4647
48+ func TestMarshalHeader (t * testing.T ) {
49+ for name , test := range unmarshalHeaderTests {
50+ if test .wantError != nil {
51+ continue
52+ }
53+ var original * Header
54+ json .Unmarshal ([]byte (test .input ), & original )
55+
56+ blob , err := json .Marshal (original )
57+ if err != nil {
58+ t .Errorf ("test %q: failed to marshal header: %v" , name , err )
59+ continue
60+ }
61+ var proced * Header
62+ if err := json .Unmarshal (blob , & proced ); err != nil {
63+ t .Errorf ("Test %q: failed to unmarshal marhsalled header: %v" , name , err )
64+ continue
65+ }
66+ if ! reflect .DeepEqual (original , proced ) {
67+ t .Errorf ("test %q: header mismatch: have %+v, want %+v" , name , proced , original )
68+ continue
69+ }
70+ }
71+ }
72+
4773var unmarshalTransactionTests = map [string ]struct {
4874 input string
4975 wantHash common.Hash
@@ -94,6 +120,32 @@ func TestUnmarshalTransaction(t *testing.T) {
94120 }
95121}
96122
123+ func TestMarshalTransaction (t * testing.T ) {
124+ for name , test := range unmarshalTransactionTests {
125+ if test .wantError != nil {
126+ continue
127+ }
128+ var original * Transaction
129+ json .Unmarshal ([]byte (test .input ), & original )
130+
131+ blob , err := json .Marshal (original )
132+ if err != nil {
133+ t .Errorf ("test %q: failed to marshal transaction: %v" , name , err )
134+ continue
135+ }
136+ var proced * Transaction
137+ if err := json .Unmarshal (blob , & proced ); err != nil {
138+ t .Errorf ("Test %q: failed to unmarshal marhsalled transaction: %v" , name , err )
139+ continue
140+ }
141+ proced .Hash () // hack private fields to pass deep equal
142+ if ! reflect .DeepEqual (original , proced ) {
143+ t .Errorf ("test %q: transaction mismatch: have %+v, want %+v" , name , proced , original )
144+ continue
145+ }
146+ }
147+ }
148+
97149var unmarshalReceiptTests = map [string ]struct {
98150 input string
99151 wantError error
@@ -119,6 +171,31 @@ func TestUnmarshalReceipt(t *testing.T) {
119171 }
120172}
121173
174+ func TestMarshalReceipt (t * testing.T ) {
175+ for name , test := range unmarshalReceiptTests {
176+ if test .wantError != nil {
177+ continue
178+ }
179+ var original * Receipt
180+ json .Unmarshal ([]byte (test .input ), & original )
181+
182+ blob , err := json .Marshal (original )
183+ if err != nil {
184+ t .Errorf ("test %q: failed to marshal receipt: %v" , name , err )
185+ continue
186+ }
187+ var proced * Receipt
188+ if err := json .Unmarshal (blob , & proced ); err != nil {
189+ t .Errorf ("Test %q: failed to unmarshal marhsalled receipt: %v" , name , err )
190+ continue
191+ }
192+ if ! reflect .DeepEqual (original , proced ) {
193+ t .Errorf ("test %q: receipt mismatch: have %+v, want %+v" , name , proced , original )
194+ continue
195+ }
196+ }
197+ }
198+
122199func checkError (t * testing.T , testname string , got , want error ) bool {
123200 if got == nil {
124201 if want != nil {
0 commit comments