aboutsummaryrefslogtreecommitdiff
path: root/contrib/bmake/unit-tests/cond-cmp-string.mk
blob: e3346e08b5a09e493ea7c892b7fca7430aefb1e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# $NetBSD: cond-cmp-string.mk,v 1.19 2024/04/23 22:51:28 rillig Exp $
#
# Tests for string comparisons in .if conditions.

# This is a simple comparison of string literals.
# Nothing surprising here.
.if "str" != "str"
.  error
.endif

# The right-hand side of the comparison may be written without quotes.
.if "str" != str
.  error
.endif

# The left-hand side of the comparison must be enclosed in quotes.
# This one is not enclosed in quotes and thus generates an error message.
# expect+1: Malformed conditional (str != str)
.if str != str
.  error
.endif

# An expression that occurs on the left-hand side of the comparison must be
# defined.
#
# The variable named "" is never defined, nevertheless it can be used as a
# starting point for an expression.  Applying the :U modifier to such an
# undefined expression turns it into a defined expression.
#
# See ApplyModifier_Defined and DEF_DEFINED.
.if ${:Ustr} != "str"
.  error
.endif

# Any character in a string literal may be escaped using a backslash.
# This means that "\n" does not mean a newline but a simple "n".
.if "string" != "\s\t\r\i\n\g"
.  error
.endif

# It is not possible to concatenate two string literals to form a single
# string.  In C, Python and the shell this is possible, but not in make.
# expect+1: Malformed conditional ("string" != "str""ing")
.if "string" != "str""ing"
.  error
.else
.  error
.endif

# There is no = operator for strings.
# expect+1: Malformed conditional (!("value" = "value"))
.if !("value" = "value")
.  error
.else
.  error
.endif

# There is no === operator for strings either.
# expect+1: Malformed conditional (!("value" === "value"))
.if !("value" === "value")
.  error
.else
.  error
.endif

# An expression can be enclosed in double quotes.
.if ${:Uword} != "${:Uword}"
.  error
.endif

# Between 2003-01-01 (maybe even earlier) and 2020-10-30, adding one of the
# characters " \t!=><" directly after an expression in a string literal
# resulted in a "Malformed conditional", even though the string was
# well-formed.
.if ${:Uword } != "${:Uword} "
.  error
.endif
# Some other characters worked though, and some didn't.
# Those that are mentioned in is_separator didn't work.
.if ${:Uword0} != "${:Uword}0"
.  error
.endif
.if ${:Uword&} != "${:Uword}&"
.  error
.endif
.if ${:Uword!} != "${:Uword}!"
.  error
.endif
.if ${:Uword<} != "${:Uword}<"
.  error
.endif

# Adding another expression to the string literal works though.
.if ${:Uword} != "${:Uwo}${:Urd}"
.  error
.endif

# Adding a space at the beginning of the quoted expression works though.
.if ${:U word } != " ${:Uword} "
.  error
.endif

# If at least one side of the comparison is a string literal, the string
# comparison is performed.
.if 12345 != "12345"
.  error
.endif

# If at least one side of the comparison is a string literal, the string
# comparison is performed.  The ".0" in the left-hand side makes the two
# sides of the equation unequal.
.if 12345.0 == "12345"
.  error
.endif

# Strings cannot be compared relationally, only for equality.
# expect+1: Comparison with '<' requires both operands 'string' and 'string' to be numeric
.if "string" < "string"
.  error
.else
.  error
.endif

# Strings cannot be compared relationally, only for equality.
# expect+1: Comparison with '<=' requires both operands 'string' and 'string' to be numeric
.if "string" <= "string"
.  error
.else
.  error
.endif

# Strings cannot be compared relationally, only for equality.
# expect+1: Comparison with '>' requires both operands 'string' and 'string' to be numeric
.if "string" > "string"
.  error
.else
.  error
.endif

# Strings cannot be compared relationally, only for equality.
# expect+1: Comparison with '>=' requires both operands 'string' and 'string' to be numeric
.if "string" >= "string"
.  error
.else
.  error
.endif

# Two expressions with different values compare unequal.
VAR1=	value1
VAR2=	value2
.if ${VAR1} != ${VAR2}
.else
.  error
.endif