Run statements in the second argument of assert
I've edited the test to use a list instead of a set because the order of evaluation probably ought to be guaranteed.
This commit is contained in:
parent
3afb4fdabe
commit
d99cf80986
4
NEWS.rst
4
NEWS.rst
@ -9,6 +9,10 @@ Removals
|
||||
* Support for attribute lists in `defclass` has been removed. Use `setv`
|
||||
and `defn` instead.
|
||||
|
||||
Bug Fixes
|
||||
------------------------------
|
||||
* Statements in the second argument of `assert` are now executed.
|
||||
|
||||
0.17.0
|
||||
==============================
|
||||
|
||||
|
@ -820,11 +820,22 @@ class HyASTCompiler(object):
|
||||
|
||||
@special("assert", [FORM, maybe(FORM)])
|
||||
def compile_assert_expression(self, expr, root, test, msg):
|
||||
ret = self.compile(test)
|
||||
e = ret.force_expr
|
||||
if msg is not None:
|
||||
msg = self.compile(msg).force_expr
|
||||
return ret + asty.Assert(expr, test=e, msg=msg)
|
||||
if msg is None or type(msg) is HySymbol:
|
||||
ret = self.compile(test)
|
||||
return ret + asty.Assert(
|
||||
expr,
|
||||
test=ret.force_expr,
|
||||
msg=(None if msg is None else self.compile(msg).force_expr))
|
||||
|
||||
# The `msg` part may involve statements, which we only
|
||||
# want to be executed if the assertion fails. Rewrite the
|
||||
# form to set `msg` to a variable.
|
||||
msg_var = self.get_anon_var()
|
||||
return self.compile(mkexpr(
|
||||
'if*', mkexpr('and', '__debug__', mkexpr('not', [test])),
|
||||
mkexpr('do',
|
||||
mkexpr('setv', msg_var, [msg]),
|
||||
mkexpr('assert', 'False', msg_var))).replace(expr))
|
||||
|
||||
@special(["global", "nonlocal"], [oneplus(SYM)])
|
||||
def compile_global_or_nonlocal(self, expr, root, syms):
|
||||
|
@ -1653,16 +1653,15 @@ macros()
|
||||
(= (identify-keywords 1 "bloo" :foo)
|
||||
["other" "other" "keyword"])))
|
||||
|
||||
#@(pytest.mark.xfail
|
||||
(defn test-assert-multistatements []
|
||||
; https://github.com/hylang/hy/issues/1390
|
||||
(setv s (set))
|
||||
(setv l [])
|
||||
(defn f [x]
|
||||
(.add s x)
|
||||
(.append l x)
|
||||
False)
|
||||
(with [(pytest.raises AssertionError)]
|
||||
(assert (do (f 1) (f 2)) (do (f 3) (f 4))))
|
||||
(assert (= s #{1 2 3 4}))))
|
||||
(assert (= l [1 2 3 4])))
|
||||
|
||||
(defn test-underscore_variables []
|
||||
; https://github.com/hylang/hy/issues/1340
|
||||
|
Loading…
Reference in New Issue
Block a user