| 1 | struct Abc { |
| 2 | x int |
| 3 | } |
| 4 | |
| 5 | fn i_0(x int) ?int { |
| 6 | if x == 0 { |
| 7 | return none |
| 8 | } |
| 9 | return x |
| 10 | } |
| 11 | |
| 12 | fn struct_0(x int) ?Abc { |
| 13 | if x == 0 { |
| 14 | return none |
| 15 | } |
| 16 | return Abc{x} |
| 17 | } |
| 18 | |
| 19 | fn string_0(x int) ?string { |
| 20 | if x == 0 { |
| 21 | return none |
| 22 | } |
| 23 | return '${x}' |
| 24 | } |
| 25 | |
| 26 | fn b_0(b bool) ?bool { |
| 27 | if b == false { |
| 28 | return none |
| 29 | } |
| 30 | return b |
| 31 | } |
| 32 | |
| 33 | fn return_a_string() string { |
| 34 | return 'abcdef' |
| 35 | } |
| 36 | |
| 37 | // |
| 38 | fn test_option_int() { |
| 39 | a := i_0(0) or { 4999 } |
| 40 | assert a == 4999 |
| 41 | b := i_0(4123) or { 4999 } |
| 42 | assert b == 4123 |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | fn test_option_bool() { |
| 47 | a := true && b_0(false) { |
| 48 | true |
| 49 | } |
| 50 | assert a == true |
| 51 | b := false || b_0(true) { |
| 52 | false |
| 53 | } |
| 54 | assert b == true |
| 55 | } |
| 56 | */ |
| 57 | fn test_option_struct() { |
| 58 | sa := struct_0(0) or { Abc{7999} } |
| 59 | assert sa.x == 7999 |
| 60 | sb := struct_0(3456) or { Abc{7999} } |
| 61 | assert sb.x == 3456 |
| 62 | } |
| 63 | |
| 64 | fn test_option_with_statements_before_last_expression() { |
| 65 | s := struct_0(0) or { |
| 66 | eprintln('hello') |
| 67 | Abc{12345} |
| 68 | } |
| 69 | assert s.x == 12345 |
| 70 | b := b_0(true) or { false } |
| 71 | assert b == true |
| 72 | } |
| 73 | |
| 74 | fn test_option_with_fn_call_as_last_expression() { |
| 75 | s := string_0(0) or { return_a_string() } |
| 76 | assert s == 'abcdef' |
| 77 | } |
| 78 | |
| 79 | fn test_option_with_fn_call_last_expr_and_preceding_statements() { |
| 80 | s := string_0(0) or { |
| 81 | eprintln('hello') |
| 82 | println('world') |
| 83 | return_a_string() |
| 84 | } |
| 85 | assert s == 'abcdef' |
| 86 | } |
| 87 | |
| 88 | fn test_nested_option() { |
| 89 | a := i_0(1) or { |
| 90 | b := i_0(0) or { 3 } |
| 91 | b |
| 92 | } |
| 93 | assert a == 1 |
| 94 | b := i_0(0) or { |
| 95 | c := i_0(1) or { 3 } |
| 96 | c |
| 97 | } |
| 98 | assert b == 1 |
| 99 | c := i_0(0) or { |
| 100 | d := i_0(0) or { 3 } |
| 101 | d |
| 102 | } |
| 103 | assert c == 3 |
| 104 | } |
| 105 | |
| 106 | fn test_nested_option_with_opt_fn_call_as_last_value() { |
| 107 | a := i_0(1) or { i_0(0) or { 3 } } |
| 108 | assert a == 1 |
| 109 | b := i_0(0) or { i_0(1) or { 3 } } |
| 110 | assert b == 1 |
| 111 | c := i_0(0) or { i_0(0) or { 3 } } |
| 112 | assert c == 3 |
| 113 | // TODO: Enable once option in boolean expressions are working |
| 114 | // d := b_0(true) or { |
| 115 | // false && b_0(true) or { |
| 116 | // true |
| 117 | // } |
| 118 | // } |
| 119 | // assert d == false |
| 120 | // e := b_0(true) or { |
| 121 | // true && b_0(true) or { |
| 122 | // false |
| 123 | // } |
| 124 | // } |
| 125 | // assert e == false |
| 126 | } |
| 127 | |
| 128 | fn remove_suffix1(s string) string { |
| 129 | n := s.len |
| 130 | i := s.last_index('.') or { n } |
| 131 | return s[0..i] |
| 132 | } |
| 133 | |
| 134 | fn test_var_inside_or_block() { |
| 135 | assert remove_suffix1('Vlang.foo') == 'Vlang' |
| 136 | } |
| 137 | |