github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/demo/tests/tests.gno (about)

     1  package tests
     2  
     3  import (
     4  	"std"
     5  
     6  	rsubtests "gno.land/r/demo/tests/subtests"
     7  )
     8  
     9  var counter int
    10  
    11  func IncCounter() {
    12  	counter++
    13  }
    14  
    15  func Counter() int {
    16  	return counter
    17  }
    18  
    19  func CurrentRealmPath() string {
    20  	return std.CurrentRealmPath()
    21  }
    22  
    23  var initOrigCaller = std.GetOrigCaller()
    24  
    25  func InitOrigCaller() std.Address {
    26  	return initOrigCaller
    27  }
    28  
    29  func AssertOriginCall() {
    30  	std.AssertOriginCall()
    31  }
    32  
    33  func IsOriginCall() bool {
    34  	return std.IsOriginCall()
    35  }
    36  
    37  //----------------------------------------
    38  // Test structure to ensure cross-realm modification is prevented.
    39  
    40  type TestRealmObject struct {
    41  	Field string
    42  }
    43  
    44  func ModifyTestRealmObject(t *TestRealmObject) {
    45  	t.Field += "_modified"
    46  }
    47  
    48  func (t *TestRealmObject) Modify() {
    49  	t.Field += "_modified"
    50  }
    51  
    52  //----------------------------------------
    53  // Test helpers to test a particular realm bug.
    54  
    55  type TestNode struct {
    56  	Name  string
    57  	Child *TestNode
    58  }
    59  
    60  var (
    61  	gTestNode1 *TestNode
    62  	gTestNode2 *TestNode
    63  	gTestNode3 *TestNode
    64  )
    65  
    66  func InitTestNodes() {
    67  	gTestNode1 = &TestNode{Name: "first"}
    68  	gTestNode2 = &TestNode{Name: "second", Child: &TestNode{Name: "second's child"}}
    69  }
    70  
    71  func ModTestNodes() {
    72  	tmp := &TestNode{}
    73  	tmp.Child = gTestNode2.Child
    74  	gTestNode3 = tmp // set to new-real
    75  	// gTestNode1 = tmp.Child // set back to original is-real
    76  	gTestNode3 = nil // delete.
    77  }
    78  
    79  func PrintTestNodes() {
    80  	println(gTestNode2.Child.Name)
    81  }
    82  
    83  func GetPrevRealm() std.Realm {
    84  	return std.PrevRealm()
    85  }
    86  
    87  func GetRSubtestsPrevRealm() std.Realm {
    88  	return rsubtests.GetPrevRealm()
    89  }
    90  
    91  func Exec(fn func()) {
    92  	fn()
    93  }