forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
175 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"gno.land/r/demo/tests" | ||
) | ||
|
||
func main() { | ||
println("IsOriginCall:", tests.IsOriginCall()) | ||
tests.AssertOriginCall() | ||
println("AssertOriginCall doesn't panic when called directly") | ||
|
||
func() { | ||
// if called inside a function literal, this is no longer an origin call | ||
// because there's one additional frame (the function literal). | ||
println("IsOriginCall:", tests.IsOriginCall()) | ||
defer func() { | ||
r := recover() | ||
println("AssertOriginCall panics if when called inside a function literal:", r) | ||
}() | ||
tests.AssertOriginCall() | ||
}() | ||
} | ||
|
||
// Output: | ||
// IsOriginCall: true | ||
// AssertOriginCall doesn't panic when called directly | ||
// IsOriginCall: false | ||
// AssertOriginCall panics if when called inside a function literal: invalid non-origin call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestAssertOriginCall(t *testing.T) { | ||
// No-panic case | ||
AssertOriginCall() | ||
if !IsOriginCall() { | ||
t.Errorf("expected IsOriginCall=true but got false") | ||
} | ||
|
||
// Panic case | ||
expectedReason := "invalid non-origin call" | ||
defer func() { | ||
r := recover() | ||
if r == nil || r.(string) != expectedReason { | ||
t.Errorf("expected panic with '%v', got '%v'", expectedReason, r) | ||
} | ||
}() | ||
func() { | ||
// if called inside a function literal, this is no longer an origin call | ||
// because there's one additional frame (the function literal). | ||
if IsOriginCall() { | ||
t.Errorf("expected IsOriginCall=false but got true") | ||
} | ||
AssertOriginCall() | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters