xref: /freebsd/contrib/bmake/unit-tests/cond-token-var.mk (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1# $NetBSD: cond-token-var.mk,v 1.11 2025/01/11 21:21:33 rillig Exp $
2#
3# Tests for expressions in .if conditions.
4#
5# Note the fine distinction between a variable and an expression.
6# A variable has a name and a value.  To access the value, one writes an
7# expression of the form ${VAR}.  This is a simple
8# expression.  Expressions can get more complicated by adding
9# variable modifiers such as in ${VAR:Mpattern}.
10#
11# XXX: Strictly speaking, variable modifiers should be called expression
12# modifiers instead since they only modify the expression, not the variable.
13# Well, except for the assignment modifiers, these do indeed change the value
14# of the variable.
15
16D=	defined
17DEF=	defined
18
19
20# A defined variable may appear on either side of the comparison.
21.if ${DEF} == ${DEF}
22# expect+1: ok
23.  info ok
24.else
25.  error
26.endif
27
28# A variable that appears on the left-hand side must be defined.
29# expect+1: Variable "UNDEF" is undefined
30.if ${UNDEF} == ${DEF}
31.  error
32.endif
33
34# A variable that appears on the right-hand side must be defined.
35# expect+1: Variable "UNDEF" is undefined
36.if ${DEF} == ${UNDEF}
37.  error
38.endif
39
40# A defined variable may appear as an expression of its own.
41.if ${DEF}
42.endif
43
44# An undefined variable on its own generates a parse error.
45# expect+1: Variable "UNDEF" is undefined
46.if ${UNDEF}
47.endif
48
49# The :U modifier turns an undefined expression into a defined expression.
50# Since the expression is defined now, it doesn't generate any parse error.
51.if ${UNDEF:U}
52.endif
53
54
55# The same as above, for single-letter variables without braces or
56# parentheses.
57
58# A defined variable may appear on either side of the comparison.
59.if $D == $D
60.endif
61
62# A variable on the left-hand side must be defined.
63# expect+1: Variable "U" is undefined
64.if $U == $D
65.endif
66
67# A variable on the right-hand side must be defined.
68# expect+1: Variable "U" is undefined
69.if $D == $U
70.endif
71
72# A defined variable may appear as an expression of its own.
73.if $D
74.endif
75
76# An undefined variable without a comparison operator generates a parse error.
77# expect+1: Variable "U" is undefined
78.if $U
79.endif
80
81
82# If the value of the expression is a number, it is compared against
83# zero.
84.if ${:U0}
85.  error
86.endif
87.if !${:U1}
88.  error
89.endif
90
91# If the value of the expression is not a number, any non-empty
92# value evaluates to true, even if there is only whitespace.
93.if ${:U}
94.  error
95.endif
96.if !${:U }
97.  error
98.endif
99.if !${:Uanything}
100.  error
101.endif
102
103.MAKEFLAGS: -dv
104# The left-hand side of a comparison must not be an unquoted word.
105# expect+1: Malformed conditional 'x${UNDEF1}y == "${UNDEF2}" || 0x${UNDEF3}'
106.if x${UNDEF1}y == "${UNDEF2}" || 0x${UNDEF3}
107.endif
108
109# The left-hand side of a comparison must not be an unquoted word.
110# expect+1: Malformed conditional 'x${DEF}y == "${UNDEF2}" || 0x${UNDEF3}'
111.if x${DEF}y == "${UNDEF2}" || 0x${UNDEF3}
112.endif
113
114# The left-hand side of a comparison must not be an unquoted word.
115# expect+1: Malformed conditional 'x${DEF}y == "${DEF}" || 0x${UNDEF3}'
116.if x${DEF}y == "${DEF}" || 0x${UNDEF3}
117.endif
118
119# An expression in a condition must not be based on an undefined variable,
120# but undefined variables may occur in the variable name or in modifiers.
121#
122# expect: Var_Parse: ${VAR.param$U} (eval-defined-loud)
123# expect: Var_Parse: $U} (eval)
124VAR.param=	value of VAR.param
125.if ${VAR.param$U}
126.endif
127
128.MAKEFLAGS: -d0
129