ethclient: Simplify new test to match typical style.

This commit is contained in:
Adrian Sutton 2023-10-18 08:58:52 +10:00
parent 488f52565c
commit dc81db030d
No known key found for this signature in database
GPG key ID: ECD2E8EEAF06B1AA

View file

@ -154,32 +154,23 @@ func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) {
}
}
func TestBlockNumberOrHash_WithNumber_StringAndUnmarshal(t *testing.T) {
tests := []struct {
name string
value BlockNumberOrHash
}{
{"max", BlockNumberOrHashWithNumber(math.MaxInt64)},
{"pending", BlockNumberOrHashWithNumber(PendingBlockNumber)},
{"latest", BlockNumberOrHashWithNumber(LatestBlockNumber)},
{"earliest", BlockNumberOrHashWithNumber(EarliestBlockNumber)},
{"0x20", BlockNumberOrHashWithNumber(32)},
{"hash", BlockNumberOrHashWithHash(common.Hash{0xaa}, false)},
func TestBlockNumberOrHash_StringAndUnmarshal(t *testing.T) {
tests := []BlockNumberOrHash{
BlockNumberOrHashWithNumber(math.MaxInt64),
BlockNumberOrHashWithNumber(PendingBlockNumber),
BlockNumberOrHashWithNumber(LatestBlockNumber),
BlockNumberOrHashWithNumber(EarliestBlockNumber),
BlockNumberOrHashWithNumber(32),
BlockNumberOrHashWithHash(common.Hash{0xaa}, false),
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
bnh := test.value
// Wrap the string value in quotes to make it a JSON string.
marshalled := []byte("\"" + bnh.String() + "\"")
var unmarshalled BlockNumberOrHash
err := json.Unmarshal(marshalled, &unmarshalled)
if err != nil {
t.Fatalf("cannot unmarshal (%v): %v", string(marshalled), err)
}
if !reflect.DeepEqual(bnh, unmarshalled) {
t.Fatalf("wrong result: expected %v, got %v", bnh, unmarshalled)
}
})
for _, want := range tests {
marshalled, _ := json.Marshal(want.String())
var have BlockNumberOrHash
if err := json.Unmarshal(marshalled, &have); err != nil {
t.Fatalf("cannot unmarshal (%v): %v", string(marshalled), err)
}
if !reflect.DeepEqual(want, have) {
t.Fatalf("wrong result: have %v, want %v", have, want)
}
}
}