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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -6,18 +6,15 @@ import (
"github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGenesisBlockMainNet(t *testing.T) {
cfg, err := config.Load("../../config", config.ModeMainNet)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
block, err := createGenesisBlock(cfg.ProtocolConfiguration)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
expect := "d42561e3d30e15be6400b6df2f328e02d2bf6354c41dce433bc57687c82144bf"
assert.Equal(t, expect, block.Hash().StringLE())
@ -30,19 +27,13 @@ func TestGetConsensusAddressMainNet(t *testing.T) {
)
cfg, err := config.Load("../../config", config.ModeMainNet)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
validators, err := getValidators(cfg.ProtocolConfiguration)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
script, err := getNextConsensusAddress(validators)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
assert.Equal(t, consensusScript, script.String())
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)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
assert.Equal(t, result, merkle.Root().StringLE())
assert.Equal(t, true, merkle.root.IsRoot())
assert.Equal(t, false, merkle.root.IsLeaf())

View file

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

View file

@ -29,9 +29,7 @@ func TestSendVersion(t *testing.T) {
assert.Equal(t, uint32(0), version.StartHeight)
}
if err := p.SendVersion(); err != nil {
t.Fatal(err)
}
require.NoError(t, p.SendVersion())
}
// 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)
if err := s.handleVersionCmd(p, version); err != nil {
t.Fatal(err)
}
require.NoError(t, s.handleVersionCmd(p, version))
}
// 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/vm/opcode"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateMultiSigRedeemScript(t *testing.T) {
@ -17,18 +18,14 @@ func TestCreateMultiSigRedeemScript(t *testing.T) {
validators := []*keys.PublicKey{val1, val2, val3}
out, err := CreateMultiSigRedeemScript(3, validators)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
br := io.NewBinReaderFromBuf(out)
assert.Equal(t, opcode.PUSH3, opcode.Opcode(br.ReadB()))
for i := 0; i < len(validators); i++ {
bb := br.ReadVarBytes()
if br.Err != nil {
t.Fatal(err)
}
require.NoError(t, br.Err)
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) {
data, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// 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 {
@ -134,9 +132,7 @@ func testFile(t *testing.T, filename string) {
}
ut := new(vmUT)
if err = json.Unmarshal(data, ut); err != nil {
t.Fatal(err)
}
require.NoError(t, json.Unmarshal(data, ut))
t.Run(ut.Category+":"+ut.Name, func(t *testing.T) {
for i := range ut.Tests {

View file

@ -1694,9 +1694,7 @@ func TestSimpleCall(t *testing.T) {
result := 12
prog, err := hex.DecodeString(progStr)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
vm := load(prog)
runVM(t, vm)
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) {
if want, have := tk.Address, acc.Address; want != have {
t.Fatalf("expected %s got %s", want, have)
}
if want, have := tk.Wif, acc.wif; want != have {
t.Fatalf("expected %s got %s", want, have)
}
if want, have := tk.PublicKey, hex.EncodeToString(acc.publicKey); want != have {
t.Fatalf("expected %s got %s", want, have)
}
if want, have := tk.PrivateKey, acc.privateKey.String(); want != have {
t.Fatalf("expected %s got %s", want, have)
}
want, have := tk.Address, acc.Address
require.Equalf(t, want, have, "expected %s got %s", want, have)
want, have = tk.Wif, acc.wif
require.Equalf(t, want, have, "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)
want, have = tk.PrivateKey, acc.privateKey.String()
require.Equalf(t, want, have, "expected %s got %s", want, have)
}