DRY file loading
This commit is contained in:
parent
ac0637c413
commit
6ff956394a
@ -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)
|
||||
}
|
||||
return nil
|
||||
func loadBlockTests(file string) (map[string]*BlockTest, error) {
|
||||
bt := make(map[string]*btJSON)
|
||||
if err := readTestFile(file, &bt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 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...
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user