Home | History | Annotate | Line # | Download | only in unit-tests
varmod-sysv.mk revision 1.9
      1 # $NetBSD: varmod-sysv.mk,v 1.9 2020/10/31 11:06:24 rillig Exp $
      2 #
      3 # Tests for the ${VAR:from=to} variable modifier, which replaces the suffix
      4 # "from" with "to".  It can also use '%' as a wildcard.
      5 #
      6 # This modifier is applied when the other modifiers don't match exactly.
      7 #
      8 # See ApplyModifier_SysV.
      9 
     10 # A typical use case for the :from=to modifier is conversion of filename
     11 # extensions.
     12 .if ${src.c:L:.c=.o} != "src.o"
     13 .  error
     14 .endif
     15 
     16 # The modifier applies to each word on its own.
     17 .if ${one.c two.c three.c:L:.c=.o} != "one.o two.o three.o"
     18 .  error
     19 .endif
     20 
     21 # Words that don't match the pattern are passed unmodified.
     22 .if ${src.c src.h:L:.c=.o} != "src.o src.h"
     23 .  error
     24 .endif
     25 
     26 # The :from=to modifier is therefore often combined with the :M modifier.
     27 .if ${src.c src.h:L:M*.c:.c=.o} != "src.o"
     28 .  error
     29 .endif
     30 
     31 # Another use case for the :from=to modifier is to append a suffix to each
     32 # word.  In this case, the "from" string is empty, therefore it always
     33 # matches.  The same effect can be achieved with the :S,$,teen, modifier.
     34 .if ${four six seven nine:L:=teen} != "fourteen sixteen seventeen nineteen"
     35 .  error
     36 .endif
     37 
     38 # The :from=to modifier can also be used to surround each word by strings.
     39 # It might be tempting to use this for enclosing a string in quotes for the
     40 # shell, but that's the job of the :Q modifier.
     41 .if ${one two three:L:%=(%)} != "(one) (two) (three)"
     42 .  error
     43 .endif
     44 
     45 # When the :from=to modifier is parsed, it lasts until the closing brace
     46 # or parenthesis.  The :Q in the below expression may look like a modifier
     47 # but isn't.  It is part of the replacement string.
     48 .if ${a b c d e:L:%a=x:Q} != "x:Q b c d e"
     49 .  error
     50 .endif
     51 
     52 # In the :from=to modifier, both parts can contain variable expressions.
     53 .if ${one two:L:${:Uone}=${:U1}} != "1 two"
     54 .  error
     55 .endif
     56 
     57 # In the :from=to modifier, the "from" part is expanded exactly once.
     58 .if ${:U\$ \$\$ \$\$\$\$:${:U\$\$\$\$}=4} != "\$ \$\$ 4"
     59 .  error
     60 .endif
     61 
     62 # In the :from=to modifier, the "to" part is expanded exactly twice.
     63 # XXX: The right-hand side should be expanded only once.
     64 # XXX: It's hard to get the escaping correct here, and to read that.
     65 # XXX: It's not intuitive why the closing brace must be escaped but not
     66 #      the opening brace.
     67 .if ${:U1 2 4:4=${:Uonce\${\:Utwice\}}} != "1 2 oncetwice"
     68 .  error
     69 .endif
     70 
     71 # The replacement string can contain spaces, thereby changing the number
     72 # of words in the variable expression.
     73 .if ${In:L:%=% ${:Uthe Sun}} != "In the Sun"
     74 .  error
     75 .endif
     76 
     77 # If the variable is empty, it is debatable whether it consists of a single
     78 # empty word, or no word at all.  The :from=to modifier treats it as no
     79 # word at all.
     80 .if ${:L:=suffix} != ""
     81 .  error
     82 .endif
     83 
     84 # Before 2020-07-19, an ampersand could be used in the replacement part
     85 # of a SysV substitution modifier, and it was replaced with the whole match,
     86 # just like in the :S modifier.
     87 #
     88 # This was probably a copy-and-paste mistake since the code for the SysV
     89 # modifier looked a lot like the code for the :S and :C modifiers.
     90 # The ampersand is not mentioned in the manual page.
     91 .if ${a.bcd.e:L:a.%=%} != "bcd.e"
     92 .  error
     93 .endif
     94 # Before 2020-07-19, the result of the expression was "a.bcd.e".
     95 .if ${a.bcd.e:L:a.%=&} != "&"
     96 .  error
     97 .endif
     98 
     99 # Before 2020-07-20, when a SysV modifier was parsed, a single dollar
    100 # before the '=' was parsed (but not interpreted) as an anchor.
    101 # Parsing something without then evaluating it accordingly doesn't make
    102 # sense.
    103 .if ${value:L:e$=x} != "value"
    104 .  error
    105 .endif
    106 # Before 2020-07-20, the modifier ":e$=x" was parsed as having a left-hand
    107 # side "e" and a right-hand side "x".  The dollar was parsed (but not
    108 # interpreted) as 'anchor at the end'.  Therefore the modifier was equivalent
    109 # to ":e=x", which doesn't match the string "value$".  Therefore the whole
    110 # expression evaluated to "value$".
    111 .if ${${:Uvalue\$}:L:e$=x} != "valux"
    112 .  error
    113 .endif
    114 .if ${value:L:e=x} != "valux"
    115 .  error
    116 .endif
    117 
    118 # Words that don't match are copied unmodified.
    119 .if ${:Ufile.c file.h:%.c=%.cpp} != "file.cpp file.h"
    120 .  error
    121 .endif
    122 
    123 # The % placeholder can be anywhere in the string, it doesn't have to be at
    124 # the beginning of the pattern.
    125 .if ${:Ufile.c other.c:file.%=renamed.%} != "renamed.c other.c"
    126 .  error
    127 .endif
    128 
    129 # It's also possible to modify each word by replacing the prefix and adding
    130 # a suffix.
    131 .if ${one two:L:o%=a%w} != "anew two"
    132 .  error
    133 .endif
    134 
    135 # Each word gets the suffix "X" appended.
    136 .if ${one two:L:=X} != "oneX twoX"
    137 .  error
    138 .endif
    139 
    140 # The suffix "o" is replaced with "X".
    141 .if ${one two:L:o=X} != "one twX"
    142 .  error
    143 .endif
    144 
    145 # The suffix "o" is replaced with nothing.
    146 .if ${one two:L:o=} != "one tw"
    147 .  error
    148 .endif
    149 
    150 # The suffix "o" is replaced with a literal percent.  The percent is only
    151 # a wildcard when it appears on the left-hand side.
    152 .if ${one two:L:o=%} != "one tw%"
    153 .  error
    154 .endif
    155 
    156 # Each word with the suffix "o" is replaced with "X".  The percent is a
    157 # wildcard even though the right-hand side does not contain another percent.
    158 .if ${one two:L:%o=X} != "one X"
    159 .  error
    160 .endif
    161 
    162 # Each word with the prefix "o" is replaced with "X".  The percent is a
    163 # wildcard even though the right-hand side does not contain another percent.
    164 .if ${one two:L:o%=X} != "X two"
    165 .  error
    166 .endif
    167 
    168 # For each word with the prefix "o" and the suffix "e", the whole word is
    169 # replaced with "X".
    170 .if ${one two oe oxen:L:o%e=X} != "X two X oxen"
    171 .  error
    172 .endif
    173 
    174 # Only the first '%' is the wildcard.
    175 .if ${one two o%e other%e:L:o%%e=X} != "one two X X"
    176 .  error
    177 .endif
    178 
    179 # In the replacement, only the first '%' is the placeholder, all others
    180 # are literal percent characters.
    181 .if ${one two:L:%=%%} != "one% two%"
    182 .  error
    183 .endif
    184 
    185 # In the word "one", only a prefix of the pattern suffix "nes" matches,
    186 # the whole word is too short.  Therefore it doesn't match.
    187 .if ${one two:L:%nes=%xxx} != "one two"
    188 .  error
    189 .endif
    190 
    191 # The :from=to modifier can be used to replace both the prefix and a suffix
    192 # of a word with other strings.  This is not possible with a single :S
    193 # modifier, and using a :C modifier for the same task looks more complicated
    194 # in many cases.
    195 .if ${prefix-middle-suffix:L:prefix-%-suffix=p-%-s} != "p-middle-s"
    196 .  error
    197 .endif
    198 
    199 # As of 2020-10-06, the right-hand side of the SysV modifier is expanded
    200 # twice.  The first expansion happens in ApplyModifier_SysV, where the
    201 # modifier is split into its two parts.  The second expansion happens
    202 # when each word is replaced in ModifyWord_SYSVSubst.
    203 # XXX: This is unexpected.  Add more test case to demonstrate the effects
    204 # of removing one of the expansions.
    205 VALUE=		value
    206 INDIRECT=	1:${VALUE} 2:$${VALUE} 4:$$$${VALUE}
    207 .if ${x:L:x=${INDIRECT}} != "1:value 2:value 4:\${VALUE}"
    208 .  error
    209 .endif
    210 
    211 all:
    212