tests_test.gno

2.04 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	tests.CallAssertOriginCall(cross)
17	if !tests.CallIsOriginCall(cross) {
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 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}
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 := tests.GetPreviousRealm(cross).Address(); addr != firstRealm {
56		println(tests.GetPreviousRealm(cross))
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 := tests.GetRSubtestsPreviousRealm(cross).Address(); addr != rTestsAddr {
61		t.Errorf("want GetRSubtestsPreviousRealm().Address==%s, got %s", rTestsAddr, addr)
62	}
63}