supply test compares output as JSON

This commit is contained in:
Chris Ziogas 2024-05-30 14:29:05 +03:00
parent a2639b2fde
commit dec5d3b6d1
No known key found for this signature in database
GPG key ID: 2329CF479E36E2DA

View file

@ -18,13 +18,13 @@ package tracetest
import ( import (
"bufio" "bufio"
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math/big" "math/big"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"reflect"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -92,9 +92,7 @@ func TestSupplyGenesisAlloc(t *testing.T) {
actual := out[expected.Number] actual := out[expected.Number]
if !reflect.DeepEqual(actual, expected) { compareAsJSON(t, expected, actual)
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
}
} }
func TestSupplyRewards(t *testing.T) { func TestSupplyRewards(t *testing.T) {
@ -123,9 +121,7 @@ func TestSupplyRewards(t *testing.T) {
actual := out[expected.Number] actual := out[expected.Number]
if !reflect.DeepEqual(actual, expected) { compareAsJSON(t, expected, actual)
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
}
} }
func TestSupplyEip1559Burn(t *testing.T) { func TestSupplyEip1559Burn(t *testing.T) {
@ -185,9 +181,7 @@ func TestSupplyEip1559Burn(t *testing.T) {
) )
actual := out[expected.Number] actual := out[expected.Number]
if !reflect.DeepEqual(actual, expected) { compareAsJSON(t, expected, actual)
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
}
} }
func TestSupplyWithdrawals(t *testing.T) { func TestSupplyWithdrawals(t *testing.T) {
@ -227,9 +221,7 @@ func TestSupplyWithdrawals(t *testing.T) {
actual = out[expected.Number] actual = out[expected.Number]
) )
if !reflect.DeepEqual(actual, expected) { compareAsJSON(t, expected, actual)
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
}
} }
// Tests fund retrieval after contract's selfdestruct. // Tests fund retrieval after contract's selfdestruct.
@ -325,9 +317,7 @@ func TestSupplySelfdestruct(t *testing.T) {
actual := preCancunOutput[expected.Number] actual := preCancunOutput[expected.Number]
if !reflect.DeepEqual(actual, expected) { compareAsJSON(t, expected, actual)
t.Fatalf("Pre-cancun incorrect supply info: expected %+v, got %+v", expected, actual)
}
// 2. Test post Cancun // 2. Test post Cancun
cancunTime := uint64(0) cancunTime := uint64(0)
@ -368,9 +358,7 @@ func TestSupplySelfdestruct(t *testing.T) {
actual = postCancunOutput[expected.Number] actual = postCancunOutput[expected.Number]
if !reflect.DeepEqual(actual, expected) { compareAsJSON(t, expected, actual)
t.Fatalf("Post-cancun incorrect supply info: expected %+v, got %+v", expected, actual)
}
} }
// Tests selfdestructing contract to send its balance to itself (burn). // Tests selfdestructing contract to send its balance to itself (burn).
@ -518,9 +506,7 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) {
actual := output[expected.Number] actual := output[expected.Number]
if !reflect.DeepEqual(actual, expected) { compareAsJSON(t, expected, actual)
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
}
} }
func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, *core.BlockChain, error) { func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, *core.BlockChain, error) {
@ -575,3 +561,19 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG
return output, chain, nil return output, chain, nil
} }
func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) {
want, err := json.Marshal(expected)
if err != nil {
t.Fatalf("failed to marshal expected value to JSON: %v", err)
}
have, err := json.Marshal(actual)
if err != nil {
t.Fatalf("failed to marshal actual value to JSON: %v", err)
}
if !bytes.Equal(want, have) {
t.Fatalf("incorrect supply info: expected %s, got %s", string(want), string(have))
}
}