xref: /freebsd/contrib/bmake/unit-tests/cond-cmp-string.mk (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1# $NetBSD: cond-cmp-string.mk,v 1.17 2023/03/28 14:38:29 rillig Exp $
2#
3# Tests for string comparisons in .if conditions.
4
5# This is a simple comparison of string literals.
6# Nothing surprising here.
7.if "str" != "str"
8.  error
9.endif
10
11# The right-hand side of the comparison may be written without quotes.
12.if "str" != str
13.  error
14.endif
15
16# The left-hand side of the comparison must be enclosed in quotes.
17# This one is not enclosed in quotes and thus generates an error message.
18# expect+1: Malformed conditional (str != str)
19.if str != str
20.  error
21.endif
22
23# The left-hand side of the comparison requires that any variable expression
24# is defined.
25#
26# The variable named "" is never defined, nevertheless it can be used as a
27# starting point for variable expressions.  Applying the :U modifier to such
28# an undefined expression turns it into a defined expression.
29#
30# See ApplyModifier_Defined and DEF_DEFINED.
31.if ${:Ustr} != "str"
32.  error
33.endif
34
35# Any character in a string literal may be escaped using a backslash.
36# This means that "\n" does not mean a newline but a simple "n".
37.if "string" != "\s\t\r\i\n\g"
38.  error
39.endif
40
41# It is not possible to concatenate two string literals to form a single
42# string.  In C, Python and the shell this is possible, but not in make.
43# expect+1: Malformed conditional ("string" != "str""ing")
44.if "string" != "str""ing"
45.  error
46.else
47.  error
48.endif
49
50# There is no = operator for strings.
51# expect+1: Malformed conditional (!("value" = "value"))
52.if !("value" = "value")
53.  error
54.else
55.  error
56.endif
57
58# There is no === operator for strings either.
59# expect+1: Malformed conditional (!("value" === "value"))
60.if !("value" === "value")
61.  error
62.else
63.  error
64.endif
65
66# A variable expression can be enclosed in double quotes.
67.if ${:Uword} != "${:Uword}"
68.  error
69.endif
70
71# Between 2003-01-01 (maybe even earlier) and 2020-10-30, adding one of the
72# characters " \t!=><" directly after a variable expression resulted in a
73# "Malformed conditional", even though the string was well-formed.
74.if ${:Uword } != "${:Uword} "
75.  error
76.endif
77# Some other characters worked though, and some didn't.
78# Those that are mentioned in is_separator didn't work.
79.if ${:Uword0} != "${:Uword}0"
80.  error
81.endif
82.if ${:Uword&} != "${:Uword}&"
83.  error
84.endif
85.if ${:Uword!} != "${:Uword}!"
86.  error
87.endif
88.if ${:Uword<} != "${:Uword}<"
89.  error
90.endif
91
92# Adding another variable expression to the string literal works though.
93.if ${:Uword} != "${:Uwo}${:Urd}"
94.  error
95.endif
96
97# Adding a space at the beginning of the quoted variable expression works
98# though.
99.if ${:U word } != " ${:Uword} "
100.  error
101.endif
102
103# If at least one side of the comparison is a string literal, the string
104# comparison is performed.
105.if 12345 != "12345"
106.  error
107.endif
108
109# If at least one side of the comparison is a string literal, the string
110# comparison is performed.  The ".0" in the left-hand side makes the two
111# sides of the equation unequal.
112.if 12345.0 == "12345"
113.  error
114.endif
115
116# Strings cannot be compared relationally, only for equality.
117# expect+1: Comparison with '<' requires both operands 'string' and 'string' to be numeric
118.if "string" < "string"
119.  error
120.else
121.  error
122.endif
123
124# Strings cannot be compared relationally, only for equality.
125# expect+1: Comparison with '<=' requires both operands 'string' and 'string' to be numeric
126.if "string" <= "string"
127.  error
128.else
129.  error
130.endif
131
132# Strings cannot be compared relationally, only for equality.
133# expect+1: Comparison with '>' requires both operands 'string' and 'string' to be numeric
134.if "string" > "string"
135.  error
136.else
137.  error
138.endif
139
140# Strings cannot be compared relationally, only for equality.
141# expect+1: Comparison with '>=' requires both operands 'string' and 'string' to be numeric
142.if "string" >= "string"
143.  error
144.else
145.  error
146.endif
147
148# Two variables with different values compare unequal.
149VAR1=	value1
150VAR2=	value2
151.if ${VAR1} != ${VAR2}
152.else
153.  error
154.endif
155