Skip to content

Commit

Permalink
Improved TestNonce
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed May 27, 2024
1 parent cb90ba0 commit a58841e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
48 changes: 27 additions & 21 deletions rpc/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rpc

import (
"context"
"log"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -381,46 +380,53 @@ func TestNonce(t *testing.T) {

type testSetType struct {
ContractAddress *felt.Felt
Block BlockID
ExpectedNonce *felt.Felt
}
testSet := map[string][]testSetType{
"mock": {
{
ContractAddress: utils.TestHexToFelt(t, "0x0207acc15dc241e7d167e67e30e769719a727d3e0fa47f9e187707289885dfde"),
Block: WithBlockTag("latest"),
ExpectedNonce: utils.TestHexToFelt(t, "0xdeadbeef"),
},
},
"devnet": {
{
ContractAddress: utils.TestHexToFelt(t, "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"),
Block: WithBlockTag("latest"),
ExpectedNonce: utils.TestHexToFelt(t, "0x0"),
},
},
"testnet": {
{
ContractAddress: utils.TestHexToFelt(t, "0x00a3d19d9e80d74dd6140fed379e2c10a21609374811b244cc9d7d1f6d9e0037"),
ExpectedNonce: utils.TestHexToFelt(t, "0x0"),
ContractAddress: utils.TestHexToFelt(t, "0x0200AB5CE3D7aDE524335Dc57CaF4F821A0578BBb2eFc2166cb079a3D29cAF9A"),
Block: WithBlockNumber(69399),
ExpectedNonce: utils.TestHexToFelt(t, "0x1"),
},
},
"mainnet": {
{
ContractAddress: utils.TestHexToFelt(t, "0x00bE9AeF00Ec751Ba252A595A473315FBB8DA629850e13b8dB83d0fACC44E4f2"),
Block: WithBlockNumber(644060),
ExpectedNonce: utils.TestHexToFelt(t, "0x2"),
},
},
"mainnet": {},
}[testEnv]

for _, test := range testSet {
require := require.New(t)
spy := NewSpy(testConfig.provider.c)
testConfig.provider.c = spy
nonce, err := testConfig.provider.Nonce(context.Background(), WithBlockTag("latest"), test.ContractAddress)
if err != nil {
t.Fatal(err)
}
nonce, err := testConfig.provider.Nonce(context.Background(), test.Block, test.ContractAddress)
require.NoError(err)
require.NotNil(nonce, "should return a nonce")

diff, err := spy.Compare(nonce, false)
if err != nil {
t.Fatal("expecting to match", err)
}
if diff != "FullMatch" {
if _, err := spy.Compare(nonce, true); err != nil {
log.Fatal(err)
}
t.Fatal("structure expecting to be FullMatch, instead", diff)
}
require.NoError(err, "expecting to match")
require.Equal(diff, "FullMatch", "structure expecting to be FullMatch")

if nonce == nil {
t.Fatalf("should return a nonce, instead %v", nonce)
}
require.Equal(t, test.ExpectedNonce, nonce)
require.Equal(test.ExpectedNonce, nonce)
}
}

Expand Down
2 changes: 1 addition & 1 deletion rpc/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ func mock_starknet_getNonce(result interface{}, method string, args ...interface
fmt.Printf("args[0] should be *felt.Felt, got %T\n", args[1])
return errWrongArgs
}
output, err := utils.HexToFelt("0x0")
output, err := utils.HexToFelt("0xdeadbeef")
if err != nil {
return err
}
Expand Down

0 comments on commit a58841e

Please sign in to comment.