xref: /freebsd/contrib/bmake/unit-tests/varmod-undefined.mk (revision 36d6566e5985030fd2f1100bd9c1387bbe0bd290)
1# $NetBSD: varmod-undefined.mk,v 1.6 2020/10/24 08:46:08 rillig Exp $
2#
3# Tests for the :U variable modifier, which returns the given string
4# if the variable is undefined.
5#
6# See also:
7#	varmod-defined.mk
8
9# The pattern ${:Uword} is heavily used when expanding .for loops.
10#
11# This is how an expanded .for loop looks like.
12# .for word in one
13# .  if ${word} != one
14.if ${:Uone} != one
15# .    error ${word}
16.  error ${:Uone}
17# .  endif
18.endif
19# .endfor
20
21# The variable expressions in the text of the :U modifier may be arbitrarily
22# nested.
23
24.if ${:U${:Unested}${${${:Udeeply}}}} != nested
25.  error
26.endif
27
28# The nested variable expressions may contain braces, and these braces don't
29# need to match pairwise.  In the following example, the :S modifier uses '{'
30# as delimiter, which confuses both editors and humans because the opening
31# and # closing braces don't match anymore.  It's syntactically valid though.
32# For more similar examples, see varmod-subst.mk, mod-subst-delimiter.
33
34.if ${:U${:Uvalue:S{a{X{}} != vXlue
35.  error
36.endif
37
38# The escaping rules for the :U modifier (left-hand side) and condition
39# string literals (right-hand side) are completely different.
40#
41# In the :U modifier, the backslash only escapes very few characters, all
42# other backslashes are retained.
43#
44# In condition string literals, the backslash always escapes the following
45# character, no matter whether it would be necessary or not.
46#
47# In both contexts, \n is an escaped letter n, not a newline; that's what
48# the .newline variable is for.
49#
50# Whitespace at the edges is preserved, on both sides of the comparison.
51#
52.if ${:U \: \} \$ \\ \a \b \n } != " : } \$ \\ \\a \\b \\n "
53.  error
54.endif
55
56# Even after the :U modifier has been applied, the expression still remembers
57# that it originated from an undefined variable, and the :U modifier can
58# be used to overwrite the value of the expression.
59#
60.if ${UNDEF:Uvalue:S,a,X,} != "vXlue"
61.  error
62.elif ${UNDEF:Uvalue:S,a,X,:Uwas undefined} != "was undefined"
63.  error
64.endif
65
66all:
67	@:;
68