Home | History | Annotate | Line # | Download | only in unit-tests
      1 # $NetBSD: cond-func.mk,v 1.19 2025/06/28 22:39:28 rillig Exp $
      2 #
      3 # Tests for those parts of the functions in .if conditions that are common
      4 # among several functions.
      5 #
      6 # The below test uses the 'defined' function since it has no side-effects.
      7 # The other functions would work equally well, except for 'empty', which
      8 # parses its argument differently from the other functions.
      9 #
     10 
     11 DEF=			defined
     12 ${:UA B}=		variable name with spaces
     13 ${:UVAR(value)}=	variable name with parentheses
     14 ${:UVAR{value}}=	variable name with balanced braces
     15 
     16 # Really strange variable names must be given indirectly via another variable,
     17 # so that no unbalanced braces appear in the top-level expression.
     18 VARNAME_UNBALANCED_BRACES=	VAR{{{value
     19 ${VARNAME_UNBALANCED_BRACES}=	variable name with unbalanced braces
     20 
     21 .if !defined(DEF)
     22 .  error
     23 .endif
     24 
     25 # Horizontal whitespace (space tab) after the opening parenthesis is ignored.
     26 .if !defined( 	DEF)
     27 .  error
     28 .endif
     29 
     30 # Horizontal whitespace (space tab) before the closing parenthesis is ignored.
     31 .if !defined(DEF 	)
     32 .  error
     33 .endif
     34 
     35 # The argument of a function must not directly contain whitespace.
     36 # expect+1: Missing ")" after argument "A" for "defined"
     37 .if !defined(A B)
     38 .  error
     39 .endif
     40 
     41 # If necessary, the whitespace can be generated by an expression.
     42 .if !defined(${:UA B})
     43 .  error
     44 .endif
     45 
     46 # Characters that could be mistaken for operators must not appear directly
     47 # in a function argument.  As with whitespace, these can be generated
     48 # indirectly.
     49 #
     50 # It's not entirely clear why these characters are forbidden.
     51 # The most plausible reason seems to be typo detection.
     52 # expect+1: Missing ")" after argument "A" for "defined"
     53 .if !defined(A&B)
     54 .  error
     55 .endif
     56 # expect+1: Missing ")" after argument "A" for "defined"
     57 .if !defined(A|B)
     58 .  error
     59 .endif
     60 
     61 # Even parentheses may appear in variable names.
     62 # They must be balanced though.
     63 .if !defined(VAR(value))
     64 .  error
     65 .endif
     66 
     67 # Braces do not have any special meaning when parsing arguments.
     68 .if !defined(VAR{value})
     69 .  error
     70 .endif
     71 
     72 # Braces do not have any special meaning when parsing arguments.
     73 # They don't need to be balanced.
     74 .if !defined(VAR{{{value)
     75 .  error
     76 .endif
     77 
     78 # There may be spaces around the operators and parentheses, and even
     79 # inside the parentheses.  The spaces inside the parentheses are not
     80 # allowed for the 'empty' function (see cond-func-empty.mk), therefore
     81 # they are typically omitted for the other functions as well.
     82 .if ! defined ( DEF )
     83 .  error
     84 .endif
     85 
     86 # Before cond.c 1.366 from 2024-07-06, the following condition was
     87 # interpreted as defined(A) && defined(B). Each kind of .if directive has a
     88 # default function that is called when a bare word is parsed.  For the plain
     89 # .if directive, this function is 'defined'; see "struct If ifs" in cond.c.
     90 # expect+1: Unknown operator "&"
     91 .if A&B
     92 .  error
     93 .endif
     94 
     95 # The empty variable is never defined.
     96 .if defined()
     97 .  error
     98 .endif
     99 
    100 # The plain word 'defined' is interpreted as 'defined(defined)', see
    101 # CondParser_ComparisonOrLeaf.
    102 # That variable is not defined (yet).
    103 .if defined
    104 .  error
    105 .else
    106 # expect+1: A plain function name is parsed as defined(...).
    107 .  info A plain function name is parsed as defined(...).
    108 .endif
    109 
    110 # If a variable named 'defined' is actually defined, the bare word 'defined'
    111 # is interpreted as 'defined(defined)', and the condition evaluates to true.
    112 defined=	# defined but empty
    113 .if defined
    114 # expect+1: A plain function name is parsed as defined(...).
    115 .  info A plain function name is parsed as defined(...).
    116 .else
    117 .  error
    118 .endif
    119 
    120 # A plain symbol name may start with one of the function names, in this case
    121 # 'defined'.
    122 .if defined-var
    123 .  error
    124 .else
    125 # expect+1: Symbols may start with a function name.
    126 .  info Symbols may start with a function name.
    127 .endif
    128 
    129 defined-var=	# defined but empty
    130 .if defined-var
    131 # expect+1: Symbols may start with a function name.
    132 .  info Symbols may start with a function name.
    133 .else
    134 .  error
    135 .endif
    136 
    137 # expect+1: Missing ")" after argument "" for "defined"
    138 .if defined(
    139 .  error
    140 .else
    141 .  error
    142 .endif
    143 
    144 # expect+1: Missing ")" after argument "${:UVARNAME}.param" for "defined"
    145 .if defined(${:UVARNAME}.param extra)
    146 .  error
    147 .else
    148 .  error
    149 .endif
    150