xref: /freebsd/contrib/bmake/unit-tests/varmod-ifelse.mk (revision 36d6566e5985030fd2f1100bd9c1387bbe0bd290)
1# $NetBSD: varmod-ifelse.mk,v 1.5 2020/10/23 14:24:51 rillig Exp $
2#
3# Tests for the ${cond:?then:else} variable modifier, which evaluates either
4# the then-expression or the else-expression, depending on the condition.
5#
6# The modifier was added on 1998-04-01.
7#
8# Until 2015-10-11, the modifier always evaluated both the "then" and the
9# "else" expressions.
10
11# TODO: Implementation
12
13# The variable name of the expression is expanded and then taken as the
14# condition.  In this case it becomes:
15#
16#	variable expression == "variable expression"
17#
18# This confuses the parser, which expects an operator instead of the bare
19# word "expression".  If the name were expanded lazily, everything would be
20# fine since the condition would be:
21#
22#	${:Uvariable expression} == "literal"
23#
24# Evaluating the variable name lazily would require additional code in
25# Var_Parse and ParseVarname, it would be more useful and predictable
26# though.
27.if ${${:Uvariable expression} == "literal":?bad:bad}
28.  error
29.else
30.  error
31.endif
32
33# In a variable assignment, undefined variables are not an error.
34# Because of the early expansion, the whole condition evaluates to
35# ' == ""' though, which cannot be parsed because the left-hand side looks
36# empty.
37COND:=	${${UNDEF} == "":?bad-assign:bad-assign}
38
39# In a condition, undefined variables generate a "Malformed conditional"
40# error.  That error message is wrong though.  In lint mode, the correct
41# "Undefined variable" error message is generated.
42# The difference to the ':=' variable assignment is the additional
43# "Malformed conditional" error message.
44.if ${${UNDEF} == "":?bad-cond:bad-cond}
45.  error
46.else
47.  error
48.endif
49
50# When the :? is parsed, it is greedy.  The else branch spans all the
51# text, up until the closing character '}', even if the text looks like
52# another modifier.
53.if ${1:?then:else:Q} != "then"
54.  error
55.endif
56.if ${0:?then:else:Q} != "else:Q"
57.  error
58.endif
59
60all:
61	@:;
62