| 1 | fn test_if_expression_precedence_false_condition() { |
| 2 | b := 10 |
| 3 | c := 20 |
| 4 | res := 1 + if b > c { b } else { c } + 1 |
| 5 | assert res == c + 2 |
| 6 | } |
| 7 | |
| 8 | fn test_if_expression_precedence_true_condition() { |
| 9 | b := 20 |
| 10 | c := 10 |
| 11 | res := 1 + if b > c { b } else { c } + 1 |
| 12 | assert res == b + 2 |
| 13 | } |
| 14 | |
| 15 | fn test_if_expression_with_stmts() { |
| 16 | a := if true { |
| 17 | b := 1 |
| 18 | b |
| 19 | } else { |
| 20 | b := 4 |
| 21 | b |
| 22 | } |
| 23 | assert a == 1 |
| 24 | mut b := 0 |
| 25 | b = if false { 42 } else { 24 } |
| 26 | assert b == 24 |
| 27 | } |
| 28 | |
| 29 | fn noop() { |
| 30 | } |
| 31 | |
| 32 | fn test_if_expression_with_function_assign() { |
| 33 | a := if true { |
| 34 | my_fn := noop |
| 35 | my_fn() |
| 36 | 0 |
| 37 | } else { |
| 38 | 1 |
| 39 | } |
| 40 | assert a == 0 |
| 41 | } |
| 42 | |
| 43 | fn get_bool_str(b bool) string { |
| 44 | return b.str() |
| 45 | } |
| 46 | |
| 47 | fn test_if_expression_mutate_var() { |
| 48 | mut b := false |
| 49 | r := b && if true { |
| 50 | b = true |
| 51 | true |
| 52 | } else { |
| 53 | true |
| 54 | } |
| 55 | assert r == false |
| 56 | // test in function call |
| 57 | assert get_bool_str(b && if true { |
| 58 | b = true |
| 59 | true |
| 60 | } else { |
| 61 | true |
| 62 | }) == 'false' |
| 63 | // test on method call |
| 64 | assert (b && if true { |
| 65 | b = true |
| 66 | true |
| 67 | } else { |
| 68 | true |
| 69 | }).str() == 'false' |
| 70 | // test on array |
| 71 | mut a := [1, 2] |
| 72 | assert a.len == 2 && if true { |
| 73 | a << 3 |
| 74 | true |
| 75 | } else { |
| 76 | false |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fn test_simple_nested_if_expressions() { |
| 81 | a := if false { |
| 82 | b := 1 |
| 83 | if b == 0 { |
| 84 | 0 |
| 85 | } else { |
| 86 | b |
| 87 | } |
| 88 | } else { |
| 89 | println('Hello world !') |
| 90 | if 1 == 1 { |
| 91 | t := 12 |
| 92 | t + 42 |
| 93 | } else { |
| 94 | 43 |
| 95 | } |
| 96 | } |
| 97 | assert a == 54 |
| 98 | } |
| 99 | |
| 100 | fn test_complex_nested_if_expressions() { |
| 101 | mut a := false |
| 102 | a = (1 == 2 || true) && (if true { |
| 103 | g := 6 |
| 104 | h := if false { 3 } else { 5 } |
| 105 | mut d := false |
| 106 | if h == 2 { |
| 107 | d = g + 4 == 5 |
| 108 | } |
| 109 | if d { |
| 110 | if true { |
| 111 | d = false |
| 112 | } else { |
| 113 | d = true |
| 114 | } |
| 115 | } |
| 116 | d |
| 117 | } else { |
| 118 | x := 6 |
| 119 | y := 8 |
| 120 | if x + y > 0 { |
| 121 | x > 0 |
| 122 | } else { |
| 123 | false |
| 124 | } |
| 125 | }) |
| 126 | assert a == false |
| 127 | } |
| 128 | |
| 129 | fn test_lots_of_if_expressions() { |
| 130 | mut a := 0 |
| 131 | if true { |
| 132 | if true { |
| 133 | if true { |
| 134 | if true { |
| 135 | if true { |
| 136 | if true { |
| 137 | if true { |
| 138 | if true { |
| 139 | if true { |
| 140 | if true { |
| 141 | if true { |
| 142 | a = 1 |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | assert a == 1 |
| 155 | } |
| 156 | |
| 157 | fn test_if_expr_with_infix() { |
| 158 | a := if true { 1 } else { 0 } + 5 |
| 159 | assert a == 6 |
| 160 | } |
| 161 | |
| 162 | fn test_multi_if_expr_with_infix() { |
| 163 | a := if 1 == 0 { |
| 164 | 1 |
| 165 | } else if 1 == 0 { |
| 166 | 2 |
| 167 | } else { |
| 168 | 3 |
| 169 | } + 4 |
| 170 | assert a == 7 |
| 171 | } |
| 172 | |
| 173 | fn test_nested_if_expr_with_infix_in_branch() { |
| 174 | a := if false { 0 } else { if false { 2 } else { 3 } + 4 |
| 175 | } + 5 |
| 176 | assert a == 12 |
| 177 | |
| 178 | b := if false { 'a' } else { if false { 'c' } else { 'd' } + 'e' |
| 179 | } + 'f' |
| 180 | assert b == 'def' |
| 181 | } |
| 182 | |
| 183 | fn test_if_expr_with_array_map() { |
| 184 | num_string := '2 3' |
| 185 | |
| 186 | assigned := if num_string.len > 1 { num_string.split(' ').map(it.int()) } else { [ |
| 187 | 789, |
| 188 | ] } |
| 189 | |
| 190 | println(assigned) |
| 191 | assert assigned == [2, 3] |
| 192 | } |
| 193 | |
| 194 | fn test_if_epxr_with_array_conditions() { |
| 195 | num_arr := [1, 2, 3] |
| 196 | if num_arr == [] { |
| 197 | assert false |
| 198 | } |
| 199 | str_arr := [['foo'], ['bar']] |
| 200 | if str_arr == [][]string{} { |
| 201 | assert false |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | fn min[T](a T, b T) T { |
| 206 | return if a < b { a } else { b } |
| 207 | } |
| 208 | |
| 209 | fn test_if_expr_with_fn_generic() { |
| 210 | assert min(42, 13) == 13 |
| 211 | } |
| 212 | |
| 213 | fn test_if_expr_with_complex_array_methods() { |
| 214 | mut ret := []string{} |
| 215 | entries := ['a', 'b', 'c'] |
| 216 | |
| 217 | if false { |
| 218 | ret = entries.map(it.capitalize()) |
| 219 | } else if entries.any(it == 'a') { |
| 220 | ret = entries.map(it) |
| 221 | } |
| 222 | |
| 223 | println(ret) |
| 224 | assert ret == ['a', 'b', 'c'] |
| 225 | } |
| 226 | |
| 227 | fn return_option() ?int { |
| 228 | return 1 |
| 229 | } |
| 230 | |
| 231 | fn test_if_expr_with_option() { |
| 232 | m := map[string]int{} |
| 233 | v := if a := m['a'] { |
| 234 | println('${a}') |
| 235 | return_option()? |
| 236 | } else { |
| 237 | 2 |
| 238 | } |
| 239 | assert v == 2 |
| 240 | } |
| 241 | |
| 242 | fn test_if_expr_with_or_block() { |
| 243 | arr := ['a'] |
| 244 | a := if arr.len == 0 || arr[0] == '-' { 123 } else { return_option() or { -1 } } |
| 245 | assert a == 1 |
| 246 | } |
| 247 | |
| 248 | type Num = f32 | f64 | i64 | int |
| 249 | |
| 250 | @[noreturn] |
| 251 | fn assert_false_noreturn() { |
| 252 | assert false |
| 253 | exit(1) |
| 254 | } |
| 255 | |
| 256 | fn test_noreturn() { |
| 257 | n := Num(int(0)) |
| 258 | _ := if n is int { |
| 259 | n |
| 260 | } else if n is f32 { |
| 261 | int(n) |
| 262 | } else { |
| 263 | exit(1) |
| 264 | } |
| 265 | |
| 266 | _ := if 1 == 0 { |
| 267 | 0 |
| 268 | } else if 1 == 1 { |
| 269 | 1 |
| 270 | } else if 1 == 2 { |
| 271 | panic('err') |
| 272 | } else { |
| 273 | assert_false_noreturn() |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // for issue 20300 |
| 278 | // Phenomenon of issue: |
| 279 | // infix expr generates wraparound parentheses, but misses the case where `array_contains()` is used. |
| 280 | fn test_in_array_init() { |
| 281 | if 0 in []int{} { |
| 282 | assert false |
| 283 | } |
| 284 | assert true |
| 285 | } |
| 286 | |