Home | History | Annotate | Line # | Download | only in unit-tests
varmod-match.mk revision 1.10
      1 # $NetBSD: varmod-match.mk,v 1.10 2022/06/11 07:54:25 rillig Exp $
      2 #
      3 # Tests for the :M variable modifier, which filters words that match the
      4 # given pattern.
      5 #
      6 # See ApplyModifier_Match and ModifyWord_Match for the implementation.
      7 
      8 .MAKEFLAGS: -dc
      9 
     10 NUMBERS=	One Two Three Four five six seven
     11 
     12 # Only keep words that start with an uppercase letter.
     13 .if ${NUMBERS:M[A-Z]*} != "One Two Three Four"
     14 .  error
     15 .endif
     16 
     17 # Only keep words that start with a character other than an uppercase letter.
     18 .if ${NUMBERS:M[^A-Z]*} != "five six seven"
     19 .  error
     20 .endif
     21 
     22 # Only keep words that don't start with s and at the same time end with
     23 # either of [ex].
     24 #
     25 # This test case ensures that the negation from the first character class
     26 # does not propagate to the second character class.
     27 .if ${NUMBERS:M[^s]*[ex]} != "One Three five"
     28 .  error
     29 .endif
     30 
     31 # Before 2020-06-13, this expression called Str_Match 601,080,390 times.
     32 # Since 2020-06-13, this expression calls Str_Match 1 time.
     33 .if ${:U****************:M****************b}
     34 .endif
     35 
     36 # As of 2022-06-11, this expression calls Str_Match 5,242,223 times.
     37 # Adding another '*?' to the pattern calls Str_Match 41,261,143 times.
     38 .if ${:U..................................................b:M*?*?*?*?*?a}
     39 .endif
     40 
     41 # To match a dollar sign in a word, double it.
     42 #
     43 # This is different from the :S and :C variable modifiers, where a '$'
     44 # has to be escaped as '\$'.
     45 .if ${:Ua \$ sign:M*$$*} != "\$"
     46 .  error
     47 .endif
     48 
     49 # In the :M modifier, '\$' does not escape a dollar.  Instead it is
     50 # interpreted as a backslash followed by whatever expression the
     51 # '$' starts.
     52 #
     53 # This differs from the :S, :C and several other variable modifiers.
     54 ${:U*}=		asterisk
     55 .if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk"
     56 .  error
     57 .endif
     58 
     59 # TODO: ${VAR:M(((}}}}
     60 # TODO: ${VAR:M{{{)))}
     61 # TODO: ${VAR:M${UNBALANCED}}
     62 # TODO: ${VAR:M${:U(((\}\}\}}}
     63 
     64 .MAKEFLAGS: -d0
     65 
     66 # Special characters:
     67 #	*	matches 0 or more arbitrary characters
     68 #	?	matches a single arbitrary character
     69 #	\	starts an escape sequence, only outside ranges
     70 #	[	starts a set for matching a single character
     71 #	]	ends a set for matching a single character
     72 #	-	in a set, forms a range of characters
     73 #	^	as the first character in a set, negates the set
     74 #	(	during parsing of the pattern, starts a nesting level
     75 #	)	during parsing of the pattern, ends a nesting level
     76 #	{	during parsing of the pattern, starts a nesting level
     77 #	}	during parsing of the pattern, ends a nesting level
     78 #	:	during parsing of the pattern, finishes the pattern
     79 #	$	during parsing of the pattern, starts a nested expression
     80 #	#	in a line except a shell command, starts a comment
     81 #
     82 # Pattern parts:
     83 #	*	matches 0 or more arbitrary characters
     84 #	?	matches exactly 1 arbitrary character
     85 #	\x	matches exactly the character 'x'
     86 #	[...]	matches exactly 1 character from the set
     87 #	[^...]	matches exactly 1 character outside the set
     88 #	[a-z]	matches exactly 1 character from the range 'a' to 'z'
     89 #
     90 
     91 #	[]	matches never
     92 .if ${ ab a[]b a[b a b :L:M[]} != ""
     93 .  error
     94 .endif
     95 
     96 #	a[]b	matches never
     97 .if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != ""
     98 .  error
     99 .endif
    100 
    101 #	[^]	matches exactly 1 arbitrary character
    102 .if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]"
    103 .  error
    104 .endif
    105 
    106 #	a[^]b	matches 'a', then exactly 1 arbitrary character, then 'b'
    107 .if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b"
    108 .  error
    109 .endif
    110 
    111 #	[Nn0]	matches exactly 1 character from the set 'N', 'n', '0'
    112 .if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0"
    113 .  error
    114 .endif
    115 
    116 #	[a-c]	matches exactly 1 character from the range 'a' to 'c'
    117 .if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c"
    118 .  error
    119 .endif
    120 
    121 #	[c-a]	matches the same as [a-c]
    122 .if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c"
    123 .  error
    124 .endif
    125 
    126 #	[^a-c67]
    127 #		matches a single character, except for 'a', 'b', 'c', '6' or
    128 #		'7'
    129 .if ${ A B C a b c d 5 6 7 8 [a-c] [a] :L:M[^a-c67]} != "A B C d 5 8"
    130 .  error
    131 .endif
    132 
    133 #	:	terminates the pattern
    134 .if ${ A * :L:M:} != ""
    135 .  error
    136 .endif
    137 
    138 #	\:	matches a colon
    139 .if ${ ${:U\: \:\:} :L:M\:} != ":"
    140 .  error
    141 .endif
    142 
    143 #	${:U\:}	matches a colon
    144 .if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":"
    145 .  error
    146 .endif
    147 
    148 #	[:]	matches never since the ':' starts the next modifier
    149 # expect+2: Unknown modifier "]"
    150 # expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":")
    151 .if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":"
    152 .  error
    153 .else
    154 .  error
    155 .endif
    156 
    157 #	[\]	matches exactly a backslash; no escaping takes place in
    158 #		character ranges
    159 # Without the 'a' in the below expressions, the backslash would end a word and
    160 # thus influence how the string is split into words.
    161 .if ${ ${:U\\a} ${:U\\\\a} :L:M[\]a} != "\\a"
    162 .  error
    163 .endif
    164 
    165 #.MAKEFLAGS: -dcv
    166 #
    167 # Incomplete patterns:
    168 #	[	matches TODO
    169 #	[x	matches TODO
    170 #	[^	matches TODO
    171 #	[-	matches TODO
    172 #	[xy	matches TODO
    173 #	[^x	matches TODO
    174 #	[\	matches TODO
    175 #
    176 #	[x-	matches exactly 'x', doesn't match 'x-'
    177 #	[^x-	matches TODO
    178 #	\	matches never
    179 
    180 
    181 # The modifier ':tW' prevents splitting at whitespace.  Even leading and
    182 # trailing whitespace is preserved.
    183 .if ${   plain   string   :L:tW:M*} != "   plain   string   "
    184 .  error
    185 .endif
    186 
    187 # Without the modifier ':tW', the string is split into words.  All whitespace
    188 # around and between the words is normalized to a single space.
    189 .if ${   plain    string   :L:M*} != "plain string"
    190 .  error
    191 .endif
    192 
    193 
    194 # The pattern can come from a variable expression.  For single-letter
    195 # variables, either the short form or the long form can be used, just as
    196 # everywhere else.
    197 PRIMES=	2 3 5 7 11
    198 n=	2
    199 .if ${PRIMES:M$n} != "2"
    200 .  error
    201 .endif
    202 .if ${PRIMES:M${n}} != "2"
    203 .  error
    204 .endif
    205 .if ${PRIMES:M${:U2}} != "2"
    206 .  error
    207 .endif
    208