xref: /freebsd/contrib/bmake/unit-tests/varmod-to-separator.mk (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1# $NetBSD: varmod-to-separator.mk,v 1.7 2020/11/15 20:20:58 rillig Exp $
2#
3# Tests for the :ts variable modifier, which joins the words of the variable
4# using an arbitrary character as word separator.
5
6WORDS=	one two three four five six
7
8# The words are separated by a single space, just as usual.
9.if ${WORDS:ts } != "one two three four five six"
10.  warning Space as separator does not work.
11.endif
12
13# The separator can be an arbitrary character, for example a comma.
14.if ${WORDS:ts,} != "one,two,three,four,five,six"
15.  warning Comma as separator does not work.
16.endif
17
18# After the :ts modifier, other modifiers can follow.
19.if ${WORDS:ts/:tu} != "ONE/TWO/THREE/FOUR/FIVE/SIX"
20.  warning Chaining modifiers does not work.
21.endif
22
23# To use the ':' as the separator, just write it normally.
24# The first colon is the separator, the second ends the modifier.
25.if ${WORDS:ts::tu} != "ONE:TWO:THREE:FOUR:FIVE:SIX"
26.  warning Colon as separator does not work.
27.endif
28
29# When there is just a colon but no other character, the words are
30# "separated" by an empty string, that is, they are all squashed
31# together.
32.if ${WORDS:ts:tu} != "ONETWOTHREEFOURFIVESIX"
33.  warning Colon as separator does not work.
34.endif
35
36# Applying the :tu modifier first and then the :ts modifier does not change
37# anything since neither of these modifiers is related to how the string is
38# split into words.  Beware of separating the words using a single or double
39# quote though, or other special characters like dollar or backslash.
40#
41# This example also demonstrates that the closing brace is not interpreted
42# as a separator, but as the closing delimiter of the whole variable
43# expression.
44.if ${WORDS:tu:ts} != "ONETWOTHREEFOURFIVESIX"
45.  warning Colon as separator does not work.
46.endif
47
48# The '}' plays the same role as the ':' in the preceding examples.
49# Since there is a single character before it, that character is taken as
50# the separator.
51.if ${WORDS:tu:ts/} != "ONE/TWO/THREE/FOUR/FIVE/SIX"
52.  warning Colon as separator does not work.
53.endif
54
55# Now it gets interesting and ambiguous:  The separator could either be empty
56# since it is followed by a colon.  Or it could be the colon since that
57# colon is followed by the closing brace.  It's the latter case.
58.if ${WORDS:ts:} != "one:two:three:four:five:six"
59.  warning Colon followed by closing brace does not work.
60.endif
61
62# As in the ${WORDS:tu:ts} example above, the separator is empty.
63.if ${WORDS:ts} != "onetwothreefourfivesix"
64.  warning Empty separator before closing brace does not work.
65.endif
66
67# The :ts modifier can be followed by other modifiers.
68.if ${WORDS:ts:S/two/2/} != "one2threefourfivesix"
69.  warning Separator followed by :S modifier does not work.
70.endif
71
72# The :ts modifier can follow other modifiers.
73.if ${WORDS:S/two/2/:ts} != "one2threefourfivesix"
74.  warning :S modifier followed by :ts modifier does not work.
75.endif
76
77# The :ts modifier with an actual separator can be followed by other
78# modifiers.
79.if ${WORDS:ts/:S/two/2/} != "one/2/three/four/five/six"
80.  warning The :ts modifier followed by an :S modifier does not work.
81.endif
82
83# The separator can be \n, which is a newline.
84.if ${WORDS:[1..3]:ts\n} != "one${.newline}two${.newline}three"
85.  warning The separator \n does not produce a newline.
86.endif
87
88# The separator can be \t, which is a tab.
89.if ${WORDS:[1..3]:ts\t} != "one	two	three"
90.  warning The separator \t does not produce a tab.
91.endif
92
93# The separator can be given as octal number.
94.if ${WORDS:[1..3]:ts\012:tu} != "ONE${.newline}TWO${.newline}THREE"
95.  warning The separator \012 is not interpreted in octal ASCII.
96.endif
97
98# The octal number can have as many digits as it wants.
99.if ${WORDS:[1..2]:ts\000000000000000000000000012:tu} != "ONE${.newline}TWO"
100.  warning The separator \012 cannot have many leading zeroes.
101.endif
102
103# The value of the separator character must not be outside the value space
104# for an unsigned character though.
105#
106# Since 2020-11-01, these out-of-bounds values are rejected.
107.if ${WORDS:[1..3]:ts\400:tu}
108.  warning The separator \400 is accepted even though it is out of bounds.
109.else
110.  warning The separator \400 is accepted even though it is out of bounds.
111.endif
112
113# The separator can be given as hexadecimal number.
114.if ${WORDS:[1..3]:ts\xa:tu} != "ONE${.newline}TWO${.newline}THREE"
115.  warning The separator \xa is not interpreted in hexadecimal ASCII.
116.endif
117
118# The hexadecimal number must be in the range of an unsigned char.
119#
120# Since 2020-11-01, these out-of-bounds values are rejected.
121.if ${WORDS:[1..3]:ts\x100:tu}
122.  warning The separator \x100 is accepted even though it is out of bounds.
123.else
124.  warning The separator \x100 is accepted even though it is out of bounds.
125.endif
126
127# Negative numbers are not allowed for the separator character.
128.if ${WORDS:[1..3]:ts\-300:tu}
129.  warning The separator \-300 is accepted even though it is negative.
130.else
131.  warning The separator \-300 is accepted even though it is negative.
132.endif
133
134# The character number is interpreted as octal number by default.
135# The digit '8' is not an octal digit though.
136.if ${1 2 3:L:ts\8:tu}
137.  warning The separator \8 is accepted even though it is not octal.
138.else
139.  warning The separator \8 is accepted even though it is not octal.
140.endif
141
142# Trailing characters after the octal character number are rejected.
143.if ${1 2 3:L:ts\100L}
144.  warning The separator \100L is accepted even though it contains an 'L'.
145.else
146.  warning The separator \100L is accepted even though it contains an 'L'.
147.endif
148
149# Trailing characters after the hexadecimal character number are rejected.
150.if ${1 2 3:L:ts\x40g}
151.  warning The separator \x40g is accepted even though it contains a 'g'.
152.else
153.  warning The separator \x40g is accepted even though it contains a 'g'.
154.endif
155
156
157# In the :t modifier, the :t must be followed by any of A, l, s, u.
158.if ${WORDS:tx} != "anything"
159.  info This line is not reached because of the malformed condition.
160.  info If this line were reached, it would be visible in the -dcpv log.
161.endif
162
163# After the backslash, only n, t, an octal number, or x and a hexadecimal
164# number are allowed.
165.if ${WORDS:t\X} != "anything"
166.  info This line is not reached.
167.endif
168
169# TODO: This modifier used to accept decimal numbers as well, in the form
170# ':ts\120'.  When has this been changed to octal, and what happens now
171# for ':ts\90' ('Z' in decimal ASCII, undefined in octal)?
172
173# TODO: :ts\x1F600
174
175all:
176