| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/tomwright/dasel/v3/execution" |
| 9 | "github.com/tomwright/dasel/v3/model" |
| 10 | ) |
| 11 | |
| 12 | func TestMapValues(t *testing.T) { |
| 13 | t.Run("multiply int values", testCase{ |
| 14 | inFn: func() *model.Value { |
| 15 | res := model.NewMapValue() |
| 16 | _ = res.SetMapKey("a", model.NewIntValue(1)) |
| 17 | _ = res.SetMapKey("b", model.NewIntValue(2)) |
| 18 | _ = res.SetMapKey("c", model.NewIntValue(3)) |
| 19 | return res |
| 20 | }, |
| 21 | s: `mapValues($this * 2)`, |
| 22 | outFn: func() *model.Value { |
| 23 | res := model.NewMapValue() |
| 24 | _ = res.SetMapKey("a", model.NewIntValue(2)) |
| 25 | _ = res.SetMapKey("b", model.NewIntValue(4)) |
| 26 | _ = res.SetMapKey("c", model.NewIntValue(6)) |
| 27 | return res |
| 28 | }, |
| 29 | }.run) |
| 30 | |
| 31 | t.Run("add to int values", testCase{ |
| 32 | inFn: func() *model.Value { |
| 33 | res := model.NewMapValue() |
| 34 | _ = res.SetMapKey("x", model.NewIntValue(10)) |
| 35 | _ = res.SetMapKey("y", model.NewIntValue(20)) |
| 36 | return res |
| 37 | }, |
| 38 | s: `mapValues($this + 1)`, |
| 39 | outFn: func() *model.Value { |
| 40 | res := model.NewMapValue() |
| 41 | _ = res.SetMapKey("x", model.NewIntValue(11)) |
| 42 | _ = res.SetMapKey("y", model.NewIntValue(21)) |
| 43 | return res |
| 44 | }, |
| 45 | }.run) |
| 46 | |
| 47 | t.Run("boolean expression", testCase{ |
| 48 | inFn: func() *model.Value { |
| 49 | res := model.NewMapValue() |
| 50 | _ = res.SetMapKey("a", model.NewIntValue(3)) |
| 51 | _ = res.SetMapKey("b", model.NewIntValue(8)) |
| 52 | return res |
| 53 | }, |
| 54 | s: `mapValues($this > 5)`, |
| 55 | outFn: func() *model.Value { |
| 56 | res := model.NewMapValue() |
| 57 | _ = res.SetMapKey("a", model.NewBoolValue(false)) |
| 58 | _ = res.SetMapKey("b", model.NewBoolValue(true)) |
| 59 | return res |
| 60 | }, |
| 61 | }.run) |
| 62 | |
| 63 | t.Run("empty map", testCase{ |
| 64 | inFn: func() *model.Value { |
| 65 | return model.NewMapValue() |
| 66 | }, |
| 67 | s: `mapValues($this * 2)`, |
| 68 | outFn: func() *model.Value { |
| 69 | return model.NewMapValue() |
| 70 | }, |
| 71 | }.run) |
| 72 | |
| 73 | t.Run("non-map input errors", func(t *testing.T) { |
| 74 | in := model.NewStringValue("not a map") |
| 75 | _, err := execution.ExecuteSelector(context.Background(), `mapValues($this * 2)`, in, execution.NewOptions()) |
| 76 | if err == nil { |
| 77 | t.Fatal("expected error for non-map input") |
| 78 | } |
| 79 | if !strings.Contains(err.Error(), "cannot mapValues over non-map") { |
| 80 | t.Fatalf("unexpected error: %s", err) |
| 81 | } |
| 82 | }) |
| 83 | |
| 84 | t.Run("mapValues with $key", testCase{ |
| 85 | inFn: func() *model.Value { |
| 86 | res := model.NewMapValue() |
| 87 | _ = res.SetMapKey("a", model.NewIntValue(1)) |
| 88 | _ = res.SetMapKey("b", model.NewIntValue(2)) |
| 89 | return res |
| 90 | }, |
| 91 | s: `mapValues($key)`, |
| 92 | outFn: func() *model.Value { |
| 93 | res := model.NewMapValue() |
| 94 | _ = res.SetMapKey("a", model.NewStringValue("a")) |
| 95 | _ = res.SetMapKey("b", model.NewStringValue("b")) |
| 96 | return res |
| 97 | }, |
| 98 | }.run) |
| 99 | |
| 100 | t.Run("chaining after mapValues", testCase{ |
| 101 | inFn: func() *model.Value { |
| 102 | res := model.NewMapValue() |
| 103 | _ = res.SetMapKey("a", model.NewIntValue(1)) |
| 104 | _ = res.SetMapKey("b", model.NewIntValue(2)) |
| 105 | return res |
| 106 | }, |
| 107 | s: `mapValues($this + 10).a`, |
| 108 | out: model.NewIntValue(11), |
| 109 | }.run) |
| 110 | } |