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