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