Home | History | Annotate | Line # | Download | only in unit-tests
varmod-sysv.mk revision 1.8
      1 # $NetBSD: varmod-sysv.mk,v 1.8 2020/10/31 10:18:32 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 # When the :from=to modifier is parsed, it lasts until the closing brace
     39 # or parenthesis.  The :Q in the below expression may look like a modifier
     40 # but isn't.  It is part of the replacement string.
     41 .if ${a b c d e:L:%a=x:Q} != "x:Q b c d e"
     42 .  error
     43 .endif
     44 
     45 # Before 2020-07-19, an ampersand could be used in the replacement part
     46 # of a SysV substitution modifier, and it was replaced with the whole match,
     47 # just like in the :S modifier.
     48 #
     49 # This was probably a copy-and-paste mistake since the code for the SysV
     50 # modifier looked a lot like the code for the :S and :C modifiers.
     51 # The ampersand is not mentioned in the manual page.
     52 .if ${a.bcd.e:L:a.%=%} != "bcd.e"
     53 .  error
     54 .endif
     55 # Before 2020-07-19, the result of the expression was "a.bcd.e".
     56 .if ${a.bcd.e:L:a.%=&} != "&"
     57 .  error
     58 .endif
     59 
     60 # Before 2020-07-20, when a SysV modifier was parsed, a single dollar
     61 # before the '=' was parsed (but not interpreted) as an anchor.
     62 # Parsing something without then evaluating it accordingly doesn't make
     63 # sense.
     64 .if ${value:L:e$=x} != "value"
     65 .  error
     66 .endif
     67 # Before 2020-07-20, the modifier ":e$=x" was parsed as having a left-hand
     68 # side "e" and a right-hand side "x".  The dollar was parsed (but not
     69 # interpreted) as 'anchor at the end'.  Therefore the modifier was equivalent
     70 # to ":e=x", which doesn't match the string "value$".  Therefore the whole
     71 # expression evaluated to "value$".
     72 .if ${${:Uvalue\$}:L:e$=x} != "valux"
     73 .  error
     74 .endif
     75 .if ${value:L:e=x} != "valux"
     76 .  error
     77 .endif
     78 
     79 # Words that don't match are copied unmodified.
     80 .if ${:Ufile.c file.h:%.c=%.cpp} != "file.cpp file.h"
     81 .  error
     82 .endif
     83 
     84 # The % placeholder can be anywhere in the string, it doesn't have to be at
     85 # the beginning of the pattern.
     86 .if ${:Ufile.c other.c:file.%=renamed.%} != "renamed.c other.c"
     87 .  error
     88 .endif
     89 
     90 # Each word gets the suffix "X" appended.
     91 .if ${one two:L:=X} != "oneX twoX"
     92 .  error
     93 .endif
     94 
     95 # The suffix "o" is replaced with "X".
     96 .if ${one two:L:o=X} != "one twX"
     97 .  error
     98 .endif
     99 
    100 # The suffix "o" is replaced with nothing.
    101 .if ${one two:L:o=} != "one tw"
    102 .  error
    103 .endif
    104 
    105 # The suffix "o" is replaced with a literal percent.  The percent is only
    106 # a wildcard when it appears on the left-hand side.
    107 .if ${one two:L:o=%} != "one tw%"
    108 .  error
    109 .endif
    110 
    111 # Each word with the suffix "o" is replaced with "X".  The percent is a
    112 # wildcard even though the right-hand side does not contain another percent.
    113 .if ${one two:L:%o=X} != "one X"
    114 .  error
    115 .endif
    116 
    117 # Each word with the prefix "o" is replaced with "X".  The percent is a
    118 # wildcard even though the right-hand side does not contain another percent.
    119 .if ${one two:L:o%=X} != "X two"
    120 .  error
    121 .endif
    122 
    123 # For each word with the prefix "o" and the suffix "e", the whole word is
    124 # replaced with "X".
    125 .if ${one two oe oxen:L:o%e=X} != "X two X oxen"
    126 .  error
    127 .endif
    128 
    129 # Only the first '%' is the wildcard.
    130 .if ${one two o%e other%e:L:o%%e=X} != "one two X X"
    131 .  error
    132 .endif
    133 
    134 # In the replacement, only the first '%' is the placeholder, all others
    135 # are literal percent characters.
    136 .if ${one two:L:%=%%} != "one% two%"
    137 .  error
    138 .endif
    139 
    140 # In the word "one", only a prefix of the pattern suffix "nes" matches,
    141 # the whole word is too short.  Therefore it doesn't match.
    142 .if ${one two:L:%nes=%xxx} != "one two"
    143 .  error
    144 .endif
    145 
    146 # As of 2020-10-06, the right-hand side of the SysV modifier is expanded
    147 # twice.  The first expansion happens in ApplyModifier_SysV, where the
    148 # modifier is split into its two parts.  The second expansion happens
    149 # when each word is replaced in ModifyWord_SYSVSubst.
    150 # XXX: This is unexpected.  Add more test case to demonstrate the effects
    151 # of removing one of the expansions.
    152 VALUE=		value
    153 INDIRECT=	1:${VALUE} 2:$${VALUE} 4:$$$${VALUE}
    154 .if ${x:L:x=${INDIRECT}} != "1:value 2:value 4:\${VALUE}"
    155 .  error
    156 .endif
    157 
    158 all:
    159