More tests
This commit is contained in:
7
vm/vm.go
7
vm/vm.go
@@ -316,6 +316,13 @@ func (vm *VM) Run() error {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if vm.Debug {
|
||||
log.Printf(
|
||||
"%-25s [ip=%02d fp=%02d, sp=%02d]",
|
||||
"", ip, vm.framesIndex-1, vm.sp,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -8,7 +8,9 @@ import (
|
||||
"monkey/object"
|
||||
"monkey/parser"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -831,6 +833,47 @@ func TestAssignmentStatements(t *testing.T) {
|
||||
runVmTests(t, tests)
|
||||
}
|
||||
|
||||
func TestIntegration(t *testing.T) {
|
||||
matches, err := filepath.Glob("../testdata/*.monkey")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
for _, match := range matches {
|
||||
basename := path.Base(match)
|
||||
name := strings.TrimSuffix(basename, filepath.Ext(basename))
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
b, err := os.ReadFile(match)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
input := string(b)
|
||||
program := parse(input)
|
||||
|
||||
c := compiler.New()
|
||||
err = c.Compile(program)
|
||||
if err != nil {
|
||||
t.Log(input)
|
||||
t.Fatalf("compiler error: %s", err)
|
||||
}
|
||||
|
||||
vm := New(c.Bytecode())
|
||||
|
||||
err = vm.Run()
|
||||
if err != nil {
|
||||
t.Log(input)
|
||||
t.Fatalf("vm error: %s", err)
|
||||
}
|
||||
if vm.sp != 0 {
|
||||
t.Log(input)
|
||||
t.Fatal("vm stack pointer non-zero")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExamples(t *testing.T) {
|
||||
matches, err := filepath.Glob("../examples/*.monkey")
|
||||
if err != nil {
|
||||
@@ -838,32 +881,36 @@ func TestExamples(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, match := range matches {
|
||||
b, err := os.ReadFile(match)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
basename := path.Base(match)
|
||||
name := strings.TrimSuffix(basename, filepath.Ext(basename))
|
||||
|
||||
input := string(b)
|
||||
program := parse(input)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
b, err := os.ReadFile(match)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
c := compiler.New()
|
||||
err = c.Compile(program)
|
||||
if err != nil {
|
||||
t.Log(input)
|
||||
t.Fatalf("compiler error: %s", err)
|
||||
}
|
||||
input := string(b)
|
||||
program := parse(input)
|
||||
|
||||
vm := New(c.Bytecode())
|
||||
c := compiler.New()
|
||||
err = c.Compile(program)
|
||||
if err != nil {
|
||||
t.Log(input)
|
||||
t.Fatalf("compiler error: %s", err)
|
||||
}
|
||||
|
||||
err = vm.Run()
|
||||
if err != nil {
|
||||
t.Log(input)
|
||||
t.Fatalf("vm error: %s", err)
|
||||
}
|
||||
vm := New(c.Bytecode())
|
||||
|
||||
if vm.sp != 0 {
|
||||
t.Log(input)
|
||||
t.Fatal("vm stack pointer non-zero")
|
||||
}
|
||||
err = vm.Run()
|
||||
if err != nil {
|
||||
t.Log(input)
|
||||
t.Fatalf("vm error: %s", err)
|
||||
}
|
||||
if vm.sp != 0 {
|
||||
t.Log(input)
|
||||
t.Fatal("vm stack pointer non-zero")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user