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

[GnoVM] TestSetRealm not working properly in gno test #2371

Open
leohhhn opened this issue Jun 17, 2024 · 2 comments
Open

[GnoVM] TestSetRealm not working properly in gno test #2371

leohhhn opened this issue Jun 17, 2024 · 2 comments
Assignees
Labels
📦 🤖 gnovm Issues or PRs gnovm related

Comments

@leohhhn
Copy link
Contributor

leohhhn commented Jun 17, 2024

Description

Example package:

package example

import "std"

func GetPrevRealm() std.Realm {
	return std.PrevRealm()
}

Example tests:

package example

import (
	"std"
	"testing"

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

func TestGetPrevRealmUser(t *testing.T) {
	alice := testutils.TestAddress("alice")
	std.TestSetRealm(std.NewUserRealm(alice))

	got := GetPrevRealm().Addr()

	if alice != got {
		t.Fatalf("expected %s, got %s", alice, got)
	}
}

func TestGetPrevRealmCode(t *testing.T) {
	std.TestSetRealm(std.NewCodeRealm("gno.land/r/demo/users"))
	expected := "g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s" // derived from the path above

	got := GetPrevRealm().Addr()

	if expected != got {
		t.Fatalf("expected %s, got %s", expected, got)
	}
}

Both tests fail with:

❯ gno test . -v
=== RUN   TestGetPrevRealmUser
expected g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh, got g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm
--- FAIL: TestGetPrevRealmUser (0.00s)
=== RUN   TestGetPrevRealmCode
expected g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s, got g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm
--- FAIL: TestGetPrevRealmCode (0.00s)
.: test pkg: failed: "TestGetPrevRealmUser"; failed: "TestGetPrevRealmCode"
FAIL
FAIL    .       0.72s
FAIL
FAIL
FAIL: 0 build errors, 1 test errors
@leohhhn leohhhn added the 📦 🤖 gnovm Issues or PRs gnovm related label Jun 17, 2024
@leohhhn leohhhn changed the title [GnoVM] TestSetRealm not working properly [GnoVM] TestSetRealm not working properly in gno test Jun 17, 2024
@thehowl thehowl self-assigned this Jun 24, 2024
@thehowl
Copy link
Member

thehowl commented Jun 24, 2024

see-also

func TestNew(t *testing.T) {

@thehowl
Copy link
Member

thehowl commented Jun 28, 2024

The problem concerns using TestSetRealm in p/. See the following commit, where the tests succeed: 799ed1c

Note that the solution to making TestSetRealm work in p/, is by making the call pass through another "mocked realm" first:

func TestSetRealm_UserRealm(t *testing.T) {
	alice := testutils.TestAddress("alice")
	std.TestSetRealm(std.NewUserRealm(alice))

	var got std.Address
	proxyRealm(func() {
		got = tests.GetPrevRealm().Addr()
	})

	if alice != got {
		t.Fatalf("expected %s, got %s", alice, got)
	}
}

func TestSetRealm_CodeRealm(t *testing.T) {
	std.TestSetRealm(std.NewCodeRealm("gno.land/r/demo/users"))
	expected := "g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s" // derived from the path above

	var got std.Address
	proxyRealm(func() {
		got = tests.GetPrevRealm().Addr()
	})

	if expected != got.String() {
		t.Fatalf("expected %s, got %s", expected, got)
	}
}

func proxyRealm(f func()) {
	std.TestSetRealm(std.NewCodeRealm("gno.land/r/proxy"))
	f()
}

This is because of the way TestSetRealm works, which I don't think is entirely incorrect. The "Realm" changes only when a realm boundary is crossed; so if we're working with packages, it doesn't. Consequently, in the example you provided @leohhhn, calling PrevRealm will yield the OrigCaller, because the CurrentRealm is the one you set with TestSetRealm.

I'm considering whether to just make TestSetRealm set both CurrentRealm and PrevRealm, when called from a package. Packages cannot import realms, so this may be OK. Need to sleep on it, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: Triage
Development

No branches or pull requests

2 participants