xref: /freebsd/contrib/bmake/unit-tests/varmod-to-separator.mk (revision 6132212808e8dccedc9e5d85fea4390c2f38059a)
1# $NetBSD: varmod-to-separator.mk,v 1.3 2020/08/31 19:58:21 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 separator can be given as hexadecimal number.
99.if ${WORDS:[1..3]:ts\xa:tu} != "ONE${.newline}TWO${.newline}THREE"
100.  warning The separator \xa is not interpreted in hexadecimal ASCII.
101.endif
102
103# In the :t modifier, the :t must be followed by any of A, l, s, u.
104.if ${WORDS:tx} != "anything"
105.  info This line is not reached because of the malformed condition.
106.  info If this line were reached, it would be visible in the -dcpv log.
107.endif
108.info Parsing continues here.
109
110# After the backslash, only n, t, an octal number, or x and a hexadecimal
111# number are allowed.
112.if ${WORDS:t\X} != "anything"
113.  info This line is not reached.
114.endif
115.info Parsing continues here.
116
117all:
118	@:;
119