Search Apps Documentation Source Content File Folder Download Copy Actions Download

tests.gno

2.31 Kb ยท 122 lines
  1package tests
  2
  3import (
  4	"chain/runtime"
  5
  6	"gno.land/p/demo/nestedpkg"
  7	rsubtests "gno.land/r/tests/vm/subtests"
  8)
  9
 10var counter int
 11
 12func IncCounter(cur realm) {
 13	counter++
 14}
 15
 16func Counter(cur realm) int {
 17	return counter
 18}
 19
 20func CurrentRealmPath(cur realm) string {
 21	return runtime.CurrentRealm().PkgPath()
 22}
 23
 24var initOriginCaller = runtime.OriginCaller()
 25
 26func InitOriginCaller(cur realm) address {
 27	return initOriginCaller
 28}
 29
 30func CallAssertOriginCall(cur realm) {
 31	runtime.AssertOriginCall()
 32}
 33
 34func CallIsOriginCall(cur realm) bool {
 35	// XXX: consider return !runtime.PreviousRealm().IsCode()
 36	return runtime.PreviousRealm().IsUser()
 37}
 38
 39func CallSubtestsAssertOriginCall(cur realm) {
 40	rsubtests.CallAssertOriginCall(cross)
 41}
 42
 43func CallSubtestsIsOriginCall(cur realm) bool {
 44	return rsubtests.CallIsOriginCall(cross)
 45}
 46
 47//----------------------------------------
 48// Test structure to ensure cross-realm modification is prevented.
 49
 50type TestRealmObject struct {
 51	Field string
 52}
 53
 54var TestRealmObjectValue TestRealmObject
 55
 56func ModifyTestRealmObject(cur realm, t *TestRealmObject) {
 57	t.Field += "_modified"
 58}
 59
 60func (t *TestRealmObject) Modify() {
 61	t.Field += "_modified"
 62}
 63
 64//----------------------------------------
 65// Test helpers to test a particular realm bug.
 66
 67type TestNode struct {
 68	Name  string
 69	Child *TestNode
 70}
 71
 72var (
 73	gTestNode1 *TestNode
 74	gTestNode2 *TestNode
 75	gTestNode3 *TestNode
 76)
 77
 78func InitTestNodes(cur realm) {
 79	gTestNode1 = &TestNode{Name: "first"}
 80	gTestNode2 = &TestNode{Name: "second", Child: &TestNode{Name: "second's child"}}
 81}
 82
 83func ModTestNodes(cur realm) {
 84	tmp := &TestNode{}
 85	tmp.Child = gTestNode2.Child
 86	gTestNode3 = tmp // set to new-real
 87	// gTestNode1 = tmp.Child // set back to original is-real
 88	gTestNode3 = nil // delete.
 89}
 90
 91func PrintTestNodes() {
 92	println(gTestNode2.Child.Name)
 93}
 94
 95func GetPreviousRealm(cur realm) runtime.Realm {
 96	return runtime.PreviousRealm()
 97}
 98
 99func GetRSubtestsPreviousRealm(cur realm) runtime.Realm {
100	return rsubtests.GetPreviousRealm(cross)
101}
102
103func Exec(fn func()) {
104	// no realm switching.
105	fn()
106}
107
108func ExecSwitch(cur realm, fn func()) {
109	fn()
110}
111
112func IsCallerSubPath(cur realm) bool {
113	return nestedpkg.IsCallerSubPath()
114}
115
116func IsCallerParentPath(cur realm) bool {
117	return nestedpkg.IsCallerParentPath()
118}
119
120func HasCallerSameNamespace(cur realm) bool {
121	return nestedpkg.IsSameNamespace()
122}