tests: replace t.Fatal with require where possible

This makes tests less verbose and unifies the style
they are written in.
This commit is contained in:
Evgenii Stratonikov 2020-02-29 18:55:16 +03:00
parent 66f96e3f32
commit a3dacd3b74
14 changed files with 52 additions and 120 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/encoding/address" "github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/network" "github.com/CityOfZion/neo-go/pkg/network"
"github.com/CityOfZion/neo-go/pkg/rpc/request" "github.com/CityOfZion/neo-go/pkg/rpc/request"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
) )
@ -38,12 +39,8 @@ func BenchmarkTXPerformanceTest(t *testing.B) {
t.ResetTimer() t.ResetTimer()
for n := 0; n < t.N; n++ { for n := 0; n < t.N; n++ {
if server.RelayTxn(data[n]) != network.RelaySucceed { assert.Equal(t, network.RelaySucceed, server.RelayTxn(data[n]))
t.Fail() assert.Equal(t, network.RelayAlreadyExists, server.RelayTxn(data[n]))
}
if server.RelayTxn(data[n]) != network.RelayAlreadyExists {
t.Fail()
}
} }
chain.Close() chain.Close()
} }

View file

@ -16,42 +16,37 @@ const exampleSavePath = exampleCompilePath + "/save"
type compilerTestCase struct { type compilerTestCase struct {
name string name string
function func() function func(*testing.T)
} }
func TestCompiler(t *testing.T) { func TestCompiler(t *testing.T) {
testCases := []compilerTestCase{ testCases := []compilerTestCase{
{ {
name: "TestCompile", name: "TestCompile",
function: func() { function: func(t *testing.T) {
infos, err := ioutil.ReadDir(examplePath) infos, err := ioutil.ReadDir(examplePath)
require.NoError(t, err) require.NoError(t, err)
for _, info := range infos { for _, info := range infos {
infos, err := ioutil.ReadDir(path.Join(examplePath, info.Name())) infos, err := ioutil.ReadDir(path.Join(examplePath, info.Name()))
require.NoError(t, err) require.NoError(t, err)
if len(infos) == 0 { require.False(t, len(infos) == 0, "detected smart contract folder with no contract in it")
t.Fatal("detected smart contract folder with no contract in it")
}
filename := filterFilename(infos) filename := filterFilename(infos)
targetPath := path.Join(examplePath, info.Name(), filename) targetPath := path.Join(examplePath, info.Name(), filename)
if err := compileFile(targetPath); err != nil { require.NoError(t, compileFile(targetPath))
t.Fatal(err)
}
} }
}, },
}, },
{ {
name: "TestCompileAndSave", name: "TestCompileAndSave",
function: func() { function: func(t *testing.T) {
infos, err := ioutil.ReadDir(exampleCompilePath) infos, err := ioutil.ReadDir(exampleCompilePath)
require.NoError(t, err) require.NoError(t, err)
err = os.MkdirAll(exampleSavePath, os.ModePerm) err = os.MkdirAll(exampleSavePath, os.ModePerm)
require.NoError(t, err) require.NoError(t, err)
outfile := exampleSavePath + "/test.avm" outfile := exampleSavePath + "/test.avm"
if _, err := compiler.CompileAndSave(exampleCompilePath+"/"+infos[0].Name(), &compiler.Options{Outfile: outfile}); err != nil { _, err = compiler.CompileAndSave(exampleCompilePath+"/"+infos[0].Name(), &compiler.Options{Outfile: outfile})
t.Fatal(err) require.NoError(t, err)
}
defer func() { defer func() {
err := os.RemoveAll(exampleSavePath) err := os.RemoveAll(exampleSavePath)
require.NoError(t, err) require.NoError(t, err)
@ -61,9 +56,7 @@ func TestCompiler(t *testing.T) {
} }
for _, tcase := range testCases { for _, tcase := range testCases {
t.Run(tcase.name, func(t *testing.T) { t.Run(tcase.name, tcase.function)
tcase.function()
})
} }
} }

View file

@ -51,9 +51,7 @@ func vmAndCompile(t *testing.T, src string) *vm.VM {
vm.RegisterInteropGetter(storePlugin.getInterop) vm.RegisterInteropGetter(storePlugin.getInterop)
b, err := compiler.Compile(strings.NewReader(src)) b, err := compiler.Compile(strings.NewReader(src))
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
vm.Load(b) vm.Load(b)
return vm return vm
} }

View file

@ -67,9 +67,8 @@ func TestAddBlock(t *testing.T) {
for _, block := range blocks { for _, block := range blocks {
key := storage.AppendPrefix(storage.DataBlock, block.Hash().BytesLE()) key := storage.AppendPrefix(storage.DataBlock, block.Hash().BytesLE())
if _, err := bc.dao.store.Get(key); err != nil { _, err := bc.dao.store.Get(key)
t.Fatalf("block %s not persisted", block.Hash()) require.NoErrorf(t, err, "block %s not persisted", block.Hash())
}
} }
assert.Equal(t, lastBlock.Index, bc.BlockHeight()) assert.Equal(t, lastBlock.Index, bc.BlockHeight())
@ -126,9 +125,7 @@ func TestGetBlock(t *testing.T) {
for j := 0; j < 2; j++ { for j := 0; j < 2; j++ {
for i := 0; i < len(blocks); i++ { for i := 0; i < len(blocks); i++ {
block, err := bc.GetBlock(blocks[i].Hash()) block, err := bc.GetBlock(blocks[i].Hash())
if err != nil { require.NoErrorf(t, err, "can't get block %d: %s, attempt %d", i, err, j)
t.Fatalf("can't get block %d: %s, attempt %d", i, err, j)
}
assert.Equal(t, blocks[i].Index, block.Index) assert.Equal(t, blocks[i].Index, block.Index)
assert.Equal(t, blocks[i].Hash(), block.Hash()) assert.Equal(t, blocks[i].Hash(), block.Hash())
} }

View file

@ -35,13 +35,9 @@ var privNetKeys = []string{
// global state. // global state.
func newTestChain(t *testing.T) *Blockchain { func newTestChain(t *testing.T) *Blockchain {
unitTestNetCfg, err := config.Load("../../config", config.ModeUnitTestNet) unitTestNetCfg, err := config.Load("../../config", config.ModeUnitTestNet)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
chain, err := NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t)) chain, err := NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t))
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
go chain.Run() go chain.Run()
return chain return chain
} }
@ -115,21 +111,15 @@ func newMinerTX() *transaction.Transaction {
func getDecodedBlock(t *testing.T, i int) *block.Block { func getDecodedBlock(t *testing.T, i int) *block.Block {
data, err := getBlockData(i) data, err := getBlockData(i)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
b, err := hex.DecodeString(data["raw"].(string)) b, err := hex.DecodeString(data["raw"].(string))
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
block := &block.Block{} block := &block.Block{}
r := io.NewBinReaderFromBuf(b) r := io.NewBinReaderFromBuf(b)
block.DecodeBinary(r) block.DecodeBinary(r)
if r.Err != nil { require.NoError(t, r.Err)
t.Fatal(r.Err)
}
return block return block
} }
@ -183,9 +173,7 @@ func _(t *testing.T) {
tx1 := newMinerTX() tx1 := newMinerTX()
avm, err := ioutil.ReadFile("../rpc/testdata/test_contract.avm") avm, err := ioutil.ReadFile("../rpc/testdata/test_contract.avm")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
var props smartcontract.PropertyState var props smartcontract.PropertyState
script := io.NewBufBinWriter() script := io.NewBufBinWriter()
@ -207,9 +195,7 @@ func _(t *testing.T) {
tx2 := transaction.NewInvocationTX(txScript, util.Fixed8FromFloat(100)) tx2 := transaction.NewInvocationTX(txScript, util.Fixed8FromFloat(100))
block := bc.newBlock(tx1, tx2) block := bc.newBlock(tx1, tx2)
if err := bc.AddBlock(block); err != nil { require.NoError(t, bc.AddBlock(block))
t.Fatal(err)
}
script = io.NewBufBinWriter() script = io.NewBufBinWriter()
emit.String(script.BinWriter, "testvalue") emit.String(script.BinWriter, "testvalue")
@ -224,9 +210,7 @@ func _(t *testing.T) {
require.NoError(t, bc.AddBlock(b)) require.NoError(t, bc.AddBlock(b))
outStream, err := os.Create("../rpc/testdata/testblocks.acc") outStream, err := os.Create("../rpc/testdata/testblocks.acc")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
defer outStream.Close() defer outStream.Close()
writer := io.NewBinWriterFromIO(outStream) writer := io.NewBinWriterFromIO(outStream)
@ -237,15 +221,11 @@ func _(t *testing.T) {
for i := 1; i < int(count); i++ { for i := 1; i < int(count); i++ {
bh := bc.GetHeaderHash(i) bh := bc.GetHeaderHash(i)
b, err := bc.GetBlock(bh) b, err := bc.GetBlock(bh)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
buf := io.NewBufBinWriter() buf := io.NewBufBinWriter()
b.EncodeBinary(buf.BinWriter) b.EncodeBinary(buf.BinWriter)
bytes := buf.Bytes() bytes := buf.Bytes()
writer.WriteBytes(bytes) writer.WriteBytes(bytes)
if writer.Err != nil { require.NoError(t, writer.Err)
t.Fatal(err)
}
} }
} }

View file

@ -9,6 +9,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestRegisterTX(t *testing.T) { func TestRegisterTX(t *testing.T) {
@ -43,9 +44,7 @@ func TestRegisterTX(t *testing.T) {
func TestDecodeRegisterTXFromRawString(t *testing.T) { func TestDecodeRegisterTXFromRawString(t *testing.T) {
rawTX := "400000455b7b226c616e67223a227a682d434e222c226e616d65223a22e5b08fe89a81e882a1227d2c7b226c616e67223a22656e222c226e616d65223a22416e745368617265227d5d0000c16ff28623000000da1745e9b549bd0bfa1a569971c77eba30cd5a4b00000000" rawTX := "400000455b7b226c616e67223a227a682d434e222c226e616d65223a22e5b08fe89a81e882a1227d2c7b226c616e67223a22656e222c226e616d65223a22416e745368617265227d5d0000c16ff28623000000da1745e9b549bd0bfa1a569971c77eba30cd5a4b00000000"
b, err := hex.DecodeString(rawTX) b, err := hex.DecodeString(rawTX)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
tx := &Transaction{} tx := &Transaction{}
r := io.NewBinReaderFromBuf(b) r := io.NewBinReaderFromBuf(b)

View file

@ -6,18 +6,15 @@ import (
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/encoding/address" "github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestGenesisBlockMainNet(t *testing.T) { func TestGenesisBlockMainNet(t *testing.T) {
cfg, err := config.Load("../../config", config.ModeMainNet) cfg, err := config.Load("../../config", config.ModeMainNet)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
block, err := createGenesisBlock(cfg.ProtocolConfiguration) block, err := createGenesisBlock(cfg.ProtocolConfiguration)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
expect := "d42561e3d30e15be6400b6df2f328e02d2bf6354c41dce433bc57687c82144bf" expect := "d42561e3d30e15be6400b6df2f328e02d2bf6354c41dce433bc57687c82144bf"
assert.Equal(t, expect, block.Hash().StringLE()) assert.Equal(t, expect, block.Hash().StringLE())
@ -30,19 +27,13 @@ func TestGetConsensusAddressMainNet(t *testing.T) {
) )
cfg, err := config.Load("../../config", config.ModeMainNet) cfg, err := config.Load("../../config", config.ModeMainNet)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
validators, err := getValidators(cfg.ProtocolConfiguration) validators, err := getValidators(cfg.ProtocolConfiguration)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
script, err := getNextConsensusAddress(validators) script, err := getNextConsensusAddress(validators)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
assert.Equal(t, consensusScript, script.String()) assert.Equal(t, consensusScript, script.String())
assert.Equal(t, consensusAddr, address.Uint160ToString(script)) assert.Equal(t, consensusAddr, address.Uint160ToString(script))

View file

@ -17,9 +17,7 @@ func testComputeMerkleTree(t *testing.T, hexHashes []string, result string) {
} }
merkle, err := NewMerkleTree(hashes) merkle, err := NewMerkleTree(hashes)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
assert.Equal(t, result, merkle.Root().StringLE()) assert.Equal(t, result, merkle.Root().StringLE())
assert.Equal(t, true, merkle.root.IsRoot()) assert.Equal(t, true, merkle.root.IsRoot())
assert.Equal(t, false, merkle.root.IsLeaf()) assert.Equal(t, false, merkle.root.IsLeaf())

View file

@ -15,9 +15,7 @@ func TestUint160DecodeEncodeAddress(t *testing.T) {
} }
for _, addr := range addrs { for _, addr := range addrs {
val, err := StringToUint160(addr) val, err := StringToUint160(addr)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
assert.Equal(t, addr, Uint160ToString(val)) assert.Equal(t, addr, Uint160ToString(val))
} }
} }
@ -26,9 +24,7 @@ func TestUint160DecodeKnownAddress(t *testing.T) {
address := "AJeAEsmeD6t279Dx4n2HWdUvUmmXQ4iJvP" address := "AJeAEsmeD6t279Dx4n2HWdUvUmmXQ4iJvP"
val, err := StringToUint160(address) val, err := StringToUint160(address)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
assert.Equal(t, "b28427088a3729b2536d10122960394e8be6721f", val.StringLE()) assert.Equal(t, "b28427088a3729b2536d10122960394e8be6721f", val.StringLE())
assert.Equal(t, "1f72e68b4e39602912106d53b229378a082784b2", val.String()) assert.Equal(t, "1f72e68b4e39602912106d53b229378a082784b2", val.String())

View file

@ -29,9 +29,7 @@ func TestSendVersion(t *testing.T) {
assert.Equal(t, uint32(0), version.StartHeight) assert.Equal(t, uint32(0), version.StartHeight)
} }
if err := p.SendVersion(); err != nil { require.NoError(t, p.SendVersion())
t.Fatal(err)
}
} }
// Server should reply with a verack after receiving a valid version. // Server should reply with a verack after receiving a valid version.
@ -49,9 +47,7 @@ func TestVerackAfterHandleVersionCmd(t *testing.T) {
} }
version := payload.NewVersion(1337, 3000, "/NEO-GO/", 0, true) version := payload.NewVersion(1337, 3000, "/NEO-GO/", 0, true)
if err := s.handleVersionCmd(p, version); err != nil { require.NoError(t, s.handleVersionCmd(p, version))
t.Fatal(err)
}
} }
// Server should not reply with a verack after receiving a // Server should not reply with a verack after receiving a

View file

@ -7,6 +7,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/vm/opcode" "github.com/CityOfZion/neo-go/pkg/vm/opcode"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestCreateMultiSigRedeemScript(t *testing.T) { func TestCreateMultiSigRedeemScript(t *testing.T) {
@ -17,18 +18,14 @@ func TestCreateMultiSigRedeemScript(t *testing.T) {
validators := []*keys.PublicKey{val1, val2, val3} validators := []*keys.PublicKey{val1, val2, val3}
out, err := CreateMultiSigRedeemScript(3, validators) out, err := CreateMultiSigRedeemScript(3, validators)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
br := io.NewBinReaderFromBuf(out) br := io.NewBinReaderFromBuf(out)
assert.Equal(t, opcode.PUSH3, opcode.Opcode(br.ReadB())) assert.Equal(t, opcode.PUSH3, opcode.Opcode(br.ReadB()))
for i := 0; i < len(validators); i++ { for i := 0; i < len(validators); i++ {
bb := br.ReadVarBytes() bb := br.ReadVarBytes()
if br.Err != nil { require.NoError(t, br.Err)
t.Fatal(err)
}
assert.Equal(t, validators[i].Bytes(), bb) assert.Equal(t, validators[i].Bytes(), bb)
} }

View file

@ -124,9 +124,7 @@ func getTestingInterop(id uint32) *InteropFuncPrice {
func testFile(t *testing.T, filename string) { func testFile(t *testing.T, filename string) {
data, err := ioutil.ReadFile(filename) data, err := ioutil.ReadFile(filename)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
// FIXME remove when NEO 3.0 https://github.com/nspcc-dev/neo-go/issues/477 // FIXME remove when NEO 3.0 https://github.com/nspcc-dev/neo-go/issues/477
if len(data) > 2 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf { if len(data) > 2 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf {
@ -134,9 +132,7 @@ func testFile(t *testing.T, filename string) {
} }
ut := new(vmUT) ut := new(vmUT)
if err = json.Unmarshal(data, ut); err != nil { require.NoError(t, json.Unmarshal(data, ut))
t.Fatal(err)
}
t.Run(ut.Category+":"+ut.Name, func(t *testing.T) { t.Run(ut.Category+":"+ut.Name, func(t *testing.T) {
for i := range ut.Tests { for i := range ut.Tests {

View file

@ -1694,9 +1694,7 @@ func TestSimpleCall(t *testing.T) {
result := 12 result := 12
prog, err := hex.DecodeString(progStr) prog, err := hex.DecodeString(progStr)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
vm := load(prog) vm := load(prog)
runVM(t, vm) runVM(t, vm)
assert.Equal(t, result, int(vm.estack.Pop().BigInt().Int64())) assert.Equal(t, result, int(vm.estack.Pop().BigInt().Int64()))

View file

@ -134,16 +134,12 @@ func convertPubs(t *testing.T, hexKeys []string) []*keys.PublicKey {
} }
func compareFields(t *testing.T, tk keytestcases.Ktype, acc *Account) { func compareFields(t *testing.T, tk keytestcases.Ktype, acc *Account) {
if want, have := tk.Address, acc.Address; want != have { want, have := tk.Address, acc.Address
t.Fatalf("expected %s got %s", want, have) require.Equalf(t, want, have, "expected %s got %s", want, have)
} want, have = tk.Wif, acc.wif
if want, have := tk.Wif, acc.wif; want != have { require.Equalf(t, want, have, "expected %s got %s", want, have)
t.Fatalf("expected %s got %s", want, have) want, have = tk.PublicKey, hex.EncodeToString(acc.publicKey)
} require.Equalf(t, want, have, "expected %s got %s", want, have)
if want, have := tk.PublicKey, hex.EncodeToString(acc.publicKey); want != have { want, have = tk.PrivateKey, acc.privateKey.String()
t.Fatalf("expected %s got %s", want, have) require.Equalf(t, want, have, "expected %s got %s", want, have)
}
if want, have := tk.PrivateKey, acc.privateKey.String(); want != have {
t.Fatalf("expected %s got %s", want, have)
}
} }