Debian Codegraph @

execution/execute_variable_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 TestVariable(t *testing.T) {
13 t.Run("use passed variable", testCase{
14 s: `$x + 1`,
15 out: model.NewIntValue(11),
16 opts: []execution.ExecuteOptionFn{
17 execution.WithVariable("x", model.NewIntValue(10)),
18 },
19 }.run)
20
21 t.Run("variable in expression", testCase{
22 s: `$x + $y`,
23 out: model.NewIntValue(30),
24 opts: []execution.ExecuteOptionFn{
25 execution.WithVariable("x", model.NewIntValue(10)),
26 execution.WithVariable("y", model.NewIntValue(20)),
27 },
28 }.run)
29
30 t.Run("undefined variable errors", func(t *testing.T) {
31 in := model.NewNullValue()
32 _, err := execution.ExecuteSelector(context.Background(), `$undefined_var_xyz`, in, execution.NewOptions())
33 if err == nil {
34 t.Fatal("expected error for undefined variable")
35 }
36 if !strings.Contains(err.Error(), "not found") {
37 t.Fatalf("unexpected error: %s", err)
38 }
39 })
40
41 t.Run("env variable", func(t *testing.T) {
42 t.Setenv("DASEL_TEST_VAR_ABC", "from_env")
43 in := model.NewNullValue()
44 res, err := execution.ExecuteSelector(context.Background(), `$DASEL_TEST_VAR_ABC`, in, execution.NewOptions())
45 if err != nil {
46 t.Fatal(err)
47 }
48 got, err := res.StringValue()
49 if err != nil {
50 t.Fatal(err)
51 }
52 if got != "from_env" {
53 t.Errorf("expected 'from_env', got %s", got)
54 }
55 })
56 }