2021-12-28 20:18:01 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-10-29 20:42:41 +00:00
|
|
|
"strconv"
|
2021-12-28 20:18:01 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-10-28 07:53:37 +00:00
|
|
|
"code.betamike.com/mediocregopher/ginger/gg"
|
2021-12-28 20:18:01 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-12-28 20:18:01 +00:00
|
|
|
}
|