varmod-edge.mk revision 1.2
1# $NetBSD: varmod-edge.mk,v 1.2 2019/11/30 02:31:19 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 13TESTS+= M-paren 14INP.M-paren= (parentheses) {braces} (opening closing) () 15MOD.M-paren= ${INP.M-paren:M(*)} 16EXP.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. 22TESTS+= M-mixed 23INP.M-mixed= (paren-brace} ( 24MOD.M-mixed= ${INP.M-mixed:M(*}} 25EXP.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. 38TESTS+= M-nest-mix 39INP.M-nest-mix= (parentheses) 40MOD.M-nest-mix= ${INP.M-nest-mix:M${:U*)}} 41EXP.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. 48TESTS+= M-nest-brk 49INP.M-nest-brk= [ [[ [[[ 50MOD.M-nest-brk= ${INP.M-nest-brk:M${:U[[[[[]}} 51EXP.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# See str.c, function Str_Match. 58TESTS+= M-pat-err 59INP.M-pat-err= [ [[ [[[ 60MOD.M-pat-err= ${INP.M-pat-err:M${:U[[}} 61EXP.M-pat-err= [ 62 63# The first backslash does not escape the second backslash. 64# Therefore, the second backslash escapes the parenthesis. 65# This means that the pattern ends there. 66# The final } in the output comes from the end of MOD.M-bsbs. 67# 68# If the first backslash were to escape the second backslash, the first 69# closing brace would match the opening parenthesis (see M-mixed), and 70# the second closing brace would be needed to close the variable. 71TESTS+= M-bsbs 72INP.M-bsbs= \( \(} 73MOD.M-bsbs= ${INP.M-bsbs:M\\(}} 74EXP.M-bsbs= \(} 75#EXP.M-bsbs= \( # If the first backslash were to escape ... 76 77all: 78.for test in ${TESTS} 79. if ${MOD.${test}} == ${EXP.${test}} 80 @printf 'ok %s\n' ${test:Q}'' 81. else 82 @printf 'error in %s: expected %s, got %s\n' \ 83 ${test:Q}'' ${EXP.${test}:Q}'' ${MOD.${test}:Q}'' 84. endif 85.endfor 86