From 571e5227dab8e7e57f96a391246b87ac3b003441 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Mon, 17 Jul 2023 16:59:16 +0200 Subject: [PATCH 1/5] feat: go-based advances testing suite for contracts Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- .../r/demo/banktest/integration_test.go | 127 ++++++++++++++++++ gnovm/tests/imports.go | 6 + 2 files changed, 133 insertions(+) create mode 100644 examples/gno.land/r/demo/banktest/integration_test.go diff --git a/examples/gno.land/r/demo/banktest/integration_test.go b/examples/gno.land/r/demo/banktest/integration_test.go new file mode 100644 index 00000000000..c150e291cec --- /dev/null +++ b/examples/gno.land/r/demo/banktest/integration_test.go @@ -0,0 +1,127 @@ +package banktest + +import ( + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/gnolang/gno/gnovm/pkg/gnolang" + "github.com/gnolang/gno/gnovm/tests" + "github.com/gnolang/gno/tm2/pkg/std" +) + +func TestSimpleFlow(t *testing.T) { + var ( + mode = tests.ImportModeStdlibsOnly + rootDir = filepath.Join("..", "..", "..", "..", "..") + stdin = os.Stdin + stdout = os.Stdout + stderr = os.Stderr + store = tests.TestStore(rootDir, "", stdin, stdout, stderr, mode) + ) + store.SetStrictGo2GnoMapping(true) // natives must be registered + gnolang.DisableDebug() // until main call + m := tests.TestMachine(store, stdout, "main") + pkgName := "main" + pkgPath := "gno.land/r/demo/banktest_test" + /* + pkgPath := "./" + memPkg := gnolang.ReadMemPackage(".", ".") + files := &gnolang.FileSet{} + for _, mfile := range memPkg.Files { + if !strings.HasSuffix(mfile.Name, ".gno") { + continue + } + if strings.HasSuffix(mfile.Name, "_filetest.gno") { + continue + } + n, err := gnolang.ParseFile(mfile.Name, mfile.Body) + if err != nil { + t.Fatalf("parsing %q: %v", mfile.Name, err) + } + files.AddFiles(n) + } + // TODO: use temporary stdout, stderr for assertion + m.RunMemPackage(memPkg, true) + */ + + // main1 + if true { + println("main1") + memPkg := &std.MemPackage{ + Name: pkgName, + Path: pkgPath, + Files: []*std.MemFile{ + { + Name: "main1.gno", + Body: main1, + }, + }, + } + m.RunMemPackage(memPkg, true) + store.ClearCache() + m.PreprocessAllFilesAndSaveBlockNodes() + // store.Print() // debug + pv2 := store.GetPackage(pkgPath, false) + m.SetActivePackage(pv2) + gnolang.EnableDebug() + defer func() { + if r := recover(); r != nil { + fmt.Printf("main1 panic: %v\n%s\n", r, m.String()) + panic(r) + } + }() + m.RunStatement(gnolang.S(gnolang.Call(gnolang.X("main1")))) + m.CheckEmpty() + } + // main2 + if true { + println("main2") + memPkg := &std.MemPackage{ + Name: pkgName, + Path: pkgPath, + Files: []*std.MemFile{ + { + Name: "main2.gno", + Body: main2, + }, + }, + } + m.RunMemPackage(memPkg, true) + store.ClearCache() + m.PreprocessAllFilesAndSaveBlockNodes() + // store.Print() // debug + //pv2 := store.GetPackage(pkgPath, false) + //m.SetActivePackage(pv2) + gnolang.EnableDebug() + defer func() { + if r := recover(); r != nil { + fmt.Printf("main2 panic: %v\n%s\n", r, m.String()) + panic(r) + } + }() + m.RunStatement(gnolang.S(gnolang.Call(gnolang.X("main2")))) + // TODO: m.CheckEmpty() + } + println("end") + +} + +const main1 = `package main + +import "gno.land/r/demo/banktest" + +func main1() { + banktest.Deposit("ugnot", 0) + /* std.TestSetCaller */ + println("HELLO WORLD!") +} +` + +const main2 = `package main + +func main2() { + /* std.TestSetCaller... */ + println("hello world 2") +}` diff --git a/gnovm/tests/imports.go b/gnovm/tests/imports.go index 58d62f02d8d..d9c180e9d20 100644 --- a/gnovm/tests/imports.go +++ b/gnovm/tests/imports.go @@ -471,6 +471,12 @@ func testPackageInjector(store gno.Store, pn *gno.PackageNode) { if strings.HasPrefix(string(tname), "init.") { return len(m.Frames) == 3 } + + // support multi-transaction tests + if strings.HasPrefix(string(tname), "main") { + return len(m.Frames) == 3 + } + panic("unable to determine if test is a _test or a _filetest") } // Test specific injections: From 754171f79c75af2e7957afade30b8e5f17252a9a Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Tue, 18 Jul 2023 14:44:34 +0200 Subject: [PATCH 2/5] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- .../r/demo/banktest/integration_test.go | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/gno.land/r/demo/banktest/integration_test.go b/examples/gno.land/r/demo/banktest/integration_test.go index c150e291cec..8f9840a9146 100644 --- a/examples/gno.land/r/demo/banktest/integration_test.go +++ b/examples/gno.land/r/demo/banktest/integration_test.go @@ -102,7 +102,7 @@ func TestSimpleFlow(t *testing.T) { } }() m.RunStatement(gnolang.S(gnolang.Call(gnolang.X("main2")))) - // TODO: m.CheckEmpty() + m.CheckEmpty() } println("end") @@ -111,17 +111,31 @@ func TestSimpleFlow(t *testing.T) { const main1 = `package main import "gno.land/r/demo/banktest" +import "std" func main1() { banktest.Deposit("ugnot", 0) /* std.TestSetCaller */ - println("HELLO WORLD!") + printStats("main1") +} + +func printStats(name string) { + println("stats", name, std.GetHeight(), std.GetOrigCaller()) } ` const main2 = `package main +import "gno.land/r/demo/banktest" +import "std" + func main2() { + banktest.Deposit("ugnot", 0) /* std.TestSetCaller... */ - println("hello world 2") -}` + printStats2("main2") +} + +func printStats2(name string) { + println("stats", name, std.GetHeight(), std.GetOrigCaller()) +} +` From c03c845c88891d1cfc81edac656a43f9c2306df2 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Wed, 19 Jul 2023 08:46:21 +0200 Subject: [PATCH 3/5] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- .../gno.land/r/demo/banktest/integration_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/gno.land/r/demo/banktest/integration_test.go b/examples/gno.land/r/demo/banktest/integration_test.go index 8f9840a9146..8ea04b77e91 100644 --- a/examples/gno.land/r/demo/banktest/integration_test.go +++ b/examples/gno.land/r/demo/banktest/integration_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/gnolang/gno/gnovm/pkg/gnolang" + "github.com/gnolang/gno/gnovm/stdlibs" "github.com/gnolang/gno/gnovm/tests" "github.com/gnolang/gno/tm2/pkg/std" ) @@ -48,6 +49,9 @@ func TestSimpleFlow(t *testing.T) { // main1 if true { + ctx := m.Context.(stdlibs.ExecContext) + ctx.OrigSend = std.MustParseCoins("1234500000ugnot") + m.Context = ctx println("main1") memPkg := &std.MemPackage{ Name: pkgName, @@ -77,6 +81,12 @@ func TestSimpleFlow(t *testing.T) { } // main2 if true { + ctx := m.Context.(stdlibs.ExecContext) + ctx.OrigSend = std.MustParseCoins("12345ugnot") + ctx.Height++ + ctx.Timestamp++ + m.Context = ctx + println("main2") memPkg := &std.MemPackage{ Name: pkgName, @@ -115,6 +125,7 @@ import "std" func main1() { banktest.Deposit("ugnot", 0) + println(banktest.Render("")) /* std.TestSetCaller */ printStats("main1") } @@ -132,6 +143,7 @@ import "std" func main2() { banktest.Deposit("ugnot", 0) /* std.TestSetCaller... */ + println(banktest.Render("")) printStats2("main2") } From 2e3410891be3a9261fc2493220c0482aa88edf4a Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Wed, 2 Aug 2023 01:35:32 +0200 Subject: [PATCH 4/5] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- .../r/demo/banktest/integration_test.go | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/examples/gno.land/r/demo/banktest/integration_test.go b/examples/gno.land/r/demo/banktest/integration_test.go index 8ea04b77e91..9164c33a8a1 100644 --- a/examples/gno.land/r/demo/banktest/integration_test.go +++ b/examples/gno.land/r/demo/banktest/integration_test.go @@ -1,3 +1,7 @@ +// integration_test.go - Not Gno, and ignored onchain +// This file is for advanced contract testing not managed purely with .gno files. +// It enables multi-transaction integration tests and features currently unsupported by Gno. +// We may replace or deprecate it in the future as Gno evolves. Testing purposes only. package banktest import ( @@ -124,14 +128,14 @@ import "gno.land/r/demo/banktest" import "std" func main1() { - banktest.Deposit("ugnot", 0) - println(banktest.Render("")) - /* std.TestSetCaller */ - printStats("main1") + banktest.Deposit("ugnot", 0) + println(banktest.Render("")) + /* std.TestSetCaller */ + printStats("main1") } func printStats(name string) { - println("stats", name, std.GetHeight(), std.GetOrigCaller()) + println("stats", name, std.GetHeight(), std.GetOrigCaller()) } ` @@ -141,13 +145,13 @@ import "gno.land/r/demo/banktest" import "std" func main2() { - banktest.Deposit("ugnot", 0) - /* std.TestSetCaller... */ - println(banktest.Render("")) - printStats2("main2") + banktest.Deposit("ugnot", 0) + /* std.TestSetCaller... */ + println(banktest.Render("")) + printStats2("main2") } func printStats2(name string) { - println("stats", name, std.GetHeight(), std.GetOrigCaller()) + println("stats", name, std.GetHeight(), std.GetOrigCaller()) } ` From 8702a9e2be5dc5cf6677dbb0b4e049bcf65c1ab1 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Wed, 2 Aug 2023 03:43:57 +0200 Subject: [PATCH 5/5] chore: fix doc typo Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- tm2/pkg/sdk/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm2/pkg/sdk/options.go b/tm2/pkg/sdk/options.go index 4a00681c727..eb265567cd4 100644 --- a/tm2/pkg/sdk/options.go +++ b/tm2/pkg/sdk/options.go @@ -68,7 +68,7 @@ func (app *BaseApp) SetDB(db dbm.DB) { func (app *BaseApp) SetCMS(cms store.CommitMultiStore) { if app.sealed { - panic("SetEndBlocker() on sealed BaseApp") + panic("SetCMS() on sealed BaseApp") } app.cms = cms }