| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | ) |
| 8 | |
| 9 | func TestTernary(t *testing.T) { |
| 10 | t.Run("true literal", testCase{ |
| 11 | s: `true ? "yes" : "no"`, |
| 12 | out: model.NewStringValue("yes"), |
| 13 | }.run) |
| 14 | t.Run("false literal", testCase{ |
| 15 | s: `false ? "yes" : "no"`, |
| 16 | out: model.NewStringValue("no"), |
| 17 | }.run) |
| 18 | t.Run("condition with comparison true", testCase{ |
| 19 | s: `1 + 1 == 2 ? "math" : "broken"`, |
| 20 | out: model.NewStringValue("math"), |
| 21 | }.run) |
| 22 | t.Run("condition with comparison false", testCase{ |
| 23 | s: `1 + 1 == 3 ? "math" : "broken"`, |
| 24 | out: model.NewStringValue("broken"), |
| 25 | }.run) |
| 26 | t.Run("nested ternary in then", testCase{ |
| 27 | s: `true ? (false ? "a" : "b") : "c"`, |
| 28 | out: model.NewStringValue("b"), |
| 29 | }.run) |
| 30 | t.Run("nested ternary in else", testCase{ |
| 31 | s: `false ? "a" : (true ? "b" : "c")`, |
| 32 | out: model.NewStringValue("b"), |
| 33 | }.run) |
| 34 | t.Run("double nested ternary", testCase{ |
| 35 | s: `true ? (true ? (false ? "a" : "b") : "c") : "d"`, |
| 36 | out: model.NewStringValue("b"), |
| 37 | }.run) |
| 38 | t.Run("with input data true", testCase{ |
| 39 | inFn: func() *model.Value { |
| 40 | return model.NewValue(map[string]interface{}{"age": 25}) |
| 41 | }, |
| 42 | s: `age >= 18 ? "adult" : "minor"`, |
| 43 | out: model.NewStringValue("adult"), |
| 44 | }.run) |
| 45 | t.Run("with input data false", testCase{ |
| 46 | inFn: func() *model.Value { |
| 47 | return model.NewValue(map[string]interface{}{"age": 10}) |
| 48 | }, |
| 49 | s: `age >= 18 ? "adult" : "minor"`, |
| 50 | out: model.NewStringValue("minor"), |
| 51 | }.run) |
| 52 | t.Run("returns number", testCase{ |
| 53 | s: `true ? 42 : 0`, |
| 54 | out: model.NewIntValue(42), |
| 55 | }.run) |
| 56 | t.Run("returns float", testCase{ |
| 57 | s: `true ? 3.14 : 0.0`, |
| 58 | out: model.NewFloatValue(3.14), |
| 59 | }.run) |
| 60 | t.Run("returns null in else", testCase{ |
| 61 | s: `false ? "yes" : null`, |
| 62 | out: model.NewNullValue(), |
| 63 | }.run) |
| 64 | t.Run("returns null in then", testCase{ |
| 65 | s: `true ? null : "no"`, |
| 66 | out: model.NewNullValue(), |
| 67 | }.run) |
| 68 | t.Run("returns property", testCase{ |
| 69 | inFn: func() *model.Value { |
| 70 | return model.NewValue(map[string]interface{}{"active": true, "name": "Alice"}) |
| 71 | }, |
| 72 | s: `active ? name : "unknown"`, |
| 73 | out: model.NewStringValue("Alice"), |
| 74 | }.run) |
| 75 | t.Run("returns property from else", testCase{ |
| 76 | inFn: func() *model.Value { |
| 77 | return model.NewValue(map[string]interface{}{"active": false, "name": "Alice", "fallback": "Bob"}) |
| 78 | }, |
| 79 | s: `active ? name : fallback`, |
| 80 | out: model.NewStringValue("Bob"), |
| 81 | }.run) |
| 82 | t.Run("chained property in condition", testCase{ |
| 83 | inFn: func() *model.Value { |
| 84 | return model.NewValue(map[string]interface{}{ |
| 85 | "user": map[string]interface{}{"active": true}, |
| 86 | }) |
| 87 | }, |
| 88 | s: `user.active ? "yes" : "no"`, |
| 89 | out: model.NewStringValue("yes"), |
| 90 | }.run) |
| 91 | t.Run("arithmetic in then branch", testCase{ |
| 92 | s: `true ? 1 + 2 : 3 + 4`, |
| 93 | out: model.NewIntValue(3), |
| 94 | }.run) |
| 95 | t.Run("arithmetic in else branch", testCase{ |
| 96 | s: `false ? 1 + 2 : 3 + 4`, |
| 97 | out: model.NewIntValue(7), |
| 98 | }.run) |
| 99 | t.Run("logical and condition", testCase{ |
| 100 | inFn: func() *model.Value { |
| 101 | return model.NewValue(map[string]interface{}{"a": 5, "b": 10}) |
| 102 | }, |
| 103 | s: `a > 1 && b > 5 ? "yes" : "no"`, |
| 104 | out: model.NewStringValue("yes"), |
| 105 | }.run) |
| 106 | t.Run("logical and condition false", testCase{ |
| 107 | inFn: func() *model.Value { |
| 108 | return model.NewValue(map[string]interface{}{"a": 0, "b": 10}) |
| 109 | }, |
| 110 | s: `a > 1 && b > 5 ? "yes" : "no"`, |
| 111 | out: model.NewStringValue("no"), |
| 112 | }.run) |
| 113 | t.Run("logical or condition", testCase{ |
| 114 | inFn: func() *model.Value { |
| 115 | return model.NewValue(map[string]interface{}{"a": 0, "b": 10}) |
| 116 | }, |
| 117 | s: `a > 1 || b > 5 ? "yes" : "no"`, |
| 118 | out: model.NewStringValue("yes"), |
| 119 | }.run) |
| 120 | t.Run("logical or condition false", testCase{ |
| 121 | inFn: func() *model.Value { |
| 122 | return model.NewValue(map[string]interface{}{"a": 0, "b": 1}) |
| 123 | }, |
| 124 | s: `a > 1 || b > 5 ? "yes" : "no"`, |
| 125 | out: model.NewStringValue("no"), |
| 126 | }.run) |
| 127 | t.Run("func call in then branch", testCase{ |
| 128 | s: `true ? len("abc") : len("de")`, |
| 129 | out: model.NewIntValue(3), |
| 130 | }.run) |
| 131 | t.Run("func call in else branch", testCase{ |
| 132 | s: `false ? len("abc") : len("de")`, |
| 133 | out: model.NewIntValue(2), |
| 134 | }.run) |
| 135 | t.Run("string concatenation in then", testCase{ |
| 136 | s: `true ? "hello" + " " + "world" : "goodbye"`, |
| 137 | out: model.NewStringValue("hello world"), |
| 138 | }.run) |
| 139 | t.Run("string concatenation in else", testCase{ |
| 140 | s: `false ? "hello" : "good" + "bye"`, |
| 141 | out: model.NewStringValue("goodbye"), |
| 142 | }.run) |
| 143 | t.Run("equality check false condition", testCase{ |
| 144 | s: `1 == 2 ? "yes" : "no"`, |
| 145 | out: model.NewStringValue("no"), |
| 146 | }.run) |
| 147 | t.Run("not equal condition", testCase{ |
| 148 | s: `1 != 2 ? "different" : "same"`, |
| 149 | out: model.NewStringValue("different"), |
| 150 | }.run) |
| 151 | t.Run("less than condition", testCase{ |
| 152 | s: `1 < 2 ? "less" : "not less"`, |
| 153 | out: model.NewStringValue("less"), |
| 154 | }.run) |
| 155 | t.Run("greater than or equal condition", testCase{ |
| 156 | inFn: func() *model.Value { |
| 157 | return model.NewValue(map[string]interface{}{"val": 5}) |
| 158 | }, |
| 159 | s: `val >= 5 ? "gte" : "lt"`, |
| 160 | out: model.NewStringValue("gte"), |
| 161 | }.run) |
| 162 | t.Run("less than or equal condition", testCase{ |
| 163 | inFn: func() *model.Value { |
| 164 | return model.NewValue(map[string]interface{}{"val": 5}) |
| 165 | }, |
| 166 | s: `val <= 5 ? "lte" : "gt"`, |
| 167 | out: model.NewStringValue("lte"), |
| 168 | }.run) |
| 169 | t.Run("subtraction in condition", testCase{ |
| 170 | s: `10 - 3 == 7 ? "yes" : "no"`, |
| 171 | out: model.NewStringValue("yes"), |
| 172 | }.run) |
| 173 | t.Run("bool property as condition", testCase{ |
| 174 | inFn: func() *model.Value { |
| 175 | return model.NewValue(map[string]interface{}{"enabled": true}) |
| 176 | }, |
| 177 | s: `enabled ? "on" : "off"`, |
| 178 | out: model.NewStringValue("on"), |
| 179 | }.run) |
| 180 | t.Run("bool property false as condition", testCase{ |
| 181 | inFn: func() *model.Value { |
| 182 | return model.NewValue(map[string]interface{}{"enabled": false}) |
| 183 | }, |
| 184 | s: `enabled ? "on" : "off"`, |
| 185 | out: model.NewStringValue("off"), |
| 186 | }.run) |
| 187 | t.Run("multiplication in condition", testCase{ |
| 188 | s: `2 * 3 == 6 ? "yes" : "no"`, |
| 189 | out: model.NewStringValue("yes"), |
| 190 | }.run) |
| 191 | t.Run("modulo in condition", testCase{ |
| 192 | s: `10 % 3 == 1 ? "yes" : "no"`, |
| 193 | out: model.NewStringValue("yes"), |
| 194 | }.run) |
| 195 | } |