github.com/expr-lang/expr@v1.16.9/patcher/value/value_test.go (about) 1 package value 2 3 import ( 4 "testing" 5 6 "github.com/expr-lang/expr/internal/testify/require" 7 8 "github.com/expr-lang/expr" 9 "github.com/expr-lang/expr/vm" 10 ) 11 12 type customInt struct { 13 Int int 14 } 15 16 func (v *customInt) AsInt() int { 17 return v.Int 18 } 19 20 func (v *customInt) AsAny() any { 21 return v.Int 22 } 23 24 type customTypedInt struct { 25 Int int 26 } 27 28 func (v *customTypedInt) AsInt() int { 29 return v.Int 30 } 31 32 type customUntypedInt struct { 33 Int int 34 } 35 36 func (v *customUntypedInt) AsAny() any { 37 return v.Int 38 } 39 40 type customString struct { 41 String string 42 } 43 44 func (v *customString) AsString() string { 45 return v.String 46 } 47 48 func (v *customString) AsAny() any { 49 return v.String 50 } 51 52 type customTypedString struct { 53 String string 54 } 55 56 func (v *customTypedString) AsString() string { 57 return v.String 58 } 59 60 type customUntypedString struct { 61 String string 62 } 63 64 func (v *customUntypedString) AsAny() any { 65 return v.String 66 } 67 68 type customTypedArray struct { 69 Array []any 70 } 71 72 func (v *customTypedArray) AsArray() []any { 73 return v.Array 74 } 75 76 type customTypedMap struct { 77 Map map[string]any 78 } 79 80 func (v *customTypedMap) AsMap() map[string]any { 81 return v.Map 82 } 83 84 func Test_valueAddInt(t *testing.T) { 85 env := make(map[string]any) 86 env["ValueOne"] = &customInt{1} 87 env["ValueTwo"] = &customInt{2} 88 89 program, err := expr.Compile("ValueOne + ValueTwo", expr.Env(env), ValueGetter) 90 require.NoError(t, err) 91 92 out, err := vm.Run(program, env) 93 94 require.NoError(t, err) 95 require.Equal(t, 3, out.(int)) 96 } 97 98 func Test_valueUntypedAddInt(t *testing.T) { 99 env := make(map[string]any) 100 env["ValueOne"] = &customUntypedInt{1} 101 env["ValueTwo"] = &customUntypedInt{2} 102 103 program, err := expr.Compile("ValueOne + ValueTwo", expr.Env(env), ValueGetter) 104 require.NoError(t, err) 105 106 out, err := vm.Run(program, env) 107 108 require.NoError(t, err) 109 require.Equal(t, 3, out.(int)) 110 } 111 112 func Test_valueTypedAddInt(t *testing.T) { 113 env := make(map[string]any) 114 env["ValueOne"] = &customTypedInt{1} 115 env["ValueTwo"] = &customTypedInt{2} 116 117 program, err := expr.Compile("ValueOne + ValueTwo", expr.Env(env), ValueGetter) 118 require.NoError(t, err) 119 120 out, err := vm.Run(program, env) 121 122 require.NoError(t, err) 123 require.Equal(t, 3, out.(int)) 124 } 125 126 func Test_valueTypedAddMismatch(t *testing.T) { 127 env := make(map[string]any) 128 env["ValueOne"] = &customTypedInt{1} 129 env["ValueTwo"] = &customTypedString{"test"} 130 131 _, err := expr.Compile("ValueOne + ValueTwo", expr.Env(env), ValueGetter) 132 require.Error(t, err) 133 } 134 135 func Test_valueUntypedAddMismatch(t *testing.T) { 136 env := make(map[string]any) 137 env["ValueOne"] = &customUntypedInt{1} 138 env["ValueTwo"] = &customUntypedString{"test"} 139 140 program, err := expr.Compile("ValueOne + ValueTwo", expr.Env(env), ValueGetter) 141 require.NoError(t, err) 142 143 _, err = vm.Run(program, env) 144 145 require.Error(t, err) 146 } 147 148 func Test_valueTypedArray(t *testing.T) { 149 env := make(map[string]any) 150 env["ValueOne"] = &customTypedArray{[]any{1, 2}} 151 152 program, err := expr.Compile("ValueOne[0] + ValueOne[1]", expr.Env(env), ValueGetter) 153 require.NoError(t, err) 154 155 out, err := vm.Run(program, env) 156 157 require.NoError(t, err) 158 require.Equal(t, 3, out.(int)) 159 } 160 161 func Test_valueTypedMap(t *testing.T) { 162 env := make(map[string]any) 163 env["ValueOne"] = &customTypedMap{map[string]any{"one": 1, "two": 2}} 164 165 program, err := expr.Compile("ValueOne.one + ValueOne.two", expr.Env(env), ValueGetter) 166 require.NoError(t, err) 167 168 out, err := vm.Run(program, env) 169 170 require.NoError(t, err) 171 require.Equal(t, 3, out.(int)) 172 }