| 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 TestFuncAdd(t *testing.T) { |
| 11 | t.Run("int", testCase{ |
| 12 | s: `add(1, 2, 3)`, |
| 13 | out: model.NewIntValue(6), |
| 14 | }.run) |
| 15 | t.Run("float", testCase{ |
| 16 | s: `add(1f, 2.5, 3.5)`, |
| 17 | out: model.NewFloatValue(7), |
| 18 | }.run) |
| 19 | t.Run("mixed", testCase{ |
| 20 | s: `add(1, 2f)`, |
| 21 | out: model.NewFloatValue(3), |
| 22 | }.run) |
| 23 | t.Run("properties", func(t *testing.T) { |
| 24 | in := func() *model.Value { |
| 25 | return model.NewValue(orderedmap.NewMap(). |
| 26 | Set("numbers", orderedmap.NewMap(). |
| 27 | Set("one", 1). |
| 28 | Set("two", 2). |
| 29 | Set("three", 3)). |
| 30 | Set("nums", []any{1, 2, 3})) |
| 31 | } |
| 32 | t.Run("nested props", testCase{ |
| 33 | inFn: in, |
| 34 | s: `numbers.one + add(numbers.two, numbers.three)`, |
| 35 | out: model.NewIntValue(6), |
| 36 | }.run) |
| 37 | t.Run("add on end of chain", testCase{ |
| 38 | inFn: in, |
| 39 | s: `numbers.one + numbers.add(two, three)`, |
| 40 | out: model.NewIntValue(6), |
| 41 | }.run) |
| 42 | t.Run("add with map and spread on slice with $this addition and grouping", testCase{ |
| 43 | inFn: in, |
| 44 | s: `add(nums.map(($this + 1))...)`, |
| 45 | out: model.NewIntValue(9), |
| 46 | }.run) |
| 47 | t.Run("add with map and spread on slice with $this addition", testCase{ |
| 48 | inFn: in, |
| 49 | s: `add(nums.map($this + 1 - 2)...)`, |
| 50 | out: model.NewIntValue(3), |
| 51 | }.run) |
| 52 | }) |
| 53 | } |