ginger/vm/vm_test.go

53 lines
964 B
Go
Raw Normal View History

package vm
import (
"bytes"
2023-10-29 20:42:41 +00:00
"strconv"
"testing"
2023-10-28 07:53:37 +00:00
"code.betamike.com/mediocregopher/ginger/gg"
"github.com/stretchr/testify/assert"
)
func TestVM(t *testing.T) {
2023-10-29 20:42:41 +00:00
tests := []struct {
src string
in Value
exp Value
expErr string
}{
{
src: `{
incr = { !out = !add < (1, !in); };
!out = incr < incr < !in;
}`,
in: Value{Value: gg.Number(5)},
exp: Value{Value: gg.Number(7)},
},
{
src: `{
!foo = in;
!out = !foo;
}`,
in: Value{Value: gg.Number(1)},
expErr: "name !foo cannot start with a '!'",
},
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
val, err := EvaluateSource(
bytes.NewBufferString(test.src), test.in, GlobalScope,
)
if test.expErr != "" {
assert.Error(t, err)
assert.Equal(t, test.expErr, err.Error())
} else {
assert.NoError(t, err)
assert.True(t, val.Equal(test.exp), "%v != %v", test.exp, val)
}
})
}
}