Skip to content

Commit

Permalink
fix: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaymatrosov committed Feb 17, 2024
1 parent f499da0 commit d9b833c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion conf/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func FindSuitableOperatorOverload(fns []string, types TypesTable, funcs Function

func FindSuitableOperatorOverloadInTypes(fns []string, types TypesTable, l, r reflect.Type) (reflect.Type, string, bool) {
for _, fn := range fns {
fnType := types[fn]
fnType, ok := types[fn]
if !ok {
continue
}
firstInIndex := 0
if fnType.Method {
firstInIndex = 1 // As first argument to method is receiver.
Expand Down
30 changes: 30 additions & 0 deletions test/operator/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,33 @@ func TestOperator_CanBeDefinedEitherInTypesOrInFunctions(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 3, output)
}

func TestOperator_Polymorphic(t *testing.T) {
env := struct {
Add func(a, b int) int
Foo Value
Bar Value
}{
Add: func(a, b int) int {
return a + b
},
Foo: Value{1},
Bar: Value{2},
}

program, err := expr.Compile(
`1 + 2 + (Foo + Bar)`,
expr.Env(env),
expr.Operator("+", "Add", "AddValues"),
expr.Function("AddValues", func(args ...interface{}) (interface{}, error) {
return args[0].(Value).Int + args[1].(Value).Int, nil
},
new(func(_ Value, __ Value) int),
),
)
require.NoError(t, err)

output, err := expr.Run(program, env)
require.NoError(t, err)
require.Equal(t, 6, output)
}

0 comments on commit d9b833c

Please sign in to comment.