Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Nov 19, 2024
1 parent 694ec88 commit 9c2f3c2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 48 deletions.
63 changes: 19 additions & 44 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"context"
"encoding/json"
"flag"
Expand All @@ -14,8 +13,8 @@ import (
"path/filepath"
"runtime/debug"
"sort"
"strconv"
"strings"
"text/template"
"time"

"github.com/gnolang/gno/gnovm"
Expand Down Expand Up @@ -462,11 +461,6 @@ func (cfg *testPkgCfg) runTestFiles(
}
loadTestFuncs(memPkg.Name, testFuncs, files)

testmain, err := formatTestmain(testFuncs)
if err != nil {
log.Fatal(err)
}

var alloc *gno.Allocator

// run package and write to upper cw / gs
Expand All @@ -486,7 +480,16 @@ func (cfg *testPkgCfg) runTestFiles(
pv := m.Package

m.RunFiles(files.Files...)
n := gno.MustParseFile("main_test.gno", testmain)
n := gno.MustParseFile("main_test.gno", `package `+memPkg.Name+`
import (
"testing"
)
func gnointernal_runtest(runFlag string, verbose bool, testName string, testFunc func(t *testing.T)) string {
return testing.RunTest(runFlag, verbose, testing.InternalTest{testName, testFunc})
}
`)
m.RunFiles(n)

for _, tf := range testFuncs.Tests {
Expand All @@ -502,9 +505,13 @@ func (cfg *testPkgCfg) runTestFiles(
m.Alloc = alloc
m.SetActivePackage(pv)

testFuncStr := fmt.Sprintf("%q", tf.Name)

eval := m.Eval(gno.Call("runtest", testFuncStr))
eval := m.Eval(gno.Call(
"gnointernal_runtest",
gno.Str(runFlag),
gno.Nx(strconv.FormatBool(cfg.verbose)),
gno.Str(tf.Name),
gno.Nx(tf.Name),
))

if cfg.printEvents {
events := m.Context.(*teststd.TestExecContext).EventLogger.Events()
Expand All @@ -527,7 +534,7 @@ func (cfg *testPkgCfg) runTestFiles(

// TODO: replace with amino or send native type?
var rep report
err = json.Unmarshal([]byte(ret), &rep)
err := json.Unmarshal([]byte(ret), &rep)
if err != nil {
errs = multierr.Append(errs, err)
cfg.io.ErrPrintfln("--- FAIL: %s [internal gno testing error]", tf.Name)
Expand Down Expand Up @@ -566,30 +573,6 @@ type report struct {
Skipped bool
}

var testmainTmpl = template.Must(template.New("testmain").Parse(`
package {{ .PackageName }}
import (
"testing"
)
var tests = []testing.InternalTest{
{{range .Tests}}
{"{{.Name}}", {{.Name}}},
{{end}}
}
func runtest(name string) (report string) {
for _, test := range tests {
if test.Name == name {
return testing.RunTest({{printf "%q" .RunFlag}}, {{.Verbose}}, test)
}
}
panic("no such test: " + name)
return ""
}
`))

type testFuncs struct {
Tests []testFunc
PackageName string
Expand All @@ -609,14 +592,6 @@ func getPkgNameFromFileset(files *gno.FileSet) string {
return string(files.Files[0].PkgName)
}

func formatTestmain(t *testFuncs) (string, error) {
var buf bytes.Buffer
if err := testmainTmpl.Execute(&buf, t); err != nil {
return "", err
}
return buf.String(), nil
}

func loadTestFuncs(pkgName string, t *testFuncs, tfiles *gno.FileSet) *testFuncs {
for _, tf := range tfiles.Files {
for _, d := range tf.Decls {
Expand Down
3 changes: 1 addition & 2 deletions gnovm/stdlibs/strconv/example_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package strconv_test

import (
"fmt"
"log"
"strconv"
)

Expand Down Expand Up @@ -409,7 +408,7 @@ func ExampleUnquote() {
func ExampleUnquoteChar() {
v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
if err != nil {
log.Fatal(err)
panic(err)
}

fmt.Println("value:", string(v))
Expand Down
13 changes: 11 additions & 2 deletions gnovm/tests/stdlibs/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ func IsOriginCall(m *gno.Machine) bool {
tname := m.Frames[0].Func.Name
switch tname {
case "main": // test is a _filetest
// 0. main
// 1. $RealmFuncName
// 2. std.IsOriginCall
return len(m.Frames) == 3
case "runtest": // test is a _test
return len(m.Frames) == 7
case "gnointernal_runtest": // test is a _test
// 0. gnointernal_runtest
// 1. testing.RunTest
// 2. tRunner
// 3. $TestFuncName
// 4. $RealmFuncName
// 5. std.IsOriginCall
return len(m.Frames) == 6
}
// support init() in _filetest
// XXX do we need to distinguish from 'runtest'/_test?
Expand Down

0 comments on commit 9c2f3c2

Please sign in to comment.