Debian Codegraph @

execution/execute_reduce_test.go

raw symbols SCIP
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 TestReduce(t *testing.T) {
13 t.Run("sum ints", testCase{
14 inFn: func() *model.Value {
15 res := model.NewSliceValue()
16 _ = res.Append(model.NewIntValue(1))
17 _ = res.Append(model.NewIntValue(2))
18 _ = res.Append(model.NewIntValue(3))
19 _ = res.Append(model.NewIntValue(4))
20 return res
21 },
22 s: `reduce($this, 0, $acc + $this)`,
23 out: model.NewIntValue(10),
24 }.run)
25
26 t.Run("concatenate strings", testCase{
27 inFn: func() *model.Value {
28 res := model.NewSliceValue()
29 _ = res.Append(model.NewStringValue("a"))
30 _ = res.Append(model.NewStringValue("b"))
31 _ = res.Append(model.NewStringValue("c"))
32 return res
33 },
34 s: `reduce($this, "", $acc + $this)`,
35 out: model.NewStringValue("abc"),
36 }.run)
37
38 t.Run("reduce by field", testCase{
39 inFn: func() *model.Value {
40 res := model.NewSliceValue()
41 p1 := model.NewMapValue()
42 _ = p1.SetMapKey("name", model.NewStringValue("Alice"))
43 _ = p1.SetMapKey("age", model.NewIntValue(30))
44 _ = res.Append(p1)
45 p2 := model.NewMapValue()
46 _ = p2.SetMapKey("name", model.NewStringValue("Bob"))
47 _ = p2.SetMapKey("age", model.NewIntValue(25))
48 _ = res.Append(p2)
49 return res
50 },
51 s: `reduce(age, 0, $acc + $this)`,
52 out: model.NewIntValue(55),
53 }.run)
54
55 t.Run("empty array returns init", testCase{
56 inFn: func() *model.Value {
57 return model.NewSliceValue()
58 },
59 s: `reduce($this, 0, $acc + $this)`,
60 out: model.NewIntValue(0),
61 }.run)
62
63 t.Run("non-slice input errors", func(t *testing.T) {
64 in := model.NewStringValue("not a slice")
65 _, err := execution.ExecuteSelector(context.Background(), `reduce($this, 0, $acc + $this)`, in, execution.NewOptions())
66 if err == nil {
67 t.Fatal("expected error for non-slice input")
68 }
69 if !strings.Contains(err.Error(), "cannot reduce over non-array") {
70 t.Fatalf("unexpected error: %s", err)
71 }
72 })
73
74 t.Run("chaining after reduce", testCase{
75 inFn: func() *model.Value {
76 res := model.NewSliceValue()
77 _ = res.Append(model.NewIntValue(1))
78 _ = res.Append(model.NewIntValue(2))
79 _ = res.Append(model.NewIntValue(3))
80 return res
81 },
82 s: `reduce($this, 0, $acc + $this) > 5`,
83 out: model.NewBoolValue(true),
84 }.run)
85
86 t.Run("product of ints", testCase{
87 inFn: func() *model.Value {
88 res := model.NewSliceValue()
89 _ = res.Append(model.NewIntValue(2))
90 _ = res.Append(model.NewIntValue(3))
91 _ = res.Append(model.NewIntValue(4))
92 return res
93 },
94 s: `reduce($this, 1, $acc * $this)`,
95 out: model.NewIntValue(24),
96 }.run)
97
98 t.Run("reduce with $key", testCase{
99 inFn: func() *model.Value {
100 res := model.NewSliceValue()
101 _ = res.Append(model.NewStringValue("a"))
102 _ = res.Append(model.NewStringValue("b"))
103 _ = res.Append(model.NewStringValue("c"))
104 return res
105 },
106 s: `reduce($this, 0, $acc + $key)`,
107 out: model.NewIntValue(3), // 0+0+1+2
108 }.run)
109
110 t.Run("single element", testCase{
111 inFn: func() *model.Value {
112 res := model.NewSliceValue()
113 _ = res.Append(model.NewIntValue(42))
114 return res
115 },
116 s: `reduce($this, 0, $acc + $this)`,
117 out: model.NewIntValue(42),
118 }.run)
119 }