1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/compiler/tests/match_test.v

93 lines
1.3 KiB
V
Raw Normal View History

2019-08-27 00:39:11 +03:00
enum Color{
red, green, blue
}
fn test_match_integers() {
2019-10-31 18:58:19 +03: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-27 00:39:11 +03:00
assert match 2 {
2019-10-31 18:58:19 +03:00
1 { 2 }
2 { 3 }
else { 5 }
2019-08-27 00:39:11 +03:00
} == 3
assert match 0 {
2019-10-31 18:58:19 +03:00
1 { 2 }
2 { 3 }
else 5
2019-08-27 00:39:11 +03:00
} == 5
assert match 1 {
2019-10-31 18:58:19 +03:00
else { 5 }
2019-08-27 00:39:11 +03:00
} == 5
2019-10-31 18:58:19 +03:00
a = 0
2019-08-27 00:39:11 +03:00
match 2 {
2019-10-31 18:58:19 +03:00
0 { a = 1 }
1 { a = 2 }
else {
2019-08-27 00:39:11 +03:00
a = 3
println('a is $a')
}
}
assert a == 3
a = 0
match 1 {
2019-10-31 18:58:19 +03:00
0 { a = 1 }
1 {
2019-08-27 00:39:11 +03:00
a = 2
a = a + 2
a = a + 2
}
}
assert a == 6
a = 0
match 1 {
2019-10-31 18:58:19 +03:00
else {
2019-08-27 00:39:11 +03:00
a = -2
}
}
assert a == -2
2019-07-17 02:43:59 +03:00
}
2019-08-27 00:39:11 +03:00
fn test_match_enums(){
mut b := Color.red
match b{
2019-10-31 18:58:19 +03:00
.red {
2019-08-27 00:39:11 +03:00
b = .green
}
2019-10-31 18:58:19 +03:00
.green {
b = .blue
}
else {
2019-08-27 00:39:11 +03:00
println('b is ${b.str()}')
b = .red
}
}
assert b == .green
match b{
2019-10-31 18:58:19 +03:00
.red {
2019-08-27 00:39:11 +03:00
b = .green
}
2019-10-31 18:58:19 +03:00
else {
2019-08-27 00:39:11 +03:00
println('b is ${b.str()}')
b = .blue
}
}
assert b == .blue
}