chore: run _test/*.go as tests, not examples (#134)

This commit is contained in:
Marc Vertes
2019-03-28 17:38:51 +01:00
committed by Ludovic Fernandez
parent 47faed9853
commit d055747bef
17 changed files with 173 additions and 8172 deletions

View File

@@ -13,10 +13,7 @@ gen_all_syscall: cmd/goexports/goexports
cmd/goexports/goexports: cmd/goexports/goexports.go
go generate cmd/goexports/goexports.go
gen_tests:
make -C _test
generate: gen_all_syscall gen_tests
generate: gen_all_syscall
go generate
.PHONY: check gen_all_syscall gen_tests

View File

@@ -1,6 +0,0 @@
tests = $(wildcard *.go)
generate:
./gen_example.sh | gofmt > ../interp/interp_test.go
.PHONY: generate

View File

@@ -4,3 +4,6 @@ func main() {
f := println
f("Hello")
}
// Error:
// 4:7: use of builtin println not in function call

View File

@@ -31,7 +31,7 @@ func server(ln net.Listener, ready chan bool) {
}
func main() {
ln, err := net.Listen("tcp", ":0")
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
log.Fatal(err)
}

View File

@@ -3,3 +3,6 @@ package main
func Test() {
println("Hello from test")
}
// Output:
//

View File

@@ -5,3 +5,6 @@ type Sample struct{ Name string }
func (s *Sample) Test() {
println("Hello from test", s.Name)
}
// Output:
//

View File

@@ -1,34 +0,0 @@
#!/bin/sh
export LANG=C
echo '// Code Generated by ../_test/gen_example.sh. DO NOT EDIT.'
echo
echo 'package interp_test'
echo
echo 'import ('
echo ' "github.com/containous/yaegi/interp"'
echo ' "github.com/containous/yaegi/stdlib"'
echo ')'
echo
for file in *.go
do
awk '
$0 == "// Output:" { done = 1 }
{ if (done) out = out "\n" $0; else src = src "\n" $0 }
END {
print "func Example_'${file%.*}'() {"
print "src := `" src "`"
print "i := interp.New(interp.Opt{Entry: \"main\"})"
print "i.Use(stdlib.Value)"
print "_, err := i.Eval(src)"
print "if err != nil {"
print " panic(err)"
print "}"
print out
print "}"
}
' $file
echo
done

View File

@@ -14,3 +14,7 @@ func main() {
println(a.count)
run(a, "truc")
}
// Output:
// 2
// 2 truc

View File

@@ -7,3 +7,44 @@ func main() {
}
}
}
// Output:
// 2048
// 2304
// 2560
// 2816
// 3072
// 3328
// 3584
// 3840
// 6144
// 6400
// 6656
// 6912
// 7168
// 7424
// 7680
// 7936
// 10240
// 10496
// 10752
// 11008
// 11264
// 11520
// 11776
// 12032
// 14336
// 14592
// 14848
// 15104
// 15360
// 15616
// 15872
// 16128
// 18432
// 18688
// 18944
// 19200
// 19456
// 19712
// 19968

View File

@@ -5,3 +5,6 @@ func main() {
a += 1.3
println(a)
}
// Error:
// 5:2: illegal operand types for '+=' operator

View File

@@ -3,3 +3,6 @@ package main
func f(i int) (o int) { o = i + 1; return }
func main() { println(f(4)) }
// Output:
// 5

View File

@@ -12,3 +12,6 @@ func main() {
println("unknown")
}
}
// Error:
// 9:2: i is not a type

View File

@@ -46,3 +46,6 @@ func main() {
inCall(boo)
inCall(Bar{})
}
// Error:
// 37:2: duplicate case Bir in type switch

View File

@@ -5,3 +5,6 @@ func main() {
fallthrough
println("world")
}
// Error:
// 5:2: fallthrough statement out of place

View File

@@ -11,3 +11,6 @@ func main() {
println("unknown")
}
}
// Error:
// 9:3: cannot fallthrough in type switch

View File

@@ -0,0 +1,99 @@
package interp_test
import (
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/containous/yaegi/interp"
"github.com/containous/yaegi/stdlib"
)
func TestFile(t *testing.T) {
filePath := "../_test/str.go"
runCheck(t, filePath)
baseDir := filepath.Join("..", "_test")
files, err := ioutil.ReadDir(baseDir)
if err != nil {
t.Fatal(err)
}
for _, file := range files {
if filepath.Ext(file.Name()) != ".go" {
continue
}
file := file
t.Run(file.Name(), func(t *testing.T) {
runCheck(t, filepath.Join(baseDir, file.Name()))
})
}
}
func runCheck(t *testing.T, p string) {
wanted, errWanted := wantedFromComment(p)
if wanted == "" {
t.Skip(p, "has no comment 'Output:' or 'Error:'")
}
wanted = strings.TrimSpace(wanted)
src, err := ioutil.ReadFile(p)
if err != nil {
t.Fatal(err)
}
// catch stdout
backupStdout := os.Stdout
defer func() { os.Stdout = backupStdout }()
r, w, _ := os.Pipe()
os.Stdout = w
i := interp.New(interp.Opt{Entry: "main"})
i.Use(stdlib.Value)
_, err = i.Eval(string(src))
if errWanted {
if err == nil {
t.Fatalf("got nil error, want: %q", wanted)
}
if res := strings.TrimSpace(err.Error()); res != wanted {
t.Errorf("got %q, want: %q", res, wanted)
}
return
}
if err != nil {
t.Fatal(err)
}
// read stdout
if err = w.Close(); err != nil {
t.Fatal(err)
}
outInterp, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
if res := strings.TrimSpace(string(outInterp)); res != wanted {
t.Errorf("\ngot: %q,\nwant: %q", res, wanted)
}
}
func wantedFromComment(p string) (res string, err bool) {
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, p, nil, parser.ParseComments)
if len(f.Comments) == 0 {
return
}
text := f.Comments[len(f.Comments)-1].Text()
if strings.HasPrefix(text, "Output:\n") {
return strings.TrimPrefix(text, "Output:\n"), false
}
if strings.HasPrefix(text, "Error:\n") {
return strings.TrimPrefix(text, "Error:\n"), true
}
return
}

File diff suppressed because it is too large Load Diff