Home | History | Annotate | Line # | Download | only in unit-tests
varmod-edge.mk revision 1.1
      1 # $NetBSD: varmod-edge.mk,v 1.1 2019/11/30 00:38:51 rillig Exp $
      2 #
      3 # Tests for edge cases in variable modifiers.
      4 #
      5 # These tests demonstrate the current implementation in small examples.
      6 # They may contain surprising behavior.
      7 #
      8 # Each test consists of:
      9 # - INP, the input to the test
     10 # - MOD, the expression for testing the modifier
     11 # - EXP, the expected output
     12 
     13 TESTS+=		M-paren
     14 INP.M-paren=	(parentheses) {braces} (opening closing) ()
     15 MOD.M-paren=	${INP.M-paren:M(*)}
     16 EXP.M-paren=	(parentheses) ()
     17 
     18 # The first closing brace matches the opening parenthesis.
     19 # The second closing brace actually ends the variable expression.
     20 #
     21 # XXX: This is unexpected but rarely occurs in practice.
     22 TESTS+=		M-mixed
     23 INP.M-mixed=	(paren-brace} (
     24 MOD.M-mixed=	${INP.M-mixed:M(*}}
     25 EXP.M-mixed=	(paren-brace}
     26 
     27 # When the :M and :N modifiers are parsed, the pattern finishes as soon
     28 # as open_parens + open_braces == closing_parens + closing_braces. This
     29 # means that ( and } form a matching pair.
     30 #
     31 # Nested variable expressions are not parsed as such. Instead, only the
     32 # parentheses and braces are counted. This leads to a parse error since
     33 # the nested expression is not "${:U*)}" but only "${:U*)", which is
     34 # missing the closing brace. The expression is evaluated anyway.
     35 # The final brace in the output comes from the end of M.nest-mix.
     36 #
     37 # XXX: This is unexpected but rarely occurs in practice.
     38 TESTS+=		M-nest-mix
     39 INP.M-nest-mix=	(parentheses)
     40 MOD.M-nest-mix=	${INP.M-nest-mix:M${:U*)}}
     41 EXP.M-nest-mix=	(parentheses)}
     42 
     43 # In contrast to parentheses and braces, the brackets are not counted
     44 # when the :M modifier is parsed since Makefile variables only take the
     45 # ${VAR} or $(VAR) forms, but not $[VAR].
     46 #
     47 # The final ] in the pattern is needed to close the character class.
     48 TESTS+=		M-nest-brk
     49 INP.M-nest-brk=	[ [[ [[[
     50 MOD.M-nest-brk=	${INP.M-nest-brk:M${:U[[[[[]}}
     51 EXP.M-nest-brk=	[
     52 
     53 # The pattern in the nested variable has an unclosed character class.
     54 # No error is reported though, and the pattern is closed implicitly.
     55 #
     56 # XXX: It is unexpected that no error is reported.
     57 TESTS+=		M-pat-err
     58 INP.M-pat-err=	[ [[ [[[
     59 MOD.M-pat-err=	${INP.M-pat-err:M${:U[[}}
     60 EXP.M-pat-err=	[
     61 
     62 all:
     63 .for test in ${TESTS}
     64 .  if ${MOD.${test}} == ${EXP.${test}}
     65 	@printf 'ok %s\n' ${test:Q}''
     66 .  else
     67 	@printf 'error in %s: expected %s, got %s\n' \
     68 		${test:Q}'' ${EXP.${test}:Q}'' ${MOD.${test}:Q}''
     69 .  endif
     70 .endfor
     71