Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add p/demo/pausable #1328

Merged
merged 8 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/gno.land/p/demo/pausable/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module gno.land/p/demo/pausable

require gno.land/p/demo/ownable v0.0.0-latest
49 changes: 49 additions & 0 deletions examples/gno.land/p/demo/pausable/pausable.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pausable

import "gno.land/p/demo/ownable"

type Pausable struct {
*ownable.Ownable
paused bool
}

// New returns a new Pausable struct with non-paused state as default
func New() *Pausable {
return &Pausable{
Ownable: ownable.New(),
paused: false,
}
}

// NewFromOwnable is the same as New, but with a pre-existing top-level ownable
func NewFromOwnable(ownable *ownable.Ownable) *Pausable {
return &Pausable{
Ownable: ownable,
paused: false,
}
}

// IsPaused checks if Pausable is paused
func (p Pausable) IsPaused() bool {
return p.paused
}

// Pause sets the state of Pausable to true, meaning all pausable functions are paused
func (p *Pausable) Pause() error {
if err := p.CallerIsOwner(); err != nil {
return err
}

p.paused = true
return nil
}

// Unpause sets the state of Pausable to false, meaning all pausable functions are resumed
func (p *Pausable) Unpause() error {
if err := p.CallerIsOwner(); err != nil {
return err
}

p.paused = false
return nil
}
77 changes: 77 additions & 0 deletions examples/gno.land/p/demo/pausable/pausable_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package pausable

import (
"std"
"testing"

"gno.land/p/demo/ownable"
)

var (
firstCaller = std.Address("g1l9aypkr8xfvs82zeux486ddzec88ty69lue9de")
secondCaller = std.Address("g127jydsh6cms3lrtdenydxsckh23a8d6emqcvfa")
)

func TestNew(t *testing.T) {
std.TestSetOrigCaller(firstCaller)

result := New()

if result.paused != false {
t.Fatalf("Expected result to be unpaused, got %t\n", result.paused)
}

if result.Owner() != firstCaller {
t.Fatalf("Expected %s, got %s\n", firstCaller, result.Owner())
}
}

func TestNewFromOwnable(t *testing.T) {
std.TestSetOrigCaller(firstCaller)
o := ownable.New()

std.TestSetOrigCaller(secondCaller)
result := NewFromOwnable(o)

if result.Owner() != firstCaller {
t.Fatalf("Expected %s, got %s\n", firstCaller, result.Owner())
}
}

func TestSetUnpaused(t *testing.T) {
std.TestSetOrigCaller(firstCaller)

result := New()
result.Unpause()

if result.IsPaused() {
t.Fatalf("Expected result to be unpaused, got %t\n", result.IsPaused())
}
}

func TestSetPaused(t *testing.T) {
std.TestSetOrigCaller(firstCaller)

result := New()
result.Pause()

if !result.IsPaused() {
t.Fatalf("Expected result to be paused, got %t\n", result.IsPaused())
}
}

func TestIsPaused(t *testing.T) {
std.TestSetOrigCaller(firstCaller)

result := New()

if result.IsPaused() {
t.Fatalf("Expected result to be unpaused, got %t\n", result.IsPaused())
}

result.Pause()

if !result.IsPaused() {
t.Fatalf("Expected result to be paused, got %t\n", result.IsPaused())
}
}
Loading