xref: /freebsd/contrib/bmake/unit-tests/directive-if.mk (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1# $NetBSD: directive-if.mk,v 1.12 2023/06/01 20:56:35 rillig Exp $
2#
3# Tests for the .if directive.
4#
5# See also:
6#	cond-*.mk
7
8# TODO: Implementation
9
10.if 0
11.  error
12.else
13# expect+1: 0 evaluates to false.
14.  info 0 evaluates to false.
15.endif
16
17.if 1
18# expect+1: 1 evaluates to true.
19.  info 1 evaluates to true.
20.else
21.  error
22.endif
23
24# There is no '.ifx'.
25#
26# The commit from 2005-05-01 intended to detect this situation, but it failed
27# to do this since the call to is_token had its arguments switched.  They were
28# expected as (str, token, token_len) but were actually passed as (token, str,
29# token_len).  This made is_token return true even if the directive was
30# directly followed by alphanumerical characters, which was wrong.  The
31# typical cases produced an error message such as "Malformed conditional
32# (x 123)", while the intended error message was "Unknown directive".
33#
34# Back at that time, the commits only modified the main code but did not add
35# the corresponding unit tests.  This allowed the bug to hide for more than
36# 15 years.
37#
38# Since 2020-11-10, the correct error message is produced.  The '.ifx' is no
39# longer interpreted as a variant of '.if', therefore the '.error' and '.else'
40# are interpreted as ordinary directives, producing the error messages
41# "if-less else" and "if-less endif".
42# expect+1: Unknown directive "ifx"
43.ifx 123
44# expect+1: This is not conditional.
45.info This is not conditional.
46# expect+1: if-less else
47.else
48# expect+1: This is not conditional.
49.info This is not conditional.
50# expect+1: if-less endif
51.endif
52
53# Missing condition.
54# expect+1: Malformed conditional ()
55.if
56.  error
57.else
58.  error
59.endif
60
61# A plain word must not start with a '"'.  It may contain a embedded quotes
62# though, which are kept.  The quotes need not be balanced.  The next space
63# ends the word, and the remaining " || 1" is parsed as "or true".
64.if ${:Uplain"""""} == plain""""" || 1
65# expect+1: Quotes in plain words are probably a mistake.
66.  info Quotes in plain words are probably a mistake.
67# XXX: Accepting quotes in plain words is probably a mistake as well.
68.else
69.  error
70.endif
71
72.if0
73.  error
74.else
75# expect+1: Don't do this, always put a space after a directive.
76.  info Don't do this, always put a space after a directive.
77.endif
78
79.if${:U-3}
80# expect+1: Don't do this, always put a space after a directive.
81.  info Don't do this, always put a space after a directive.
82.else
83.  error
84.endif
85
86.if${:U-3}>-4
87# expect+1: Don't do this, always put a space around comparison operators.
88.  info Don't do this, always put a space around comparison operators.
89.else
90.  error
91.endif
92
93.if(1)
94# expect+1: Don't do this, always put a space after a directive.
95.  info Don't do this, always put a space after a directive.
96.endif
97
98.if!0
99# expect+1: Don't do this, always put a space after a directive.
100.  info Don't do this, always put a space after a directive.
101.endif
102
103
104# The directives '.ifdef' and '.ifmake' can be negated by inserting an 'n'.
105# This doesn't work for a plain '.if' though.
106#
107# expect+1: Unknown directive "ifn"
108.ifn 0
109