Home | History | Annotate | Line # | Download | only in unit-tests
varmod-match.mk revision 1.19
      1 # $NetBSD: varmod-match.mk,v 1.19 2023/12/17 00:19:11 rillig Exp $
      2 #
      3 # Tests for the ':M' modifier, which keeps only those words that match the
      4 # given pattern.
      5 #
      6 # Table of contents
      7 #
      8 # 1. Pattern characters '*', '?' and '\'
      9 # 2. Character lists and character ranges
     10 # 3. Parsing and escaping
     11 # 4. Interaction with other modifiers
     12 # 5. Performance
     13 # 6. Error handling
     14 # 7. Historical bugs
     15 #
     16 # See ApplyModifier_Match, ParseModifier_Match, ModifyWord_Match and
     17 # Str_Match.
     18 
     19 
     20 # 1. Pattern characters '*', '?' and '\'
     21 #
     22 #	*	matches 0 or more characters
     23 #	?	matches 1 character
     24 #	\x	matches the character 'x'
     25 
     26 # The pattern is anchored both at the beginning and at the end of the word.
     27 # Since the pattern 'e' does not contain any pattern matching characters, it
     28 # matches exactly the word 'e', twice.
     29 .if ${a c e aa cc ee e f g:L:Me} != "e e"
     30 .  error
     31 .endif
     32 
     33 # The pattern character '?' matches exactly 1 character, the pattern character
     34 # '*' matches 0 or more characters.  The whole pattern matches all words that
     35 # start with 's' and have 3 or more characters.
     36 .if ${One Two Three Four five six seven:L:Ms??*} != "six seven"
     37 .  error
     38 .endif
     39 
     40 # Ensure that a pattern without placeholders only matches itself.
     41 .if ${a aa aaa b ba baa bab:L:Ma} != "a"
     42 .  error
     43 .endif
     44 
     45 # Ensure that a pattern that ends with '*' is properly anchored at the
     46 # beginning.
     47 .if ${a aa aaa b ba baa bab:L:Ma*} != "a aa aaa"
     48 .  error
     49 .endif
     50 
     51 # Ensure that a pattern that starts with '*' is properly anchored at the end.
     52 .if ${a aa aaa b ba baa bab:L:M*a} != "a aa aaa ba baa"
     53 .  error
     54 .endif
     55 
     56 
     57 # 2. Character lists and character ranges
     58 #
     59 #	[...]	matches 1 character from the listed characters
     60 #	[^...]	matches 1 character from the unlisted characters
     61 #	[a-z]	matches 1 character from the range 'a' to 'z'
     62 #	[z-a]	matches 1 character from the range 'a' to 'z'
     63 
     64 # Only keep words that start with an uppercase letter.
     65 .if ${One Two Three Four five six seven:L:M[A-Z]*} != "One Two Three Four"
     66 .  error
     67 .endif
     68 
     69 # Only keep words that start with a character other than an uppercase letter.
     70 .if ${One Two Three Four five six seven:L:M[^A-Z]*} != "five six seven"
     71 .  error
     72 .endif
     73 
     74 #	[]	matches never
     75 .if ${ ab a[]b a[b a b :L:M[]} != ""
     76 .  error
     77 .endif
     78 
     79 #	a[]b	matches never
     80 .if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != ""
     81 .  error
     82 .endif
     83 
     84 #	[^]	matches exactly 1 arbitrary character
     85 .if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]"
     86 .  error
     87 .endif
     88 
     89 #	a[^]b	matches 'a', then exactly 1 arbitrary character, then 'b'
     90 .if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b"
     91 .  error
     92 .endif
     93 
     94 #	[Nn0]	matches exactly 1 character from the set 'N', 'n', '0'
     95 .if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0"
     96 .  error
     97 .endif
     98 
     99 #	[a-c]	matches exactly 1 character from the range 'a' to 'c'
    100 .if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c"
    101 .  error
    102 .endif
    103 
    104 #	[c-a]	matches the same as [a-c]
    105 .if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c"
    106 .  error
    107 .endif
    108 
    109 #	[^a-c67]
    110 #		matches a single character, except for 'a', 'b', 'c', '6' or
    111 #		'7'
    112 .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"
    113 .  error
    114 .endif
    115 
    116 #	[\]	matches a single backslash; no escaping takes place in
    117 #		character ranges
    118 # Without the 'b' in the below words, the backslash would end a word and thus
    119 # influence how the string is split into words.
    120 WORDS=		a\b a[\]b ab a\\b
    121 .if ${WORDS:Ma[\]b} != "a\\b"
    122 .  error
    123 .endif
    124 
    125 #	[[-]]	May look like it would match a single '[', '\' or ']', but
    126 #		the inner ']' has two roles: it is the upper bound of the
    127 #		character range as well as the closing character of the
    128 #		character list.  The outer ']' is just a regular character.
    129 WORDS=		[ ] [] \] ]]
    130 .if ${WORDS:M[[-]]} != "[] \\] ]]"
    131 .  error
    132 .endif
    133 
    134 #	[b[-]a]
    135 #		Same as for '[[-]]': the character list stops at the first
    136 #		']', and the 'a]' is treated as a literal string.
    137 WORDS=		[a \a ]a []a \]a ]]a [a] \a] ]a] ba]
    138 .if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]"
    139 .  error
    140 .endif
    141 
    142 #	[-]	Matches a single '-' since the '-' only becomes part of a
    143 #		character range if it is preceded and followed by another
    144 #		character.
    145 WORDS=		- -]
    146 .if ${WORDS:M[-]} != "-"
    147 .  error
    148 .endif
    149 
    150 # Only keep words that don't start with s and at the same time end with
    151 # either of [ex].
    152 #
    153 # This test case ensures that the negation from the first character list
    154 # '[^s]' does not propagate to the second character list '[ex]'.
    155 .if ${One Two Three Four five six seven:L:M[^s]*[ex]} != "One Three five"
    156 .  error
    157 .endif
    158 
    159 
    160 # 3. Parsing and escaping
    161 #
    162 #	*	matches 0 or more characters
    163 #	?	matches 1 character
    164 #	\	outside a character list, escapes the following character
    165 #	[	starts a character list for matching 1 character
    166 #	]	ends a character list for matching 1 character
    167 #	-	in a character list, forms a character range
    168 #	^	at the beginning of a character list, negates the list
    169 #	(	while parsing the pattern, starts a nesting level
    170 #	)	while parsing the pattern, ends a nesting level
    171 #	{	while parsing the pattern, starts a nesting level
    172 #	}	while parsing the pattern, ends a nesting level
    173 #	:	while parsing the pattern, terminates the pattern
    174 #	$	while parsing the pattern, starts a nested expression
    175 #	#	in a line except a shell command, starts a comment
    176 
    177 # The pattern can come from an expression.  For single-letter
    178 # variables, either the short form or the long form can be used, just as
    179 # everywhere else.
    180 PRIMES=	2 3 5 7 11
    181 n=	2
    182 .if ${PRIMES:M$n} != "2"
    183 .  error
    184 .endif
    185 .if ${PRIMES:M${n}} != "2"
    186 .  error
    187 .endif
    188 .if ${PRIMES:M${:U2}} != "2"
    189 .  error
    190 .endif
    191 
    192 #	:	terminates the pattern
    193 .if ${ A * :L:M:} != ""
    194 .  error
    195 .endif
    196 
    197 #	\:	matches a colon
    198 .if ${ ${:U\: \:\:} :L:M\:} != ":"
    199 .  error
    200 .endif
    201 
    202 #	${:U\:}	matches a colon
    203 .if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":"
    204 .  error
    205 .endif
    206 
    207 # To match a dollar sign in a word, double it.
    208 #
    209 # This is different from the :S and :C modifiers, where a '$' has to be
    210 # escaped as '\$'.
    211 .if ${:Ua \$ sign:M*$$*} != "\$"
    212 .  error
    213 .endif
    214 
    215 # In the :M modifier, '\$' does not escape a dollar.  Instead it is
    216 # interpreted as a backslash followed by whatever expression the
    217 # '$' starts.
    218 #
    219 # This differs from the :S, :C and several other modifiers.
    220 ${:U*}=		asterisk
    221 .if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk"
    222 .  error
    223 .endif
    224 
    225 # TODO: ${VAR:M(((}}}}
    226 # TODO: ${VAR:M{{{)))}
    227 # TODO: ${VAR:M${UNBALANCED}}
    228 # TODO: ${VAR:M${:U(((\}\}\}}}
    229 
    230 
    231 # 4. Interaction with other modifiers
    232 
    233 # The modifier ':tW' prevents splitting at whitespace.  Even leading and
    234 # trailing whitespace is preserved.
    235 .if ${   plain   string   :L:tW:M*} != "   plain   string   "
    236 .  error
    237 .endif
    238 
    239 # Without the modifier ':tW', the string is split into words.  All whitespace
    240 # around and between the words is normalized to a single space.
    241 .if ${   plain    string   :L:M*} != "plain string"
    242 .  error
    243 .endif
    244 
    245 
    246 # 5. Performance
    247 
    248 # Before 2020-06-13, this expression called Str_Match 601,080,390 times.
    249 # Since 2020-06-13, this expression calls Str_Match 1 time.
    250 .if ${:U****************:M****************b}
    251 .endif
    252 
    253 # Before 2023-06-22, this expression called Str_Match 2,621,112 times.
    254 # Adding another '*?' to the pattern called Str_Match 20,630,572 times.
    255 # Adding another '*?' to the pattern called Str_Match 136,405,672 times.
    256 # Adding another '*?' to the pattern called Str_Match 773,168,722 times.
    257 # Adding another '*?' to the pattern called Str_Match 3,815,481,072 times.
    258 # Since 2023-06-22, Str_Match no longer backtracks.
    259 .if ${:U..................................................b:M*?*?*?*?*?a}
    260 .endif
    261 
    262 
    263 # 6. Error handling
    264 
    265 #	[	Incomplete empty character list, never matches.
    266 WORDS=		a a[
    267 # expect+1: warning: Unfinished character list in pattern 'a[' of modifier ':M'
    268 .if ${WORDS:Ma[} != ""
    269 .  error
    270 .endif
    271 
    272 #	[^	Incomplete negated empty character list, matches any single
    273 #		character.
    274 WORDS=		a a[ aX
    275 # expect+1: warning: Unfinished character list in pattern 'a[^' of modifier ':M'
    276 .if ${WORDS:Ma[^} != "a[ aX"
    277 .  error
    278 .endif
    279 
    280 #	[-x1-3	Incomplete character list, matches those elements that can be
    281 #		parsed without lookahead.
    282 WORDS=		- + x xx 0 1 2 3 4 [x1-3
    283 # expect+1: warning: Unfinished character list in pattern '[-x1-3' of modifier ':M'
    284 .if ${WORDS:M[-x1-3} != "- x 1 2 3"
    285 .  error
    286 .endif
    287 
    288 #	*[-x1-3	Incomplete character list after a wildcard, matches those
    289 #		words that end with one of the characters from the list.
    290 WORDS=		- + x xx 0 1 2 3 4 00 01 10 11 000 001 010 011 100 101 110 111 [x1-3
    291 # expect+1: warning: Unfinished character list in pattern '*[-x1-3' of modifier ':M'
    292 .if ${WORDS:M*[-x1-3} != "- x xx 1 2 3 01 11 001 011 101 111 [x1-3"
    293 .  warning ${WORDS:M*[-x1-3}
    294 .endif
    295 
    296 #	[^-x1-3
    297 #		Incomplete negated character list, matches any character
    298 #		except those elements that can be parsed without lookahead.
    299 WORDS=		- + x xx 0 1 2 3 4 [x1-3
    300 # expect+1: warning: Unfinished character list in pattern '[^-x1-3' of modifier ':M'
    301 .if ${WORDS:M[^-x1-3} != "+ 0 4"
    302 .  error
    303 .endif
    304 
    305 #	[\	Incomplete character list containing a single '\'.
    306 #
    307 #		A word can only end with a backslash if the preceding
    308 #		character is a backslash as well; in all other cases the final
    309 #		backslash would escape the following space, making the space
    310 #		part of the word.  Only the very last word of a string can be
    311 #		'\', as there is no following space that could be escaped.
    312 WORDS=		\\ \a ${:Ux\\}
    313 PATTERN=	${:U?[\\}
    314 # expect+1: warning: Unfinished character list in pattern '?[\' of modifier ':M'
    315 .if ${WORDS:M${PATTERN}} != "\\\\ x\\"
    316 .  error
    317 .endif
    318 
    319 #	[x-	Incomplete character list containing an incomplete character
    320 #		range, matches only the 'x'.
    321 WORDS=		[x- x x- y
    322 # expect+1: warning: Unfinished character range in pattern '[x-' of modifier ':M'
    323 .if ${WORDS:M[x-} != "x"
    324 .  error
    325 .endif
    326 
    327 #	[^x-	Incomplete negated character list containing an incomplete
    328 #		character range; matches each word that does not have an 'x'
    329 #		at the position of the character list.
    330 #
    331 #		XXX: Even matches strings that are longer than a single
    332 #		character.
    333 WORDS=		[x- x x- y yyyyy
    334 # expect+1: warning: Unfinished character range in pattern '[^x-' of modifier ':M'
    335 .if ${WORDS:M[^x-} != "[x- y yyyyy"
    336 .  error
    337 .endif
    338 
    339 #	[:]	matches never since the ':' starts the next modifier
    340 # expect+3: warning: Unfinished character list in pattern '[' of modifier ':M'
    341 # expect+2: Unknown modifier "]"
    342 # expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":")
    343 .if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":"
    344 .  error
    345 .else
    346 .  error
    347 .endif
    348 
    349 
    350 # 7. Historical bugs
    351 
    352 # Before var.c 1.1031 from 2022-08-24, the following expressions caused an
    353 # out-of-bounds read beyond the indirect ':M' modifiers.
    354 .if ${:U:${:UM\\}}		# The ':M' pattern need not be unescaped, the
    355 .  error			# resulting pattern is '\', it never matches
    356 .endif				# anything.
    357 .if ${:U:${:UM\\\:\\}}		# The ':M' pattern must be unescaped, the
    358 .  error			# resulting pattern is ':\', it never matches
    359 .endif				# anything.
    360