xref: /freebsd/contrib/bmake/unit-tests/cond-func.mk (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1# $NetBSD: cond-func.mk,v 1.11 2022/01/07 19:30:17 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 'defined(defined)', see
98# CondParser_ComparisonOrLeaf.
99# That variable is not defined (yet).
100.if defined
101.  error
102.else
103.  info A plain function name is parsed as defined(...).
104.endif
105
106# If a variable named 'defined' is actually defined, the bare word 'defined'
107# is interpreted as 'defined(defined)', and the condition evaluates to true.
108defined=	# defined but empty
109.if defined
110.  info A plain function name is parsed as defined(...).
111.else
112.  error
113.endif
114
115# A plain symbol name may start with one of the function names, in this case
116# 'defined'.
117.if defined-var
118.  error
119.else
120.  info Symbols may start with a function name.
121.endif
122
123defined-var=	# defined but empty
124.if defined-var
125.  info Symbols may start with a function name.
126.else
127.  error
128.endif
129
130# Missing closing parenthesis when parsing the function argument.
131.if defined(
132.  error
133.else
134.  error
135.endif
136