Skip to content

Commit

Permalink
test(gnovm): in eval_test, test all files in gnovm/tests subdirs (#2754)
Browse files Browse the repository at this point in the history
Changes the EvalFiles tests to support sub-directories in `gnovm/tests`,
skipping the `extern` directory as it is used for imports.

Other changes mostly concern not having tests where some output lines
terminate with spaces:

- Changed FuncValue.String, so that in case of a closure value the
signature is printed, as shown in the `assign_unnamed_type` tests.
- Require all directives to be on a new line (mostly for consistency
with 99% of the testing corpus)
- use commas in some places instead of spaces; use `;` for end of line
on `Println` statements when they're supposed to print an empty string.
- remove the leading space in the `what the firetruck?` tests.

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [x] Provided any useful hints for running manual tests
- [x] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
thehowl committed Sep 12, 2024
1 parent 974dc43 commit bde8afc
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 44 deletions.
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/debugger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func evalTest(debugAddr, in, file string) (out, err, stacktrace string) {
if r := recover(); r != nil {
err = fmt.Sprintf("%v", r)
}
out = strings.TrimSpace(out)
out = strings.TrimSuffix(out, "\n")
err = strings.TrimSpace(strings.ReplaceAll(err, "../../tests/files/", "files/"))
}()

Expand Down
57 changes: 39 additions & 18 deletions gnovm/pkg/gnolang/eval_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package gnolang_test

import (
"io/fs"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"
)

func TestEvalFiles(t *testing.T) {
dir := "../../tests/files"
files, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
wantOut, wantErr, wantStacktrace, ok := testData(dir, f)
err := fs.WalkDir(os.DirFS(dir), ".", func(path string, de fs.DirEntry, err error) error {
switch {
case err != nil:
return err
case path == "extern":
return fs.SkipDir
case de.IsDir():
return nil
}

fullPath := filepath.Join(dir, path)
wantOut, wantErr, wantStacktrace, ok := testData(fullPath)
if !ok {
continue
return nil
}
t.Run(f.Name(), func(t *testing.T) {
out, err, stacktrace := evalTest("", "", path.Join(dir, f.Name()))

t.Run(path, func(t *testing.T) {
out, err, stacktrace := evalTest("", "", fullPath)

if wantErr != "" && !strings.Contains(err, wantErr) ||
wantErr == "" && err != "" {
Expand All @@ -34,15 +43,16 @@ func TestEvalFiles(t *testing.T) {
t.Fatalf("unexpected output\nWant: %s\n Got: %s", wantOut, out)
}
})

return nil
})
if err != nil {
t.Fatal(err)
}
}

// testData returns the expected output and error string, and true if entry is valid.
func testData(dir string, f os.DirEntry) (testOut, testErr, testStacktrace string, ok bool) {
if f.IsDir() {
return
}
name := path.Join(dir, f.Name())
func testData(name string) (testOut, testErr, testStacktrace string, ok bool) {
if !strings.HasSuffix(name, ".gno") || strings.HasSuffix(name, "_long.gno") {
return
}
Expand All @@ -54,8 +64,11 @@ func testData(dir string, f os.DirEntry) (testOut, testErr, testStacktrace strin
if strings.Contains(str, "// PKGPATH:") {
return
}

res := commentFrom(str, []string{"\n// Output:", "\n// Error:", "\n// Stacktrace:"})
res := commentFrom(str, []string{
"// Output:",
"// Error:",
"// Stacktrace:",
})

return res[0], res[1], res[2], true
}
Expand All @@ -66,12 +79,17 @@ type directive struct {
index int
}

// (?m) makes ^ and $ match start/end of string
var reCommentPrefix = regexp.MustCompile("(?m)^//(?: |$)")

// commentFrom returns the comments from s that are between the delimiters.
func commentFrom(s string, delims []string) []string {
directives := make([]directive, len(delims))
directivesFound := make([]*directive, 0, len(delims))

for i, delim := range delims {
// must find delim isolated on one line
delim = "\n" + delim + "\n"
index := strings.Index(s, delim)
directives[i] = directive{delim: delim, index: index}
if index >= 0 {
Expand All @@ -88,7 +106,10 @@ func commentFrom(s string, delims []string) []string {
next = directivesFound[i+1].index
}

directivesFound[i].res = strings.TrimSpace(strings.ReplaceAll(s[directivesFound[i].index+len(directivesFound[i].delim):next], "\n// ", "\n"))
parsed := reCommentPrefix.ReplaceAllLiteralString(
s[directivesFound[i].index+len(directivesFound[i].delim):next],
"")
directivesFound[i].res = strings.TrimSuffix(parsed, "\n")
}

res := make([]string, len(directives))
Expand Down
3 changes: 3 additions & 0 deletions gnovm/pkg/gnolang/values_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ func (fv *FuncValue) String() string {
if fv.Type == nil {
return fmt.Sprintf("incomplete-func ?%s(?)?", name)
}
if name == "" {
return fmt.Sprintf("%s{...}", fv.Type.String())
}
return name
}

Expand Down
4 changes: 2 additions & 2 deletions gnovm/tests/files/a31.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

func main() {
for range []int{0, 1, 2} {
print("hello ")
print("hello,")
}
println("")
}

// Output:
// hello hello hello
// hello,hello,hello,
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func main() {

// Output:
// 1 (inc main.op)
// 0
// 0 func(n int)( int){...}
// -1 dec
// 0 ( main.op)
// 0 (func(n int)( int){...} main.op)
// -1 dec
// 1 inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
}

// Output:
// func(n int)( int){...}
// 1
// ( main.op)
// (func(n int)( int){...} main.op)
// -1
3 changes: 2 additions & 1 deletion gnovm/tests/files/convert4.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ func main() {
println(int(nil))
}

// Error: cannot convert (undefined) to int
// Error:
// main/files/convert4.gno:4:10: cannot convert (undefined) to int
3 changes: 2 additions & 1 deletion gnovm/tests/files/convert5.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ func main() {
println(ints)
}

// Error: cannot convert (undefined) to int
// Error:
// main/files/convert5.gno:3:1: cannot convert (undefined) to int
16 changes: 8 additions & 8 deletions gnovm/tests/files/defer6.gno
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package main

func f1() {
defer print("f1-begin ")
defer print("f1-begin,")
f2()
defer print("f1-end ")
defer print("f1-end,")
}

func f2() {
defer print("f2-begin ")
defer print("f2-begin,")
f3()
defer print("f2-end ")
defer print("f2-end,")
}

func f3() {
defer print("f3-begin ")
print("hello ")
defer print("f3-end ")
defer print("f3-begin,")
print("hello,")
defer print("f3-end,")
}

func main() {
Expand All @@ -24,4 +24,4 @@ func main() {
}

// Output:
// hello f3-end f3-begin f2-end f2-begin f1-end f1-begin
// hello,f3-end,f3-begin,f2-end,f2-begin,f1-end,f1-begin,
4 changes: 2 additions & 2 deletions gnovm/tests/files/map15.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ func main() {
users := make(map[string]string)

v := users["a"]
fmt.Println("v:", v)
fmt.Println("v:", v, ";")
}

// Output:
// v:
// v: ;
4 changes: 2 additions & 2 deletions gnovm/tests/files/method27.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type AuthenticatedRequest struct {

func main() {
a := &AuthenticatedRequest{}
fmt.Println("ua:", a.UserAgent())
fmt.Println("ua:", a.UserAgent(), ";")
}

// Output:
// ua:
// ua: ;
2 changes: 1 addition & 1 deletion gnovm/tests/files/types/cmp_iface_2.gno
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (e Error) Error() string {
func main() {
var e0 E
e0 = Error(0)
fmt.Printf("%T \n", e0)
fmt.Printf("%T\n", e0)
if e0 == Error(0) {
println("what the firetruck?")
} else {
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/types/eql_0f2d.gno
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (e Error) Error() string {
func main() {
var e0 E
e0 = Error(0)
fmt.Printf("%T \n", e0)
fmt.Printf("%T\n", e0)
if e0 == Error(0) {
println("what the firetruck?")
} else {
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/types/eql_0f2e.gno
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (e Error) Error() string {
func main() {
var e0 E
e0 = Error(0)
fmt.Printf("%T \n", e0)
fmt.Printf("%T\n", e0)
if Error(0) == e0 {
println("what the firetruck?")
} else {
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/types/runtime_a2.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func gen() interface{} {

func main() {
r := gen()
fmt.Printf("%T \n", r)
fmt.Printf("%T\n", r)
}

// Output:
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/types/shift_a12.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ func main() {
a := uint(1)
r := uint64(1) << a
println(r)
fmt.Printf("%T \n", r)
fmt.Printf("%T\n", r)
}

// Output:
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/types/shift_a13.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "fmt"

func main() {
var r uint64 = 1 << int8(1)
fmt.Printf("%T \n", r)
fmt.Printf("%T\n", r)
println(r)
}

Expand Down

0 comments on commit bde8afc

Please sign in to comment.