| 1 | fn foo(a int) !int { |
| 2 | if a < 0 { |
| 3 | return error('foo') |
| 4 | } |
| 5 | return a |
| 6 | } |
| 7 | |
| 8 | fn bar(a int) !int { |
| 9 | return foo(a) or { |
| 10 | if a < 0 { |
| 11 | -1 |
| 12 | } else { |
| 13 | return error('bar') |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | fn baz(a int) !int { |
| 19 | return foo(a) or { |
| 20 | if a < 0 { |
| 21 | -1 |
| 22 | } else { |
| 23 | -2 |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn qux(a int) ?int { |
| 29 | if a < 0 { |
| 30 | return none |
| 31 | } |
| 32 | return a |
| 33 | } |
| 34 | |
| 35 | fn quux(a int) ?int { |
| 36 | return qux(a) or { |
| 37 | if a < 0 { |
| 38 | -1 |
| 39 | } else { |
| 40 | return none |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn test_if_expr_in_result_or_block_with_return() { |
| 46 | assert bar(0)! == 0 |
| 47 | assert bar(-1)! == -1 |
| 48 | assert bar(1)! == 1 |
| 49 | } |
| 50 | |
| 51 | fn test_if_expr_in_result_or_block_simple() { |
| 52 | assert baz(0)! == 0 |
| 53 | assert baz(-1)! == -1 |
| 54 | assert baz(1)! == 1 |
| 55 | } |
| 56 | |
| 57 | fn test_if_expr_in_option_or_block_with_return() { |
| 58 | assert quux(0)? == 0 |
| 59 | assert quux(-1)? == -1 |
| 60 | assert quux(1)? == 1 |
| 61 | } |
| 62 | |
| 63 | fn test_nested_if_in_or_block() { |
| 64 | get_val := fn (x int) !int { |
| 65 | if x < 0 { |
| 66 | return error('negative') |
| 67 | } |
| 68 | return x * 2 |
| 69 | } |
| 70 | result := get_val(-5) or { |
| 71 | if true { |
| 72 | if false { |
| 73 | 100 |
| 74 | } else { |
| 75 | -10 |
| 76 | } |
| 77 | } else { |
| 78 | 200 |
| 79 | } |
| 80 | } |
| 81 | assert result == -10 |
| 82 | } |
| 83 | |
| 84 | fn test_if_expr_with_multiple_branches_in_or_block() { |
| 85 | compute := fn (x int) !int { |
| 86 | if x == 0 { |
| 87 | return error('zero') |
| 88 | } |
| 89 | return x |
| 90 | } |
| 91 | val := compute(0) or { |
| 92 | match err.msg() { |
| 93 | 'zero' { |
| 94 | 1 |
| 95 | } |
| 96 | 'negative' { |
| 97 | 2 |
| 98 | } |
| 99 | else { |
| 100 | 3 |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | assert val == 1 |
| 105 | } |
| 106 | |
| 107 | struct IfExprEmptyReply { |
| 108 | value int |
| 109 | } |
| 110 | |
| 111 | struct IfExprRedisError { |
| 112 | msg_ string |
| 113 | } |
| 114 | |
| 115 | fn (e IfExprRedisError) msg() string { |
| 116 | return e.msg_ |
| 117 | } |
| 118 | |
| 119 | fn (e IfExprRedisError) code() int { |
| 120 | return 0 |
| 121 | } |
| 122 | |
| 123 | const if_expr_nil_reply = IfExprRedisError{'nil'} |
| 124 | |
| 125 | type IfExprReply = IfExprEmptyReply | IfExprRedisError |
| 126 | |
| 127 | fn if_expr_read_reply(msg string) !IfExprReply { |
| 128 | return IError(IfExprRedisError{msg}) |
| 129 | } |
| 130 | |
| 131 | fn if_expr_decode_reply(msg string) IfExprReply { |
| 132 | return if_expr_read_reply(msg) or { |
| 133 | if err is IfExprRedisError { |
| 134 | if err == if_expr_nil_reply { |
| 135 | IfExprEmptyReply{0} |
| 136 | } else { |
| 137 | err |
| 138 | } |
| 139 | } else { |
| 140 | panic(err) |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn test_if_expr_in_or_block_with_smartcasted_interface_sumtype_value() { |
| 146 | reply1 := if_expr_decode_reply('nil') |
| 147 | assert reply1 is IfExprEmptyReply |
| 148 | if reply1 is IfExprEmptyReply { |
| 149 | assert reply1.value == 0 |
| 150 | } |
| 151 | |
| 152 | reply2 := if_expr_decode_reply('x') |
| 153 | assert reply2 is IfExprRedisError |
| 154 | if reply2 is IfExprRedisError { |
| 155 | assert reply2.msg_ == 'x' |
| 156 | } |
| 157 | } |
| 158 | |