| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | ) |
| 8 | |
| 9 | func TestEach(t *testing.T) { |
| 10 | t.Run("all true", testCase{ |
| 11 | s: "[1,2,3].each($this = $this + 1)", |
| 12 | outFn: func() *model.Value { |
| 13 | s := model.NewSliceValue() |
| 14 | if err := s.Append(model.NewIntValue(2)); err != nil { |
| 15 | t.Fatalf("unexpected error: %s", err) |
| 16 | } |
| 17 | if err := s.Append(model.NewIntValue(3)); err != nil { |
| 18 | t.Fatalf("unexpected error: %s", err) |
| 19 | } |
| 20 | if err := s.Append(model.NewIntValue(4)); err != nil { |
| 21 | t.Fatalf("unexpected error: %s", err) |
| 22 | } |
| 23 | return s |
| 24 | }, |
| 25 | }.run) |
| 26 | |
| 27 | t.Run("each with $key", testCase{ |
| 28 | s: "[10,20,30].each($this = $this + $key)", |
| 29 | outFn: func() *model.Value { |
| 30 | s := model.NewSliceValue() |
| 31 | _ = s.Append(model.NewIntValue(10)) |
| 32 | _ = s.Append(model.NewIntValue(21)) |
| 33 | _ = s.Append(model.NewIntValue(32)) |
| 34 | return s |
| 35 | }, |
| 36 | }.run) |
| 37 | t.Run("multiply each element", testCase{ |
| 38 | s: "[2,3,4].each($this = $this * 2)", |
| 39 | outFn: func() *model.Value { |
| 40 | s := model.NewSliceValue() |
| 41 | _ = s.Append(model.NewIntValue(4)) |
| 42 | _ = s.Append(model.NewIntValue(6)) |
| 43 | _ = s.Append(model.NewIntValue(8)) |
| 44 | return s |
| 45 | }, |
| 46 | }.run) |
| 47 | } |