From a3dacd3b7465cb156d18f0353ea4a5227a89dc57 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 29 Feb 2020 18:55:16 +0300 Subject: [PATCH] tests: replace t.Fatal with require where possible This makes tests less verbose and unifies the style they are written in. --- integration/performance_test.go | 9 ++---- pkg/compiler/compiler_test.go | 23 ++++++--------- pkg/compiler/vm_test.go | 4 +-- pkg/core/blockchain_test.go | 9 ++---- pkg/core/helper_test.go | 40 +++++++-------------------- pkg/core/transaction/register_test.go | 5 ++-- pkg/core/util_test.go | 21 ++++---------- pkg/crypto/hash/merkle_tree_test.go | 4 +-- pkg/encoding/address/address_test.go | 8 ++---- pkg/network/server_test.go | 8 ++---- pkg/smartcontract/contract_test.go | 9 ++---- pkg/vm/json_test.go | 8 ++---- pkg/vm/vm_test.go | 4 +-- pkg/wallet/account_test.go | 20 ++++++-------- 14 files changed, 52 insertions(+), 120 deletions(-) diff --git a/integration/performance_test.go b/integration/performance_test.go index f3b63be2f..e75e45087 100644 --- a/integration/performance_test.go +++ b/integration/performance_test.go @@ -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() } diff --git a/pkg/compiler/compiler_test.go b/pkg/compiler/compiler_test.go index 0f3903860..94d270228 100644 --- a/pkg/compiler/compiler_test.go +++ b/pkg/compiler/compiler_test.go @@ -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) } } diff --git a/pkg/compiler/vm_test.go b/pkg/compiler/vm_test.go index 4e0312c03..3e4780279 100644 --- a/pkg/compiler/vm_test.go +++ b/pkg/compiler/vm_test.go @@ -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 } diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index 53ceba287..523f3b448 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -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()) } diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 11790ab25..78cd7bb34 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -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) } } diff --git a/pkg/core/transaction/register_test.go b/pkg/core/transaction/register_test.go index ba073bbf5..4381cb4e8 100644 --- a/pkg/core/transaction/register_test.go +++ b/pkg/core/transaction/register_test.go @@ -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) diff --git a/pkg/core/util_test.go b/pkg/core/util_test.go index aaa39ae63..e4498c35c 100644 --- a/pkg/core/util_test.go +++ b/pkg/core/util_test.go @@ -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)) diff --git a/pkg/crypto/hash/merkle_tree_test.go b/pkg/crypto/hash/merkle_tree_test.go index 9afd15b1c..f7cd88dc5 100644 --- a/pkg/crypto/hash/merkle_tree_test.go +++ b/pkg/crypto/hash/merkle_tree_test.go @@ -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()) diff --git a/pkg/encoding/address/address_test.go b/pkg/encoding/address/address_test.go index db7d24c03..20483ed59 100644 --- a/pkg/encoding/address/address_test.go +++ b/pkg/encoding/address/address_test.go @@ -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()) diff --git a/pkg/network/server_test.go b/pkg/network/server_test.go index f5dede1bd..99828f63b 100644 --- a/pkg/network/server_test.go +++ b/pkg/network/server_test.go @@ -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 diff --git a/pkg/smartcontract/contract_test.go b/pkg/smartcontract/contract_test.go index 756d8ac31..dbebbdab2 100644 --- a/pkg/smartcontract/contract_test.go +++ b/pkg/smartcontract/contract_test.go @@ -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) } diff --git a/pkg/vm/json_test.go b/pkg/vm/json_test.go index 9216d75be..c8ba85e35 100644 --- a/pkg/vm/json_test.go +++ b/pkg/vm/json_test.go @@ -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 { diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index ff0116157..7b4b9e225 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -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())) diff --git a/pkg/wallet/account_test.go b/pkg/wallet/account_test.go index 654566c1e..1a8e0330a 100644 --- a/pkg/wallet/account_test.go +++ b/pkg/wallet/account_test.go @@ -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) }