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

     1  package tests
     2  
     3  import (
     4  	"std"
     5  	"testing"
     6  
     7  	"gno.land/p/demo/testutils"
     8  )
     9  
    10  func TestAssertOriginCall(t *testing.T) {
    11  	// No-panic case
    12  	AssertOriginCall()
    13  	if !IsOriginCall() {
    14  		t.Errorf("expected IsOriginCall=true but got false")
    15  	}
    16  
    17  	// Panic case
    18  	expectedReason := "invalid non-origin call"
    19  	defer func() {
    20  		r := recover()
    21  		if r == nil || r.(string) != expectedReason {
    22  			t.Errorf("expected panic with '%v', got '%v'", expectedReason, r)
    23  		}
    24  	}()
    25  	func() {
    26  		// if called inside a function literal, this is no longer an origin call
    27  		// because there's one additional frame (the function literal).
    28  		if IsOriginCall() {
    29  			t.Errorf("expected IsOriginCall=false but got true")
    30  		}
    31  		AssertOriginCall()
    32  	}()
    33  }
    34  
    35  func TestPrevRealm(t *testing.T) {
    36  	var (
    37  		user1Addr  = std.DerivePkgAddr("user1.gno")
    38  		rTestsAddr = std.DerivePkgAddr("gno.land/r/demo/tests")
    39  	)
    40  	// When a single realm in the frames, PrevRealm returns the user
    41  	if addr := GetPrevRealm().Addr(); addr != user1Addr {
    42  		t.Errorf("want GetPrevRealm().Addr==%s, got %s", user1Addr, addr)
    43  	}
    44  	// When 2 or more realms in the frames, PrevRealm returns the second to last
    45  	if addr := GetRSubtestsPrevRealm().Addr(); addr != rTestsAddr {
    46  		t.Errorf("want GetRSubtestsPrevRealm().Addr==%s, got %s", rTestsAddr, addr)
    47  	}
    48  }