assert(): Make message argument optional

pull/6326/head
Xavier Claessens 5 years ago committed by Jussi Pakkanen
parent 9c72d0fdb2
commit 552e78da4d
  1. 3
      docs/markdown/Reference-manual.md
  2. 7
      mesonbuild/ast/printer.py
  3. 20
      mesonbuild/interpreter.py
  4. 6
      run_unittests.py

@ -144,6 +144,9 @@ build target (e.g. return value of [executable()](#executable), custom_target(),
Abort with an error message if `condition` evaluates to `false`.
*Since 0.53.0* `message` argument is optional and defaults to print the condition
statement instead.
### benchmark()
``` meson

@ -100,7 +100,7 @@ class AstPrinter(AstVisitor):
def visit_ComparisonNode(self, node: mparser.ComparisonNode):
node.left.accept(self)
self.append_padded(mparser.comparison_map[node.ctype], node)
self.append_padded(node.ctype, node)
node.right.accept(self)
def visit_ArithmeticNode(self, node: mparser.ArithmeticNode):
@ -192,7 +192,10 @@ class AstPrinter(AstVisitor):
if break_args:
self.newline()
for key, val in node.kwargs.items():
self.append(key, node)
if isinstance(key, str):
self.append(key, node)
else:
key.accept(self)
self.append_padded(':', node)
val.accept(self)
self.append(', ', node)

@ -2366,14 +2366,24 @@ external dependencies (including libraries) must go to "dependencies".''')
@noKwargs
def func_assert(self, node, args, kwargs):
if len(args) != 2:
raise InterpreterException('Assert takes exactly two arguments')
value, message = args
if len(args) == 1:
FeatureNew('assert function without message argument', '0.53.0').use(self.subproject)
value = args[0]
message = None
elif len(args) == 2:
value, message = args
if not isinstance(message, str):
raise InterpreterException('Assert message not a string.')
else:
raise InterpreterException('Assert takes between one and two arguments')
if not isinstance(value, bool):
raise InterpreterException('Assert value not bool.')
if not isinstance(message, str):
raise InterpreterException('Assert message not a string.')
if not value:
if message is None:
from .ast import AstPrinter
printer = AstPrinter()
node.args.arguments[0].accept(printer)
message = printer.result
raise InterpreterException('Assert failed: ' + message)
def validate_arguments(self, args, argcount, arg_types):

@ -4417,6 +4417,12 @@ class FailureTests(BasePlatformTests):
match = 'Meson version is.*but project requires >=2000'
self.assertMesonRaises("", match, meson_version='>=2000', options=options)
def test_assert_default_message(self):
self.assertMesonRaises("k1 = 'a'\n" +
"assert({\n" +
" k1: 1,\n" +
"}['a'] == 2)\n",
r"Assert failed: {k1 : 1}\['a'\] == 2")
@unittest.skipUnless(is_windows() or is_cygwin(), "requires Windows (or Windows via Cygwin)")
class WindowsTests(BasePlatformTests):

Loading…
Cancel
Save