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