| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | ) |
| 8 | |
| 9 | func TestFuncUnique(t *testing.T) { |
| 10 | t.Run("ints with duplicates", testCase{ |
| 11 | s: `unique([1, 2, 2, 3, 1])`, |
| 12 | outFn: func() *model.Value { |
| 13 | r := model.NewSliceValue() |
| 14 | for _, v := range []int64{1, 2, 3} { |
| 15 | if err := r.Append(model.NewIntValue(v)); err != nil { |
| 16 | panic(err) |
| 17 | } |
| 18 | } |
| 19 | return r |
| 20 | }, |
| 21 | }.run) |
| 22 | t.Run("strings with duplicates", testCase{ |
| 23 | s: `unique(["a", "b", "a", "c"])`, |
| 24 | outFn: func() *model.Value { |
| 25 | r := model.NewSliceValue() |
| 26 | for _, v := range []string{"a", "b", "c"} { |
| 27 | if err := r.Append(model.NewStringValue(v)); err != nil { |
| 28 | panic(err) |
| 29 | } |
| 30 | } |
| 31 | return r |
| 32 | }, |
| 33 | }.run) |
| 34 | t.Run("already unique", testCase{ |
| 35 | s: `unique([1, 2, 3])`, |
| 36 | outFn: func() *model.Value { |
| 37 | r := model.NewSliceValue() |
| 38 | for _, v := range []int64{1, 2, 3} { |
| 39 | if err := r.Append(model.NewIntValue(v)); err != nil { |
| 40 | panic(err) |
| 41 | } |
| 42 | } |
| 43 | return r |
| 44 | }, |
| 45 | }.run) |
| 46 | t.Run("chained", testCase{ |
| 47 | s: `[1, 1, 2].unique()`, |
| 48 | outFn: func() *model.Value { |
| 49 | r := model.NewSliceValue() |
| 50 | for _, v := range []int64{1, 2} { |
| 51 | if err := r.Append(model.NewIntValue(v)); err != nil { |
| 52 | panic(err) |
| 53 | } |
| 54 | } |
| 55 | return r |
| 56 | }, |
| 57 | }.run) |
| 58 | } |