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 c4e374d
Show file tree
Hide file tree
Showing 4 changed files with 96 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 @@ -55,6 +55,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/nxadm/tail v1.4.11 // indirect
Expand Down
32 changes: 32 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,32 @@
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"),
}

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><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)
}
}

0 comments on commit c4e374d

Please sign in to comment.