xref: /freebsd/contrib/bmake/unit-tests/cond-cmp-string.mk (revision 548bfc56eb0b2cefa0fb8dc2478240bfef610309)
1*548bfc56SSimon J. Gerraty# $NetBSD: cond-cmp-string.mk,v 1.19 2024/04/23 22:51:28 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Tests for string comparisons in .if conditions.
42c3632d1SSimon J. Gerraty
52c3632d1SSimon J. Gerraty# This is a simple comparison of string literals.
62c3632d1SSimon J. Gerraty# Nothing surprising here.
72c3632d1SSimon J. Gerraty.if "str" != "str"
82c3632d1SSimon J. Gerraty.  error
92c3632d1SSimon J. Gerraty.endif
102c3632d1SSimon J. Gerraty
112c3632d1SSimon J. Gerraty# The right-hand side of the comparison may be written without quotes.
122c3632d1SSimon J. Gerraty.if "str" != str
132c3632d1SSimon J. Gerraty.  error
142c3632d1SSimon J. Gerraty.endif
152c3632d1SSimon J. Gerraty
162c3632d1SSimon J. Gerraty# The left-hand side of the comparison must be enclosed in quotes.
172c3632d1SSimon J. Gerraty# This one is not enclosed in quotes and thus generates an error message.
188c973ee2SSimon J. Gerraty# expect+1: Malformed conditional (str != str)
192c3632d1SSimon J. Gerraty.if str != str
202c3632d1SSimon J. Gerraty.  error
212c3632d1SSimon J. Gerraty.endif
222c3632d1SSimon J. Gerraty
23*548bfc56SSimon J. Gerraty# An expression that occurs on the left-hand side of the comparison must be
24*548bfc56SSimon J. Gerraty# defined.
25e2eeea75SSimon J. Gerraty#
26e2eeea75SSimon J. Gerraty# The variable named "" is never defined, nevertheless it can be used as a
27*548bfc56SSimon J. Gerraty# starting point for an expression.  Applying the :U modifier to such an
28*548bfc56SSimon J. Gerraty# undefined expression turns it into a defined expression.
29e2eeea75SSimon J. Gerraty#
3012904384SSimon J. Gerraty# See ApplyModifier_Defined and DEF_DEFINED.
312c3632d1SSimon J. Gerraty.if ${:Ustr} != "str"
322c3632d1SSimon J. Gerraty.  error
332c3632d1SSimon J. Gerraty.endif
342c3632d1SSimon J. Gerraty
352c3632d1SSimon J. Gerraty# Any character in a string literal may be escaped using a backslash.
362c3632d1SSimon J. Gerraty# This means that "\n" does not mean a newline but a simple "n".
372c3632d1SSimon J. Gerraty.if "string" != "\s\t\r\i\n\g"
382c3632d1SSimon J. Gerraty.  error
392c3632d1SSimon J. Gerraty.endif
402c3632d1SSimon J. Gerraty
412c3632d1SSimon J. Gerraty# It is not possible to concatenate two string literals to form a single
42e2eeea75SSimon J. Gerraty# string.  In C, Python and the shell this is possible, but not in make.
438c973ee2SSimon J. Gerraty# expect+1: Malformed conditional ("string" != "str""ing")
442c3632d1SSimon J. Gerraty.if "string" != "str""ing"
452c3632d1SSimon J. Gerraty.  error
46e2eeea75SSimon J. Gerraty.else
47e2eeea75SSimon J. Gerraty.  error
482c3632d1SSimon J. Gerraty.endif
49956e45f6SSimon J. Gerraty
50956e45f6SSimon J. Gerraty# There is no = operator for strings.
518c973ee2SSimon J. Gerraty# expect+1: Malformed conditional (!("value" = "value"))
52956e45f6SSimon J. Gerraty.if !("value" = "value")
53956e45f6SSimon J. Gerraty.  error
54956e45f6SSimon J. Gerraty.else
55956e45f6SSimon J. Gerraty.  error
56956e45f6SSimon J. Gerraty.endif
57956e45f6SSimon J. Gerraty
58956e45f6SSimon J. Gerraty# There is no === operator for strings either.
598c973ee2SSimon J. Gerraty# expect+1: Malformed conditional (!("value" === "value"))
60956e45f6SSimon J. Gerraty.if !("value" === "value")
61956e45f6SSimon J. Gerraty.  error
62956e45f6SSimon J. Gerraty.else
63956e45f6SSimon J. Gerraty.  error
64956e45f6SSimon J. Gerraty.endif
65956e45f6SSimon J. Gerraty
66d5e0a182SSimon J. Gerraty# An expression can be enclosed in double quotes.
67956e45f6SSimon J. Gerraty.if ${:Uword} != "${:Uword}"
68956e45f6SSimon J. Gerraty.  error
69956e45f6SSimon J. Gerraty.endif
70956e45f6SSimon J. Gerraty
71956e45f6SSimon J. Gerraty# Between 2003-01-01 (maybe even earlier) and 2020-10-30, adding one of the
72*548bfc56SSimon J. Gerraty# characters " \t!=><" directly after an expression in a string literal
73*548bfc56SSimon J. Gerraty# resulted in a "Malformed conditional", even though the string was
74*548bfc56SSimon J. Gerraty# well-formed.
75956e45f6SSimon J. Gerraty.if ${:Uword } != "${:Uword} "
76956e45f6SSimon J. Gerraty.  error
77956e45f6SSimon J. Gerraty.endif
78956e45f6SSimon J. Gerraty# Some other characters worked though, and some didn't.
79956e45f6SSimon J. Gerraty# Those that are mentioned in is_separator didn't work.
80956e45f6SSimon J. Gerraty.if ${:Uword0} != "${:Uword}0"
81956e45f6SSimon J. Gerraty.  error
82956e45f6SSimon J. Gerraty.endif
83956e45f6SSimon J. Gerraty.if ${:Uword&} != "${:Uword}&"
84956e45f6SSimon J. Gerraty.  error
85956e45f6SSimon J. Gerraty.endif
86956e45f6SSimon J. Gerraty.if ${:Uword!} != "${:Uword}!"
87956e45f6SSimon J. Gerraty.  error
88956e45f6SSimon J. Gerraty.endif
89956e45f6SSimon J. Gerraty.if ${:Uword<} != "${:Uword}<"
90956e45f6SSimon J. Gerraty.  error
91956e45f6SSimon J. Gerraty.endif
92956e45f6SSimon J. Gerraty
93d5e0a182SSimon J. Gerraty# Adding another expression to the string literal works though.
94956e45f6SSimon J. Gerraty.if ${:Uword} != "${:Uwo}${:Urd}"
95956e45f6SSimon J. Gerraty.  error
96956e45f6SSimon J. Gerraty.endif
97956e45f6SSimon J. Gerraty
98*548bfc56SSimon J. Gerraty# Adding a space at the beginning of the quoted expression works though.
99956e45f6SSimon J. Gerraty.if ${:U word } != " ${:Uword} "
100956e45f6SSimon J. Gerraty.  error
101956e45f6SSimon J. Gerraty.endif
102e2eeea75SSimon J. Gerraty
103e2eeea75SSimon J. Gerraty# If at least one side of the comparison is a string literal, the string
104e2eeea75SSimon J. Gerraty# comparison is performed.
105e2eeea75SSimon J. Gerraty.if 12345 != "12345"
106e2eeea75SSimon J. Gerraty.  error
107e2eeea75SSimon J. Gerraty.endif
108e2eeea75SSimon J. Gerraty
109e2eeea75SSimon J. Gerraty# If at least one side of the comparison is a string literal, the string
110e2eeea75SSimon J. Gerraty# comparison is performed.  The ".0" in the left-hand side makes the two
111e2eeea75SSimon J. Gerraty# sides of the equation unequal.
112e2eeea75SSimon J. Gerraty.if 12345.0 == "12345"
113e2eeea75SSimon J. Gerraty.  error
114e2eeea75SSimon J. Gerraty.endif
115dba7b0efSSimon J. Gerraty
116dba7b0efSSimon J. Gerraty# Strings cannot be compared relationally, only for equality.
1178c973ee2SSimon J. Gerraty# expect+1: Comparison with '<' requires both operands 'string' and 'string' to be numeric
118dba7b0efSSimon J. Gerraty.if "string" < "string"
119dba7b0efSSimon J. Gerraty.  error
120dba7b0efSSimon J. Gerraty.else
121dba7b0efSSimon J. Gerraty.  error
122dba7b0efSSimon J. Gerraty.endif
123dba7b0efSSimon J. Gerraty
124dba7b0efSSimon J. Gerraty# Strings cannot be compared relationally, only for equality.
1258c973ee2SSimon J. Gerraty# expect+1: Comparison with '<=' requires both operands 'string' and 'string' to be numeric
126dba7b0efSSimon J. Gerraty.if "string" <= "string"
127dba7b0efSSimon J. Gerraty.  error
128dba7b0efSSimon J. Gerraty.else
129dba7b0efSSimon J. Gerraty.  error
130dba7b0efSSimon J. Gerraty.endif
131dba7b0efSSimon J. Gerraty
132dba7b0efSSimon J. Gerraty# Strings cannot be compared relationally, only for equality.
1338c973ee2SSimon J. Gerraty# expect+1: Comparison with '>' requires both operands 'string' and 'string' to be numeric
134dba7b0efSSimon J. Gerraty.if "string" > "string"
135dba7b0efSSimon J. Gerraty.  error
136dba7b0efSSimon J. Gerraty.else
137dba7b0efSSimon J. Gerraty.  error
138dba7b0efSSimon J. Gerraty.endif
139dba7b0efSSimon J. Gerraty
140dba7b0efSSimon J. Gerraty# Strings cannot be compared relationally, only for equality.
1418c973ee2SSimon J. Gerraty# expect+1: Comparison with '>=' requires both operands 'string' and 'string' to be numeric
142dba7b0efSSimon J. Gerraty.if "string" >= "string"
143dba7b0efSSimon J. Gerraty.  error
144dba7b0efSSimon J. Gerraty.else
145dba7b0efSSimon J. Gerraty.  error
146dba7b0efSSimon J. Gerraty.endif
147954401e6SSimon J. Gerraty
148*548bfc56SSimon J. Gerraty# Two expressions with different values compare unequal.
149954401e6SSimon J. GerratyVAR1=	value1
150954401e6SSimon J. GerratyVAR2=	value2
151954401e6SSimon J. Gerraty.if ${VAR1} != ${VAR2}
152954401e6SSimon J. Gerraty.else
153954401e6SSimon J. Gerraty.  error
154954401e6SSimon J. Gerraty.endif
155