53 lines
964 B
Go
53 lines
964 B
Go
package vm
|
|
|
|
import (
|
|
"bytes"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"code.betamike.com/mediocregopher/ginger/gg"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestVM(t *testing.T) {
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|