v / vlib / v2 / tests / conditional_struct_field_test.vv2
34 lines · 31 sloc · 884 bytes · 5ac1641b93ea1bf3bd615384d85469068f241fcb
Raw
1// End-to-end test for v2's conditional struct field parsing.
2// Exercises `$if cond { ... }` / `$else $if` / `$else` blocks and the
3// per-field `@[if cond ?]` attribute. Compiled and run via the v2 binary.
4
5module main
6
7struct Container {
8 name string
9$if !no_log ? {
10 logger string = 'default'
11}
12 always int = 42
13$if my_feature ? {
14 feature_val string = 'on'
15} $else {
16 feature_val string = 'off'
17}
18 dev_menu string = 'dev' @[if !no_dev_menu ?]
19 never_built string = 'nope' @[if absent_flag ?]
20}
21
22fn main() {
23 c := Container{
24 name: 'app'
25 }
26 assert c.name == 'app'
27 assert c.logger == 'default'
28 assert c.always == 42
29 // `my_feature` is not defined → `$else` branch contributes the field.
30 assert c.feature_val == 'off'
31 // `no_dev_menu` is not defined → `!no_dev_menu` is true → field kept.
32 assert c.dev_menu == 'dev'
33 println('conditional_struct_field_test PASS')
34}
35