Skip to content

Commit

Permalink
test: test cases to overwrite Prev*
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Mar 28, 2024
1 parent 7f67588 commit 201a27f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
8 changes: 8 additions & 0 deletions examples/gno.land/r/x/test_prev/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module gno.land/r/x/test_prev

require (
gno.land/p/demo/grc/grc20 v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
gno.land/r/demo/users v0.0.0-latest
)
18 changes: 18 additions & 0 deletions examples/gno.land/r/x/test_prev/test_prev.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test_prev

import (
"std"

pusers "gno.land/p/demo/users"
"gno.land/r/demo/foo20"
)

func DoSomeWithUserBalance() string {
caller := std.GetOrigCaller()
balance := foo20.BalanceOf(pusers.AddressOrName(caller))

if balance > 100 {
return "rich user"
}
return "poor user"
}
32 changes: 32 additions & 0 deletions examples/gno.land/r/x/test_prev/test_prev_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package test_prev

import (
"std"
"testing"

"gno.land/r/demo/foo20"
)

func TestDoSomeWithUserBalancePoor(t *testing.T) {
std.TestSetOrigCaller("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
if DoSomeWithUserBalance() != "poor user" {
t.Error("expected poor user")
}
}

func TestDoSomeWithUserBalanceRichButPoor(t *testing.T) {
std.TestSetOrigCaller("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
foo20.Faucet() // foo20 will mint tokens to this realm(std.PrevRealm) not user
if DoSomeWithUserBalance() != "poor user" {
t.Error("expected poor user")
}
}

func TestDoSomeWithUserBalanceRich(t *testing.T) {
std.TestSetPrevAddr("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")

foo20.Faucet() // foo20 will mint tokens to this realm not user
if DoSomeWithUserBalance() != "rich user" {
t.Error("expected rich user")
}
}
3 changes: 2 additions & 1 deletion gnovm/tests/stdlibs/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package std

import (
"fmt"
"github.com/gnolang/gno/gnovm/stdlibs"
"strings"
"testing"

"github.com/gnolang/gno/gnovm/stdlibs"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/stdlibs/std"
"github.com/gnolang/gno/tm2/pkg/crypto"
Expand Down

0 comments on commit 201a27f

Please sign in to comment.