v/vlib/compiler/tests/enum_test.v

117 lines
1.6 KiB
V
Raw Normal View History

2019-07-13 08:45:40 +02:00
enum Color {
red
blue
green
}
2020-01-31 16:00:31 +01:00
fn enum_optional_helper(b bool) ?Color {
if b {
return .red
}
return error('failed')
}
fn test_enum_optional() {
a := enum_optional_helper(true) or {
assert false
return
}
assert a == .red
}
2019-07-13 08:45:40 +02:00
fn test_enum() {
assert Color.red == .red
assert Color.blue == .blue
assert Color.green == .green
2019-10-30 14:38:47 +01:00
assert Color.red != .blue
assert Color.red != .green
assert Color.blue != .green
2019-07-13 08:45:40 +02:00
mut color := Color.red
2019-10-30 14:38:47 +01:00
assert color == .red
2019-07-13 08:45:40 +02:00
color = .green
2019-10-30 14:38:47 +01:00
assert color == .green
2019-07-13 08:45:40 +02:00
}
2019-10-24 13:56:02 +02:00
fn test_in() {
color := Color.red
num := 3 // used to be an expr bug before `in`
2019-10-24 13:56:02 +02:00
assert color in [.red, .green]
assert num == 3
2019-12-18 03:45:48 +01:00
println(color)
assert true
}
fn test_match() {
2019-12-18 03:45:48 +01:00
color := Color.green
num := 3
match color {
2019-12-18 03:45:48 +01:00
.red { assert false }
.green { assert true }
else { assert false }
2019-12-18 03:45:48 +01:00
}
println(color)
assert num == 3
2019-12-18 03:45:48 +01:00
}
2019-12-21 08:35:29 +01:00
enum Foo {
a = 1
b = 2
c = 3
d = -10
}
fn test_nums() {
foo := Foo.a
assert foo == 1
assert Foo.c == 3
d := Foo.d
assert d == -10
}
2019-12-22 22:31:28 +01:00
enum Expr {
BoolExpr(bool)
IntExpr(int)
//FloatExpr(int)
}
fn get_expr() Expr {
return Expr.IntExpr(10)
}
fn test_typed_enum() {
i := Expr.IntExpr(10)
expr := Expr.BoolExpr(true)
//println(i)
//if i == expr {
//}
println('done')
// expr = i
/*
match expr {
IntExpr(n) { println('INT $n') }
BoolExpr(b) { println('BOOL $b') }
}
*/
}
/*
fn test_typed_enum() {
Expr i = { .obj = 10, .typ = IntExpr_type };
Expr expr = { .obj = true, .typ = BoolExpr_type };
// val = expr;
if (expr.typ == IntExpr_type) {
int n = (int)expr.obj;
println('INT $n');
}
else if (expr.typ == BoolExpr_type) {
int b = (bool)expr.obj;
println('BOOL $b');
}
}
*/