xref: /freebsd/contrib/bmake/unit-tests/cond-token-string.mk (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
1# $NetBSD: cond-token-string.mk,v 1.11 2024/07/05 19:47:22 rillig Exp $
2#
3# Tests for quoted string literals in .if conditions.
4#
5# See also:
6#	cond-token-plain.mk
7#		Covers string literals without quotes (called "bare words").
8
9# TODO: Implementation
10
11# Cover the code in CondParser_String that frees the memory after parsing
12# an expression based on an undefined variable.
13# expect+2: Malformed conditional ("" != "${:Uvalue:Z}")
14# expect+1: while evaluating "${:Uvalue:Z}"" with value "value": Unknown modifier "Z"
15.if "" != "${:Uvalue:Z}"
16.  error
17.else
18.  error
19.endif
20
21.if x${:Uvalue}
22.  error
23.else
24# expect+1: xvalue is not defined.
25.  info xvalue is not defined.
26.endif
27
28# The 'x' produces a "Malformed conditional" since the left-hand side of a
29# comparison in an .if directive must be either an expression, a
30# quoted string literal or a number that starts with a digit.
31# expect+1: Malformed conditional (x${:Uvalue} == "")
32.if x${:Uvalue} == ""
33.  error
34.else
35.  error
36.endif
37
38# In plain words, a '\' can be used to escape any character, just as in
39# double-quoted string literals.  See CondParser_String.
40.if \x${:Uvalue} == "xvalue"
41# expect+1: Expected.
42.  info Expected.
43.else
44.  error
45.endif
46
47.MAKEFLAGS: -dc
48
49# A string in quotes is checked whether it is not empty.
50.if "UNDEF"
51# expect+1: The string literal "UNDEF" is not empty.
52.  info The string literal "UNDEF" is not empty.
53.else
54.  error
55.endif
56
57# A space is not empty as well.
58# This differs from many other places where whitespace is trimmed.
59.if " "
60# expect+1: The string literal " " is not empty, even though it consists of whitespace only.
61.  info The string literal " " is not empty, even though it consists of $\
62	whitespace only.
63.else
64.  error
65.endif
66
67.if "${UNDEF}"
68.  error
69.else
70# expect+1: An undefined variable in quotes expands to an empty string, which then evaluates to false.
71.  info An undefined variable in quotes expands to an empty string, which $\
72	then evaluates to false.
73.endif
74
75.if "${:Uvalue}"
76# expect+1: A nonempty expression evaluates to true.
77.  info A nonempty expression evaluates to true.
78.else
79.  error
80.endif
81
82.if "${:U}"
83.  error
84.else
85# expect+1: An empty variable evaluates to false.
86.  info An empty variable evaluates to false.
87.endif
88
89# A non-empty string evaluates to true, no matter if it's a literal string or
90# if it contains expressions.  The parentheses are not necessary for
91# the parser, in this case their only purpose is to make the code harder to
92# read for humans.
93VAR=	value
94.if ("${VAR}")
95.else
96.  error
97.endif
98
99# In the conditions in .if directives, the left-hand side of a comparison must
100# be enclosed in quotes.  The right-hand side does not need to be enclosed in
101# quotes.
102.if "quoted" == quoted
103.else
104.  error
105.endif
106
107.MAKEFLAGS: -d0
108
109all: .PHONY
110