| 2099 | } |
| 2100 | |
| 2101 | func TestFunction(t *testing.T) { |
| 2102 | add := expr.Function( |
| 2103 | "add", |
| 2104 | func(p ...any) (any, error) { |
| 2105 | out := 0 |
| 2106 | for _, each := range p { |
| 2107 | out += each.(int) |
| 2108 | } |
| 2109 | return out, nil |
| 2110 | }, |
| 2111 | new(func(...int) int), |
| 2112 | ) |
| 2113 | |
| 2114 | p, err := expr.Compile(`add() + add(1) + add(1, 2) + add(1, 2, 3) + add(1, 2, 3, 4)`, add) |
| 2115 | assert.NoError(t, err) |
| 2116 | |
| 2117 | out, err := expr.Run(p, nil) |
| 2118 | assert.NoError(t, err) |
| 2119 | assert.Equal(t, 20, out) |
| 2120 | } |
| 2121 | |
| 2122 | // Nil coalescing operator |
| 2123 | func TestRun_NilCoalescingOperator(t *testing.T) { |