Skip to content

Commit

Permalink
feat: add p/demo/uassert & p/demo/urequire (#928)
Browse files Browse the repository at this point in the history
Addresses #923 
Addresses #777

---------

Signed-off-by: Manfred Touron <[email protected]>
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul authored Jul 2, 2024
1 parent 6a78488 commit 326dae7
Show file tree
Hide file tree
Showing 14 changed files with 714 additions and 53 deletions.
5 changes: 5 additions & 0 deletions examples/gno.land/p/demo/ownable/gno.mod
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
module gno.land/p/demo/ownable

require (
gno.land/p/demo/testutils v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
)
55 changes: 17 additions & 38 deletions examples/gno.land/p/demo/ownable/ownable_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package ownable
import (
"std"
"testing"

"gno.land/p/demo/testutils"
"gno.land/p/demo/uassert"
)

var (
firstCaller = std.Address("g1l9aypkr8xfvs82zeux486ddzec88ty69lue9de")
secondCaller = std.Address("g127jydsh6cms3lrtdenydxsckh23a8d6emqcvfa")
firstCaller = testutils.TestAddress("first")
secondCaller = testutils.TestAddress("second")
)

func TestNew(t *testing.T) {
Expand All @@ -16,18 +19,14 @@ func TestNew(t *testing.T) {

o := New()
got := o.Owner()
if firstCaller != got {
t.Fatalf("Expected %s, got: %s", firstCaller, got)
}
uassert.Equal(t, firstCaller, got)
}

func TestNewWithAddress(t *testing.T) {
o := NewWithAddress(firstCaller)

got := o.Owner()
if firstCaller != got {
t.Fatalf("Expected %s, got: %s", firstCaller, got)
}
uassert.Equal(t, firstCaller, got)
}

func TestOwner(t *testing.T) {
Expand All @@ -36,9 +35,7 @@ func TestOwner(t *testing.T) {
o := New()
expected := firstCaller
got := o.Owner()
if expected != got {
t.Fatalf("Expected %s, got: %s", expected, got)
}
uassert.Equal(t, expected, got)
}

func TestTransferOwnership(t *testing.T) {
Expand All @@ -47,14 +44,10 @@ func TestTransferOwnership(t *testing.T) {
o := New()

err := o.TransferOwnership(secondCaller)
if err != nil {
t.Fatalf("TransferOwnership failed, %v", err)
}
uassert.NoError(t, err, "TransferOwnership failed")

got := o.Owner()
if secondCaller != got {
t.Fatalf("Expected: %s, got: %s", secondCaller, got)
}
uassert.Equal(t, secondCaller, got)
}

func TestCallerIsOwner(t *testing.T) {
Expand All @@ -67,9 +60,7 @@ func TestCallerIsOwner(t *testing.T) {
std.TestSetOrigCaller(unauthorizedCaller) // TODO(bug): should not be needed

err := o.CallerIsOwner()
if err == nil {
t.Fatalf("Expected %s to not be owner", unauthorizedCaller)
}
uassert.Error(t, err) // XXX: IsError(..., unauthorizedCaller)
}

func TestDropOwnership(t *testing.T) {
Expand All @@ -78,14 +69,10 @@ func TestDropOwnership(t *testing.T) {
o := New()

err := o.DropOwnership()
if err != nil {
t.Fatalf("DropOwnership failed, %v", err)
}
uassert.NoError(t, err, "DropOwnership failed")

owner := o.Owner()
if owner != "" {
t.Fatalf("Expected owner to be empty, not %s", owner)
}
uassert.Empty(t, owner, "owner should be empty")
}

// Errors
Expand All @@ -100,14 +87,10 @@ func TestErrUnauthorized(t *testing.T) {
std.TestSetOrigCaller(secondCaller) // TODO(bug): should not be needed

err := o.TransferOwnership(firstCaller)
if err != ErrUnauthorized {
t.Fatalf("Should've been ErrUnauthorized, was %v", err)
}
uassert.ErrorContains(t, err, ErrUnauthorized.Error())

err = o.DropOwnership()
if err != ErrUnauthorized {
t.Fatalf("Should've been ErrUnauthorized, was %v", err)
}
uassert.ErrorContains(t, err, ErrUnauthorized.Error())
}

func TestErrInvalidAddress(t *testing.T) {
Expand All @@ -116,12 +99,8 @@ func TestErrInvalidAddress(t *testing.T) {
o := New()

err := o.TransferOwnership("")
if err != ErrInvalidAddress {
t.Fatalf("Should've been ErrInvalidAddress, was %v", err)
}
uassert.ErrorContains(t, err, ErrInvalidAddress.Error())

err = o.TransferOwnership("10000000001000000000100000000010000000001000000000")
if err != ErrInvalidAddress {
t.Fatalf("Should've been ErrInvalidAddress, was %v", err)
}
uassert.ErrorContains(t, err, ErrInvalidAddress.Error())
}
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/uassert/doc.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package uassert // import "gno.land/p/demo/uassert"
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/uassert/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/uassert
51 changes: 51 additions & 0 deletions examples/gno.land/p/demo/uassert/helpers.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uassert

import "strings"

func fail(t TestingT, customMsgs []string, failureMessage string, args ...interface{}) bool {
customMsg := ""
if len(customMsgs) > 0 {
customMsg = strings.Join(customMsgs, " ")
}
if customMsg != "" {
failureMessage += " - " + customMsg
}
t.Errorf(failureMessage, args...)
return false
}

func autofail(t TestingT, success bool, customMsgs []string, failureMessage string, args ...interface{}) bool {
if success {
return true
}
return fail(t, customMsgs, failureMessage, args...)
}

func checkDidPanic(f func()) (didPanic bool, message string) {
didPanic = true
defer func() {
r := recover()

if r == nil {
message = "nil"
return
}

err, ok := r.(error)
if ok {
message = err.Error()
return
}

errStr, ok := r.(string)
if ok {
message = errStr
return
}

message = "recover: unsupported type"
}()
f()
didPanic = false
return
}
59 changes: 59 additions & 0 deletions examples/gno.land/p/demo/uassert/mock_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package uassert

import (
"fmt"
"testing"
)

type mockTestingT struct {
fmt string
args []interface{}
}

// --- interface mock

var _ TestingT = (*mockTestingT)(nil)

func (mockT *mockTestingT) Helper() { /* noop */ }
func (mockT *mockTestingT) Skip(args ...interface{}) { /* not implmented */ }
func (mockT *mockTestingT) Fail() { /* not implmented */ }
func (mockT *mockTestingT) FailNow() { /* not implmented */ }
func (mockT *mockTestingT) Logf(fmt string, args ...interface{}) { /* noop */ }

func (mockT *mockTestingT) Fatalf(fmt string, args ...interface{}) {
mockT.fmt = "fatal: " + fmt
mockT.args = args
}

func (mockT *mockTestingT) Errorf(fmt string, args ...interface{}) {
mockT.fmt = "error: " + fmt
mockT.args = args
}

// --- helpers

func (mockT *mockTestingT) actualString() string {
res := fmt.Sprintf(mockT.fmt, mockT.args...)
mockT.reset()
return res
}

func (mockT *mockTestingT) reset() {
mockT.fmt = ""
mockT.args = nil
}

func (mockT *mockTestingT) equals(t *testing.T, expected string) {
actual := mockT.actualString()

if expected != actual {
t.Errorf("mockT differs:\n- expected: %s\n- actual: %s\n", expected, actual)
}
}

func (mockT *mockTestingT) empty(t *testing.T) {
if mockT.fmt != "" || mockT.args != nil {
actual := mockT.actualString()
t.Errorf("mockT should be empty, got %s", actual)
}
}
11 changes: 11 additions & 0 deletions examples/gno.land/p/demo/uassert/types.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package uassert

type TestingT interface {
Helper()
Skip(args ...interface{})
Fatalf(fmt string, args ...interface{})
Errorf(fmt string, args ...interface{})
Logf(fmt string, args ...interface{})
Fail()
FailNow()
}
Loading

0 comments on commit 326dae7

Please sign in to comment.