Debian Codegraph @

execution/execute_any_test.go

raw symbols SCIP
1 package execution_test
2
3 import (
4 "testing"
5
6 "github.com/tomwright/dasel/v3/model"
7 "github.com/tomwright/dasel/v3/model/orderedmap"
8 )
9
10 func TestAnyExpr(t *testing.T) {
11 t.Run("some match", testCase{
12 s: `[1, 2, 3].any($this > 2)`,
13 out: model.NewBoolValue(true),
14 }.run)
15 t.Run("none match", testCase{
16 s: `[1, 2, 3].any($this > 5)`,
17 out: model.NewBoolValue(false),
18 }.run)
19 t.Run("all match", testCase{
20 s: `[1, 2, 3].any($this > 0)`,
21 out: model.NewBoolValue(true),
22 }.run)
23 t.Run("with equality", testCase{
24 s: `["a", "b", "c"].any($this == "b")`,
25 out: model.NewBoolValue(true),
26 }.run)
27 t.Run("empty array", testCase{
28 s: `[].any($this > 0)`,
29 out: model.NewBoolValue(false),
30 }.run)
31 t.Run("boolean literal true", testCase{
32 s: `[1, 2, 3].any(true)`,
33 out: model.NewBoolValue(true),
34 }.run)
35 t.Run("boolean literal false", testCase{
36 s: `[1, 2, 3].any(false)`,
37 out: model.NewBoolValue(false),
38 }.run)
39 t.Run("with negation", testCase{
40 s: `[1, 2, 3].any(!($this > 2))`,
41 out: model.NewBoolValue(true),
42 }.run)
43 t.Run("with nested property", testCase{
44 inFn: func() *model.Value {
45 s := model.NewSliceValue()
46 _ = s.Append(model.NewValue(orderedmap.NewMap().Set("age", int64(20))))
47 _ = s.Append(model.NewValue(orderedmap.NewMap().Set("age", int64(30))))
48 return s
49 },
50 s: `any(age > 25)`,
51 out: model.NewBoolValue(true),
52 }.run)
53 t.Run("chained after filter", testCase{
54 s: `[1, 2, 3, 4, 5].filter($this > 2).any($this > 4)`,
55 out: model.NewBoolValue(true),
56 }.run)
57 t.Run("any with $key", testCase{
58 s: `[10, 20, 30].any($key == 2)`,
59 out: model.NewBoolValue(true),
60 }.run)
61 t.Run("any with $key no match", testCase{
62 s: `[10, 20, 30].any($key == 5)`,
63 out: model.NewBoolValue(false),
64 }.run)
65 }