Skip to content

Commit

Permalink
Add tests to add coverage and silence flaky codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Jan 9, 2025
1 parent a87a2fd commit f981e89
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,10 @@ func (m *Machine) Printf(format string, args ...interface{}) {
}

func (m *Machine) String() string {
if m == nil {
return "Machine:nil"
}

// Calculate some reasonable total length to avoid reallocation
// Assuming an average length of 32 characters per string
var (
Expand Down
59 changes: 59 additions & 0 deletions gnovm/pkg/gnolang/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/store/dbadapter"
"github.com/gnolang/gno/tm2/pkg/store/iavl"
stypes "github.com/gnolang/gno/tm2/pkg/store/types"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -56,3 +57,61 @@ func TestRunMemPackageWithOverrides_revertToOld(t *testing.T) {
assert.Equal(t, StringKind, v.T.Kind())
assert.Equal(t, StringValue("1"), v.V)
}

func TestMachineString(t *testing.T) {
cases := []struct {
name string
in *Machine
want string
}{
{
"nil Machine",
nil,
"Machine:nil",
},
{
"created with defaults",
NewMachineWithOptions(MachineOptions{}),
"Machine:\n PreprocessorMode: false\n Op: []\n Values: (len: 0)\n Exprs:\n Stmts:\n Blocks:\n Blocks (other):\n Frames:\n Exceptions:\n",
},
{
"created with store and defaults",
func() *Machine {
db := memdb.NewMemDB()
baseStore := dbadapter.StoreConstructor(db, stypes.StoreOptions{})
iavlStore := iavl.StoreConstructor(db, stypes.StoreOptions{})
store := NewStore(nil, baseStore, iavlStore)
return NewMachine("std", store)
}(),
"Machine:\n PreprocessorMode: false\n Op: []\n Values: (len: 0)\n Exprs:\n Stmts:\n Blocks:\n Blocks (other):\n Frames:\n Exceptions:\n",
},
{
"filled in",
func() *Machine {
db := memdb.NewMemDB()
baseStore := dbadapter.StoreConstructor(db, stypes.StoreOptions{})
iavlStore := iavl.StoreConstructor(db, stypes.StoreOptions{})
store := NewStore(nil, baseStore, iavlStore)
m := NewMachine("std", store)
m.PushOp(OpHalt)
m.PushExpr(&BasicLitExpr{
Kind: INT,
Value: "100",
})
m.Blocks = make([]*Block, 1, 1)
m.PushStmts(S(Call(X("Redecl"), 11)))
return m
}(),
"Machine:\n PreprocessorMode: false\n Op: [OpHalt]\n Values: (len: 0)\n Exprs:\n #0 100\n Stmts:\n #0 Redecl<VPUverse(0)>(11)\n Blocks:\n Blocks (other):\n Frames:\n Exceptions:\n",
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got := tt.in.String()
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Fatalf("Mismatch: got - want +\n%s", diff)
}
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0
github.com/fortytw2/leaktest v1.3.0
github.com/google/go-cmp v0.6.0
github.com/google/gofuzz v1.2.0
github.com/gorilla/websocket v1.5.3
github.com/libp2p/go-buffer-pool v0.1.0
Expand Down
33 changes: 33 additions & 0 deletions tm2/pkg/bft/rpc/lib/server/write_endpoints_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package rpcserver

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"

types "github.com/gnolang/gno/tm2/pkg/bft/rpc/lib/types"
)

func TestWriteListOfEndpoints(t *testing.T) {
funcMap := map[string]*RPCFunc{
"c": NewWSRPCFunc(func(ctx *types.Context, s string, i int) (string, error) { return "foo", nil }, "s,i"),
"d": {},
}

req, _ := http.NewRequest("GET", "http://localhost/", nil)
rec := httptest.NewRecorder()
writeListOfEndpoints(rec, req, funcMap)
res := rec.Result()
assert.Equal(t, res.StatusCode, 200, "Should always return 200")
blob, err := io.ReadAll(res.Body)
assert.NoError(t, err)
gotResp := string(blob)
wantResp := `<html><body><br>Available endpoints:<br><a href="//localhost/d">//localhost/d</a></br><br>Endpoints that require arguments:<br><a href="//localhost/c?s=_&i=_">//localhost/c?s=_&i=_</a></br></body></html>`
if diff := cmp.Diff(gotResp, wantResp); diff != "" {
t.Fatalf("Mismatch response: got - want +\n%s", diff)
}
}
24 changes: 24 additions & 0 deletions tm2/pkg/sdk/auth/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -105,3 +106,26 @@ func TestNewParams(t *testing.T) {
t.Errorf("NewParams() = %+v, want %+v", params, expectedParams)
}
}

func TestParamsString(t *testing.T) {
cases := []struct {
name string
params Params
want string
}{
{"blank params", Params{}, "Params: \nMaxMemoBytes: 0\nTxSigLimit: 0\nTxSizeCostPerByte: 0\nSigVerifyCostED25519: 0\nSigVerifyCostSecp256k1: 0\nGasPricesChangeCompressor: 0\nTargetGasRatio: 0\n"},
{"some values", Params{
MaxMemoBytes: 1_000_000,
TxSizeCostPerByte: 8192,
}, "Params: \nMaxMemoBytes: 1000000\nTxSigLimit: 0\nTxSizeCostPerByte: 8192\nSigVerifyCostED25519: 0\nSigVerifyCostSecp256k1: 0\nGasPricesChangeCompressor: 0\nTargetGasRatio: 0\n"},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got := tt.params.String()
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Fatalf("Mismatch: got - want +\n%s", diff)
}
})
}
}

0 comments on commit f981e89

Please sign in to comment.