MCPcopy
hub / github.com/expr-lang/expr / TestOperator_Function

Function TestOperator_Function

test/operator/operator_test.go:59–104  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

57}
58
59func 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
106func TestOperator_Function_WithTypes(t *testing.T) {
107 env := map[string]interface{}{

Callers

nothing calls this directly

Calls 9

CompileFunction · 0.92
EnvStruct · 0.92
OperatorFunction · 0.92
FunctionFunction · 0.92
NoErrorFunction · 0.92
RunFunction · 0.92
EqualFunction · 0.92
RunMethod · 0.80
SprintfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…