Home | History | Annotate | Line # | Download | only in unit-tests
modmisc.mk revision 1.27
      1 # $Id: modmisc.mk,v 1.27 2020/07/27 21:54:25 rillig Exp $
      2 #
      3 # miscellaneous modifier tests
      4 
      5 # do not put any dirs in this list which exist on some
      6 # but not all target systems - an exists() check is below.
      7 path=:/bin:/tmp::/:.:/no/such/dir:.
      8 # strip cwd from path.
      9 MOD_NODOT=S/:/ /g:N.:ts:
     10 # and decorate, note that $'s need to be doubled. Also note that 
     11 # the modifier_variable can be used with other modifiers.
     12 MOD_NODOTX=S/:/ /g:N.:@d@'$$d'@
     13 # another mod - pretend it is more interesting
     14 MOD_HOMES=S,/home/,/homes/,
     15 MOD_OPT=@d@$${exists($$d):?$$d:$${d:S,/usr,/opt,}}@
     16 MOD_SEP=S,:, ,g
     17 
     18 all:	modvar modvarloop modsysv mod-HTE emptyvar undefvar
     19 all:	mod-subst
     20 all:	mod-regex
     21 all:	mod-loop-varname mod-loop-resolve mod-loop-varname-dollar
     22 all:	mod-subst-dollar mod-loop-dollar
     23 all:	mod-regex-limits
     24 all:	mod-regex-errors
     25 all:	mod-assign
     26 all:	mod-assign-nested
     27 all:	mod-tu-space
     28 all:	mod-quote
     29 all:	mod-break-many-words
     30 all:	mod-remember
     31 
     32 # See also sysv.mk.
     33 modsysv:
     34 	@echo "The answer is ${libfoo.a:L:libfoo.a=42}"
     35 
     36 # Demonstrates modifiers that are given indirectly from a variable.
     37 modvar:
     38 	@echo "path='${path}'"
     39 	@echo "path='${path:${MOD_NODOT}}'"
     40 	@echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
     41 	@echo "path=${path:${MOD_NODOTX}:ts:}"
     42 	@echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
     43 
     44 .for d in ${path:${MOD_SEP}:N.} /usr/xbin
     45 path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
     46 paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
     47 .endfor
     48 
     49 modvarloop:
     50 	@echo "path_/usr/xbin=${path_/usr/xbin}"
     51 	@echo "paths=${paths}"
     52 	@echo "PATHS=${paths:tu}"
     53 
     54 PATHNAMES=	a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
     55 mod-HTE:
     56 	@echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
     57 	@echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
     58 	@echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
     59 	@echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
     60 
     61 # When a modifier is applied to the "" variable, the result is discarded.
     62 emptyvar:
     63 	@echo S:${:S,^$,empty,}
     64 	@echo C:${:C,^$,empty,}
     65 	@echo @:${:@var@${var}@}
     66 
     67 # The :U modifier turns even the "" variable into something that has a value.
     68 # The resulting variable is empty, but is still considered to contain a
     69 # single empty word. This word can be accessed by the :S and :C modifiers,
     70 # but not by the :@ modifier since it explicitly skips empty words.
     71 undefvar:
     72 	@echo S:${:U:S,^$,empty,}
     73 	@echo C:${:U:C,^$,empty,}
     74 	@echo @:${:U:@var@empty@}
     75 
     76 WORDS=		sequences of letters
     77 .if ${WORDS:S,,,} != ${WORDS}
     78 .warning The empty pattern matches something.
     79 .endif
     80 .if ${WORDS:S,e,*,1} != "s*quences of letters"
     81 .warning The :S modifier flag '1' is not applied exactly once.
     82 .endif
     83 .if ${WORDS:S,e,*,} != "s*quences of l*tters"
     84 .warning The :S modifier does not replace every first match per word.
     85 .endif
     86 .if ${WORDS:S,e,*,g} != "s*qu*nc*s of l*tt*rs"
     87 .warning The :S modifier flag 'g' does not replace every occurrence.
     88 .endif
     89 .if ${WORDS:S,^sequ,occurr,} != "occurrences of letters"
     90 .warning The :S modifier fails for a short match anchored at the start.
     91 .endif
     92 .if ${WORDS:S,^of,with,} != "sequences with letters"
     93 .warning The :S modifier fails for an exact match anchored at the start.
     94 .endif
     95 .if ${WORDS:S,^office,does not match,} != ${WORDS}
     96 .warning The :S modifier matches a too long pattern anchored at the start.
     97 .endif
     98 .if ${WORDS:S,f$,r,} != "sequences or letters"
     99 .warning The :S modifier fails for a short match anchored at the end.
    100 .endif
    101 .if ${WORDS:S,s$,,} != "sequence of letter"
    102 .warning The :S modifier fails to replace one occurrence per word.
    103 .endif
    104 .if ${WORDS:S,of$,,} != "sequences letters"
    105 .warning The :S modifier fails for an exact match anchored at the end.
    106 .endif
    107 .if ${WORDS:S,eof$,,} != ${WORDS}
    108 .warning The :S modifier matches a too long pattern anchored at the end.
    109 .endif
    110 .if ${WORDS:S,^of$,,} != "sequences letters"
    111 .warning The :S modifier does not match a word anchored at both ends.
    112 .endif
    113 .if ${WORDS:S,^o$,,} != ${WORDS}
    114 .warning The :S modifier matches a prefix anchored at both ends.
    115 .endif
    116 .if ${WORDS:S,^f$,,} != ${WORDS}
    117 .warning The :S modifier matches a suffix anchored at both ends.
    118 .endif
    119 .if ${WORDS:S,^eof$,,} != ${WORDS}
    120 .warning The :S modifier matches a too long prefix anchored at both ends.
    121 .endif
    122 .if ${WORDS:S,^office$,,} != ${WORDS}
    123 .warning The :S modifier matches a too long suffix anchored at both ends.
    124 .endif
    125 
    126 mod-subst:
    127 	@echo $@:
    128 	@echo :${:Ua b b c:S,a b,,:Q}:
    129 	@echo :${:Ua b b c:S,a b,,1:Q}:
    130 	@echo :${:Ua b b c:S,a b,,W:Q}:
    131 	@echo :${:Ua b b c:S,b,,g:Q}:
    132 	@echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
    133 	@echo ${:U12345:S,,sep,g:Q}
    134 
    135 mod-regex:
    136 	@echo $@:
    137 	@echo :${:Ua b b c:C,a b,,:Q}:
    138 	@echo :${:Ua b b c:C,a b,,1:Q}:
    139 	@echo :${:Ua b b c:C,a b,,W:Q}:
    140 	@echo :${:Uword1 word2:C,****,____,g:C,word,____,:Q}:
    141 	@echo :${:Ua b b c:C,b,,g:Q}:
    142 	@echo :${:U1 2 3 1 2 3:C,1 2,___,Wg:C,_,x,:Q}:
    143 
    144 # In the :@ modifier, the name of the loop variable can even be generated
    145 # dynamically.  There's no practical use-case for this, and hopefully nobody
    146 # will ever depend on this, but technically it's possible.
    147 mod-loop-varname:
    148 	@echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
    149 
    150 # The :@ modifier resolves the variables a little more often than expected.
    151 # In particular, it resolves _all_ variables from the context, and not only
    152 # the loop variable (in this case v).
    153 #
    154 # The d means direct reference, the i means indirect reference.
    155 RESOLVE=	${RES1} $${RES1}
    156 RES1=		1d${RES2} 1i$${RES2}
    157 RES2=		2d${RES3} 2i$${RES3}
    158 RES3=		3
    159 
    160 mod-loop-resolve:
    161 	@echo $@:${RESOLVE:@v@w${v}w@:Q}:
    162 
    163 # Until 2020-07-20, the variable name of the :@ modifier could end with one
    164 # or two dollar signs, which were silently ignored.
    165 # There's no point in allowing a dollar sign in that position.
    166 mod-loop-varname-dollar:
    167 	@echo $@:${1 2 3:L:@v$@($v)@:Q}.
    168 	@echo $@:${1 2 3:L:@v$$@($v)@:Q}.
    169 	@echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
    170 
    171 # No matter how many dollar characters there are, they all get merged
    172 # into a single dollar by the :S modifier.
    173 mod-subst-dollar:
    174 	@echo $@:${:U1:S,^,$,:Q}:
    175 	@echo $@:${:U2:S,^,$$,:Q}:
    176 	@echo $@:${:U3:S,^,$$$,:Q}:
    177 	@echo $@:${:U4:S,^,$$$$,:Q}:
    178 	@echo $@:${:U5:S,^,$$$$$,:Q}:
    179 	@echo $@:${:U6:S,^,$$$$$$,:Q}:
    180 	@echo $@:${:U7:S,^,$$$$$$$,:Q}:
    181 	@echo $@:${:U8:S,^,$$$$$$$$,:Q}:
    182 # This generates no dollar at all:
    183 	@echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
    184 # Here is an alternative way to generate dollar characters.
    185 # It's unexpectedly complicated though.
    186 	@echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
    187 
    188 # Demonstrate that it is possible to generate dollar characters using the
    189 # :@ modifier.
    190 #
    191 # These are edge cases that could have resulted in a parse error as well
    192 # since the $@ at the end could have been interpreted as a variable, which
    193 # would mean a missing closing @ delimiter.
    194 mod-loop-dollar:
    195 	@echo $@:${:U1:@word@${word}$@:Q}:
    196 	@echo $@:${:U2:@word@$${word}$$@:Q}:
    197 	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
    198 	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
    199 	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
    200 	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
    201 
    202 mod-regex-limits:
    203 	@echo $@:00-ok:${:U1 23 456:C,..,\0\0,:Q}
    204 	@echo $@:11-missing:${:U1 23 456:C,..,\1\1,:Q}
    205 	@echo $@:11-ok:${:U1 23 456:C,(.).,\1\1,:Q}
    206 	@echo $@:22-missing:${:U1 23 456:C,..,\2\2,:Q}
    207 	@echo $@:22-missing:${:U1 23 456:C,(.).,\2\2,:Q}
    208 	@echo $@:22-ok:${:U1 23 456:C,(.)(.),\2\2,:Q}
    209 	# The :C modifier only handles single-digit capturing groups,
    210 	# which is more than enough for daily use.
    211 	@echo $@:capture:${:UabcdefghijABCDEFGHIJrest:C,(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.),\9\8\7\6\5\4\3\2\1\0\10\11\12,}
    212 
    213 mod-regex-errors:
    214 	@echo $@: ${UNDEF:Uvalue:C,[,,}
    215 
    216 # Just a bit of basic code coverage for the obscure ::= assignment modifiers.
    217 mod-assign:
    218 	@echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
    219 	@echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
    220 	@echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
    221 	@echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
    222 	# The assignments happen in the global scope and thus are
    223 	# preserved even after the shell command has been run.
    224 	@echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
    225 
    226 mod-assign-nested:
    227 	@echo $@: ${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}}${THEN1}${ELSE1}${IT1}${IE1}
    228 	@echo $@: ${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}}${THEN2}${ELSE2}${IT2}${IE2}
    229 	@echo $@: ${SINK3:Q}
    230 	@echo $@: ${SINK4:Q}
    231 SINK3:=	${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}}${THEN3}${ELSE3}${IT3}${IE3}
    232 SINK4:=	${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}}${THEN4}${ELSE4}${IT4}${IE4}
    233 
    234 mod-tu-space:
    235 	# The :tu and :tl modifiers operate on the variable value
    236 	# as a single string, not as a list of words. Therefore,
    237 	# the adjacent spaces are preserved.
    238 	@echo $@: ${a   b:L:tu:Q}
    239 
    240 mod-quote:
    241 	@echo $@: new${.newline:Q}${.newline:Q}line
    242 
    243 # Cover the bmake_realloc in brk_string.
    244 mod-break-many-words:
    245 	@echo $@: ${UNDEF:U:range=500:[#]}
    246 
    247 # Demonstrate the :_ modifier.
    248 # In the parameterized form, having the variable name on the right side
    249 # of the = assignment operator is confusing. Luckily this modifier is
    250 # only rarely needed.
    251 mod-remember:
    252 	@echo $@: ${1 2 3:L:_:@var@${_}@}
    253 	@echo $@: ${1 2 3:L:@var@${var:_=SAVED:}@}, SAVED=${SAVED}
    254