xref: /freebsd/contrib/bmake/unit-tests/varmod-sysv.mk (revision e2eeea75eb8b6dd50c1298067a0655880d186734)
1# $NetBSD: varmod-sysv.mk,v 1.11 2020/11/01 22:28:52 rillig Exp $
2#
3# Tests for the ${VAR:from=to} variable modifier, which replaces the suffix
4# "from" with "to".  It can also use '%' as a wildcard.
5#
6# This modifier is applied when the other modifiers don't match exactly.
7#
8# See ApplyModifier_SysV.
9
10# A typical use case for the :from=to modifier is conversion of filename
11# extensions.
12.if ${src.c:L:.c=.o} != "src.o"
13.  error
14.endif
15
16# The modifier applies to each word on its own.
17.if ${one.c two.c three.c:L:.c=.o} != "one.o two.o three.o"
18.  error
19.endif
20
21# Words that don't match the pattern are passed unmodified.
22.if ${src.c src.h:L:.c=.o} != "src.o src.h"
23.  error
24.endif
25
26# The :from=to modifier is therefore often combined with the :M modifier.
27.if ${src.c src.h:L:M*.c:.c=.o} != "src.o"
28.  error
29.endif
30
31# Another use case for the :from=to modifier is to append a suffix to each
32# word.  In this case, the "from" string is empty, therefore it always
33# matches.  The same effect can be achieved with the :S,$,teen, modifier.
34.if ${four six seven nine:L:=teen} != "fourteen sixteen seventeen nineteen"
35.  error
36.endif
37
38# The :from=to modifier can also be used to surround each word by strings.
39# It might be tempting to use this for enclosing a string in quotes for the
40# shell, but that's the job of the :Q modifier.
41.if ${one two three:L:%=(%)} != "(one) (two) (three)"
42.  error
43.endif
44
45# When the :from=to modifier is parsed, it lasts until the closing brace
46# or parenthesis.  The :Q in the below expression may look like a modifier
47# but isn't.  It is part of the replacement string.
48.if ${a b c d e:L:%a=x:Q} != "x:Q b c d e"
49.  error
50.endif
51
52# In the :from=to modifier, both parts can contain variable expressions.
53.if ${one two:L:${:Uone}=${:U1}} != "1 two"
54.  error
55.endif
56
57# In the :from=to modifier, the "from" part is expanded exactly once.
58.if ${:U\$ \$\$ \$\$\$\$:${:U\$\$\$\$}=4} != "\$ \$\$ 4"
59.  error
60.endif
61
62# In the :from=to modifier, the "to" part is expanded exactly twice.
63# XXX: The right-hand side should be expanded only once.
64# XXX: It's hard to get the escaping correct here, and to read that.
65# XXX: It's not intuitive why the closing brace must be escaped but not
66#      the opening brace.
67.if ${:U1 2 4:4=${:Uonce\${\:Utwice\}}} != "1 2 oncetwice"
68.  error
69.endif
70
71# The replacement string can contain spaces, thereby changing the number
72# of words in the variable expression.
73.if ${In:L:%=% ${:Uthe Sun}} != "In the Sun"
74.  error
75.endif
76
77# If the variable value is empty, it is debatable whether it consists of a
78# single empty word, or no word at all.  The :from=to modifier treats it as
79# no word at all.
80.if ${:L:=suffix} != ""
81.  error
82.endif
83
84# If the variable value is empty, it is debatable whether it consists of a
85# single empty word, or no word at all.  The :from=to modifier treats it as
86# no word at all.
87.if ${:L:%=suffix} != ""
88.  error
89.endif
90
91# Before 2020-07-19, an ampersand could be used in the replacement part
92# of a SysV substitution modifier, and it was replaced with the whole match,
93# just like in the :S modifier.
94#
95# This was probably a copy-and-paste mistake since the code for the SysV
96# modifier looked a lot like the code for the :S and :C modifiers.
97# The ampersand is not mentioned in the manual page.
98.if ${a.bcd.e:L:a.%=%} != "bcd.e"
99.  error
100.endif
101# Before 2020-07-19, the result of the expression was "a.bcd.e".
102.if ${a.bcd.e:L:a.%=&} != "&"
103.  error
104.endif
105
106# Before 2020-07-20, when a SysV modifier was parsed, a single dollar
107# before the '=' was parsed (but not interpreted) as an anchor.
108# Parsing something without then evaluating it accordingly doesn't make
109# sense.
110.if ${value:L:e$=x} != "value"
111.  error
112.endif
113# Before 2020-07-20, the modifier ":e$=x" was parsed as having a left-hand
114# side "e" and a right-hand side "x".  The dollar was parsed (but not
115# interpreted) as 'anchor at the end'.  Therefore the modifier was equivalent
116# to ":e=x", which doesn't match the string "value$".  Therefore the whole
117# expression evaluated to "value$".
118.if ${${:Uvalue\$}:L:e$=x} != "valux"
119.  error
120.endif
121.if ${value:L:e=x} != "valux"
122.  error
123.endif
124
125# Words that don't match are copied unmodified.
126.if ${:Ufile.c file.h:%.c=%.cpp} != "file.cpp file.h"
127.  error
128.endif
129
130# The % placeholder can be anywhere in the string, it doesn't have to be at
131# the beginning of the pattern.
132.if ${:Ufile.c other.c:file.%=renamed.%} != "renamed.c other.c"
133.  error
134.endif
135
136# It's also possible to modify each word by replacing the prefix and adding
137# a suffix.
138.if ${one two:L:o%=a%w} != "anew two"
139.  error
140.endif
141
142# Each word gets the suffix "X" appended.
143.if ${one two:L:=X} != "oneX twoX"
144.  error
145.endif
146
147# The suffix "o" is replaced with "X".
148.if ${one two:L:o=X} != "one twX"
149.  error
150.endif
151
152# The suffix "o" is replaced with nothing.
153.if ${one two:L:o=} != "one tw"
154.  error
155.endif
156
157# The suffix "o" is replaced with a literal percent.  The percent is only
158# a wildcard when it appears on the left-hand side.
159.if ${one two:L:o=%} != "one tw%"
160.  error
161.endif
162
163# Each word with the suffix "o" is replaced with "X".  The percent is a
164# wildcard even though the right-hand side does not contain another percent.
165.if ${one two:L:%o=X} != "one X"
166.  error
167.endif
168
169# Each word with the prefix "o" is replaced with "X".  The percent is a
170# wildcard even though the right-hand side does not contain another percent.
171.if ${one two:L:o%=X} != "X two"
172.  error
173.endif
174
175# For each word with the prefix "o" and the suffix "e", the whole word is
176# replaced with "X".
177.if ${one two oe oxen:L:o%e=X} != "X two X oxen"
178.  error
179.endif
180
181# Only the first '%' is the wildcard.
182.if ${one two o%e other%e:L:o%%e=X} != "one two X X"
183.  error
184.endif
185
186# In the replacement, only the first '%' is the placeholder, all others
187# are literal percent characters.
188.if ${one two:L:%=%%} != "one% two%"
189.  error
190.endif
191
192# In the word "one", only a prefix of the pattern suffix "nes" matches,
193# the whole word is too short.  Therefore it doesn't match.
194.if ${one two:L:%nes=%xxx} != "one two"
195.  error
196.endif
197
198# The :from=to modifier can be used to replace both the prefix and a suffix
199# of a word with other strings.  This is not possible with a single :S
200# modifier, and using a :C modifier for the same task looks more complicated
201# in many cases.
202.if ${prefix-middle-suffix:L:prefix-%-suffix=p-%-s} != "p-middle-s"
203.  error
204.endif
205
206# This is not a SysV modifier since the nested variable expression expands
207# to an empty string.  The '=' in it should be irrelevant during parsing.
208# As of 2020-11-01, this seemingly correct modifier leads to a parse error.
209# XXX
210.if ${word203:L:from${:D=}to}
211.  error
212.endif
213
214# XXX: This specially constructed case demonstrates that the SysV modifier
215# lasts longer than expected.  The whole expression initially has the value
216# "fromto}...".  The next modifier is a SysV modifier.  ApplyModifier_SysV
217# parses the modifier as "from${:D=}to", ending at the '}'.  Next, the two
218# parts of the modifier are parsed using ParseModifierPart, which scans
219# differently, properly handling nested variable expressions.  The two parts
220# are now "fromto}..." and "replaced".
221.if "${:Ufromto\}...:from${:D=}to}...=replaced}" != "replaced"
222.  error
223.endif
224
225# As of 2020-10-06, the right-hand side of the SysV modifier is expanded
226# twice.  The first expansion happens in ApplyModifier_SysV, where the
227# modifier is split into its two parts.  The second expansion happens
228# when each word is replaced in ModifyWord_SYSVSubst.
229# XXX: This is unexpected.  Add more test case to demonstrate the effects
230# of removing one of the expansions.
231VALUE=		value
232INDIRECT=	1:${VALUE} 2:$${VALUE} 4:$$$${VALUE}
233.if ${x:L:x=${INDIRECT}} != "1:value 2:value 4:\${VALUE}"
234.  error
235.endif
236
237all:
238