Home | History | Annotate | Line # | Download | only in unit-tests
modmisc.mk revision 1.19
      1 # $Id: modmisc.mk,v 1.19 2020/07/21 21:34:41 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-S mod-C mod-at-varname mod-at-resolve mod-at-dollar
     20 all:	mod-subst-dollar mod-loop-dollar
     21 all:	mod-C-limits
     22 all:	mod-assign
     23 all:	mod-assign-nested
     24 all:	mod-tu-space
     25 all:	mod-Q
     26 
     27 modsysv:
     28 	@echo "The answer is ${libfoo.a:L:libfoo.a=42}"
     29 
     30 modvar:
     31 	@echo "path='${path}'"
     32 	@echo "path='${path:${MOD_NODOT}}'"
     33 	@echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
     34 	@echo "path=${path:${MOD_NODOTX}:ts:}"
     35 	@echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
     36 
     37 .for d in ${path:${MOD_SEP}:N.} /usr/xbin
     38 path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
     39 paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
     40 .endfor
     41 
     42 modvarloop:
     43 	@echo "path_/usr/xbin=${path_/usr/xbin}"
     44 	@echo "paths=${paths}"
     45 	@echo "PATHS=${paths:tu}"
     46 
     47 PATHNAMES=	a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
     48 mod-HTE:
     49 	@echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
     50 	@echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
     51 	@echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
     52 	@echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
     53 
     54 # When a modifier is applied to the "" variable, the result is discarded.
     55 emptyvar:
     56 	@echo S:${:S,^$,empty,}
     57 	@echo C:${:C,^$,empty,}
     58 	@echo @:${:@var@${var}@}
     59 
     60 # The :U modifier turns even the "" variable into something that has a value.
     61 # The resulting variable is empty, but is still considered to contain a
     62 # single empty word. This word can be accessed by the :S and :C modifiers,
     63 # but not by the :@ modifier since it explicitly skips empty words.
     64 undefvar:
     65 	@echo S:${:U:S,^$,empty,}
     66 	@echo C:${:U:C,^$,empty,}
     67 	@echo @:${:U:@var@empty@}
     68 
     69 mod-S:
     70 	@echo :${:Ua b b c:S,a b,,:Q}:
     71 	@echo :${:Ua b b c:S,a b,,1:Q}:
     72 	@echo :${:Ua b b c:S,a b,,W:Q}:
     73 	@echo :${:Ua b b c:S,b,,g:Q}:
     74 	@echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
     75 
     76 mod-C:
     77 	@echo :${:Ua b b c:C,a b,,:Q}:
     78 	@echo :${:Ua b b c:C,a b,,1:Q}:
     79 	@echo :${:Ua b b c:C,a b,,W:Q}:
     80 	@echo :${:Uword1 word2:C,****,____,g:C,word,____,:Q}:
     81 	@echo :${:Ua b b c:C,b,,g:Q}:
     82 	@echo :${:U1 2 3 1 2 3:C,1 2,___,Wg:C,_,x,:Q}:
     83 
     84 # In the :@ modifier, the name of the loop variable can even be generated
     85 # dynamically.  There's no practical use-case for this, and hopefully nobody
     86 # will ever depend on this, but technically it's possible.
     87 mod-at-varname:
     88 	@echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
     89 
     90 # The :@ modifier resolves the variables a little more often than expected.
     91 # In particular, it resolves _all_ variables from the context, and not only
     92 # the loop variable (in this case v).
     93 #
     94 # The d means direct reference, the i means indirect reference.
     95 RESOLVE=	${RES1} $${RES1}
     96 RES1=		1d${RES2} 1i$${RES2}
     97 RES2=		2d${RES3} 2i$${RES3}
     98 RES3=		3
     99 
    100 mod-at-resolve:
    101 	@echo $@:${RESOLVE:@v@w${v}w@:Q}:
    102 
    103 # As of 2020-07-19, the variable name of the :@ modifier may end with one
    104 # or two dollar signs, which are silently ignored.  There's no point in
    105 # allowing a dollar sign in that position.
    106 mod-at-dollar:
    107 	@echo $@:${1 2 3:L:@v$@($v)@:Q}.
    108 	@echo $@:${1 2 3:L:@v$$@($v)@:Q}.
    109 	@echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
    110 
    111 # No matter how many dollar characters there are, they all get merged
    112 # into a single dollar by the :S modifier.
    113 mod-subst-dollar:
    114 	@echo $@:${:U1:S,^,$,:Q}:
    115 	@echo $@:${:U2:S,^,$$,:Q}:
    116 	@echo $@:${:U3:S,^,$$$,:Q}:
    117 	@echo $@:${:U4:S,^,$$$$,:Q}:
    118 	@echo $@:${:U5:S,^,$$$$$,:Q}:
    119 	@echo $@:${:U6:S,^,$$$$$$,:Q}:
    120 	@echo $@:${:U7:S,^,$$$$$$$,:Q}:
    121 	@echo $@:${:U8:S,^,$$$$$$$$,:Q}:
    122 # This generates no dollar at all:
    123 	@echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
    124 # Here is an alternative way to generate dollar characters.
    125 # It's unexpectedly complicated though.
    126 	@echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
    127 
    128 # Demonstrate that it is possible to generate dollar characters using the
    129 # :@ modifier.
    130 #
    131 # These are edge cases that could have resulted in a parse error as well
    132 # since the $@ at the end could have been interpreted as a variable, which
    133 # would mean a missing closing @ delimiter.
    134 mod-loop-dollar:
    135 	@echo $@:${:U1:@word@${word}$@:Q}:
    136 	@echo $@:${:U2:@word@$${word}$$@:Q}:
    137 	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
    138 	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
    139 	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
    140 	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
    141 
    142 mod-C-limits:
    143 	@echo $@:00-ok:${:U1 23 456:C,..,\0\0,:Q}
    144 	@echo $@:11-missing:${:U1 23 456:C,..,\1\1,:Q}
    145 	@echo $@:11-ok:${:U1 23 456:C,(.).,\1\1,:Q}
    146 	@echo $@:22-missing:${:U1 23 456:C,..,\2\2,:Q}
    147 	@echo $@:22-missing:${:U1 23 456:C,(.).,\2\2,:Q}
    148 	@echo $@:22-ok:${:U1 23 456:C,(.)(.),\2\2,:Q}
    149 	# The :C modifier only handles single-digit capturing groups,
    150 	# which is more than enough for daily use.
    151 	@echo $@:capture:${:UabcdefghijABCDEFGHIJrest:C,(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.),\9\8\7\6\5\4\3\2\1\0\10\11\12,}
    152 
    153 # Just a bit of basic code coverage for the obscure ::= assignment modifiers.
    154 mod-assign:
    155 	@echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
    156 	@echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
    157 	@echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
    158 	@echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
    159 	# The assignments happen in the global scope and thus are
    160 	# preserved even after the shell command has been run.
    161 	@echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
    162 
    163 mod-assign-nested:
    164 	@echo $@: ${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}}${THEN1}${ELSE1}${IT1}${IE1}
    165 	@echo $@: ${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}}${THEN2}${ELSE2}${IT2}${IE2}
    166 	@echo $@: ${SINK3:Q}
    167 	@echo $@: ${SINK4:Q}
    168 SINK3:=	${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}}${THEN3}${ELSE3}${IT3}${IE3}
    169 SINK4:=	${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}}${THEN4}${ELSE4}${IT4}${IE4}
    170 
    171 mod-tu-space:
    172 	# The :tu and :tl modifiers operate on the variable value
    173 	# as a single string, not as a list of words. Therefore,
    174 	# the adjacent spaces are preserved.
    175 	@echo $@: ${a   b:L:tu:Q}
    176 
    177 mod-Q:
    178 	@echo $@: new${.newline:Q}${.newline:Q}line
    179