Home | History | Annotate | Line # | Download | only in unit-tests
varmod-subst.mk revision 1.13
      1 # $NetBSD: varmod-subst.mk,v 1.13 2023/12/17 14:07:22 rillig Exp $
      2 #
      3 # Tests for the :S,from,to, variable modifier.
      4 
      5 all: mod-subst
      6 all: mod-subst-delimiter
      7 all: mod-subst-chain
      8 all: mod-subst-dollar
      9 
     10 WORDS=		sequences of letters
     11 
     12 # The empty pattern never matches anything, except if it is anchored at the
     13 # beginning or the end of the word.
     14 .if ${WORDS:S,,,} != ${WORDS}
     15 .  error
     16 .endif
     17 
     18 # The :S modifier flag '1' is applied exactly once.
     19 .if ${WORDS:S,e,*,1} != "s*quences of letters"
     20 .  error
     21 .endif
     22 
     23 # The :S modifier flag '1' is applied to the first occurrence, no matter if
     24 # the occurrence is in the first word or not.
     25 .if ${WORDS:S,f,*,1} != "sequences o* letters"
     26 .  error
     27 .endif
     28 
     29 # The :S modifier replaces every first match per word.
     30 .if ${WORDS:S,e,*,} != "s*quences of l*tters"
     31 .  error
     32 .endif
     33 
     34 # The :S modifier flag 'g' replaces every occurrence.
     35 .if ${WORDS:S,e,*,g} != "s*qu*nc*s of l*tt*rs"
     36 .  error
     37 .endif
     38 
     39 # The '^' in the search pattern anchors the pattern at the beginning of each
     40 # word, thereby matching a prefix.
     41 .if ${WORDS:S,^sequ,occurr,} != "occurrences of letters"
     42 .  error
     43 .endif
     44 
     45 # The :S modifier with a '^' anchor replaces the whole word if that word is
     46 # exactly the pattern.
     47 .if ${WORDS:S,^of,with,} != "sequences with letters"
     48 .  error
     49 .endif
     50 
     51 # The :S modifier does not match if the pattern is longer than the word.
     52 .if ${WORDS:S,^office,does not match,} != ${WORDS}
     53 .  warning
     54 .endif
     55 
     56 # The '$' in the search pattern anchors the pattern at the end of each word,
     57 # thereby matching a suffix.
     58 .if ${WORDS:S,f$,r,} != "sequences or letters"
     59 .  error
     60 .endif
     61 
     62 # The :S modifier with a '$' anchor replaces at most one occurrence per word.
     63 .if ${WORDS:S,s$,,} != "sequence of letter"
     64 .  error
     65 .endif
     66 
     67 # The :S modifier with a '$' anchor replaces the whole word if that word is
     68 # exactly the pattern.
     69 .if ${WORDS:S,of$,,} != "sequences letters"
     70 .  error
     71 .endif
     72 
     73 # The :S modifier with a '$' anchor and a pattern that is longer than a word
     74 # cannot match that word.
     75 .if ${WORDS:S,eof$,,} != ${WORDS}
     76 .  warning
     77 .endif
     78 
     79 # The :S modifier with the '^' and '$' anchors matches an exact word.
     80 .if ${WORDS:S,^of$,,} != "sequences letters"
     81 .  error
     82 .endif
     83 
     84 # The :S modifier with the '^' and '$' anchors does not match a word that
     85 # starts with the pattern but is longer than the pattern.
     86 .if ${WORDS:S,^o$,,} != ${WORDS}
     87 .  error
     88 .endif
     89 
     90 # The :S modifier with the '^' and '$' anchors does not match a word that ends
     91 # with the pattern but is longer than the pattern.
     92 .if ${WORDS:S,^f$,,} != ${WORDS}
     93 .  error
     94 .endif
     95 
     96 # The :S modifier with the '^' and '$' anchors does not match a word if the
     97 # pattern ends with the word but is longer than the word.
     98 .if ${WORDS:S,^eof$,,} != ${WORDS}
     99 .  error
    100 .endif
    101 
    102 # The :S modifier with the '^' and '$' anchors does not match a word if the
    103 # pattern starts with the word but is longer than the word.
    104 .if ${WORDS:S,^office$,,} != ${WORDS}
    105 .  error
    106 .endif
    107 
    108 # Except for the '^' and '$' anchors, the pattern does not contain any special
    109 # characters, so the '*' from the pattern would only match a literal '*' in a
    110 # word.
    111 .if ${WORDS:S,*,replacement,} != ${WORDS}
    112 .  error
    113 .endif
    114 
    115 # Except for the '^' and '$' anchors, the pattern does not contain any special
    116 # characters, so the '.' from the pattern would only match a literal '.' in a
    117 # word.
    118 .if ${WORDS:S,.,replacement,} != ${WORDS}
    119 .  error
    120 .endif
    121 
    122 # The '&' in the replacement is a placeholder for the text matched by the
    123 # pattern.
    124 .if ${:Uvalue:S,^val,&,} != "value"
    125 .  error
    126 .endif
    127 .if ${:Uvalue:S,ue$,&,} != "value"
    128 .  error
    129 .endif
    130 .if ${:Uvalue:S,^val,&-&-&,} != "val-val-value"
    131 .  error
    132 .endif
    133 .if ${:Uvalue:S,ue$,&-&-&,} != "value-ue-ue"
    134 .  error
    135 .endif
    136 
    137 
    138 # When a word is replaced with nothing, the remaining words are separated by a
    139 # single space, not two.
    140 .if ${1 2 3:L:S,2,,} != "1 3"
    141 .  error
    142 .endif
    143 
    144 
    145 # In an empty expression, the ':S' modifier matches a single time if the
    146 # search string is anchored at the beginning or at the end.
    147 .if ${:U:S,,out-of-nothing,} != ""
    148 .  error
    149 .endif
    150 .if ${:U:S,^,out-of-nothing,} != "out-of-nothing"
    151 .  error
    152 .endif
    153 .if ${:U:S,$,out-of-nothing,} != "out-of-nothing"
    154 .  error
    155 .endif
    156 .if ${:U:S,^$,out-of-nothing,} != "out-of-nothing"
    157 .  error
    158 .endif
    159 
    160 
    161 mod-subst:
    162 	@echo $@:
    163 	@echo :${:Ua b b c:S,a b,,:Q}:
    164 	@echo :${:Ua b b c:S,a b,,1:Q}:
    165 	@echo :${:Ua b b c:S,a b,,W:Q}:
    166 	@echo :${:Ua b b c:S,b,,g:Q}:
    167 	@echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
    168 	@echo ${:U12345:S,,sep,g:Q}
    169 
    170 # The :S and :C modifiers accept an arbitrary character as the delimiter,
    171 # including characters that are otherwise used as escape characters or
    172 # interpreted in a special way.  This can be used to confuse humans.
    173 mod-subst-delimiter:
    174 	@echo $@:
    175 	@echo ${:U1 2 3:S	2	two	:Q} horizontal tabulator
    176 	@echo ${:U1 2 3:S 2 two :Q} space
    177 	@echo ${:U1 2 3:S!2!two!:Q} exclamation mark
    178 	@echo ${:U1 2 3:S"2"two":Q} quotation mark
    179 	# In shell command lines, the hash does not need to be escaped.
    180 	# It needs to be escaped in variable assignment lines though.
    181 	@echo ${:U1 2 3:S#2#two#:Q} number sign
    182 	@echo ${:U1 2 3:S$2$two$:Q} dollar sign
    183 	@echo ${:U1 2 3:S%2%two%:Q} percent sign
    184 	@echo ${:U1 2 3:S&2&two&:Q} ampersand
    185 	@echo ${:U1 2 3:S'2'two':Q} apostrophe
    186 	@echo ${:U1 2 3:S(2(two(:Q} left parenthesis
    187 	@echo ${:U1 2 3:S)2)two):Q} right parenthesis
    188 	@echo ${:U1 2 3:S*2*two*:Q} asterisk
    189 	@echo ${:U1 2 3:S+2+two+:Q} plus sign
    190 	@echo ${:U1 2 3:S,2,two,:Q} comma
    191 	@echo ${:U1 2 3:S-2-two-:Q} hyphen-minus
    192 	@echo ${:U1 2 3:S.2.two.:Q} full stop
    193 	@echo ${:U1 2 3:S/2/two/:Q} solidus
    194 	@echo ${:U1 2 3:S121two1:Q} digit
    195 	@echo ${:U1 2 3:S:2:two::Q} colon
    196 	@echo ${:U1 2 3:S;2;two;:Q} semicolon
    197 	@echo ${:U1 2 3:S<2<two<:Q} less-than sign
    198 	@echo ${:U1 2 3:S=2=two=:Q} equals sign
    199 	@echo ${:U1 2 3:S>2>two>:Q} greater-than sign
    200 	@echo ${:U1 2 3:S?2?two?:Q} question mark
    201 	@echo ${:U1 2 3:S@2@two@:Q} commercial at
    202 	@echo ${:U1 2 3:SA2AtwoA:Q} capital letter
    203 	@echo ${:U1 2 3:S[2[two[:Q} left square bracket
    204 	@echo ${:U1 2 3:S\2\two\:Q} reverse solidus
    205 	@echo ${:U1 2 3:S]2]two]:Q} right square bracket
    206 	@echo ${:U1 2 3:S^2^two^:Q} circumflex accent
    207 	@echo ${:U1 2 3:S_2_two_:Q} low line
    208 	@echo ${:U1 2 3:S`2`two`:Q} grave accent
    209 	@echo ${:U1 2 3:Sa2atwoa:Q} small letter
    210 	@echo ${:U1 2 3:S{2{two{:Q} left curly bracket
    211 	@echo ${:U1 2 3:S|2|two|:Q} vertical line
    212 	@echo ${:U1 2 3:S}2}two}:Q} right curly bracket
    213 	@echo ${:U1 2 3:S~2~two~:Q} tilde
    214 
    215 # The :S and :C modifiers can be chained without a separating ':'.
    216 # This is not documented in the manual page.
    217 # It works because ApplyModifier_Subst scans for the known modifiers g1W
    218 # and then just returns to ApplyModifiers.  There, the colon is optionally
    219 # skipped (see the *st.next == ':' at the end of the loop).
    220 #
    221 # Most other modifiers cannot be chained since their parsers skip until
    222 # the next ':' or '}' or ')'.
    223 mod-subst-chain:
    224 	@echo $@:
    225 	@echo ${:Ua b c:S,a,A,S,b,B,}.
    226 	# There is no 'i' modifier for the :S or :C modifiers.
    227 	# The error message is "make: Unknown modifier 'i'", which is
    228 	# kind of correct, although it is mixing the terms for variable
    229 	# modifiers with the matching modifiers.
    230 	@echo ${:Uvalue:S,a,x,i}.
    231 
    232 # No matter how many dollar signs there are, they all get merged
    233 # into a single dollar by the :S modifier.
    234 #
    235 # As of 2020-08-09, this is because ParseModifierPart sees a '$' and
    236 # calls Var_Parse to expand the variable.  In all other places, the "$$"
    237 # is handled outside of Var_Parse.  Var_Parse therefore considers "$$"
    238 # one of the "really stupid names", skips the first dollar, and parsing
    239 # continues with the next character.  This repeats for the other dollar
    240 # signs, except the one before the delimiter.  That one is handled by
    241 # the code that optionally interprets the '$' as the end-anchor in the
    242 # first part of the :S modifier.  That code doesn't call Var_Parse but
    243 # simply copies the dollar to the result.
    244 mod-subst-dollar:
    245 	@echo $@:${:U1:S,^,$,:Q}:
    246 	@echo $@:${:U2:S,^,$$,:Q}:
    247 	@echo $@:${:U3:S,^,$$$,:Q}:
    248 	@echo $@:${:U4:S,^,$$$$,:Q}:
    249 	@echo $@:${:U5:S,^,$$$$$,:Q}:
    250 	@echo $@:${:U6:S,^,$$$$$$,:Q}:
    251 	@echo $@:${:U7:S,^,$$$$$$$,:Q}:
    252 	@echo $@:${:U8:S,^,$$$$$$$$,:Q}:
    253 	@echo $@:${:U40:S,^,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,:Q}:
    254 # This generates no dollar at all:
    255 	@echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
    256 # Here is an alternative way to generate dollar signs.
    257 # It's unexpectedly complicated though.
    258 	@echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
    259 # In modifiers, dollars are escaped using the backslash, not using another
    260 # dollar sign.  Therefore, creating a dollar sign is pretty simple:
    261 	@echo $@:${:Ugood3:S,^,\$\$\$,:Q}
    262