xref: /freebsd/contrib/bmake/unit-tests/cond-cmp-string.mk (revision 8c973ee23d647bbdebd2c12cb51460d80101e11a)
1*8c973ee2SSimon J. Gerraty# $NetBSD: cond-cmp-string.mk,v 1.17 2023/03/28 14:38:29 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.
18*8c973ee2SSimon 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
23e2eeea75SSimon J. Gerraty# The left-hand side of the comparison requires that any variable expression
24e2eeea75SSimon J. Gerraty# is defined.
25e2eeea75SSimon J. Gerraty#
26e2eeea75SSimon J. Gerraty# The variable named "" is never defined, nevertheless it can be used as a
27e2eeea75SSimon J. Gerraty# starting point for variable expressions.  Applying the :U modifier to such
28e2eeea75SSimon J. Gerraty# an 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.
43*8c973ee2SSimon 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.
51*8c973ee2SSimon 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.
59*8c973ee2SSimon 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
66956e45f6SSimon J. Gerraty# A variable 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
72956e45f6SSimon J. Gerraty# characters " \t!=><" directly after a variable expression resulted in a
73956e45f6SSimon J. Gerraty# "Malformed conditional", even though the string was well-formed.
74956e45f6SSimon J. Gerraty.if ${:Uword } != "${:Uword} "
75956e45f6SSimon J. Gerraty.  error
76956e45f6SSimon J. Gerraty.endif
77956e45f6SSimon J. Gerraty# Some other characters worked though, and some didn't.
78956e45f6SSimon J. Gerraty# Those that are mentioned in is_separator didn't work.
79956e45f6SSimon J. Gerraty.if ${:Uword0} != "${:Uword}0"
80956e45f6SSimon J. Gerraty.  error
81956e45f6SSimon J. Gerraty.endif
82956e45f6SSimon J. Gerraty.if ${:Uword&} != "${:Uword}&"
83956e45f6SSimon J. Gerraty.  error
84956e45f6SSimon J. Gerraty.endif
85956e45f6SSimon J. Gerraty.if ${:Uword!} != "${:Uword}!"
86956e45f6SSimon J. Gerraty.  error
87956e45f6SSimon J. Gerraty.endif
88956e45f6SSimon J. Gerraty.if ${:Uword<} != "${:Uword}<"
89956e45f6SSimon J. Gerraty.  error
90956e45f6SSimon J. Gerraty.endif
91956e45f6SSimon J. Gerraty
92956e45f6SSimon J. Gerraty# Adding another variable expression to the string literal works though.
93956e45f6SSimon J. Gerraty.if ${:Uword} != "${:Uwo}${:Urd}"
94956e45f6SSimon J. Gerraty.  error
95956e45f6SSimon J. Gerraty.endif
96956e45f6SSimon J. Gerraty
97956e45f6SSimon J. Gerraty# Adding a space at the beginning of the quoted variable expression works
98956e45f6SSimon J. Gerraty# 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.
117*8c973ee2SSimon 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.
125*8c973ee2SSimon 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.
133*8c973ee2SSimon 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.
141*8c973ee2SSimon 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
148954401e6SSimon J. Gerraty# Two variables 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