Skip to content

Commit

Permalink
Add test for BlockNumber marshal and unmarshal together
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Apr 27, 2023
1 parent 6c512cd commit ebf9743
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions rpc/types_test.go
Expand Up @@ -19,6 +19,7 @@ package rpc
import (
"encoding/json"
"testing"
"reflect"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/common/math"
Expand Down Expand Up @@ -122,3 +123,33 @@ func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) {
}
}
}

func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) {
tests := []struct {
name string
number int64
}{
{"max", math.MaxInt64},
{"pending", int64(PendingBlockNumber)},
{"latest", int64(LatestBlockNumber)},
{"earliest", int64(EarliestBlockNumber)},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
bnh := BlockNumberOrHashWithNumber(BlockNumber(test.number))
marshalled, err := json.Marshal(bnh)
if err != nil {
t.Fatal("cannot marshal:", err)
}
var unmarshalled BlockNumberOrHash
err = json.Unmarshal(marshalled, &unmarshalled)
if err != nil {
t.Fatal("cannot unmarshal:", err)
}
if !reflect.DeepEqual(bnh, unmarshalled) {
t.Fatalf("wrong result: expected %v, got %v", bnh, unmarshalled)
}
})
}
}

0 comments on commit ebf9743

Please sign in to comment.