From 52cf367a922a029d9863dd6f56a335acfb9125cf Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 7 Aug 2020 14:09:10 +0100 Subject: [PATCH] feat: add MustMarshalJSON for more DRY outputting (#211) Small change to make encoding and printing the test vector output a bit more succinct. --- tvx/schema/schema.go | 9 ++++++ tvx/suite_messages_create_actor.go | 12 ++------ tvx/suite_messages_message_application.go | 37 +++++------------------ tvx/suite_messages_paych.go | 17 +++-------- 4 files changed, 24 insertions(+), 51 deletions(-) diff --git a/tvx/schema/schema.go b/tvx/schema/schema.go index c0162df28..fba1c4f70 100644 --- a/tvx/schema/schema.go +++ b/tvx/schema/schema.go @@ -123,3 +123,12 @@ func (tv TestVector) Validate() error { } return nil } + +// MustMarshalJSON encodes the test vector to JSON and panics if it errors. +func (tv TestVector) MustMarshalJSON() []byte { + b, err := json.Marshal(&tv) + if err != nil { + panic(err) + } + return b +} diff --git a/tvx/suite_messages_create_actor.go b/tvx/suite_messages_create_actor.go index 0d9273d4a..44e4b072b 100644 --- a/tvx/suite_messages_create_actor.go +++ b/tvx/suite_messages_create_actor.go @@ -1,7 +1,7 @@ package main import ( - "encoding/json" + "fmt" "os" "github.com/hashicorp/go-multierror" @@ -119,10 +119,7 @@ func MessageTest_AccountActorCreation() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }() @@ -184,10 +181,7 @@ func MessageTest_InitActorSequentialIDAddressCreate() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil } diff --git a/tvx/suite_messages_message_application.go b/tvx/suite_messages_message_application.go index 9656dd865..4fc852860 100644 --- a/tvx/suite_messages_message_application.go +++ b/tvx/suite_messages_message_application.go @@ -1,7 +1,7 @@ package main import ( - "encoding/json" + "fmt" "os" abi_spec "github.com/filecoin-project/specs-actors/actors/abi" @@ -40,10 +40,7 @@ func MessageTest_MessageApplicationEdgecases() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("fail to cover gas cost for message receipt on chain") @@ -81,10 +78,7 @@ func MessageTest_MessageApplicationEdgecases() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("not enough gas to pay message on-chain-size cost") @@ -132,10 +126,7 @@ func MessageTest_MessageApplicationEdgecases() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("fail not enough gas to cover account actor creation") @@ -174,10 +165,7 @@ func MessageTest_MessageApplicationEdgecases() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("invalid actor nonce") @@ -246,10 +234,7 @@ func MessageTest_MessageApplicationEdgecases() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("abort during actor execution") @@ -281,10 +266,7 @@ func MessageTest_MessageApplicationEdgecases() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("invalid method for receiver") @@ -325,10 +307,7 @@ func MessageTest_MessageApplicationEdgecases() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("receiver ID/Actor address does not exist") diff --git a/tvx/suite_messages_paych.go b/tvx/suite_messages_paych.go index 3a6e9d23c..4ba512200 100644 --- a/tvx/suite_messages_paych.go +++ b/tvx/suite_messages_paych.go @@ -1,7 +1,7 @@ package main import ( - "encoding/json" + "fmt" "os" abi_spec "github.com/filecoin-project/specs-actors/actors/abi" @@ -55,10 +55,7 @@ func MessageTest_Paych() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("happy path constructor") @@ -130,10 +127,7 @@ func MessageTest_Paych() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("happy path update") @@ -208,10 +202,7 @@ func MessageTest_Paych() error { td.Vector.Post.StateTree.RootCID = postroot // encode and output - enc := json.NewEncoder(os.Stdout) - if err := enc.Encode(&td.Vector); err != nil { - return err - } + fmt.Fprintln(os.Stdout, string(td.Vector.MustMarshalJSON())) return nil }("happy path collect")