| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/execution" |
| 7 | "github.com/tomwright/dasel/v3/model" |
| 8 | ) |
| 9 | |
| 10 | func TestAssignVariable(t *testing.T) { |
| 11 | t.Run("single assign", testCase{ |
| 12 | s: `$x=1`, |
| 13 | outFn: func() *model.Value { |
| 14 | r := model.NewIntValue(1) |
| 15 | return r |
| 16 | }, |
| 17 | opts: []execution.ExecuteOptionFn{ |
| 18 | execution.WithUnstable(), |
| 19 | }, |
| 20 | }.run) |
| 21 | t.Run("double assign", testCase{ |
| 22 | s: `$x=1;$y=$x+1`, |
| 23 | outFn: func() *model.Value { |
| 24 | r := model.NewIntValue(2) |
| 25 | return r |
| 26 | }, |
| 27 | opts: []execution.ExecuteOptionFn{ |
| 28 | execution.WithUnstable(), |
| 29 | }, |
| 30 | }.run) |
| 31 | t.Run("multiple assign with final statement", testCase{ |
| 32 | s: `$first = 'Tom'; |
| 33 | $last = 'Wright'; |
| 34 | $full = $first + ' ' + $last; |
| 35 | {first: $first, last: $last, full: $full}`, |
| 36 | outFn: func() *model.Value { |
| 37 | r := model.NewMapValue() |
| 38 | if err := r.SetMapKey("first", model.NewStringValue("Tom")); err != nil { |
| 39 | t.Fatalf("Unexpected error: %s", err) |
| 40 | } |
| 41 | if err := r.SetMapKey("last", model.NewStringValue("Wright")); err != nil { |
| 42 | t.Fatalf("Unexpected error: %s", err) |
| 43 | } |
| 44 | if err := r.SetMapKey("full", model.NewStringValue("Tom Wright")); err != nil { |
| 45 | t.Fatalf("Unexpected error: %s", err) |
| 46 | } |
| 47 | return r |
| 48 | }, |
| 49 | opts: []execution.ExecuteOptionFn{ |
| 50 | execution.WithUnstable(), |
| 51 | }, |
| 52 | }.run) |
| 53 | t.Run("multiple assign with final statement and mixed case variables", testCase{ |
| 54 | s: `$firstName = 'Tom'; |
| 55 | $lastName = 'Wright'; |
| 56 | $fullName = $firstName + ' ' + $lastName; |
| 57 | {firstName: $firstName, lastName: $lastName, fullName: $fullName}`, |
| 58 | outFn: func() *model.Value { |
| 59 | r := model.NewMapValue() |
| 60 | if err := r.SetMapKey("firstName", model.NewStringValue("Tom")); err != nil { |
| 61 | t.Fatalf("Unexpected error: %s", err) |
| 62 | } |
| 63 | if err := r.SetMapKey("lastName", model.NewStringValue("Wright")); err != nil { |
| 64 | t.Fatalf("Unexpected error: %s", err) |
| 65 | } |
| 66 | if err := r.SetMapKey("fullName", model.NewStringValue("Tom Wright")); err != nil { |
| 67 | t.Fatalf("Unexpected error: %s", err) |
| 68 | } |
| 69 | return r |
| 70 | }, |
| 71 | opts: []execution.ExecuteOptionFn{ |
| 72 | execution.WithUnstable(), |
| 73 | }, |
| 74 | }.run) |
| 75 | t.Run("self referencing variable", testCase{ |
| 76 | s: `$x=1;$x=$x*2`, |
| 77 | outFn: func() *model.Value { |
| 78 | r := model.NewIntValue(2) |
| 79 | return r |
| 80 | }, |
| 81 | opts: []execution.ExecuteOptionFn{ |
| 82 | execution.WithUnstable(), |
| 83 | }, |
| 84 | }.run) |
| 85 | } |