From 8c880fb1b28fb04c14bdd4f6bd69d4300bfc112a Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 12 Dec 2024 21:15:40 +0100 Subject: [PATCH] save --- examples/gno.land/p/demo/ownable/ownable.gno | 7 +++- .../gno.land/p/demo/ownable/ownable_test.gno | 33 +++++++------------ .../gno.land/p/demo/ownable/z0_filetest.gno | 8 +++++ 3 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 examples/gno.land/p/demo/ownable/z0_filetest.gno diff --git a/examples/gno.land/p/demo/ownable/ownable.gno b/examples/gno.land/p/demo/ownable/ownable.gno index cc9703132d3..7a8eb79b69b 100644 --- a/examples/gno.land/p/demo/ownable/ownable.gno +++ b/examples/gno.land/p/demo/ownable/ownable.gno @@ -6,7 +6,7 @@ const OwnershipTransferEvent = "OwnershipTransfer" // Ownable is meant to be used as a top-level object to make your contract ownable OR // being embedded in a Gno object to manage per-object ownership. -// Do not export this object, as its methods do not validate the caller. +// WARNING: Do not directly export this object at top-level, as its methods do not validate the caller. // For this, check out SafeOwnable. type Ownable struct { owner std.Address @@ -88,6 +88,11 @@ func (o *Ownable) NewSafeOwnable() *SafeOwnable { } } +// AssertCallerIsOwner panics if the caller is not the owner +func (so SafeOwnable) AssertCallerIsOwner() { + so.ownable.AssertCallerIsOwner() +} + func (so *SafeOwnable) TransferOwnership(newOwner std.Address) error { if !so.ownable.CallerIsOwner() { return ErrUnauthorized diff --git a/examples/gno.land/p/demo/ownable/ownable_test.gno b/examples/gno.land/p/demo/ownable/ownable_test.gno index dee40fa6e1d..16a8550f459 100644 --- a/examples/gno.land/p/demo/ownable/ownable_test.gno +++ b/examples/gno.land/p/demo/ownable/ownable_test.gno @@ -1,6 +1,7 @@ package ownable import ( + "github.com/gnolang/gno/examples/gno.land/p/demo/urequire" "std" "testing" @@ -13,24 +14,23 @@ var ( bob = testutils.TestAddress("bob") ) +// embed ownable and test +// test safeownable + func TestNew(t *testing.T) { std.TestSetRealm(std.NewUserRealm(alice)) std.TestSetOrigCaller(alice) // TODO(bug): should not be needed o := New() got := o.Owner() - if alice != got { - t.Fatalf("Expected %s, got: %s", alice, got) - } + uassert.Equal(t, got, alice) } func TestNewWithAddress(t *testing.T) { o := NewWithAddress(alice) got := o.Owner() - if alice != got { - t.Fatalf("Expected %s, got: %s", alice, got) - } + uassert.Equal(t, got, alice) } func TestTransferOwnership(t *testing.T) { @@ -39,14 +39,9 @@ func TestTransferOwnership(t *testing.T) { o := New() err := o.TransferOwnership(bob) - if err != nil { - t.Fatalf("TransferOwnership failed, %v", err) - } got := o.Owner() - if bob != got { - t.Fatalf("Expected: %s, got: %s", bob, got) - } + uassert.Equal(t, got, bob) } func TestCallerIsOwner(t *testing.T) { @@ -58,8 +53,7 @@ func TestCallerIsOwner(t *testing.T) { std.TestSetRealm(std.NewUserRealm(unauthorizedCaller)) std.TestSetOrigCaller(unauthorizedCaller) // TODO(bug): should not be needed - err := o.CallerIsOwner() - uassert.Error(t, err) // XXX: IsError(..., unauthorizedCaller) + uassert.False(t, o.CallerIsOwner()) } func TestDropOwnership(t *testing.T) { @@ -68,7 +62,7 @@ func TestDropOwnership(t *testing.T) { o := New() err := o.DropOwnership() - uassert.NoError(t, err, "DropOwnership failed") + urequire.NoError(t, err, "DropOwnership failed") owner := o.Owner() uassert.Empty(t, owner, "owner should be empty") @@ -85,13 +79,8 @@ func TestErrUnauthorized(t *testing.T) { std.TestSetRealm(std.NewUserRealm(bob)) std.TestSetOrigCaller(bob) // TODO(bug): should not be needed - err := o.TransferOwnership(alice) - if err != ErrUnauthorized { - t.Fatalf("Should've been ErrUnauthorized, was %v", err) - } - - err = o.DropOwnership() - uassert.ErrorContains(t, err, ErrUnauthorized.Error()) + uassert.ErrorContains(t, o.TransferOwnership(alice), ErrUnauthorized.Error()) + uassert.ErrorContains(t, o.DropOwnership(), ErrUnauthorized.Error()) } func TestErrInvalidAddress(t *testing.T) { diff --git a/examples/gno.land/p/demo/ownable/z0_filetest.gno b/examples/gno.land/p/demo/ownable/z0_filetest.gno new file mode 100644 index 00000000000..f94fc50e589 --- /dev/null +++ b/examples/gno.land/p/demo/ownable/z0_filetest.gno @@ -0,0 +1,8 @@ +package main + +import "gno.land/p/demo/ownable" + +func main() { + _ = ownable.New() + +}