Home | History | Annotate | Line # | Download | only in unit-tests
cond-cmp-string.mk revision 1.8
      1  1.8  rillig # $NetBSD: cond-cmp-string.mk,v 1.8 2020/10/30 13:41:14 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.8  rillig # XXX: As of 2020-10-30, adding literal characters to the string results
     61  1.8  rillig # in a parse error.  This is a bug and should have been caught much earlier.
     62  1.7  rillig # I wonder since when it exists.
     63  1.7  rillig .if ${:Uword} != "${:Uword} "
     64  1.7  rillig .  error
     65  1.7  rillig .else
     66  1.7  rillig .  error
     67  1.7  rillig .endif
     68  1.7  rillig 
     69  1.8  rillig # Adding another variable expression to the string literal works though.
     70  1.8  rillig .if ${:Uword} != "${:Uwo}${:Urd}"
     71  1.8  rillig .  error
     72  1.8  rillig .endif
     73  1.8  rillig 
     74  1.7  rillig # Adding a space at the beginning of the quoted variable expression works
     75  1.7  rillig # though.
     76  1.7  rillig .if ${:U word } != " ${:Uword} "
     77  1.7  rillig .  error
     78  1.7  rillig .endif
     79