xref: /freebsd/contrib/bmake/unit-tests/varmod-select-words.mk (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1# $NetBSD: varmod-select-words.mk,v 1.4 2022/01/23 16:09:38 rillig Exp $
2#
3# Tests for the :[...] variable modifier, which selects a single word
4# or a range of words from a variable.
5#
6# History:
7#	The variable modifier ':[...]' was added on 2003-09-27.
8#
9# See also:
10#	modword.mk		(should be migrated here)
11
12all: mod-squarebrackets mod-S-W mod-C-W mod-tW-tw
13
14LIST=		one two three four five six
15LONGLIST=	1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
16
17EMPTY=		# the space should be ignored
18ESCAPEDSPACE=	\ # escaped space before the '#', the actual value is '\ '
19REALLYSPACE:=	${:U }
20HASH=		\#
21AT=		@
22STAR=		*
23ZERO=		0
24ONE=		1
25MINUSONE=	-1
26
27mod-squarebrackets: mod-squarebrackets-0-star-at \
28	mod-squarebrackets-hash \
29	mod-squarebrackets-n \
30	mod-squarebrackets-start-end \
31	mod-squarebrackets-nested \
32	mod-squarebrackets-space
33
34mod-squarebrackets-0-star-at:
35	@echo 'LIST:[]="${LIST:[]}" is an error'
36	@echo 'LIST:[0]="${LIST:[0]}"'
37	@echo 'LIST:[0x0]="${LIST:[0x0]}"'
38	@echo 'LIST:[000]="${LIST:[000]}"'
39	@echo 'LIST:[*]="${LIST:[*]}"'
40	@echo 'LIST:[@]="${LIST:[@]}"'
41	@echo 'LIST:[0]:C/ /,/="${LIST:[0]:C/ /,/}"'
42	@echo 'LIST:[0]:C/ /,/g="${LIST:[0]:C/ /,/g}"'
43	@echo 'LIST:[0]:C/ /,/1g="${LIST:[0]:C/ /,/1g}"'
44	@echo 'LIST:[*]:C/ /,/="${LIST:[*]:C/ /,/}"'
45	@echo 'LIST:[*]:C/ /,/g="${LIST:[*]:C/ /,/g}"'
46	@echo 'LIST:[*]:C/ /,/1g="${LIST:[*]:C/ /,/1g}"'
47	@echo 'LIST:[@]:C/ /,/="${LIST:[@]:C/ /,/}"'
48	@echo 'LIST:[@]:C/ /,/g="${LIST:[@]:C/ /,/g}"'
49	@echo 'LIST:[@]:C/ /,/1g="${LIST:[@]:C/ /,/1g}"'
50	@echo 'LIST:[@]:[0]:C/ /,/="${LIST:[@]:[0]:C/ /,/}"'
51	@echo 'LIST:[0]:[@]:C/ /,/="${LIST:[0]:[@]:C/ /,/}"'
52	@echo 'LIST:[@]:[*]:C/ /,/="${LIST:[@]:[*]:C/ /,/}"'
53	@echo 'LIST:[*]:[@]:C/ /,/="${LIST:[*]:[@]:C/ /,/}"'
54
55mod-squarebrackets-hash:
56	@echo 'EMPTY="${EMPTY}"'
57	@echo 'EMPTY:[#]="${EMPTY:[#]}" == 1 ?'
58	@echo 'ESCAPEDSPACE="${ESCAPEDSPACE}"'
59	@echo 'ESCAPEDSPACE:[#]="${ESCAPEDSPACE:[#]}" == 1 ?'
60	@echo 'REALLYSPACE="${REALLYSPACE}"'
61	@echo 'REALLYSPACE:[#]="${REALLYSPACE:[#]}" == 1 ?'
62	@echo 'LIST:[#]="${LIST:[#]}"'
63	@echo 'LIST:[0]:[#]="${LIST:[0]:[#]}" == 1 ?'
64	@echo 'LIST:[*]:[#]="${LIST:[*]:[#]}" == 1 ?'
65	@echo 'LIST:[@]:[#]="${LIST:[@]:[#]}"'
66	@echo 'LIST:[1]:[#]="${LIST:[1]:[#]}"'
67	@echo 'LIST:[1..3]:[#]="${LIST:[1..3]:[#]}"'
68
69mod-squarebrackets-n:
70	@echo 'EMPTY:[1]="${EMPTY:[1]}"'
71	@echo 'ESCAPEDSPACE="${ESCAPEDSPACE}"'
72	@echo 'ESCAPEDSPACE:[1]="${ESCAPEDSPACE:[1]}"'
73	@echo 'REALLYSPACE="${REALLYSPACE}"'
74	@echo 'REALLYSPACE:[1]="${REALLYSPACE:[1]}" == "" ?'
75	@echo 'REALLYSPACE:[*]:[1]="${REALLYSPACE:[*]:[1]}" == " " ?'
76	@echo 'LIST:[1]="${LIST:[1]}"'
77	@echo 'LIST:[1.]="${LIST:[1.]}" is an error'
78	@echo 'LIST:[1].="${LIST:[1].}" is an error'
79	@echo 'LIST:[2]="${LIST:[2]}"'
80	@echo 'LIST:[6]="${LIST:[6]}"'
81	@echo 'LIST:[7]="${LIST:[7]}"'
82	@echo 'LIST:[999]="${LIST:[999]}"'
83	@echo 'LIST:[-]="${LIST:[-]}" is an error'
84	@echo 'LIST:[--]="${LIST:[--]}" is an error'
85	@echo 'LIST:[-1]="${LIST:[-1]}"'
86	@echo 'LIST:[-2]="${LIST:[-2]}"'
87	@echo 'LIST:[-6]="${LIST:[-6]}"'
88	@echo 'LIST:[-7]="${LIST:[-7]}"'
89	@echo 'LIST:[-999]="${LIST:[-999]}"'
90	@echo 'LONGLIST:[17]="${LONGLIST:[17]}"'
91	@echo 'LONGLIST:[0x11]="${LONGLIST:[0x11]}"'
92	@echo 'LONGLIST:[021]="${LONGLIST:[021]}"'
93	@echo 'LIST:[0]:[1]="${LIST:[0]:[1]}"'
94	@echo 'LIST:[*]:[1]="${LIST:[*]:[1]}"'
95	@echo 'LIST:[@]:[1]="${LIST:[@]:[1]}"'
96	@echo 'LIST:[0]:[2]="${LIST:[0]:[2]}"'
97	@echo 'LIST:[*]:[2]="${LIST:[*]:[2]}"'
98	@echo 'LIST:[@]:[2]="${LIST:[@]:[2]}"'
99	@echo 'LIST:[*]:C/ /,/:[2]="${LIST:[*]:C/ /,/:[2]}"'
100	@echo 'LIST:[*]:C/ /,/:[*]:[2]="${LIST:[*]:C/ /,/:[*]:[2]}"'
101	@echo 'LIST:[*]:C/ /,/:[@]:[2]="${LIST:[*]:C/ /,/:[@]:[2]}"'
102	@echo 'LONGLIST:[012..0x12]="${LONGLIST:[012..0x12]}"'
103
104mod-squarebrackets-start-end:
105	@echo 'LIST:[1.]="${LIST:[1.]}" is an error'
106	@echo 'LIST:[1..]="${LIST:[1..]}" is an error'
107	@echo 'LIST:[1.. ]="${LIST:[1.. ]}" is an error'
108	@echo 'LIST:[1..1]="${LIST:[1..1]}"'
109	@echo 'LIST:[1..1.]="${LIST:[1..1.]}" is an error'
110	@echo 'LIST:[1..2]="${LIST:[1..2]}"'
111	@echo 'LIST:[2..1]="${LIST:[2..1]}"'
112	@echo 'LIST:[3..-2]="${LIST:[3..-2]}"'
113	@echo 'LIST:[-4..4]="${LIST:[-4..4]}"'
114	@echo 'LIST:[0..1]="${LIST:[0..1]}" is an error'
115	@echo 'LIST:[-1..0]="${LIST:[-1..0]}" is an error'
116	@echo 'LIST:[-1..1]="${LIST:[-1..1]}"'
117	@echo 'LIST:[0..0]="${LIST:[0..0]}"'
118	@echo 'LIST:[3..99]="${LIST:[3..99]}"'
119	@echo 'LIST:[-3..-99]="${LIST:[-3..-99]}"'
120	@echo 'LIST:[-99..-3]="${LIST:[-99..-3]}"'
121
122mod-squarebrackets-nested:
123	@echo 'HASH="${HASH}" == "#" ?'
124	@echo 'LIST:[$${HASH}]="${LIST:[${HASH}]}"'
125	@echo 'LIST:[$${ZERO}]="${LIST:[${ZERO}]}"'
126	@echo 'LIST:[$${ZERO}x$${ONE}]="${LIST:[${ZERO}x${ONE}]}"'
127	@echo 'LIST:[$${ONE}]="${LIST:[${ONE}]}"'
128	@echo 'LIST:[$${MINUSONE}]="${LIST:[${MINUSONE}]}"'
129	@echo 'LIST:[$${STAR}]="${LIST:[${STAR}]}"'
130	@echo 'LIST:[$${AT}]="${LIST:[${AT}]}"'
131	@echo 'LIST:[$${EMPTY}]="${LIST:[${EMPTY}]}" is an error'
132	@echo 'LIST:[$${LONGLIST:[21]:S/2//}]="${LIST:[${LONGLIST:[21]:S/2//}]}"'
133	@echo 'LIST:[$${LIST:[#]}]="${LIST:[${LIST:[#]}]}"'
134	@echo 'LIST:[$${LIST:[$${HASH}]}]="${LIST:[${LIST:[${HASH}]}]}"'
135
136mod-squarebrackets-space:
137	# As of 2020-11-01, it is possible to have spaces before the numbers
138	# but not after them.  This is an unintended side-effect of using
139	# strtol for parsing the numbers.
140	@echo 'LIST:[  -1..   +3]="${LIST:[  -1..   +3]}"'
141
142mod-C-W:
143	@echo 'LIST:C/ /,/="${LIST:C/ /,/}"'
144	@echo 'LIST:C/ /,/W="${LIST:C/ /,/W}"'
145	@echo 'LIST:C/ /,/gW="${LIST:C/ /,/gW}"'
146	@echo 'EMPTY:C/^/,/="${EMPTY:C/^/,/}"'
147	@echo 'EMPTY:C/^/,/W="${EMPTY:C/^/,/W}"'
148
149mod-S-W:
150	@echo 'LIST:S/ /,/="${LIST:S/ /,/}"'
151	@echo 'LIST:S/ /,/W="${LIST:S/ /,/W}"'
152	@echo 'LIST:S/ /,/gW="${LIST:S/ /,/gW}"'
153	@echo 'EMPTY:S/^/,/="${EMPTY:S/^/,/}"'
154	@echo 'EMPTY:S/^/,/W="${EMPTY:S/^/,/W}"'
155
156mod-tW-tw:
157	@echo 'LIST:tW="${LIST:tW}"'
158	@echo 'LIST:tw="${LIST:tw}"'
159	@echo 'LIST:tW:C/ /,/="${LIST:tW:C/ /,/}"'
160	@echo 'LIST:tW:C/ /,/g="${LIST:tW:C/ /,/g}"'
161	@echo 'LIST:tW:C/ /,/1g="${LIST:tW:C/ /,/1g}"'
162	@echo 'LIST:tw:C/ /,/="${LIST:tw:C/ /,/}"'
163	@echo 'LIST:tw:C/ /,/g="${LIST:tw:C/ /,/g}"'
164	@echo 'LIST:tw:C/ /,/1g="${LIST:tw:C/ /,/1g}"'
165	@echo 'LIST:tw:tW:C/ /,/="${LIST:tw:tW:C/ /,/}"'
166	@echo 'LIST:tW:tw:C/ /,/="${LIST:tW:tw:C/ /,/}"'
167