tests_test.gno
2.05 Kb ยท 62 lines
1package tests_test
2
3import (
4 "chain"
5 "fmt"
6 "testing"
7
8 "gno.land/p/nt/testutils"
9 tests "gno.land/r/tests/vm"
10)
11
12func TestAssertOriginCall(t *testing.T) {
13 // CallAssertOriginCall(): no panic
14 caller := testutils.TestAddress("caller")
15 testing.SetRealm(testing.NewUserRealm(caller))
16 tests.CallAssertOriginCall(cross)
17 if !tests.CallIsOriginCall(cross) {
18 t.Errorf("expected IsOriginCall=true but got false")
19 }
20
21 testing.SetRealm(testing.NewCodeRealm("gno.land/r/tests/vm"))
22 // CallAssertOriginCall() from a block: abort
23 r := revive(func() {
24 // if called inside a function literal, this is no longer an origin call
25 // because there's one additional frame (the function literal block).
26 if tests.CallIsOriginCall(cross) {
27 t.Errorf("expected IsOriginCall=false but got true")
28 }
29 tests.CallAssertOriginCall(cross) // <---
30 })
31 if fmt.Sprintf("%v", r) != "invalid non-origin call" {
32 t.Error("expected abort but did not")
33 }
34 // CallSubtestsAssertOriginCall(): abort
35 r = revive(func() {
36 // if called inside a function literal, this is no longer an origin call
37 // because there's one additional frame (the function literal block).
38 if tests.CallSubtestsIsOriginCall(cross) {
39 t.Errorf("expected IsOriginCall=false but got true")
40 }
41 tests.CallSubtestsAssertOriginCall(cross)
42 })
43 if fmt.Sprintf("%v", r) != "invalid non-origin call" {
44 t.Error("expected abort but did not")
45 }
46}
47
48func TestPreviousRealm(t *testing.T) {
49 var (
50 firstRealm = chain.PackageAddress("gno.land/r/tests/vm_test")
51 rTestsAddr = chain.PackageAddress("gno.land/r/tests/vm")
52 )
53 // When only one realm in the frames, PreviousRealm returns the same realm
54 if addr := tests.GetPreviousRealm(cross).Address(); addr != firstRealm {
55 println(tests.GetPreviousRealm(cross))
56 t.Errorf("want GetPreviousRealm().Address==%s, got %s", firstRealm, addr)
57 }
58 // When 2 or more realms in the frames, PreviousRealm returns the second to last
59 if addr := tests.GetRSubtestsPreviousRealm(cross).Address(); addr != rTestsAddr {
60 t.Errorf("want GetRSubtestsPreviousRealm().Address==%s, got %s", rTestsAddr, addr)
61 }
62}