MCPcopy
hub / github.com/TomWright/dasel / TestVariable

Function TestVariable

execution/execute_variable_test.go:12–56  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func 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}

Callers

nothing calls this directly

Calls 8

NewIntValueFunction · 0.92
WithVariableFunction · 0.92
NewNullValueFunction · 0.92
ExecuteSelectorFunction · 0.92
NewOptionsFunction · 0.92
StringValueMethod · 0.80
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected