Debian Codegraph @

execution/execute_key_var_test.go

raw symbols SCIP
1 package execution_test
2
3 import (
4 "context"
5 "testing"
6
7 "github.com/tomwright/dasel/v3/execution"
8 "github.com/tomwright/dasel/v3/model"
9 )
10
11 func TestKeyVar(t *testing.T) {
12 intSlice := func() *model.Value {
13 s := model.NewSliceValue()
14 _ = s.Append(model.NewIntValue(10))
15 _ = s.Append(model.NewIntValue(20))
16 _ = s.Append(model.NewIntValue(30))
17 return s
18 }
19
20 t.Run("filter with $key", testCase{
21 inFn: intSlice,
22 s: "filter($key >= 1)",
23 outFn: func() *model.Value {
24 s := model.NewSliceValue()
25 _ = s.Append(model.NewIntValue(20))
26 _ = s.Append(model.NewIntValue(30))
27 return s
28 },
29 }.run)
30
31 t.Run("map with $key", testCase{
32 inFn: intSlice,
33 s: "map($key)",
34 outFn: func() *model.Value {
35 s := model.NewSliceValue()
36 _ = s.Append(model.NewIntValue(0))
37 _ = s.Append(model.NewIntValue(1))
38 _ = s.Append(model.NewIntValue(2))
39 return s
40 },
41 }.run)
42
43 t.Run("mapValues with $key", testCase{
44 inFn: func() *model.Value {
45 m := model.NewMapValue()
46 _ = m.SetMapKey("a", model.NewIntValue(1))
47 _ = m.SetMapKey("b", model.NewIntValue(2))
48 return m
49 },
50 s: "mapValues($key)",
51 outFn: func() *model.Value {
52 m := model.NewMapValue()
53 _ = m.SetMapKey("a", model.NewStringValue("a"))
54 _ = m.SetMapKey("b", model.NewStringValue("b"))
55 return m
56 },
57 }.run)
58
59 t.Run("search with $key on map", testCase{
60 inFn: func() *model.Value {
61 m := model.NewMapValue()
62 _ = m.SetMapKey("other", model.NewIntValue(1))
63 _ = m.SetMapKey("target", model.NewIntValue(42))
64 return m
65 },
66 s: `search($key == "target")`,
67 outFn: func() *model.Value {
68 s := model.NewSliceValue()
69 _ = s.Append(model.NewIntValue(42))
70 return s
71 },
72 }.run)
73
74 t.Run("search with $key on slice", testCase{
75 inFn: intSlice,
76 s: "search($key == 1)",
77 outFn: func() *model.Value {
78 s := model.NewSliceValue()
79 _ = s.Append(model.NewIntValue(20))
80 return s
81 },
82 }.run)
83
84 t.Run("reduce with $key", testCase{
85 inFn: func() *model.Value {
86 s := model.NewSliceValue()
87 _ = s.Append(model.NewStringValue("a"))
88 _ = s.Append(model.NewStringValue("b"))
89 _ = s.Append(model.NewStringValue("c"))
90 return s
91 },
92 s: "reduce($this, 0, $acc + $key)",
93 out: model.NewIntValue(3), // 0+0+1+2
94 }.run)
95
96 t.Run("each with $key", testCase{
97 inFn: intSlice,
98 s: "each($key + $this)",
99 compareRoot: true,
100 outFn: intSlice,
101 }.run)
102
103 t.Run("any with $key true", testCase{
104 inFn: intSlice,
105 s: "any($key == 2)",
106 out: model.NewBoolValue(true),
107 }.run)
108
109 t.Run("any with $key false", testCase{
110 inFn: intSlice,
111 s: "any($key == 5)",
112 out: model.NewBoolValue(false),
113 }.run)
114
115 t.Run("all with $key true", testCase{
116 inFn: intSlice,
117 s: "all($key < 3)",
118 out: model.NewBoolValue(true),
119 }.run)
120
121 t.Run("all with $key false", testCase{
122 inFn: intSlice,
123 s: "all($key < 2)",
124 out: model.NewBoolValue(false),
125 }.run)
126
127 t.Run("count with $key", testCase{
128 inFn: intSlice,
129 s: "count($key > 0)",
130 out: model.NewIntValue(2),
131 }.run)
132
133 t.Run("sortBy with $key descending", testCase{
134 inFn: intSlice,
135 s: "sortBy($key, desc)",
136 outFn: func() *model.Value {
137 s := model.NewSliceValue()
138 _ = s.Append(model.NewIntValue(30))
139 _ = s.Append(model.NewIntValue(20))
140 _ = s.Append(model.NewIntValue(10))
141 return s
142 },
143 }.run)
144
145 t.Run("groupBy with $key", testCase{
146 inFn: func() *model.Value {
147 s := model.NewSliceValue()
148 _ = s.Append(model.NewStringValue("a"))
149 _ = s.Append(model.NewStringValue("b"))
150 _ = s.Append(model.NewStringValue("c"))
151 return s
152 },
153 s: "groupBy($key)",
154 outFn: func() *model.Value {
155 m := model.NewMapValue()
156 s0 := model.NewSliceValue()
157 _ = s0.Append(model.NewStringValue("a"))
158 _ = m.SetMapKey("0", s0)
159 s1 := model.NewSliceValue()
160 _ = s1.Append(model.NewStringValue("b"))
161 _ = m.SetMapKey("1", s1)
162 s2 := model.NewSliceValue()
163 _ = s2.Append(model.NewStringValue("c"))
164 _ = m.SetMapKey("2", s2)
165 return m
166 },
167 }.run)
168
169 t.Run("$key does not leak after map", func(t *testing.T) {
170 in := model.NewSliceValue()
171 _ = in.Append(model.NewIntValue(1))
172 _ = in.Append(model.NewIntValue(2))
173
174 opts := execution.NewOptions()
175 // $key should not exist before
176 if _, exists := opts.Vars["key"]; exists {
177 t.Fatal("$key should not exist before iteration")
178 }
179
180 _, err := execution.ExecuteSelector(context.Background(), "map($key)", in, opts)
181 if err != nil {
182 t.Fatal(err)
183 }
184
185 // $key should not exist after
186 if _, exists := opts.Vars["key"]; exists {
187 t.Fatal("$key should not leak after iteration")
188 }
189 })
190
191 t.Run("$key scoping restores previous value", func(t *testing.T) {
192 in := model.NewSliceValue()
193 _ = in.Append(model.NewIntValue(1))
194
195 opts := execution.NewOptions(execution.WithVariable("key", model.NewStringValue("preserved")))
196
197 _, err := execution.ExecuteSelector(context.Background(), "map($key)", in, opts)
198 if err != nil {
199 t.Fatal(err)
200 }
201
202 // $key should be restored to the previous value
203 v, exists := opts.Vars["key"]
204 if !exists {
205 t.Fatal("$key should still exist after iteration")
206 }
207 s, err := v.StringValue()
208 if err != nil {
209 t.Fatal(err)
210 }
211 if s != "preserved" {
212 t.Fatalf("expected $key to be 'preserved', got %q", s)
213 }
214 })
215
216 t.Run("filter with $key and $this", testCase{
217 inFn: intSlice,
218 s: "filter($key == 0 || $this == 30)",
219 outFn: func() *model.Value {
220 s := model.NewSliceValue()
221 _ = s.Append(model.NewIntValue(10))
222 _ = s.Append(model.NewIntValue(30))
223 return s
224 },
225 }.run)
226
227 t.Run("map $key + $this", testCase{
228 inFn: intSlice,
229 s: "map($key + $this)",
230 outFn: func() *model.Value {
231 s := model.NewSliceValue()
232 _ = s.Append(model.NewIntValue(10)) // 0 + 10
233 _ = s.Append(model.NewIntValue(21)) // 1 + 20
234 _ = s.Append(model.NewIntValue(32)) // 2 + 30
235 return s
236 },
237 }.run)
238 }