Files
chaisql/internal/expr/functions/scalar_definition_test.go
Asdine El Hrychy f966172cee Introduce Value interface (#422)
This replaces the Value struct by an interface to allow us to override some
values behavior in the future.
It also introduces a new package types, which contains type definitions, comparison,
and arithmetics.
Concerning encoding, Genji now only uses on type of encoding for values. This simplifies
indexing logic as well as table access in general.
2021-07-21 22:05:44 +04:00

61 lines
1.6 KiB
Go

package functions_test
import (
"testing"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/internal/environment"
"github.com/genjidb/genji/internal/expr"
"github.com/genjidb/genji/internal/expr/functions"
"github.com/genjidb/genji/types"
"github.com/stretchr/testify/require"
)
func TestScalarFunctionDef(t *testing.T) {
def := functions.NewScalarDefinition(
"foo",
3,
func(args ...types.Value) (types.Value, error) {
arg1 := args[0].V().(int64)
arg2 := args[1].V().(int64)
arg3 := args[2].V().(int64)
return types.NewIntegerValue(arg1 + arg2 + arg3), nil
},
)
t.Run("Name()", func(t *testing.T) {
require.Equal(t, "foo", def.Name())
})
t.Run("Arity()", func(t *testing.T) {
require.Equal(t, 3, def.Arity())
})
t.Run("String()", func(t *testing.T) {
require.Equal(t, "foo(arg1, arg2, arg3)", def.String())
})
t.Run("Function()", func(t *testing.T) {
fb := document.NewFieldBuffer()
fb = fb.Add("a", types.NewIntegerValue(2))
env := environment.New(fb)
expr1 := expr.Add(expr.LiteralValue{Value: types.NewIntegerValue(1)}, expr.LiteralValue{Value: types.NewIntegerValue(0)})
expr2 := expr.Path(document.NewPath("a"))
expr3 := expr.Div(expr.LiteralValue{Value: types.NewIntegerValue(6)}, expr.LiteralValue{Value: types.NewIntegerValue(2)})
t.Run("OK", func(t *testing.T) {
fexpr, err := def.Function(expr1, expr2, expr3)
require.NoError(t, err)
v, err := fexpr.Eval(env)
require.NoError(t, err)
require.Equal(t, types.NewIntegerValue(1+2+3), v)
})
t.Run("NOK", func(t *testing.T) {
_, err := def.Function(expr1, expr2)
require.Error(t, err)
})
})
}