v2 / vlib / v / fmt / tests / if_ternary_keep.vv
50 lines · 44 sloc · 1.04 KB · 545525ce40d6f186a2a6f205d503ad1d113d317b
Raw
1import os
2
3const exe_extension = if os.user_os() == 'windows' { '.exe' } else { '' }
4
5struct Foo {
6 n int
7 s string
8}
9
10fn valid_single_line() {
11 // Variable initialization
12 number := if condition { 42 } else { 0 }
13 status := if code == 200 { 'OK' } else { 'Error' }
14 a, b := if true { 'a', 'b' } else { 'b', 'a' }
15 // Variable assignment
16 mut x := 'abc'
17 x = if x == 'def' { 'ghi' } else { 'def' }
18 // Array pushes
19 [0, 1] << if true { 2 } else { 3 }
20 // Empty or literal syntax struct inits
21 _ := if false { Foo{} } else { Foo{5, 6} }
22 // As argument for a function call
23 some_func(if cond { 'param1' } else { 'param2' })
24 // struct init
25 foo := Foo{
26 n: if true { 1 } else { 0 }
27 s: if false { 'false' } else { 'true' }
28 }
29}
30
31fn requires_multiple_lines() {
32 b := if bar {
33 // with comments inside
34 'some str'
35 } else {
36 'other str'
37 }
38}
39
40fn return_ternary(cond bool) int {
41 return if cond { 5 } else { 12 }
42}
43
44fn long_return_ternary() string {
45 return if false {
46 'spam and eggs'
47 } else {
48 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
49 }
50}
51