From 6ff956394a26fe13c774797284220b8231ebf809 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 10 Jun 2015 18:11:30 -0400 Subject: [PATCH] DRY file loading --- tests/block_test_util.go | 56 ++++++++-------------------------- tests/init.go | 32 ++++++++++++++++--- tests/state_test_util.go | 8 +---- tests/transaction_test_util.go | 2 +- tests/vm_test_util.go | 2 +- 5 files changed, 42 insertions(+), 58 deletions(-) diff --git a/tests/block_test_util.go b/tests/block_test_util.go index a04019111..7db47566b 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -3,9 +3,7 @@ package tests import ( "bytes" "encoding/hex" - "encoding/json" "fmt" - "io/ioutil" "math/big" "path/filepath" "runtime" @@ -87,9 +85,9 @@ type btTransaction struct { } func RunBlockTest(filepath string) error { - bt, err := LoadBlockTests(filepath) + bt, err := loadBlockTests(filepath) if err != nil { - return nil + return err } // map skipped tests to boolean set @@ -158,22 +156,6 @@ func testEthConfig() *eth.Config { } } -// LoadBlockTests loads a block test JSON file. -func LoadBlockTests(file string) (map[string]*BlockTest, error) { - bt := make(map[string]*btJSON) - if err := LoadJSON(file, &bt); err != nil { - return nil, err - } - out := make(map[string]*BlockTest) - for name, in := range bt { - var err error - if out[name], err = convertTest(in); err != nil { - return out, fmt.Errorf("bad test %q: %v", name, err) - } - } - return out, nil -} - // InsertPreState populates the given database with the genesis // accounts defined by the test. func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, error) { @@ -467,34 +449,20 @@ func mustConvertUint(in string, base int) uint64 { return out } -// LoadJSON reads the given file and unmarshals its content. -func LoadJSON(file string, val interface{}) error { - content, err := ioutil.ReadFile(file) - if err != nil { - return err - } - if err := json.Unmarshal(content, val); err != nil { - if syntaxerr, ok := err.(*json.SyntaxError); ok { - line := findLine(content, syntaxerr.Offset) - return fmt.Errorf("JSON syntax error at %v:%v: %v", file, line, err) - } - return fmt.Errorf("JSON unmarshal error in %v: %v", file, err) +func loadBlockTests(file string) (map[string]*BlockTest, error) { + bt := make(map[string]*btJSON) + if err := readTestFile(file, &bt); err != nil { + return nil, err } - return nil -} -// findLine returns the line number for the given offset into data. -func findLine(data []byte, offset int64) (line int) { - line = 1 - for i, r := range string(data) { - if int64(i) >= offset { - return - } - if r == '\n' { - line++ + out := make(map[string]*BlockTest) + for name, in := range bt { + var err error + if out[name], err = convertTest(in); err != nil { + return out, fmt.Errorf("bad test %q: %v", name, err) } } - return + return out, nil } // Nothing to see here, please move along... diff --git a/tests/init.go b/tests/init.go index 74d9499f1..aec06396b 100644 --- a/tests/init.go +++ b/tests/init.go @@ -2,6 +2,7 @@ package tests import ( "encoding/json" + "fmt" "io" "io/ioutil" "net/http" @@ -24,14 +25,35 @@ var ( func readJSON(reader io.Reader, value interface{}) error { data, err := ioutil.ReadAll(reader) - err = json.Unmarshal(data, &value) if err != nil { - return err + return fmt.Errorf("Error reading JSON file", err.Error()) + } + + if err = json.Unmarshal(data, &value); err != nil { + if syntaxerr, ok := err.(*json.SyntaxError); ok { + line := findLine(data, syntaxerr.Offset) + return fmt.Errorf("JSON syntax error at line %v: %v", line, err) + } + return fmt.Errorf("JSON unmarshal error: %v", err) } return nil } -func CreateHttpTests(uri string, value interface{}) error { +// findLine returns the line number for the given offset into data. +func findLine(data []byte, offset int64) (line int) { + line = 1 + for i, r := range string(data) { + if int64(i) >= offset { + return + } + if r == '\n' { + line++ + } + } + return +} + +func readHttpFile(uri string, value interface{}) error { resp, err := http.Get(uri) if err != nil { return err @@ -45,7 +67,7 @@ func CreateHttpTests(uri string, value interface{}) error { return nil } -func CreateFileTests(fn string, value interface{}) error { +func readTestFile(fn string, value interface{}) error { file, err := os.Open(fn) if err != nil { return err @@ -54,7 +76,7 @@ func CreateFileTests(fn string, value interface{}) error { err = readJSON(file, value) if err != nil { - return err + return fmt.Errorf("%s in file %s", err.Error(), fn) } return nil } diff --git a/tests/state_test_util.go b/tests/state_test_util.go index b507de47f..cd87ee75e 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -21,7 +21,7 @@ func RunStateTest(p string) error { } tests := make(map[string]VmTest) - CreateFileTests(p, &tests) + readTestFile(p, &tests) for name, test := range tests { if skipTest[name] { @@ -61,16 +61,10 @@ func RunStateTest(p string) error { ret, logs, _, _ = RunState(statedb, env, test.Transaction) // // Compare expected and actual return - // switch name { - // // the memory required for these tests (4294967297 bytes) would take too much time. - // // on 19 May 2015 decided to skip these tests their output. - // case "mload32bitBound_return", "mload32bitBound_return2": - // default: rexp := common.FromHex(test.Out) if bytes.Compare(rexp, ret) != 0 { return fmt.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) } - // } // check post state for addr, account := range test.Post { diff --git a/tests/transaction_test_util.go b/tests/transaction_test_util.go index 65e2c7591..2864257b7 100644 --- a/tests/transaction_test_util.go +++ b/tests/transaction_test_util.go @@ -37,7 +37,7 @@ func RunTransactionTests(file string) error { } bt := make(map[string]TransactionTest) - if err := LoadJSON(file, &bt); err != nil { + if err := readTestFile(file, &bt); err != nil { return err } diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index 55036ed82..28e0c3f40 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -19,7 +19,7 @@ func RunVmTest(p string) error { } tests := make(map[string]VmTest) - err := CreateFileTests(p, &tests) + err := readTestFile(p, &tests) if err != nil { return err } -- GitLab