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) { func TestBlockNumberOrHash_StringAndUnmarshal(t *testing.T) {
tests := []struct { tests := []BlockNumberOrHash{
name string BlockNumberOrHashWithNumber(math.MaxInt64),
value BlockNumberOrHash BlockNumberOrHashWithNumber(PendingBlockNumber),
}{ BlockNumberOrHashWithNumber(LatestBlockNumber),
{"max", BlockNumberOrHashWithNumber(math.MaxInt64)}, BlockNumberOrHashWithNumber(EarliestBlockNumber),
{"pending", BlockNumberOrHashWithNumber(PendingBlockNumber)}, BlockNumberOrHashWithNumber(32),
{"latest", BlockNumberOrHashWithNumber(LatestBlockNumber)}, BlockNumberOrHashWithHash(common.Hash{0xaa}, false),
{"earliest", BlockNumberOrHashWithNumber(EarliestBlockNumber)},
{"0x20", BlockNumberOrHashWithNumber(32)},
{"hash", BlockNumberOrHashWithHash(common.Hash{0xaa}, false)},
} }
for _, test := range tests { for _, want := range tests {
test := test marshalled, _ := json.Marshal(want.String())
t.Run(test.name, func(t *testing.T) { var have BlockNumberOrHash
bnh := test.value if err := json.Unmarshal(marshalled, &have); err != nil {
// 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) t.Fatalf("cannot unmarshal (%v): %v", string(marshalled), err)
} }
if !reflect.DeepEqual(bnh, unmarshalled) { if !reflect.DeepEqual(want, have) {
t.Fatalf("wrong result: expected %v, got %v", bnh, unmarshalled) t.Fatalf("wrong result: have %v, want %v", have, want)
} }
})
} }
} }