v/vlib/compiler/tests/match_test.v

94 lines
1.3 KiB
V
Raw Normal View History

2019-08-26 23:39:11 +02:00
enum Color{
red, green, blue
}
fn test_match_integers() {
2019-10-31 16:58:19 +01:00
mut a := 3
mut b := 0
match a {
2 { println('two') }
3 {
println('three')
b = 3
}
4 { println('four') }
else { println('???') }
}
assert b == 3
2019-08-26 23:39:11 +02:00
assert match 2 {
2019-10-31 16:58:19 +01:00
1 { 2 }
2 { 3 }
else { 5 }
2019-08-26 23:39:11 +02:00
} == 3
2019-08-26 23:39:11 +02:00
assert match 0 {
2019-10-31 16:58:19 +01:00
1 { 2 }
2 { 3 }
else 5
2019-08-26 23:39:11 +02:00
} == 5
assert match 1 {
2019-10-31 16:58:19 +01:00
else { 5 }
2019-08-26 23:39:11 +02:00
} == 5
2019-10-31 16:58:19 +01:00
a = 0
2019-08-26 23:39:11 +02:00
match 2 {
2019-10-31 16:58:19 +01:00
0 { a = 1 }
1 { a = 2 }
else {
2019-08-26 23:39:11 +02:00
a = 3
println('a is $a')
}
}
assert a == 3
2019-08-26 23:39:11 +02:00
a = 0
match 1 {
2019-10-31 16:58:19 +01:00
0 { a = 1 }
1 {
2019-08-26 23:39:11 +02:00
a = 2
a = a + 2
a = a + 2
}
else {}
2019-08-26 23:39:11 +02:00
}
assert a == 6
a = 0
match 1 {
2019-10-31 16:58:19 +01:00
else {
2019-08-26 23:39:11 +02:00
a = -2
}
}
assert a == -2
}
2019-08-26 23:39:11 +02:00
fn test_match_enums(){
mut b := Color.red
match b{
2019-10-31 16:58:19 +01:00
.red {
2019-08-26 23:39:11 +02:00
b = .green
}
2019-10-31 16:58:19 +01:00
.green {
b = .blue
}
else {
2019-08-26 23:39:11 +02:00
println('b is ${b.str()}')
b = .red
}
}
assert b == .green
match b{
2019-10-31 16:58:19 +01:00
.red {
2019-08-26 23:39:11 +02:00
b = .green
}
2019-10-31 16:58:19 +01:00
else {
2019-08-26 23:39:11 +02:00
println('b is ${b.str()}')
b = .blue
}
}
assert b == .blue
}