v2 / vlib / v / tests / comptime / comptime_if_bsd_family_test.v
133 lines · 124 sloc · 2.85 KB · a1932776a1e2f32ef2ff0f8feab658cf001ef04b
Raw
1// Tests the `$if bsd` family predicate, which should evaluate to true on
2// any BSD-family host (macos, freebsd, openbsd, netbsd, dragonfly) and
3// false on every other host. Prior to the fix in
4// vlib/v/ast/comptime_valid_idents.v, `$if bsd` was accepted by the parser
5// but silently evaluated to false everywhere.
6
7// host_is_bsd returns the runtime truth: whether this build targets a
8// BSD-family OS. Uses the exact-OS predicates, which are independently
9// known to work.
10fn host_is_bsd() bool {
11 $if macos {
12 return true
13 }
14 $if freebsd {
15 return true
16 }
17 $if openbsd {
18 return true
19 }
20 $if netbsd {
21 return true
22 }
23 $if dragonfly {
24 return true
25 }
26 return false
27}
28
29// comptime_bsd returns what `$if bsd` resolves to on this host.
30fn comptime_bsd() bool {
31 $if bsd {
32 return true
33 }
34 return false
35}
36
37// comptime_not_bsd returns what `$if !bsd` resolves to on this host.
38fn comptime_not_bsd() bool {
39 $if !bsd {
40 return true
41 }
42 return false
43}
44
45fn test_bsd_matches_exact_bsd_family_targets() {
46 assert comptime_bsd() == host_is_bsd()
47}
48
49fn test_not_bsd_is_the_inverse_of_bsd() {
50 assert comptime_not_bsd() == !comptime_bsd()
51}
52
53// An exact-OS predicate implies `bsd` on that OS. This guards against a
54// regression where `$if freebsd` and `$if bsd` could disagree on a FreeBSD
55// build (for example if someone changed the is_bsd_target() list but not
56// the comptime evaluator, or vice versa).
57fn test_exact_bsd_os_implies_bsd() {
58 $if macos {
59 $if !bsd {
60 assert false, '`\$if bsd` should be true on macos'
61 }
62 }
63 $if freebsd {
64 $if !bsd {
65 assert false, '`\$if bsd` should be true on freebsd'
66 }
67 }
68 $if openbsd {
69 $if !bsd {
70 assert false, '`\$if bsd` should be true on openbsd'
71 }
72 }
73 $if netbsd {
74 $if !bsd {
75 assert false, '`\$if bsd` should be true on netbsd'
76 }
77 }
78 $if dragonfly {
79 $if !bsd {
80 assert false, '`\$if bsd` should be true on dragonfly'
81 }
82 }
83}
84
85// Non-BSD hosts must see `$if bsd` as false.
86fn test_non_bsd_os_implies_not_bsd() {
87 $if linux {
88 $if bsd {
89 assert false, '`\$if bsd` should be false on linux'
90 }
91 }
92 $if windows {
93 $if bsd {
94 assert false, '`\$if bsd` should be false on windows'
95 }
96 }
97 $if solaris {
98 $if bsd {
99 assert false, '`\$if bsd` should be false on solaris'
100 }
101 }
102 $if haiku {
103 $if bsd {
104 assert false, '`\$if bsd` should be false on haiku'
105 }
106 }
107}
108
109// `bsd` must compose with other comptime conditions the same as any
110// other OS predicate.
111fn test_bsd_in_compound_conditions() {
112 mut linux_or_bsd := false
113 $if linux || bsd {
114 linux_or_bsd = true
115 }
116 // On any supported Unix-like host, at least one of these is true.
117 $if linux {
118 assert linux_or_bsd
119 }
120 $if bsd {
121 assert linux_or_bsd
122 }
123 // On Windows, neither should be true.
124 $if windows {
125 assert !linux_or_bsd
126 }
127
128 mut bsd_and_not_windows := false
129 $if bsd && !windows {
130 bsd_and_not_windows = true
131 }
132 assert bsd_and_not_windows == comptime_bsd()
133}
134