Skip to content

Commit d9b833c

Browse files
fix: add test
1 parent f499da0 commit d9b833c

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

conf/operators.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ func FindSuitableOperatorOverload(fns []string, types TypesTable, funcs Function
2020

2121
func FindSuitableOperatorOverloadInTypes(fns []string, types TypesTable, l, r reflect.Type) (reflect.Type, string, bool) {
2222
for _, fn := range fns {
23-
fnType := types[fn]
23+
fnType, ok := types[fn]
24+
if !ok {
25+
continue
26+
}
2427
firstInIndex := 0
2528
if fnType.Method {
2629
firstInIndex = 1 // As first argument to method is receiver.

test/operator/operator_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,33 @@ func TestOperator_CanBeDefinedEitherInTypesOrInFunctions(t *testing.T) {
192192
require.NoError(t, err)
193193
require.Equal(t, 3, output)
194194
}
195+
196+
func TestOperator_Polymorphic(t *testing.T) {
197+
env := struct {
198+
Add func(a, b int) int
199+
Foo Value
200+
Bar Value
201+
}{
202+
Add: func(a, b int) int {
203+
return a + b
204+
},
205+
Foo: Value{1},
206+
Bar: Value{2},
207+
}
208+
209+
program, err := expr.Compile(
210+
`1 + 2 + (Foo + Bar)`,
211+
expr.Env(env),
212+
expr.Operator("+", "Add", "AddValues"),
213+
expr.Function("AddValues", func(args ...interface{}) (interface{}, error) {
214+
return args[0].(Value).Int + args[1].(Value).Int, nil
215+
},
216+
new(func(_ Value, __ Value) int),
217+
),
218+
)
219+
require.NoError(t, err)
220+
221+
output, err := expr.Run(program, env)
222+
require.NoError(t, err)
223+
require.Equal(t, 6, output)
224+
}

0 commit comments

Comments
 (0)