Home | History | Annotate | Line # | Download | only in unit-tests
cond-cmp-string.mk revision 1.10
      1  1.10  rillig # $NetBSD: cond-cmp-string.mk,v 1.10 2020/10/30 14:51:47 rillig Exp $
      2   1.1  rillig #
      3   1.2  rillig # Tests for string comparisons in .if conditions.
      4   1.1  rillig 
      5   1.3  rillig # This is a simple comparison of string literals.
      6   1.3  rillig # Nothing surprising here.
      7   1.3  rillig .if "str" != "str"
      8   1.6  rillig .  error
      9   1.3  rillig .endif
     10   1.1  rillig 
     11   1.3  rillig # The right-hand side of the comparison may be written without quotes.
     12   1.3  rillig .if "str" != str
     13   1.6  rillig .  error
     14   1.3  rillig .endif
     15   1.3  rillig 
     16   1.3  rillig # The left-hand side of the comparison must be enclosed in quotes.
     17   1.3  rillig # This one is not enclosed in quotes and thus generates an error message.
     18   1.3  rillig .if str != str
     19   1.6  rillig .  error
     20   1.3  rillig .endif
     21   1.3  rillig 
     22   1.3  rillig # The left-hand side of the comparison requires a defined variable.
     23   1.3  rillig # The variable named "" is not defined, but applying the :U modifier to it
     24   1.3  rillig # makes it "kind of defined" (see VAR_KEEP).  Therefore it is ok here.
     25   1.3  rillig .if ${:Ustr} != "str"
     26   1.6  rillig .  error
     27   1.3  rillig .endif
     28   1.3  rillig 
     29   1.3  rillig # Any character in a string literal may be escaped using a backslash.
     30   1.3  rillig # This means that "\n" does not mean a newline but a simple "n".
     31   1.3  rillig .if "string" != "\s\t\r\i\n\g"
     32   1.6  rillig .  error
     33   1.3  rillig .endif
     34   1.3  rillig 
     35   1.3  rillig # It is not possible to concatenate two string literals to form a single
     36   1.3  rillig # string.
     37   1.3  rillig .if "string" != "str""ing"
     38   1.6  rillig .  error
     39   1.3  rillig .endif
     40   1.4  rillig 
     41   1.5  rillig # There is no = operator for strings.
     42   1.4  rillig .if !("value" = "value")
     43   1.4  rillig .  error
     44   1.4  rillig .else
     45   1.4  rillig .  error
     46   1.4  rillig .endif
     47   1.4  rillig 
     48   1.4  rillig # There is no === operator for strings either.
     49   1.4  rillig .if !("value" === "value")
     50   1.4  rillig .  error
     51   1.4  rillig .else
     52   1.4  rillig .  error
     53   1.4  rillig .endif
     54   1.4  rillig 
     55   1.7  rillig # A variable expression can be enclosed in double quotes.
     56   1.7  rillig .if ${:Uword} != "${:Uword}"
     57   1.7  rillig .  error
     58   1.7  rillig .endif
     59   1.7  rillig 
     60  1.10  rillig # Between 2003-01-01 (maybe even earlier) and 2020-10-30, adding one of the
     61  1.10  rillig # characters " \t!=><" directly after a variable expression resulted in a
     62  1.10  rillig # "Malformed conditional", even though the string was well-formed.
     63  1.10  rillig .if ${:Uword } != "${:Uword} "
     64   1.7  rillig .  error
     65   1.7  rillig .endif
     66   1.7  rillig 
     67   1.9  rillig # Some other characters work though, and some don't.
     68   1.9  rillig # Those that are mentioned in is_separator don't work.
     69   1.9  rillig .if ${:Uword0} != "${:Uword}0"
     70   1.9  rillig .  error
     71   1.9  rillig .endif
     72   1.9  rillig .if ${:Uword&} != "${:Uword}&"
     73   1.9  rillig .  error
     74   1.9  rillig .endif
     75   1.9  rillig .if ${:Uword!} != "${:Uword}!"
     76   1.9  rillig .  error
     77   1.9  rillig .endif
     78   1.9  rillig .if ${:Uword<} != "${:Uword}<"
     79   1.9  rillig .  error
     80   1.9  rillig .endif
     81   1.9  rillig 
     82   1.8  rillig # Adding another variable expression to the string literal works though.
     83   1.8  rillig .if ${:Uword} != "${:Uwo}${:Urd}"
     84   1.8  rillig .  error
     85   1.8  rillig .endif
     86   1.8  rillig 
     87   1.7  rillig # Adding a space at the beginning of the quoted variable expression works
     88   1.7  rillig # though.
     89   1.7  rillig .if ${:U word } != " ${:Uword} "
     90   1.7  rillig .  error
     91   1.7  rillig .endif
     92