xref: /freebsd/contrib/bmake/unit-tests/cond-func.mk (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1# $NetBSD: cond-func.mk,v 1.9 2020/11/15 14:07:53 rillig Exp $
2#
3# Tests for those parts of the functions in .if conditions that are common
4# among several functions.
5#
6# The below test uses the function defined(...) since it has no side-effects,
7# the other functions (except empty(...)) would work equally well.  The
8# function empty is special because it uses a different parsing algorithm for
9# its argument.
10
11DEF=			defined
12${:UA B}=		variable name with spaces
13${:UVAR(value)}=	variable name with parentheses
14${:UVAR{value}}=	variable name with balanced braces
15
16# Really strange variable names must be given indirectly via another variable,
17# so that no unbalanced braces appear in the top-level expression.
18VARNAME_UNBALANCED_BRACES=	VAR{{{value
19${VARNAME_UNBALANCED_BRACES}=	variable name with unbalanced braces
20
21.if !defined(DEF)
22.  error
23.endif
24
25# Horizontal whitespace (space tab) after the opening parenthesis is ignored.
26.if !defined( 	DEF)
27.  error
28.endif
29
30# Horizontal whitespace (space tab) before the closing parenthesis is ignored.
31.if !defined(DEF 	)
32.  error
33.endif
34
35# The argument of a function must not directly contain whitespace.
36.if !defined(A B)
37.  error
38.endif
39
40# If necessary, the whitespace can be generated by a variable expression.
41.if !defined(${:UA B})
42.  error
43.endif
44
45# Characters that could be mistaken for operators must not appear directly
46# in a function argument.  As with whitespace, these can be generated
47# indirectly.
48#
49# It's not entirely clear why these characters are forbidden.
50# The most plausible reason seems to be typo detection.
51.if !defined(A&B)
52.  error
53.endif
54.if !defined(A|B)
55.  error
56.endif
57
58# Even parentheses may appear in variable names.
59# They must be balanced though.
60.if !defined(VAR(value))
61.  error
62.endif
63
64# Braces do not have any special meaning when parsing arguments.
65.if !defined(VAR{value})
66.  error
67.endif
68
69# Braces do not have any special meaning when parsing arguments.
70# They don't need to be balanced.
71.if !defined(VAR{{{value)
72.  error
73.endif
74
75# There may be spaces around the operators and parentheses, and even
76# inside the parentheses.  The spaces inside the parentheses are not
77# allowed for the empty() function (see cond-func-empty.mk), therefore
78# they are typically omitted for the other functions as well.
79.if ! defined ( DEF )
80.  error
81.endif
82
83# The following condition is interpreted as defined(A) && defined(B).
84# In lack of a function call expression, each kind of .if directive has a
85# default function that is called when a bare word is parsed.  For the plain
86# .if directive, this function is defined(); see "struct If ifs" in cond.c.
87.if A&B
88.  error
89.endif
90
91.if defined()
92.  error
93.else
94.  info The empty variable is never defined.
95.endif
96
97# The plain word 'defined' is interpreted as '!empty(defined)'.
98# That variable is not defined (yet).
99.if defined
100.  error
101.else
102.  info A plain function name is parsed as !empty(...).
103.endif
104
105# If a variable named 'defined' is actually defined and not empty, the plain
106# symbol 'defined' evaluates to true.
107defined=	non-empty
108.if defined
109.  info A plain function name is parsed as !empty(...).
110.else
111.  error
112.endif
113
114# A plain symbol name may start with one of the function names, in this case
115# 'defined'.
116.if defined-var
117.  error
118.else
119.  info Symbols may start with a function name.
120.endif
121
122defined-var=	non-empty
123.if defined-var
124.  info Symbols may start with a function name.
125.else
126.  error
127.endif
128
129# Missing closing parenthesis when parsing the function argument.
130.if defined(
131.  error
132.else
133.  error
134.endif
135
136all:
137	@:;
138