module support
Some checks failed
Build / build (push) Successful in 10m22s
Test / build (push) Failing after 15m54s

This commit is contained in:
Chuck Smith
2024-03-26 16:49:38 -04:00
parent 6d234099d1
commit 110152a139
21 changed files with 541 additions and 100 deletions

View File

@@ -2,14 +2,51 @@ package evaluator
import (
"errors"
"github.com/stretchr/testify/assert"
"monkey/lexer"
"monkey/object"
"monkey/parser"
"monkey/utils"
"os"
"path/filepath"
"testing"
)
func assertEvaluated(t *testing.T, expected interface{}, actual object.Object) {
t.Helper()
assert := assert.New(t)
switch expected.(type) {
case nil:
if _, ok := actual.(*object.Null); ok {
assert.True(ok)
} else {
assert.Equal(expected, actual)
}
case int:
if i, ok := actual.(*object.Integer); ok {
assert.Equal(int64(expected.(int)), i.Value)
} else {
assert.Equal(expected, actual)
}
case error:
if e, ok := actual.(*object.Integer); ok {
assert.Equal(expected.(error).Error(), e.Value)
} else {
assert.Equal(expected, actual)
}
case string:
if s, ok := actual.(*object.String); ok {
assert.Equal(expected.(string), s.Value)
} else {
assert.Equal(expected, actual)
}
default:
t.Fatalf("unsupported type for expected got=%T", expected)
}
}
func TestEvalExpressions(t *testing.T) {
tests := []struct {
input string
@@ -814,6 +851,37 @@ func testStringObject(t *testing.T, obj object.Object, expected string) bool {
}
return true
}
func TestImportExpressions(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{`mod := import("../testdata/mod"); mod.A`, 5},
{`mod := import("../testdata/mod"); mod.Sum(2, 3)`, 5},
{`mod := import("../testdata/mod"); mod.a`, nil},
}
for _, tt := range tests {
evaluated := testEval(tt.input)
assertEvaluated(t, tt.expected, evaluated)
}
}
func TestImportSearchPaths(t *testing.T) {
utils.AddPath("../testdata")
tests := []struct {
input string
expected interface{}
}{
{`mod := import("mod"); mod.A`, 5},
}
for _, tt := range tests {
evaluated := testEval(tt.input)
assertEvaluated(t, tt.expected, evaluated)
}
}
func TestExamples(t *testing.T) {
matches, err := filepath.Glob("../examples/*.monkey")