| 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 TestCountExpr(t *testing.T) { |
| 11 | t.Run("some match", testCase{ |
| 12 | s: `[1, 2, 3, 4, 5].count($this > 3)`, |
| 13 | out: model.NewIntValue(2), |
| 14 | }.run) |
| 15 | t.Run("all match", testCase{ |
| 16 | s: `[1, 2, 3].count($this > 0)`, |
| 17 | out: model.NewIntValue(3), |
| 18 | }.run) |
| 19 | t.Run("none match", testCase{ |
| 20 | s: `[1, 2, 3].count($this > 5)`, |
| 21 | out: model.NewIntValue(0), |
| 22 | }.run) |
| 23 | t.Run("with equality", testCase{ |
| 24 | s: `["a", "b", "a", "c", "a"].count($this == "a")`, |
| 25 | out: model.NewIntValue(3), |
| 26 | }.run) |
| 27 | t.Run("empty array", testCase{ |
| 28 | s: `[].count($this > 0)`, |
| 29 | out: model.NewIntValue(0), |
| 30 | }.run) |
| 31 | t.Run("boolean literal true", testCase{ |
| 32 | s: `[1, 2, 3].count(true)`, |
| 33 | out: model.NewIntValue(3), |
| 34 | }.run) |
| 35 | t.Run("boolean literal false", testCase{ |
| 36 | s: `[1, 2, 3].count(false)`, |
| 37 | out: model.NewIntValue(0), |
| 38 | }.run) |
| 39 | t.Run("with nested property", testCase{ |
| 40 | inFn: func() *model.Value { |
| 41 | s := model.NewSliceValue() |
| 42 | _ = s.Append(model.NewValue(orderedmap.NewMap().Set("status", "active"))) |
| 43 | _ = s.Append(model.NewValue(orderedmap.NewMap().Set("status", "inactive"))) |
| 44 | _ = s.Append(model.NewValue(orderedmap.NewMap().Set("status", "active"))) |
| 45 | return s |
| 46 | }, |
| 47 | s: `count(status == "active")`, |
| 48 | out: model.NewIntValue(2), |
| 49 | }.run) |
| 50 | t.Run("chained after filter", testCase{ |
| 51 | s: `[1, 2, 3, 4, 5].filter($this > 2).count($this > 4)`, |
| 52 | out: model.NewIntValue(1), |
| 53 | }.run) |
| 54 | t.Run("count with $key", testCase{ |
| 55 | s: `[10, 20, 30].count($key > 0)`, |
| 56 | out: model.NewIntValue(2), |
| 57 | }.run) |
| 58 | } |