| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | "github.com/tomwright/dasel/v3/model/orderedmap" |
| 8 | ) |
| 9 | |
| 10 | func TestBinary(t *testing.T) { |
| 11 | t.Run("math", func(t *testing.T) { |
| 12 | t.Run("literals", func(t *testing.T) { |
| 13 | t.Run("addition", testCase{ |
| 14 | s: `1 + 2`, |
| 15 | out: model.NewIntValue(3), |
| 16 | }.run) |
| 17 | t.Run("subtraction", testCase{ |
| 18 | s: `5 - 2`, |
| 19 | out: model.NewIntValue(3), |
| 20 | }.run) |
| 21 | t.Run("multiplication", testCase{ |
| 22 | s: `5 * 2`, |
| 23 | out: model.NewIntValue(10), |
| 24 | }.run) |
| 25 | t.Run("division", testCase{ |
| 26 | s: `10 / 2`, |
| 27 | out: model.NewIntValue(5), |
| 28 | }.run) |
| 29 | t.Run("modulus", testCase{ |
| 30 | s: `10 % 3`, |
| 31 | out: model.NewIntValue(1), |
| 32 | }.run) |
| 33 | t.Run("ordering", testCase{ |
| 34 | s: `45.2 + 5 * 4 - 2 / 2`, // 45.2 + (5 * 4) - (2 / 2) = 45.2 + 20 - 1 = 64.2 |
| 35 | out: model.NewFloatValue(64.2), |
| 36 | }.run) |
| 37 | t.Run("ordering with groups", testCase{ |
| 38 | s: `(45.2 + 5) * ((4 - 2) / 2)`, // (45.2 + 5) * ((4 - 2) / 2) = (50.2) * ((2) / 2) = (50.2) * (1) = 50.2 |
| 39 | out: model.NewFloatValue(50.2), |
| 40 | }.run) |
| 41 | t.Run("ordering with groups", testCase{ |
| 42 | s: `1 + 1 - 1 + 1 * 2`, // 1 + 1 - 1 + (1 * 2) = 1 + 1 - 1 + 2 = 3 |
| 43 | out: model.NewIntValue(3), |
| 44 | }.run) |
| 45 | }) |
| 46 | t.Run("variables", func(t *testing.T) { |
| 47 | in := func() *model.Value { |
| 48 | return model.NewValue(orderedmap.NewMap(). |
| 49 | Set("one", 1). |
| 50 | Set("two", 2). |
| 51 | Set("three", 3). |
| 52 | Set("four", 4). |
| 53 | Set("five", 5). |
| 54 | Set("six", 6). |
| 55 | Set("seven", 7). |
| 56 | Set("eight", 8). |
| 57 | Set("nine", 9). |
| 58 | Set("ten", 10). |
| 59 | Set("fortyfivepoint2", 45.2)) |
| 60 | } |
| 61 | t.Run("addition", testCase{ |
| 62 | inFn: in, |
| 63 | s: `one + two`, |
| 64 | out: model.NewIntValue(3), |
| 65 | }.run) |
| 66 | t.Run("subtraction", testCase{ |
| 67 | inFn: in, |
| 68 | s: `five - two`, |
| 69 | out: model.NewIntValue(3), |
| 70 | }.run) |
| 71 | t.Run("multiplication", testCase{ |
| 72 | inFn: in, |
| 73 | s: `five * two`, |
| 74 | out: model.NewIntValue(10), |
| 75 | }.run) |
| 76 | t.Run("division", testCase{ |
| 77 | inFn: in, |
| 78 | s: `ten / two`, |
| 79 | out: model.NewIntValue(5), |
| 80 | }.run) |
| 81 | t.Run("modulus", testCase{ |
| 82 | inFn: in, |
| 83 | s: `ten % three`, |
| 84 | out: model.NewIntValue(1), |
| 85 | }.run) |
| 86 | t.Run("ordering", testCase{ |
| 87 | inFn: in, |
| 88 | s: `fortyfivepoint2 + five * four - two / two`, // 45.2 + (5 * 4) - (2 / 2) = 45.2 + 20 - 1 = 64.2 |
| 89 | out: model.NewFloatValue(64.2), |
| 90 | }.run) |
| 91 | t.Run("ordering with groups", testCase{ |
| 92 | inFn: in, |
| 93 | s: `(fortyfivepoint2 + five) * ((four - two) / two)`, // (45.2 + 5) * ((4 - 2) / 2) = (50.2) * ((2) / 2) = (50.2) * (1) = 50.2 |
| 94 | out: model.NewFloatValue(50.2), |
| 95 | }.run) |
| 96 | }) |
| 97 | }) |
| 98 | t.Run("comparison", func(t *testing.T) { |
| 99 | t.Run("literals", func(t *testing.T) { |
| 100 | t.Run("equal", testCase{ |
| 101 | s: `1 == 1`, |
| 102 | out: model.NewBoolValue(true), |
| 103 | }.run) |
| 104 | t.Run("not equal", testCase{ |
| 105 | s: `1 != 1`, |
| 106 | out: model.NewBoolValue(false), |
| 107 | }.run) |
| 108 | t.Run("greater than", testCase{ |
| 109 | s: `2 > 1`, |
| 110 | out: model.NewBoolValue(true), |
| 111 | }.run) |
| 112 | t.Run("greater than or equal", testCase{ |
| 113 | s: `2 >= 2`, |
| 114 | out: model.NewBoolValue(true), |
| 115 | }.run) |
| 116 | t.Run("less than", testCase{ |
| 117 | s: `1 < 2`, |
| 118 | out: model.NewBoolValue(true), |
| 119 | }.run) |
| 120 | t.Run("less than or equal", testCase{ |
| 121 | s: `2 <= 2`, |
| 122 | out: model.NewBoolValue(true), |
| 123 | }.run) |
| 124 | t.Run("like", testCase{ |
| 125 | s: `"hello world" =~ r/ello/`, |
| 126 | out: model.NewBoolValue(true), |
| 127 | }.run) |
| 128 | t.Run("not like", testCase{ |
| 129 | s: `"hello world" !~ r/helloworld/`, |
| 130 | out: model.NewBoolValue(true), |
| 131 | }.run) |
| 132 | }) |
| 133 | |
| 134 | t.Run("variables", func(t *testing.T) { |
| 135 | in := func() *model.Value { |
| 136 | return model.NewValue(orderedmap.NewMap(). |
| 137 | Set("one", 1). |
| 138 | Set("two", 2). |
| 139 | Set("nested", orderedmap.NewMap(). |
| 140 | Set("three", 3). |
| 141 | Set("four", 4))) |
| 142 | } |
| 143 | t.Run("equal", testCase{ |
| 144 | inFn: in, |
| 145 | s: `one == one`, |
| 146 | out: model.NewBoolValue(true), |
| 147 | }.run) |
| 148 | t.Run("not equal", testCase{ |
| 149 | inFn: in, |
| 150 | s: `one != one`, |
| 151 | out: model.NewBoolValue(false), |
| 152 | }.run) |
| 153 | t.Run("greater than", testCase{ |
| 154 | inFn: in, |
| 155 | s: `two > one`, |
| 156 | out: model.NewBoolValue(true), |
| 157 | }.run) |
| 158 | t.Run("greater than or equal", testCase{ |
| 159 | inFn: in, |
| 160 | s: `two >= two`, |
| 161 | out: model.NewBoolValue(true), |
| 162 | }.run) |
| 163 | t.Run("less than", testCase{ |
| 164 | inFn: in, |
| 165 | s: `one < two`, |
| 166 | out: model.NewBoolValue(true), |
| 167 | }.run) |
| 168 | t.Run("less than or equal", testCase{ |
| 169 | inFn: in, |
| 170 | s: `two <= two`, |
| 171 | out: model.NewBoolValue(true), |
| 172 | }.run) |
| 173 | t.Run("nested with math more than", testCase{ |
| 174 | inFn: in, |
| 175 | s: `nested.three + nested.four * 0 > one * 1`, |
| 176 | out: model.NewBoolValue(true), |
| 177 | }.run) |
| 178 | t.Run("nested with grouped math more than", testCase{ |
| 179 | inFn: in, |
| 180 | s: `(nested.three + nested.four) * 0 > one * 1`, |
| 181 | out: model.NewBoolValue(false), |
| 182 | }.run) |
| 183 | }) |
| 184 | |
| 185 | t.Run("coalesce", func(t *testing.T) { |
| 186 | t.Run("literals", func(t *testing.T) { |
| 187 | t.Run("coalesce", testCase{ |
| 188 | s: `null ?? 1`, |
| 189 | out: model.NewIntValue(1), |
| 190 | }.run) |
| 191 | t.Run("coalesce with null", testCase{ |
| 192 | s: `null ?? null`, |
| 193 | out: model.NewNullValue(), |
| 194 | }.run) |
| 195 | t.Run("coalesce with null and value", testCase{ |
| 196 | s: `null ?? 2`, |
| 197 | out: model.NewIntValue(2), |
| 198 | }.run) |
| 199 | t.Run("coalesce with value", testCase{ |
| 200 | s: `1 ?? 2`, |
| 201 | out: model.NewIntValue(1), |
| 202 | }.run) |
| 203 | }) |
| 204 | t.Run("variables", func(t *testing.T) { |
| 205 | in := func() *model.Value { |
| 206 | return model.NewValue(orderedmap.NewMap(). |
| 207 | Set("one", 1). |
| 208 | Set("two", 2). |
| 209 | Set("nested", orderedmap.NewMap(). |
| 210 | Set("one", 1). |
| 211 | Set("two", 2). |
| 212 | Set("three", 3). |
| 213 | Set("four", 4)). |
| 214 | Set("list", []any{1, 2, 3})) |
| 215 | } |
| 216 | t.Run("coalesce", testCase{ |
| 217 | inFn: in, |
| 218 | s: `nested.five ?? one`, |
| 219 | out: model.NewIntValue(1), |
| 220 | }.run) |
| 221 | t.Run("coalesce with null", testCase{ |
| 222 | inFn: in, |
| 223 | s: `nested.five ?? null`, |
| 224 | out: model.NewNullValue(), |
| 225 | }.run) |
| 226 | t.Run("coalesce with null and value", testCase{ |
| 227 | inFn: in, |
| 228 | s: `nested.five ?? 2`, |
| 229 | out: model.NewIntValue(2), |
| 230 | }.run) |
| 231 | t.Run("coalesce with value", testCase{ |
| 232 | inFn: in, |
| 233 | s: `nested.three ?? 2`, |
| 234 | out: model.NewIntValue(3), |
| 235 | }.run) |
| 236 | t.Run("coalesce with bad map key", testCase{ |
| 237 | inFn: in, |
| 238 | s: `nope ?? 2`, |
| 239 | out: model.NewIntValue(2), |
| 240 | }.run) |
| 241 | t.Run("coalesce with nested bad map key", testCase{ |
| 242 | inFn: in, |
| 243 | s: `nested.nope ?? 2`, |
| 244 | out: model.NewIntValue(2), |
| 245 | }.run) |
| 246 | t.Run("coalesce with list index", testCase{ |
| 247 | inFn: in, |
| 248 | s: `list[1] ?? 5`, |
| 249 | out: model.NewIntValue(2), |
| 250 | }.run) |
| 251 | t.Run("coalesce with list bad index", testCase{ |
| 252 | inFn: in, |
| 253 | s: `list[3] ?? 5`, |
| 254 | out: model.NewIntValue(5), |
| 255 | }.run) |
| 256 | t.Run("chained coalesce execute left to right", func(t *testing.T) { |
| 257 | // These tests ensure the coalesces run in order. |
| 258 | t.Run("no match", testCase{ |
| 259 | inFn: in, |
| 260 | s: `nested.five ?? nested.six ?? nested.seven ?? 10`, |
| 261 | out: model.NewIntValue(10), |
| 262 | }.run) |
| 263 | t.Run("first match when all exist", testCase{ |
| 264 | inFn: in, |
| 265 | s: `nested.one ?? nested.two ?? nested.three ?? 10`, |
| 266 | out: model.NewIntValue(1), |
| 267 | }.run) |
| 268 | t.Run("second match", testCase{ |
| 269 | inFn: in, |
| 270 | s: `nested.five ?? nested.two ?? nested.three ?? 10`, |
| 271 | out: model.NewIntValue(2), |
| 272 | }.run) |
| 273 | t.Run("third match", testCase{ |
| 274 | inFn: in, |
| 275 | s: `nested.five ?? nested.six ?? nested.three ?? 10`, |
| 276 | out: model.NewIntValue(3), |
| 277 | }.run) |
| 278 | }) |
| 279 | }) |
| 280 | }) |
| 281 | }) |
| 282 | } |