Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

tests_test.gno

2.12 Kb · 62 lines
 1package vm_test
 2
 3import (
 4	"chain"
 5	"fmt"
 6	"testing"
 7
 8	"gno.land/p/nt/testutils/v0"
 9	tests "gno.land/r/tests/vm"
10)
11
12func TestAssertOriginCall(cur realm, t *testing.T) {
13	// CallAssertOriginCall(): no panic
14	caller := testutils.TestAddress("caller")
15	testing.SetRealm(testing.NewUserRealm(caller))
16	tests.CallAssertOriginCall(cross(cur))
17	if !tests.CallIsOriginCall(cross(cur)) {
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(cur)) {
27			t.Errorf("expected IsOriginCall=false but got true")
28		}
29		tests.CallAssertOriginCall(cross(cur)) // <---
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(cur)) {
39			t.Errorf("expected IsOriginCall=false but got true")
40		}
41		tests.CallSubtestsAssertOriginCall(cross(cur))
42	})
43	if fmt.Sprintf("%v", r) != "invalid non-origin call" {
44		t.Error("expected abort but did not")
45	}
46}
47
48func TestPreviousRealm(cur realm, 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(cur)).Address(); addr != firstRealm {
55		println(tests.GetPreviousRealm(cross(cur)))
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(cur)).Address(); addr != rTestsAddr {
60		t.Errorf("want GetRSubtestsPreviousRealm().Address==%s, got %s", rTestsAddr, addr)
61	}
62}