| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | ) |
| 8 | |
| 9 | func TestConditional(t *testing.T) { |
| 10 | t.Run("true", testCase{ |
| 11 | s: `if (true) { "yes" } else { "no" }`, |
| 12 | out: model.NewStringValue("yes"), |
| 13 | }.run) |
| 14 | t.Run("false", testCase{ |
| 15 | s: `if (false) { "yes" } else { "no" }`, |
| 16 | out: model.NewStringValue("no"), |
| 17 | }.run) |
| 18 | t.Run("nested", testCase{ |
| 19 | s: ` |
| 20 | if (true) { |
| 21 | if (true) { "yes" } |
| 22 | else { "no" } |
| 23 | } else { "no" }`, |
| 24 | out: model.NewStringValue("yes"), |
| 25 | }.run) |
| 26 | t.Run("nested false", testCase{ |
| 27 | s: ` |
| 28 | if (true) { |
| 29 | if (false) { "yes" } |
| 30 | else { "no" } |
| 31 | } else { "no" }`, |
| 32 | out: model.NewStringValue("no"), |
| 33 | }.run) |
| 34 | t.Run("else if", testCase{ |
| 35 | s: ` |
| 36 | if (false) { "yes" } |
| 37 | elseif (true) { "no" } |
| 38 | else { "maybe" }`, |
| 39 | out: model.NewStringValue("no"), |
| 40 | }.run) |
| 41 | t.Run("else if else", testCase{ |
| 42 | s: ` |
| 43 | if (false) { "yes" } |
| 44 | elseif (false) { "no" } |
| 45 | else { "maybe" }`, |
| 46 | out: model.NewStringValue("maybe"), |
| 47 | }.run) |
| 48 | t.Run("if elseif elseif else", testCase{ |
| 49 | s: ` |
| 50 | if (false) { "yes" } |
| 51 | elseif (false) { "no" } |
| 52 | elseif (false) { "maybe" } |
| 53 | else { "nope" }`, |
| 54 | out: model.NewStringValue("nope"), |
| 55 | }.run) |
| 56 | |
| 57 | t.Run("false condition no else returns null", testCase{ |
| 58 | s: `if (false) { "yes" }`, |
| 59 | out: model.NewNullValue(), |
| 60 | }.run) |
| 61 | |
| 62 | t.Run("comparison false no else returns null", testCase{ |
| 63 | s: `if (1 > 2) { "yes" }`, |
| 64 | out: model.NewNullValue(), |
| 65 | }.run) |
| 66 | } |