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:
Kodi Arfer 2019-07-08 15:41:13 -04:00
parent 3afb4fdabe
commit d99cf80986
3 changed files with 23 additions and 9 deletions

View File

@ -9,6 +9,10 @@ Removals
* Support for attribute lists in `defclass` has been removed. Use `setv` * Support for attribute lists in `defclass` has been removed. Use `setv`
and `defn` instead. and `defn` instead.
Bug Fixes
------------------------------
* Statements in the second argument of `assert` are now executed.
0.17.0 0.17.0
============================== ==============================

View File

@ -820,11 +820,22 @@ class HyASTCompiler(object):
@special("assert", [FORM, maybe(FORM)]) @special("assert", [FORM, maybe(FORM)])
def compile_assert_expression(self, expr, root, test, msg): def compile_assert_expression(self, expr, root, test, msg):
if msg is None or type(msg) is HySymbol:
ret = self.compile(test) ret = self.compile(test)
e = ret.force_expr return ret + asty.Assert(
if msg is not None: expr,
msg = self.compile(msg).force_expr test=ret.force_expr,
return ret + asty.Assert(expr, test=e, msg=msg) 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)]) @special(["global", "nonlocal"], [oneplus(SYM)])
def compile_global_or_nonlocal(self, expr, root, syms): def compile_global_or_nonlocal(self, expr, root, syms):

View File

@ -1653,16 +1653,15 @@ macros()
(= (identify-keywords 1 "bloo" :foo) (= (identify-keywords 1 "bloo" :foo)
["other" "other" "keyword"]))) ["other" "other" "keyword"])))
#@(pytest.mark.xfail
(defn test-assert-multistatements [] (defn test-assert-multistatements []
; https://github.com/hylang/hy/issues/1390 ; https://github.com/hylang/hy/issues/1390
(setv s (set)) (setv l [])
(defn f [x] (defn f [x]
(.add s x) (.append l x)
False) False)
(with [(pytest.raises AssertionError)] (with [(pytest.raises AssertionError)]
(assert (do (f 1) (f 2)) (do (f 3) (f 4)))) (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 [] (defn test-underscore_variables []
; https://github.com/hylang/hy/issues/1340 ; https://github.com/hylang/hy/issues/1340