github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/tests/files/zrealm_crossrealm11.gno (about) 1 // PKGPATH: gno.land/r/crossrealm_test 2 package crossrealm_test 3 4 import ( 5 "std" 6 "strings" 7 8 ptests "gno.land/p/demo/tests" 9 "gno.land/p/demo/ufmt" 10 rtests "gno.land/r/demo/tests" 11 testfoo "gno.land/r/demo/tests_foo" 12 ) 13 14 func getPrevRealm() std.Realm { 15 return std.PrevRealm() 16 } 17 18 func Exec(fn func()) { 19 fn() 20 } 21 22 func main() { 23 // Create a map of the potential callers, this will give more understandable 24 // output than the bech32 addresses. 25 callersByAddr := make(map[std.Address]string) 26 for _, caller := range []string{ 27 "user1.gno", "gno.land/r/crossrealm_test", "gno.land/r/demo/tests", 28 } { 29 addr := std.DerivePkgAddr(caller) 30 callersByAddr[addr] = caller 31 } 32 33 assertRealm := func(r std.Realm) { 34 pkgPath := callersByAddr[r.Addr()] 35 if r.IsUser() && pkgPath != "user1.gno" { 36 panic(ufmt.Sprintf("ERROR: expected: 'user1.gno', got:'%s'", pkgPath)) 37 } else if !r.IsUser() && pkgPath != r.PkgPath() { 38 panic(ufmt.Sprintf("ERROR: expected: '%s', got: '%s'", pkgPath, r.PkgPath())) 39 } 40 } 41 42 tests := []struct { 43 callStackAdd string 44 callerFn func() std.Realm 45 }{ 46 { 47 callStackAdd: "", 48 callerFn: std.PrevRealm, 49 }, 50 { 51 callStackAdd: " -> r/crossrealm_test.getPrevRealm", 52 callerFn: getPrevRealm, 53 }, 54 { 55 callStackAdd: " -> p/demo/tests", 56 callerFn: ptests.GetPrevRealm, 57 }, 58 { 59 callStackAdd: " -> p/demo/tests -> p/demo/tests/subtests", 60 callerFn: ptests.GetPSubtestsPrevRealm, 61 }, 62 { 63 callStackAdd: " -> r/demo/tests", 64 callerFn: rtests.GetPrevRealm, 65 }, 66 { 67 callStackAdd: " -> r/demo/tests -> r/demo/tests/subtests", 68 callerFn: rtests.GetRSubtestsPrevRealm, 69 }, 70 { 71 callStackAdd: " -> p/demo/tests -> r/demo/tests", 72 callerFn: ptests.GetRTestsGetPrevRealm, 73 }, 74 } 75 76 println("---") // needed to have space prefixes 77 printColumns("STACK", "std.PrevRealm") 78 printColumns("-----", "------------------") 79 80 baseCallStack := "user1.gno -> r/crossrealm_test.main" 81 for i, tt := range tests { 82 printColumns(baseCallStack+tt.callStackAdd, callersByAddr[tt.callerFn().Addr()]) 83 Exec(func() { 84 r := tt.callerFn() 85 assertRealm(r) 86 printColumns(baseCallStack+" -> r/crossrealm_test.Exec"+tt.callStackAdd, callersByAddr[r.Addr()]) 87 }) 88 rtests.Exec(func() { 89 r := tt.callerFn() 90 assertRealm(r) 91 printColumns(baseCallStack+" -> r/demo/tests.Exec"+tt.callStackAdd, callersByAddr[r.Addr()]) 92 }) 93 ptests.Exec(func() { 94 r := tt.callerFn() 95 assertRealm(r) 96 printColumns(baseCallStack+" -> p/demo/tests.Exec"+tt.callStackAdd, callersByAddr[r.Addr()]) 97 }) 98 } 99 } 100 101 func printColumns(left, right string) { 102 const w = 105 103 104 output := "" 105 padding := w - len(left) 106 107 // strings.Repeat is not always available when using various imports modes. 108 for i := 0; i < padding; i++ { 109 output += " " 110 } 111 112 output += left 113 output += " = " 114 output += right 115 println(output) 116 } 117 118 // Output: 119 // --- 120 // STACK = std.PrevRealm 121 // ----- = ------------------ 122 // user1.gno -> r/crossrealm_test.main = user1.gno 123 // user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec = user1.gno 124 // user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec = gno.land/r/demo/tests 125 // user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec = user1.gno 126 // user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.getPrevRealm = user1.gno 127 // user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> r/crossrealm_test.getPrevRealm = user1.gno 128 // user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> r/crossrealm_test.getPrevRealm = gno.land/r/demo/tests 129 // user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> r/crossrealm_test.getPrevRealm = user1.gno 130 // user1.gno -> r/crossrealm_test.main -> p/demo/tests = user1.gno 131 // user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> p/demo/tests = user1.gno 132 // user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> p/demo/tests = gno.land/r/demo/tests 133 // user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> p/demo/tests = user1.gno 134 // user1.gno -> r/crossrealm_test.main -> p/demo/tests -> p/demo/tests/subtests = user1.gno 135 // user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> p/demo/tests -> p/demo/tests/subtests = user1.gno 136 // user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> p/demo/tests -> p/demo/tests/subtests = gno.land/r/demo/tests 137 // user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> p/demo/tests -> p/demo/tests/subtests = user1.gno 138 // user1.gno -> r/crossrealm_test.main -> r/demo/tests = gno.land/r/crossrealm_test 139 // user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> r/demo/tests = gno.land/r/crossrealm_test 140 // user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> r/demo/tests = gno.land/r/crossrealm_test 141 // user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> r/demo/tests = gno.land/r/crossrealm_test 142 // user1.gno -> r/crossrealm_test.main -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests 143 // user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests 144 // user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests 145 // user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests 146 // user1.gno -> r/crossrealm_test.main -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test 147 // user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test 148 // user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test 149 // user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test