|
1 | 1 | package operator_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "testing"
|
5 | 6 | "time"
|
6 | 7 |
|
@@ -55,3 +56,169 @@ func TestOperator_interface(t *testing.T) {
|
55 | 56 | require.NoError(t, err)
|
56 | 57 | require.Equal(t, true, output)
|
57 | 58 | }
|
| 59 | + |
| 60 | +type Value struct { |
| 61 | + Int int |
| 62 | +} |
| 63 | + |
| 64 | +func TestOperator_Function(t *testing.T) { |
| 65 | + env := map[string]interface{}{ |
| 66 | + "foo": Value{1}, |
| 67 | + "bar": Value{2}, |
| 68 | + } |
| 69 | + |
| 70 | + tests := []struct { |
| 71 | + input string |
| 72 | + want int |
| 73 | + }{ |
| 74 | + { |
| 75 | + input: `foo + bar`, |
| 76 | + want: 3, |
| 77 | + }, |
| 78 | + { |
| 79 | + input: `2 + 4`, |
| 80 | + want: 6, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tt := range tests { |
| 85 | + t.Run(fmt.Sprintf(`opertor function helper test %s`, tt.input), func(t *testing.T) { |
| 86 | + program, err := expr.Compile( |
| 87 | + tt.input, |
| 88 | + expr.Env(env), |
| 89 | + expr.Operator("+", "Add", "AddInt"), |
| 90 | + expr.Function("Add", func(args ...interface{}) (interface{}, error) { |
| 91 | + return args[0].(Value).Int + args[1].(Value).Int, nil |
| 92 | + }, |
| 93 | + new(func(_ Value, __ Value) int), |
| 94 | + ), |
| 95 | + expr.Function("AddInt", func(args ...interface{}) (interface{}, error) { |
| 96 | + return args[0].(int) + args[1].(int), nil |
| 97 | + }, |
| 98 | + new(func(_ int, __ int) int), |
| 99 | + ), |
| 100 | + ) |
| 101 | + require.NoError(t, err) |
| 102 | + |
| 103 | + output, err := expr.Run(program, env) |
| 104 | + require.NoError(t, err) |
| 105 | + require.Equal(t, tt.want, output) |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | +} |
| 110 | + |
| 111 | +func TestOperator_Function_WithTypes(t *testing.T) { |
| 112 | + env := map[string]interface{}{ |
| 113 | + "foo": Value{1}, |
| 114 | + "bar": Value{2}, |
| 115 | + } |
| 116 | + |
| 117 | + require.PanicsWithError(t, `function Add for + operator misses types`, func() { |
| 118 | + _, _ = expr.Compile( |
| 119 | + `foo + bar`, |
| 120 | + expr.Env(env), |
| 121 | + expr.Operator("+", "Add", "AddInt"), |
| 122 | + expr.Function("Add", func(args ...interface{}) (interface{}, error) { |
| 123 | + return args[0].(Value).Int + args[1].(Value).Int, nil |
| 124 | + }), |
| 125 | + ) |
| 126 | + }) |
| 127 | + |
| 128 | + require.PanicsWithError(t, `function Add for + operator does not have a correct signature`, func() { |
| 129 | + _, _ = expr.Compile( |
| 130 | + `foo + bar`, |
| 131 | + expr.Env(env), |
| 132 | + expr.Operator("+", "Add", "AddInt"), |
| 133 | + expr.Function("Add", func(args ...interface{}) (interface{}, error) { |
| 134 | + return args[0].(Value).Int + args[1].(Value).Int, nil |
| 135 | + }, |
| 136 | + new(func(_ Value) int), |
| 137 | + ), |
| 138 | + ) |
| 139 | + }) |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +func TestOperator_FunctionOverTypesPrecedence(t *testing.T) { |
| 144 | + env := struct { |
| 145 | + Add func(a, b int) int |
| 146 | + }{ |
| 147 | + Add: func(a, b int) int { |
| 148 | + return a + b |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + program, err := expr.Compile( |
| 153 | + `1 + 2`, |
| 154 | + expr.Env(env), |
| 155 | + expr.Operator("+", "Add"), |
| 156 | + expr.Function("Add", func(args ...interface{}) (interface{}, error) { |
| 157 | + // Wierd function that returns 100 + a + b in testing purposes. |
| 158 | + return args[0].(int) + args[1].(int) + 100, nil |
| 159 | + }, |
| 160 | + new(func(_ int, __ int) int), |
| 161 | + ), |
| 162 | + ) |
| 163 | + require.NoError(t, err) |
| 164 | + |
| 165 | + output, err := expr.Run(program, env) |
| 166 | + require.NoError(t, err) |
| 167 | + require.Equal(t, 103, output) |
| 168 | +} |
| 169 | + |
| 170 | +func TestOperator_CanBeDefinedEitherInTypesOrInFunctions(t *testing.T) { |
| 171 | + env := struct { |
| 172 | + Add func(a, b int) int |
| 173 | + }{ |
| 174 | + Add: func(a, b int) int { |
| 175 | + return a + b |
| 176 | + }, |
| 177 | + } |
| 178 | + |
| 179 | + program, err := expr.Compile( |
| 180 | + `1 + 2`, |
| 181 | + expr.Env(env), |
| 182 | + expr.Operator("+", "Add", "AddValues"), |
| 183 | + expr.Function("AddValues", func(args ...interface{}) (interface{}, error) { |
| 184 | + return args[0].(Value).Int + args[1].(Value).Int, nil |
| 185 | + }, |
| 186 | + new(func(_ Value, __ Value) int), |
| 187 | + ), |
| 188 | + ) |
| 189 | + require.NoError(t, err) |
| 190 | + |
| 191 | + output, err := expr.Run(program, env) |
| 192 | + require.NoError(t, err) |
| 193 | + require.Equal(t, 3, output) |
| 194 | +} |
| 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