gnolang / gno

Gno: An interpreted, stack-based Go virtual machine to build succinct and composable apps + Gno.land: a blockchain for timeless code and fair open-source
https://gno.land/
Other
839 stars 342 forks source link

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

Open leohhhn opened 1 week ago

leohhhn commented 1 week ago

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
thehowl commented 1 day ago

see-also

https://github.com/gnolang/gno/blob/5fdbce0e1fe533165c0c2009e93695ece677b2bd/examples/gno.land/p/demo/ownable/ownable_test.gno#L13