Search Apps Documentation Source Content File Folder Download Copy Actions Download

tests.gno

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