| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/google/go-cmp/cmp" |
| 8 | "github.com/tomwright/dasel/v3/execution" |
| 9 | "github.com/tomwright/dasel/v3/model" |
| 10 | "github.com/tomwright/dasel/v3/model/orderedmap" |
| 11 | ) |
| 12 | |
| 13 | type testCase struct { |
| 14 | in *model.Value |
| 15 | inFn func() *model.Value |
| 16 | s string |
| 17 | out *model.Value |
| 18 | outFn func() *model.Value |
| 19 | compareRoot bool |
| 20 | opts []execution.ExecuteOptionFn |
| 21 | } |
| 22 | |
| 23 | func (tc testCase) run(t *testing.T) { |
| 24 | in := tc.in |
| 25 | if tc.inFn != nil { |
| 26 | in = tc.inFn() |
| 27 | } |
| 28 | if in == nil { |
| 29 | in = model.NewValue(nil) |
| 30 | } |
| 31 | exp := tc.out |
| 32 | if tc.outFn != nil { |
| 33 | exp = tc.outFn() |
| 34 | } |
| 35 | res, err := execution.ExecuteSelector(context.Background(), tc.s, in, execution.NewOptions(tc.opts...)) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | |
| 40 | if tc.compareRoot { |
| 41 | res = in |
| 42 | } |
| 43 | |
| 44 | equal, err := res.EqualTypeValue(exp) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | if !equal { |
| 49 | t.Errorf("unexpected output:\nexp: %s\ngot: %s", exp.String(), res.String()) |
| 50 | } |
| 51 | |
| 52 | expMeta := exp.Metadata |
| 53 | gotMeta := res.Metadata |
| 54 | if !cmp.Equal(expMeta, gotMeta) { |
| 55 | t.Errorf("unexpected output metadata: %v", cmp.Diff(expMeta, gotMeta)) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestExecuteSelector_HappyPath(t *testing.T) { |
| 60 | t.Run("get", func(t *testing.T) { |
| 61 | inputMap := func() *model.Value { |
| 62 | return model.NewValue( |
| 63 | orderedmap.NewMap(). |
| 64 | Set("title", "Mr"). |
| 65 | Set("age", int64(31)). |
| 66 | Set("name", orderedmap.NewMap(). |
| 67 | Set("first", "Tom"). |
| 68 | Set("last", "Wright")), |
| 69 | ) |
| 70 | } |
| 71 | t.Run("property", testCase{ |
| 72 | in: inputMap(), |
| 73 | s: `title`, |
| 74 | out: model.NewStringValue("Mr"), |
| 75 | }.run) |
| 76 | t.Run("nested property", testCase{ |
| 77 | in: inputMap(), |
| 78 | s: `name.first`, |
| 79 | out: model.NewStringValue("Tom"), |
| 80 | }.run) |
| 81 | t.Run("concat with grouping", testCase{ |
| 82 | in: inputMap(), |
| 83 | s: `title + " " + (name.first) + " " + (name.last)`, |
| 84 | out: model.NewStringValue("Mr Tom Wright"), |
| 85 | }.run) |
| 86 | t.Run("concat", testCase{ |
| 87 | in: inputMap(), |
| 88 | s: `title + " " + name.first + " " + name.last`, |
| 89 | out: model.NewStringValue("Mr Tom Wright"), |
| 90 | }.run) |
| 91 | t.Run("add evaluated fields", testCase{ |
| 92 | in: inputMap(), |
| 93 | s: `{..., "over30": age > 30}`, |
| 94 | outFn: func() *model.Value { |
| 95 | return model.NewValue( |
| 96 | orderedmap.NewMap(). |
| 97 | Set("title", "Mr"). |
| 98 | Set("age", int64(31)). |
| 99 | Set("name", orderedmap.NewMap(). |
| 100 | Set("first", "Tom"). |
| 101 | Set("last", "Wright")). |
| 102 | Set("over30", true), |
| 103 | ) |
| 104 | }, |
| 105 | }.run) |
| 106 | }) |
| 107 | |
| 108 | t.Run("set", func(t *testing.T) { |
| 109 | inputMap := func() *model.Value { |
| 110 | return model.NewValue( |
| 111 | orderedmap.NewMap(). |
| 112 | Set("title", "Mr"). |
| 113 | Set("age", int64(31)). |
| 114 | Set("name", orderedmap.NewMap(). |
| 115 | Set("first", "Tom"). |
| 116 | Set("last", "Wright")), |
| 117 | ) |
| 118 | } |
| 119 | inputSlice := func() *model.Value { |
| 120 | return model.NewValue([]any{1, 2, 3}) |
| 121 | } |
| 122 | |
| 123 | t.Run("set property", testCase{ |
| 124 | in: inputMap(), |
| 125 | s: `title = "Mrs"`, |
| 126 | outFn: func() *model.Value { |
| 127 | res := inputMap() |
| 128 | if err := res.SetMapKey("title", model.NewStringValue("Mrs")); err != nil { |
| 129 | t.Fatalf("unexpected error: %s", err) |
| 130 | } |
| 131 | return res |
| 132 | }, |
| 133 | compareRoot: true, |
| 134 | }.run) |
| 135 | |
| 136 | t.Run("set index", testCase{ |
| 137 | in: inputSlice(), |
| 138 | s: `$this[1] = 4`, |
| 139 | outFn: func() *model.Value { |
| 140 | res := inputSlice() |
| 141 | if err := res.SetSliceIndex(1, model.NewIntValue(4)); err != nil { |
| 142 | t.Fatalf("unexpected error: %s", err) |
| 143 | } |
| 144 | return res |
| 145 | }, |
| 146 | compareRoot: true, |
| 147 | }.run) |
| 148 | }) |
| 149 | } |