Skip to content

Commit

Permalink
feat(test): support *OriginCall funcs in _test files (gnolang#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle authored Apr 12, 2023
1 parent d4e37cc commit 5b275ff
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 12 deletions.
8 changes: 8 additions & 0 deletions examples/gno.land/r/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ func CurrentRealmPath() string {
return std.CurrentRealmPath()
}

func AssertOriginCall() {
std.AssertOriginCall()
}

func IsOriginCall() bool {
return std.IsOriginCall()
}

//----------------------------------------
// Test structure to ensure cross-realm modification is prevented.

Expand Down
28 changes: 28 additions & 0 deletions examples/gno.land/r/demo/tests/tests_filetest.gno
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
32 changes: 32 additions & 0 deletions examples/gno.land/r/demo/tests/tests_test.gno
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()
}()
}
2 changes: 1 addition & 1 deletion gnovm/stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
pn.DefineGoNativeValue("IntSize", strconv.IntSize)
pn.DefineGoNativeValue("AppendUint", strconv.AppendUint)
case "std":
// NOTE: some of these are overridden in tests/imports_test.go
// NOTE: some of these are overridden in tests/imports.go
// Also see stdlibs/InjectPackage.
pn.DefineNative("AssertOriginCall",
gno.Flds( // params
Expand Down
100 changes: 94 additions & 6 deletions gnovm/tests/files/zrealm_tests0.gno
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "20",
// "Line": "28",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -301,7 +301,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "20",
// "Line": "28",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -558,6 +558,94 @@ func main() {
// {
// "T": {
// "@type": "/gno.FuncType",
// "Params": [],
// "Results": []
// },
// "V": {
// "@type": "/gno.FuncValue",
// "Closure": {
// "@type": "/gno.RefValue",
// "Escaped": true,
// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:4"
// },
// "FileName": "tests.gno",
// "IsMethod": false,
// "Name": "AssertOriginCall",
// "PkgPath": "gno.land/r/demo/tests",
// "Source": {
// "@type": "/gno.RefNode",
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "9",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
// },
// "Type": {
// "@type": "/gno.FuncType",
// "Params": [],
// "Results": []
// }
// }
// },
// {
// "T": {
// "@type": "/gno.FuncType",
// "Params": [],
// "Results": [
// {
// "Embedded": false,
// "Name": "",
// "Tag": "",
// "Type": {
// "@type": "/gno.PrimitiveType",
// "value": "4"
// }
// }
// ]
// },
// "V": {
// "@type": "/gno.FuncValue",
// "Closure": {
// "@type": "/gno.RefValue",
// "Escaped": true,
// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:4"
// },
// "FileName": "tests.gno",
// "IsMethod": false,
// "Name": "IsOriginCall",
// "PkgPath": "gno.land/r/demo/tests",
// "Source": {
// "@type": "/gno.RefNode",
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "13",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
// },
// "Type": {
// "@type": "/gno.FuncType",
// "Params": [],
// "Results": [
// {
// "Embedded": false,
// "Name": "",
// "Tag": "",
// "Type": {
// "@type": "/gno.PrimitiveType",
// "value": "4"
// }
// }
// ]
// }
// }
// },
// {
// "T": {
// "@type": "/gno.FuncType",
// "Params": [
// {
// "Embedded": false,
Expand Down Expand Up @@ -590,7 +678,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "16",
// "Line": "24",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -637,7 +725,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "38",
// "Line": "46",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -671,7 +759,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "43",
// "Line": "51",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -705,7 +793,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "51",
// "Line": "59",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down
17 changes: 12 additions & 5 deletions gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,15 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri
func testPackageInjector(store gno.Store, pn *gno.PackageNode) {
// Also inject stdlibs native functions.
stdlibs.InjectPackage(store, pn)
isOriginCall := func(m *gno.Machine) bool {
switch m.Frames[0].Func.Name {
case "main": // test is a _filetest
return len(m.Frames) == 3
case "runtest": // test is a _test
return len(m.Frames) == 7
}
panic("unable to determine if test is a _test or a _filetest")
}
// Test specific injections:
switch pn.PkgPath {
case "strconv":
Expand All @@ -474,9 +483,8 @@ func testPackageInjector(store gno.Store, pn *gno.PackageNode) {
),
*/
func(m *gno.Machine) {
isOrigin := len(m.Frames) == 3
if !isOrigin {
panic("invalid non-origin call")
if !isOriginCall(m) {
m.Panic(typedString("invalid non-origin call"))
}
},
)
Expand All @@ -489,9 +497,8 @@ func testPackageInjector(store gno.Store, pn *gno.PackageNode) {
),
*/
func(m *gno.Machine) {
isOrigin := len(m.Frames) == 3
res0 := gno.TypedValue{T: gno.BoolType}
res0.SetBool(isOrigin)
res0.SetBool(isOriginCall(m))
m.PushValue(res0)
},
)
Expand Down

0 comments on commit 5b275ff

Please sign in to comment.