Skip to content

Commit

Permalink
add pausable, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Nov 2, 2023
1 parent 0076e48 commit deccdfc
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/pausable/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gno.land/p/demo/pausable
53 changes: 53 additions & 0 deletions examples/gno.land/p/demo/pausable/pausable.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package pausable

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

type Pausable struct {
paused bool
*ownable.Ownable
}

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

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

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

// SetPaused sets the state of Pausable to true, meaning all pausable functions are paused
func (p *Pausable) SetPaused() error {
err := p.CallerIsOwner()

if err != nil {
return err
}

p.paused = true
return nil
}

// SetUnpaused sets the state of Pausable to true, meaning all pausable functions are resumed
func (p *Pausable) SetUnpaused() error {
err := p.CallerIsOwner()

if err != nil {
return err
}

p.paused = false
return nil
}
76 changes: 76 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,76 @@
package pausable

import (
"gno.land/p/demo/ownable"
"std"
"testing"
)

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.SetUnpaused()

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.SetPaused()

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.SetPaused()

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

0 comments on commit deccdfc

Please sign in to comment.