(t *testing.T)
| 57 | } |
| 58 | |
| 59 | func TestOperator_Function(t *testing.T) { |
| 60 | env := map[string]interface{}{ |
| 61 | "foo": Value{1}, |
| 62 | "bar": Value{2}, |
| 63 | } |
| 64 | |
| 65 | tests := []struct { |
| 66 | input string |
| 67 | want int |
| 68 | }{ |
| 69 | { |
| 70 | input: `foo + bar`, |
| 71 | want: 3, |
| 72 | }, |
| 73 | { |
| 74 | input: `2 + 4`, |
| 75 | want: 6, |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | for _, tt := range tests { |
| 80 | t.Run(fmt.Sprintf(`operator function helper test %s`, tt.input), func(t *testing.T) { |
| 81 | program, err := expr.Compile( |
| 82 | tt.input, |
| 83 | expr.Env(env), |
| 84 | expr.Operator("+", "Add", "AddInt"), |
| 85 | expr.Function("Add", func(args ...interface{}) (interface{}, error) { |
| 86 | return args[0].(Value).Int + args[1].(Value).Int, nil |
| 87 | }, |
| 88 | new(func(_ Value, __ Value) int), |
| 89 | ), |
| 90 | expr.Function("AddInt", func(args ...interface{}) (interface{}, error) { |
| 91 | return args[0].(int) + args[1].(int), nil |
| 92 | }, |
| 93 | new(func(_ int, __ int) int), |
| 94 | ), |
| 95 | ) |
| 96 | require.NoError(t, err) |
| 97 | |
| 98 | output, err := expr.Run(program, env) |
| 99 | require.NoError(t, err) |
| 100 | require.Equal(t, tt.want, output) |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | func TestOperator_Function_WithTypes(t *testing.T) { |
| 107 | env := map[string]interface{}{ |
nothing calls this directly
no test coverage detected
searching dependent graphs…