tests_test.gno

2.06 Kb ยท 63 lines
 1package tests_test
 2
 3import (
 4	"fmt"
 5	"std"
 6	"testing"
 7
 8	"gno.land/p/demo/testutils"
 9	"gno.land/r/demo/tests"
10)
11
12func TestAssertOriginCall(t *testing.T) {
13	// CallAssertOriginCall(): no panic
14	caller := testutils.TestAddress("caller")
15	testing.SetRealm(std.NewUserRealm(caller))
16	cross(tests.CallAssertOriginCall)()
17	if !cross(tests.CallIsOriginCall)() {
18		t.Errorf("expected IsOriginCall=true but got false")
19	}
20
21	testing.SetRealm(std.NewCodeRealm("gno.land/r/demo/tests"))
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 cross(tests.CallIsOriginCall)() {
27			t.Errorf("expected IsOriginCall=false but got true")
28		}
29		cross(tests.CallAssertOriginCall)() // <---
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 cross(tests.CallSubtestsIsOriginCall)() {
39			t.Errorf("expected IsOriginCall=false but got true")
40		}
41		cross(tests.CallSubtestsAssertOriginCall)()
42	})
43	if fmt.Sprintf("%v", r) != "invalid non-origin call" {
44		t.Error("expected abort but did not")
45	}
46
47}
48
49func TestPreviousRealm(t *testing.T) {
50	var (
51		firstRealm = std.DerivePkgAddr("gno.land/r/demo/tests_test")
52		rTestsAddr = std.DerivePkgAddr("gno.land/r/demo/tests")
53	)
54	// When only one realm in the frames, PreviousRealm returns the same realm
55	if addr := cross(tests.GetPreviousRealm)().Address(); addr != firstRealm {
56		println(cross(tests.GetPreviousRealm)())
57		t.Errorf("want GetPreviousRealm().Address==%s, got %s", firstRealm, addr)
58	}
59	// When 2 or more realms in the frames, PreviousRealm returns the second to last
60	if addr := cross(tests.GetRSubtestsPreviousRealm)().Address(); addr != rTestsAddr {
61		t.Errorf("want GetRSubtestsPreviousRealm().Address==%s, got %s", rTestsAddr, addr)
62	}
63}