| 1 | enum Color { |
| 2 | red |
| 3 | blue |
| 4 | green |
| 5 | } |
| 6 | |
| 7 | fn enum_option_helper(b bool) !Color { |
| 8 | if b { |
| 9 | return .red |
| 10 | } |
| 11 | return error('failed') |
| 12 | } |
| 13 | |
| 14 | fn test_enum_option() { |
| 15 | a := enum_option_helper(true) or { |
| 16 | assert false |
| 17 | return |
| 18 | } |
| 19 | assert a == .red |
| 20 | } |
| 21 | |
| 22 | fn test_enum() { |
| 23 | assert Color.red == .red |
| 24 | assert Color.blue == .blue |
| 25 | assert Color.green == .green |
| 26 | assert Color.red != .blue |
| 27 | assert Color.red != .green |
| 28 | assert Color.blue != .green |
| 29 | mut color := Color.red |
| 30 | assert color == .red |
| 31 | color = .green |
| 32 | assert color == .green |
| 33 | } |
| 34 | |
| 35 | fn test_short_enum_literal_equality_is_commutative() { |
| 36 | color := Color.red |
| 37 | assert color == .red |
| 38 | assert .red == color |
| 39 | assert color != .blue |
| 40 | assert .blue != color |
| 41 | } |
| 42 | |
| 43 | enum PowerDuration { |
| 44 | invulntics = 30 * 35 |
| 45 | invistics = 60 * 35 |
| 46 | infratics = 120 * 35 |
| 47 | } |
| 48 | |
| 49 | fn test_custom_values() { |
| 50 | mut p := PowerDuration.invulntics |
| 51 | assert int(p) == 30 * 35 |
| 52 | p = .invistics |
| 53 | assert int(p) == 60 * 35 |
| 54 | assert int(PowerDuration.infratics) == 120 * 35 |
| 55 | } |
| 56 | |
| 57 | fn test_in() { |
| 58 | color := Color.red |
| 59 | num := 3 // used to be an expr bug before `in` |
| 60 | assert color in [.red, .green] |
| 61 | assert num == 3 |
| 62 | println(color) |
| 63 | assert true |
| 64 | } |
| 65 | |
| 66 | fn test_match() { |
| 67 | color := Color.green |
| 68 | num := 3 |
| 69 | match color { |
| 70 | .red { |
| 71 | assert false |
| 72 | } |
| 73 | .green { |
| 74 | assert true |
| 75 | } |
| 76 | else { |
| 77 | assert false |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | println(color) |
| 82 | assert num == 3 |
| 83 | } |
| 84 | |
| 85 | enum Foo { |
| 86 | a = 1 |
| 87 | b = 2 |
| 88 | c = 3 |
| 89 | d = -10 |
| 90 | } |
| 91 | |
| 92 | fn test_nums() { |
| 93 | foo := Foo.a |
| 94 | assert foo == unsafe { Foo(1) } |
| 95 | assert Foo.c == unsafe { Foo(3) } |
| 96 | d := Foo.d |
| 97 | assert d == unsafe { Foo(-10) } |
| 98 | } |
| 99 | |
| 100 | enum Number as i32 { |
| 101 | a = 100 |
| 102 | b = 200 |
| 103 | c = 300 |
| 104 | d = 400 |
| 105 | } |
| 106 | |
| 107 | fn test_enum_as_i32() { |
| 108 | assert int(Number.a) == 100 |
| 109 | assert int(Number.b) == 200 |
| 110 | assert int(Number.c) == 300 |
| 111 | assert int(Number.d) == 400 |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | enum Expr { |
| 116 | BoolExpr(bool) |
| 117 | IntExpr(int) |
| 118 | //FloatExpr(int) |
| 119 | } |
| 120 | |
| 121 | fn get_expr() Expr { |
| 122 | return Expr.IntExpr(10) |
| 123 | |
| 124 | } |
| 125 | |
| 126 | fn test_typed_enum() { |
| 127 | i := Expr.IntExpr(10) |
| 128 | expr := Expr.BoolExpr(true) |
| 129 | //println(i) |
| 130 | //if i == expr { |
| 131 | |
| 132 | //} |
| 133 | println('done') |
| 134 | // expr = i |
| 135 | /* |
| 136 | match expr { |
| 137 | IntExpr(n) { println('INT ${n}') } |
| 138 | BoolExpr(b) { println('BOOL ${b}') } |
| 139 | } |
| 140 | */ |
| 141 | } |
| 142 | */ |
| 143 | /* |
| 144 | fn test_typed_enum() { |
| 145 | Expr i = { .obj = 10, .typ = IntExpr_type }; |
| 146 | Expr expr = { .obj = true, .typ = BoolExpr_type }; |
| 147 | // val = expr; |
| 148 | if (expr.typ == IntExpr_type) { |
| 149 | int n = (int)expr.obj; |
| 150 | println('INT ${n}'); |
| 151 | } |
| 152 | else if (expr.typ == BoolExpr_type) { |
| 153 | int b = (bool)expr.obj; |
| 154 | println('BOOL ${b}'); |
| 155 | } |
| 156 | } |
| 157 | */ |
| 158 | |
| 159 | enum FileType { |
| 160 | unknown |
| 161 | wiki |
| 162 | file |
| 163 | image |
| 164 | html |
| 165 | } |
| 166 | |
| 167 | fn test_enum_instance() { |
| 168 | mut filetype := FileType.unknown |
| 169 | eprintln(filetype) |
| 170 | s := 'x ${filetype} z' |
| 171 | assert s == 'x unknown z' |
| 172 | } |
| 173 | |
| 174 | enum Bar { |
| 175 | baz |
| 176 | } |
| 177 | |
| 178 | fn (_ Bar) baz() {} |
| 179 | |
| 180 | fn test_enum_variant_and_method_name_clash() { |
| 181 | x := Bar.baz |
| 182 | println(x) |
| 183 | } |
| 184 | |
| 185 | const base = 600000 |
| 186 | |
| 187 | enum EnumWithExpressions { |
| 188 | aa |
| 189 | bb = base |
| 190 | cc |
| 191 | dd = base + 10 |
| 192 | ee = base * 99 - 4 |
| 193 | } |
| 194 | |
| 195 | fn test_enum_variant_with_value_based_on_const_expression() { |
| 196 | assert int(EnumWithExpressions.bb) == base |
| 197 | assert int(EnumWithExpressions.cc) == base + 1 |
| 198 | assert int(EnumWithExpressions.dd) == 600010 |
| 199 | assert int(EnumWithExpressions.ee) == 59399996 |
| 200 | } |
| 201 | |