]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bmake/unit-tests/cond-func-empty.mk
Merge bmake-20201117
[FreeBSD/FreeBSD.git] / contrib / bmake / unit-tests / cond-func-empty.mk
1 # $NetBSD: cond-func-empty.mk,v 1.10 2020/11/15 14:07:53 rillig Exp $
2 #
3 # Tests for the empty() function in .if conditions, which tests a variable
4 # expression for emptiness.
5 #
6 # Note that the argument in the parentheses is indeed a variable name,
7 # optionally followed by variable modifiers.
8 #
9
10 .undef UNDEF
11 EMPTY=  # empty
12 SPACE=  ${:U }
13 WORD=   word
14
15 # An undefined variable is empty.
16 .if !empty(UNDEF)
17 .  error
18 .endif
19
20 # An undefined variable has the empty string as the value, and the :M
21 # variable modifier does not change that.
22 #
23 .if !empty(UNDEF:M*)
24 .  error
25 .endif
26
27 # The :S modifier replaces the empty value with an actual word.  The
28 # expression is now no longer empty, but it is still possible to see whether
29 # the expression was based on an undefined variable.  The expression has the
30 # flag VEF_UNDEF.
31 #
32 # The expression does not have the flag VEF_DEF though, therefore it is still
33 # considered undefined.  Yes, indeed, undefined but not empty.  There are a
34 # few variable modifiers that turn an undefined expression into a defined
35 # expression, among them :U and :D, but not :S.
36 #
37 # XXX: This is hard to explain to someone who doesn't know these
38 # implementation details.
39 #
40 .if !empty(UNDEF:S,^$,value,W)
41 .  error
42 .endif
43
44 # The :U modifier modifies expressions based on undefined variables
45 # (VAR_JUNK) by adding the VAR_KEEP flag, which marks the expression
46 # as "being interesting enough to be further processed".
47 #
48 .if empty(UNDEF:S,^$,value,W:Ufallback)
49 .  error
50 .endif
51
52 # And now to the surprising part.  Applying the following :S modifier to the
53 # undefined expression makes it non-empty, but the marker VEF_UNDEF is
54 # preserved nevertheless.  The :U modifier that follows only looks at the
55 # VEF_UNDEF flag to decide whether the variable is defined or not.  This kind
56 # of makes sense since the :U modifier tests the _variable_, not the
57 # _expression_.
58 #
59 # But since the variable was undefined to begin with, the fallback value from
60 # the :U modifier is used in this expression.
61 #
62 .if ${UNDEF:S,^$,value,W:Ufallback} != "fallback"
63 .  error
64 .endif
65
66 # The variable EMPTY is completely empty (0 characters).
67 .if !empty(EMPTY)
68 .  error
69 .endif
70
71 # The variable SPACE has a single space, which counts as being empty.
72 .if !empty(SPACE)
73 .  error
74 .endif
75
76 # The variable .newline has a single newline, which counts as being empty.
77 .if !empty(.newline)
78 .  error
79 .endif
80
81 # The empty variable named "" gets a fallback value of " ", which counts as
82 # empty.
83 #
84 # Contrary to the other functions in conditionals, the trailing space is not
85 # stripped off, as can be seen in the -dv debug log.  If the space had been
86 # stripped, it wouldn't make a difference in this case.
87 #
88 .if !empty(:U )
89 .  error
90 .endif
91
92 # Now the variable named " " gets a non-empty value, which demonstrates that
93 # neither leading nor trailing spaces are trimmed in the argument of the
94 # function.  If the spaces were trimmed, the variable name would be "" and
95 # that variable is indeed undefined.  Since ParseEmptyArg calls Var_Parse
96 # without VARE_UNDEFERR, the value of the undefined variable is returned as
97 # an empty string.
98 ${:U }= space
99 .if empty( )
100 .  error
101 .endif
102
103 # The value of the following expression is " word", which is not empty.
104 .if empty(:U word)
105 .  error
106 .endif
107
108 # The :L modifier creates a variable expression that has the same value as
109 # its name, which both are "VAR" in this case.  The value is therefore not
110 # empty.
111 .if empty(VAR:L)
112 .  error
113 .endif
114
115 # The variable WORD has the value "word", which does not count as empty.
116 .if empty(WORD)
117 .  error
118 .endif
119
120 # The expression ${} for a variable with the empty name always evaluates
121 # to an empty string (see Var_Parse, varUndefined).
122 .if !empty()
123 .  error
124 .endif
125
126 # Ensure that variable expressions that appear as part of the argument are
127 # properly parsed.  Typical use cases for this are .for loops, which are
128 # expanded to exactly these ${:U} expressions.
129 #
130 # If everything goes well, the argument expands to "WORD", and that variable
131 # is defined at the beginning of this file.  The surrounding 'W' and 'D'
132 # ensure that the parser in ParseEmptyArg has the correct position, both
133 # before and after the call to Var_Parse.
134 .if empty(W${:UOR}D)
135 .  error
136 .endif
137
138 # There may be spaces at the outside of the parentheses.
139 # Spaces inside the parentheses are interpreted as part of the variable name.
140 .if ! empty ( WORD )
141 .  error
142 .endif
143
144 ${:U WORD }=    variable name with spaces
145
146 # Now there is a variable named " WORD ", and it is not empty.
147 .if empty ( WORD )
148 .  error
149 .endif
150
151 # Parse error: missing closing parenthesis.
152 .if empty(WORD
153 .  error
154 .else
155 .  error
156 .endif
157
158 all:
159         @:;