Home | History | Annotate | Line # | Download | only in unit-tests
cond-cmp-string.mk revision 1.12
      1  1.12  rillig # $NetBSD: cond-cmp-string.mk,v 1.12 2020/11/08 23:00:09 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.11  rillig # Some other characters worked though, and some didn't.
     67  1.11  rillig # Those that are mentioned in is_separator didn't work.
     68   1.9  rillig .if ${:Uword0} != "${:Uword}0"
     69   1.9  rillig .  error
     70   1.9  rillig .endif
     71   1.9  rillig .if ${:Uword&} != "${:Uword}&"
     72   1.9  rillig .  error
     73   1.9  rillig .endif
     74   1.9  rillig .if ${:Uword!} != "${:Uword}!"
     75   1.9  rillig .  error
     76   1.9  rillig .endif
     77   1.9  rillig .if ${:Uword<} != "${:Uword}<"
     78   1.9  rillig .  error
     79   1.9  rillig .endif
     80   1.9  rillig 
     81   1.8  rillig # Adding another variable expression to the string literal works though.
     82   1.8  rillig .if ${:Uword} != "${:Uwo}${:Urd}"
     83   1.8  rillig .  error
     84   1.8  rillig .endif
     85   1.8  rillig 
     86   1.7  rillig # Adding a space at the beginning of the quoted variable expression works
     87   1.7  rillig # though.
     88   1.7  rillig .if ${:U word } != " ${:Uword} "
     89   1.7  rillig .  error
     90   1.7  rillig .endif
     91  1.12  rillig 
     92  1.12  rillig # If at least one side of the comparison is a string literal, the string
     93  1.12  rillig # comparison is performed.
     94  1.12  rillig .if 12345 != "12345"
     95  1.12  rillig .  error
     96  1.12  rillig .endif
     97  1.12  rillig 
     98  1.12  rillig # If at least one side of the comparison is a string literal, the string
     99  1.12  rillig # comparison is performed.  The ".0" in the left-hand side makes the two
    100  1.12  rillig # sides of the equation unequal.
    101  1.12  rillig .if 12345.0 == "12345"
    102  1.12  rillig .  error
    103  1.12  rillig .endif
    104