| 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 TestErrors(t *testing.T) { |
| 13 | t.Run("unknown function", func(t *testing.T) { |
| 14 | in := model.NewNullValue() |
| 15 | _, err := execution.ExecuteSelector(context.Background(), `unknownFunc123()`, in, execution.NewOptions()) |
| 16 | if err == nil { |
| 17 | t.Fatal("expected error for unknown function") |
| 18 | } |
| 19 | if !strings.Contains(err.Error(), "unknown function") { |
| 20 | t.Fatalf("unexpected error: %s", err) |
| 21 | } |
| 22 | }) |
| 23 | |
| 24 | t.Run("empty selector returns input", func(t *testing.T) { |
| 25 | in := model.NewStringValue("hello") |
| 26 | res, err := execution.ExecuteSelector(context.Background(), ``, in, execution.NewOptions()) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | got, err := res.StringValue() |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | if got != "hello" { |
| 35 | t.Errorf("expected 'hello', got %s", got) |
| 36 | } |
| 37 | }) |
| 38 | |
| 39 | t.Run("filter on non-array", func(t *testing.T) { |
| 40 | in := model.NewStringValue("not a slice") |
| 41 | _, err := execution.ExecuteSelector(context.Background(), `filter($this == "x")`, in, execution.NewOptions()) |
| 42 | if err == nil { |
| 43 | t.Fatal("expected error for filter on non-array") |
| 44 | } |
| 45 | }) |
| 46 | |
| 47 | t.Run("each on non-array", func(t *testing.T) { |
| 48 | in := model.NewStringValue("not a slice") |
| 49 | _, err := execution.ExecuteSelector(context.Background(), `each($this = $this)`, in, execution.NewOptions()) |
| 50 | if err == nil { |
| 51 | t.Fatal("expected error for each on non-array") |
| 52 | } |
| 53 | }) |
| 54 | |
| 55 | t.Run("sortBy on non-array", func(t *testing.T) { |
| 56 | in := model.NewStringValue("not a slice") |
| 57 | _, err := execution.ExecuteSelector(context.Background(), `sortBy($this)`, in, execution.NewOptions()) |
| 58 | if err == nil { |
| 59 | t.Fatal("expected error for sortBy on non-array") |
| 60 | } |
| 61 | }) |
| 62 | |
| 63 | t.Run("map on non-array", func(t *testing.T) { |
| 64 | in := model.NewStringValue("not a slice") |
| 65 | _, err := execution.ExecuteSelector(context.Background(), `map($this)`, in, execution.NewOptions()) |
| 66 | if err == nil { |
| 67 | t.Fatal("expected error for map on non-array") |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | t.Run("spread on non-spreadable", func(t *testing.T) { |
| 72 | in := model.NewIntValue(42) |
| 73 | _, err := execution.ExecuteSelector(context.Background(), `$this...`, in, execution.NewOptions()) |
| 74 | if err == nil { |
| 75 | t.Fatal("expected error for spread on int") |
| 76 | } |
| 77 | }) |
| 78 | |
| 79 | t.Run("all on non-array", func(t *testing.T) { |
| 80 | in := model.NewStringValue("not a slice") |
| 81 | _, err := execution.ExecuteSelector(context.Background(), `all($this == "x")`, in, execution.NewOptions()) |
| 82 | if err == nil { |
| 83 | t.Fatal("expected error for all on non-array") |
| 84 | } |
| 85 | }) |
| 86 | |
| 87 | t.Run("any on non-array", func(t *testing.T) { |
| 88 | in := model.NewStringValue("not a slice") |
| 89 | _, err := execution.ExecuteSelector(context.Background(), `any($this == "x")`, in, execution.NewOptions()) |
| 90 | if err == nil { |
| 91 | t.Fatal("expected error for any on non-array") |
| 92 | } |
| 93 | }) |
| 94 | |
| 95 | t.Run("count on non-array", func(t *testing.T) { |
| 96 | in := model.NewStringValue("not a slice") |
| 97 | _, err := execution.ExecuteSelector(context.Background(), `count($this == "x")`, in, execution.NewOptions()) |
| 98 | if err == nil { |
| 99 | t.Fatal("expected error for count on non-array") |
| 100 | } |
| 101 | }) |
| 102 | } |