mirror of
https://github.com/vlang/v.git
synced 2025-09-15 15:32:27 +03:00
parser: match expression + match fixes
This commit is contained in:
parent
b6336f730b
commit
7edcbeca1a
2 changed files with 276 additions and 12 deletions
|
@ -1,12 +1,88 @@
|
|||
fn test_match() {
|
||||
a := 3
|
||||
mut b := 0
|
||||
match a {
|
||||
2 => println('two')
|
||||
3 => println('three')
|
||||
b = 3
|
||||
4 => println('four')
|
||||
else => println('???')
|
||||
}
|
||||
assert b == 3
|
||||
enum Color{
|
||||
red, green, blue
|
||||
}
|
||||
|
||||
fn test_match_integers() {
|
||||
// a := 3
|
||||
// mut b := 0
|
||||
// match a {
|
||||
// 2 => println('two')
|
||||
// 3 => println('three')
|
||||
// b = 3
|
||||
// 4 => println('four')
|
||||
// else => println('???')
|
||||
// }
|
||||
// assert b == 3
|
||||
|
||||
assert match 2 {
|
||||
1 => {2}
|
||||
2 => {3}
|
||||
else => {5}
|
||||
} == 3
|
||||
|
||||
assert match 0 {
|
||||
1 => {2}
|
||||
2 => {3}
|
||||
else => 5
|
||||
} == 5
|
||||
|
||||
assert match 1 {
|
||||
else => {5}
|
||||
} == 5
|
||||
|
||||
mut a := 0
|
||||
match 2 {
|
||||
0 => {a = 1}
|
||||
1 => {a = 2}
|
||||
else => {
|
||||
a = 3
|
||||
println('a is $a')
|
||||
}
|
||||
}
|
||||
assert a == 3
|
||||
|
||||
a = 0
|
||||
match 1 {
|
||||
0 => {a = 1}
|
||||
1 => {
|
||||
a = 2
|
||||
a = a + 2
|
||||
a = a + 2
|
||||
}
|
||||
}
|
||||
assert a == 6
|
||||
|
||||
a = 0
|
||||
match 1 {
|
||||
else => {
|
||||
a = -2
|
||||
}
|
||||
}
|
||||
assert a == -2
|
||||
}
|
||||
|
||||
fn test_match_enums(){
|
||||
mut b := Color.red
|
||||
match b{
|
||||
.red => {
|
||||
b = .green
|
||||
}
|
||||
.green => {b = .blue}
|
||||
else => {
|
||||
println('b is ${b.str()}')
|
||||
b = .red
|
||||
}
|
||||
}
|
||||
assert b == .green
|
||||
|
||||
match b{
|
||||
.red => {
|
||||
b = .green
|
||||
}
|
||||
else => {
|
||||
println('b is ${b.str()}')
|
||||
b = .blue
|
||||
}
|
||||
}
|
||||
assert b == .blue
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue