2019-02-07 14:57:35 +01:00
|
|
|
;; Copyright 2019 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
;; This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
;; license. See the LICENSE.
|
|
|
|
|
2013-11-10 19:00:01 +01:00
|
|
|
(defclass WithTest [object]
|
2015-08-04 16:43:07 +02:00
|
|
|
(defn --init-- [self val]
|
|
|
|
(setv self.val val)
|
|
|
|
None)
|
2013-11-10 19:00:01 +01:00
|
|
|
|
2015-08-04 16:43:07 +02:00
|
|
|
(defn --enter-- [self]
|
|
|
|
self.val)
|
2013-11-10 19:00:01 +01:00
|
|
|
|
2015-08-04 16:43:07 +02:00
|
|
|
(defn --exit-- [self type value traceback]
|
|
|
|
(setv self.val None)))
|
2013-11-10 19:00:01 +01:00
|
|
|
|
|
|
|
(defn test-single-with []
|
|
|
|
"NATIVE: test a single with"
|
2015-08-13 16:37:56 +02:00
|
|
|
(with [t (WithTest 1)]
|
|
|
|
(assert (= t 1))))
|
2013-11-10 19:00:01 +01:00
|
|
|
|
|
|
|
(defn test-two-with []
|
|
|
|
"NATIVE: test two withs"
|
2015-08-13 16:37:56 +02:00
|
|
|
(with [t1 (WithTest 1)
|
|
|
|
t2 (WithTest 2)]
|
|
|
|
(assert (= t1 1))
|
|
|
|
(assert (= t2 2))))
|
2013-11-10 19:00:01 +01:00
|
|
|
|
|
|
|
(defn test-thrice-with []
|
|
|
|
"NATIVE: test three withs"
|
2015-08-13 16:37:56 +02:00
|
|
|
(with [t1 (WithTest 1)
|
|
|
|
t2 (WithTest 2)
|
|
|
|
t3 (WithTest 3)]
|
|
|
|
(assert (= t1 1))
|
|
|
|
(assert (= t2 2))
|
|
|
|
(assert (= t3 3))))
|
2013-11-10 19:00:01 +01:00
|
|
|
|
2017-07-24 00:22:10 +02:00
|
|
|
(defn test-quince-with []
|
|
|
|
"NATIVE: test four withs, one with no args"
|
|
|
|
(with [t1 (WithTest 1)
|
|
|
|
t2 (WithTest 2)
|
|
|
|
t3 (WithTest 3)
|
|
|
|
_ (WithTest 4)]
|
|
|
|
(assert (= t1 1))
|
|
|
|
(assert (= t2 2))
|
|
|
|
(assert (= t3 3))))
|
2017-08-19 16:35:41 +02:00
|
|
|
|
|
|
|
(defclass SuppressZDE [object]
|
|
|
|
(defn --enter-- [self])
|
|
|
|
(defn --exit-- [self exc-type exc-value traceback]
|
|
|
|
(and (not (none? exc-type)) (issubclass exc-type ZeroDivisionError))))
|
|
|
|
|
|
|
|
(defn test-exception-suppressing-with []
|
|
|
|
; https://github.com/hylang/hy/issues/1320
|
|
|
|
|
|
|
|
(setv x (with [(SuppressZDE)] 5))
|
|
|
|
(assert (= x 5))
|
|
|
|
|
|
|
|
(setv y (with [(SuppressZDE)] (/ 1 0)))
|
|
|
|
(assert (none? y))
|
|
|
|
|
|
|
|
(setv z (with [(SuppressZDE)] (/ 1 0) 5))
|
|
|
|
(assert (none? z))
|
|
|
|
|
|
|
|
(defn f [] (with [(SuppressZDE)] (/ 1 0)))
|
|
|
|
(assert (none? (f)))
|
|
|
|
|
|
|
|
(setv w 7 l [])
|
|
|
|
(setv w (with [(SuppressZDE)] (.append l w) (/ 1 0) 5))
|
|
|
|
(assert (none? w))
|
|
|
|
(assert (= l [7])))
|